swift
authenticateduser.cpp
1 // SPDX-FileCopyrightText: Copyright (C) 2015 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
5 
6 #include <QJsonValue>
7 #include <QStringBuilder>
8 #include <QtGlobal>
9 
10 #include "misc/icons.h"
11 #include "misc/propertyindexref.h"
12 #include "misc/statusmessage.h"
13 
14 SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CAuthenticatedUser)
15 
16 namespace swift::misc::network
17 {
19 
20  CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname)
21  : IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed())
22  {}
23 
24  CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname, const QString &email,
25  const QString &password)
26  : IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed()), m_email(email.trimmed()),
27  m_password(password.trimmed())
28  {}
29 
31  {
32  if (hasValidRealName()) { return m_realname % u' ' % getDbKeyAsStringInParentheses(); }
33  else { return getDbKeyAsString(); }
34  }
35 
36  QString CAuthenticatedUser::convertToQString(bool i18n) const
37  {
38  Q_UNUSED(i18n);
39  if (m_realname.isEmpty()) { return QStringLiteral("<no realname>"); }
40  return m_realname % (this->hasValidDbKey() ? this->getDbKeyAsStringInParentheses(" ") : QString());
41  }
42 
44  {
45  CAuthenticatedUser user;
46  user.setDbKey(json.value("id").toInt(-1));
47  user.setVatsimId(json.value("vatsimId").toInt(-1));
48  user.setRealName(json.value("name").toString());
49  user.setUsername(json.value("username").toString());
50  user.setEmail(json.value("email").toString(""));
51  user.setCountry(CCountry(json.value("country").toString(), json.value("countryname").toString()));
52  user.setEnabled(json.value("enabled").toBool());
53  user.setAuthenticated(json.value("authenticated").toBool());
54  CRoleList roles(CRoleList::fromDatabaseJson(json.value("roles").toArray()));
55  user.setRoles(roles);
56  return user;
57  }
58 
59  void CAuthenticatedUser::setRealName(const QString &realname)
60  {
61  const QString rn(realname.trimmed().simplified());
62  m_realname = rn;
63  }
64 
65  void CAuthenticatedUser::setUsername(const QString &username)
66  {
67  const QString un(username.trimmed().simplified().toUpper());
68  m_username = un;
69  }
70 
72  {
73  static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
74  CStatusMessageList msgs;
75  // callsign optional
76  if (!this->hasValidDbKey())
77  {
78  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid id"));
79  }
80  if (!this->hasValidRealName())
81  {
82  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid real name"));
83  }
84  if (!this->hasValidCredentials())
85  {
86  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid credentials"));
87  }
88  return msgs;
89  }
90 
91  bool CAuthenticatedUser::hasAdminRole() const { return this->hasRole("ADMIN"); }
92 
93  bool CAuthenticatedUser::hasMappingAdminRole() const { return this->hasRole("MAPPINGADMIN"); }
94 
95  bool CAuthenticatedUser::hasBulkRole() const { return this->hasRole("BULK"); }
96 
97  bool CAuthenticatedUser::hasBulkAddRole() const { return this->hasRole("BULKADD"); }
98 
99  bool CAuthenticatedUser::isAuthenticated() const { return this->isEnabled() && this->isValid() && m_authenticated; }
100 
101  bool CAuthenticatedUser::canDirectlyWriteModels() const { return this->hasBulkRole() || this->hasBulkAddRole(); }
102 
103  CIcons::IconIndex CAuthenticatedUser::toIcon() const { return CIcons::StandardIconUser16; }
104 
106  {
107  if (index.isMyself()) { return QVariant::fromValue(*this); }
108  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
109  {
110  return IDatastoreObjectWithIntegerKey::propertyByIndex(index);
111  }
112  const ColumnIndex i = index.frontCasted<ColumnIndex>();
113  switch (i)
114  {
115  case IndexVatsimId: return QVariant::fromValue(m_vatsimId);
116  case IndexEmail: return QVariant::fromValue(m_email);
117  case IndexPassword: return QVariant::fromValue(m_password);
118  case IndexRealName: return QVariant::fromValue(m_realname);
119  case IndexUsername: return QVariant::fromValue(m_username);
120  default: return CValueObject::propertyByIndex(index);
121  }
122  }
123 
124  void CAuthenticatedUser::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
125  {
126  if (index.isMyself())
127  {
128  (*this) = variant.value<CAuthenticatedUser>();
129  return;
130  }
131  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
132  {
133  IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant);
134  return;
135  }
136  const ColumnIndex i = index.frontCasted<ColumnIndex>();
137  switch (i)
138  {
139  case IndexVatsimId: this->setVatsimId(variant.toInt()); break;
140  case IndexEmail: this->setEmail(variant.value<QString>()); break;
141  case IndexPassword: this->setPassword(variant.value<QString>()); break;
142  case IndexRealName: this->setRealName(variant.value<QString>()); break;
143  case IndexUsername: this->setUsername(variant.value<QString>()); break;
144  default: CValueObject::setPropertyByIndex(index, variant); break;
145  }
146  }
147 } // namespace swift::misc::network
IconIndex
Index for each icon, allows to send them via DBus, efficiently store them, etc.
Definition: icons.h:32
A sequence of log categories.
Non-owning reference to a CPropertyIndex with a subset of its features.
CastType frontCasted() const
First element casted to given type, usually the PropertIndex enum.
bool isMyself() const
Myself index, used with nesting.
void push_back(const T &value)
Appends an element at the end of the sequence.
Definition: sequence.h:305
Streamable status message, e.g.
constexpr static auto SeverityWarning
Status severities.
Status messages, e.g. from Core -> GUI.
QString getDbKeyAsString() const
DB key as string.
Definition: datastore.cpp:30
void setDbKey(int key)
Set the DB key.
Definition: datastore.h:96
QString getDbKeyAsStringInParentheses(const QString &prefix={}) const
Db key in parentheses, e.g. "(3)".
Definition: datastore.cpp:36
bool hasValidDbKey() const
Has valid DB key.
Definition: datastore.h:102
void setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
Set property by index.
Definition: mixinindex.h:160
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:167
Value object encapsulating information of an authentiated user.
bool isAuthenticated() const
Authenticated.
QString convertToQString(bool i18n=false) const
Cast as QString.
void setRealName(const QString &realname)
Set real name.
bool canDirectlyWriteModels() const
Entitled to directly update models.
QString getRealNameAndId() const
Full name + id.
bool hasRole(const QString &roleName) const
Has role?
static CAuthenticatedUser fromDatabaseJson(const QJsonObject &json)
From our database JSON format.
void setAuthenticated(bool authenticated)
Authenticated.
bool hasValidRealName() const
Valid real name?
swift::misc::CIcons::IconIndex toIcon() const
As icon, not implemented by all classes.
void setEmail(const QString &email)
Set email.
bool hasValidCredentials() const
Valid credentials?
QVariant propertyByIndex(swift::misc::CPropertyIndexRef index) const
Property by index.
void setCountry(const swift::misc::CCountry &country)
Country.
void setPropertyByIndex(swift::misc::CPropertyIndexRef index, const QVariant &variant)
Set property by index.
void setRoles(const CRoleList &roles)
Roles.
swift::misc::CStatusMessageList validate() const
Validate, provide details about issues.
void setPassword(const QString &pw)
Set password.
bool hasBulkAddRole() const
Has bulk add role?
void setUsername(const QString &username)
Set username.
bool isValid() const
Valid user object?
Value object encapsulating a list of servers.
Definition: rolelist.h:27
static CRoleList fromDatabaseJson(const QJsonArray &array)
From our database JSON format.
Definition: rolelist.cpp:42
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67