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  : IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed())
20  {}
21 
22  CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname, const QString &email,
23  const QString &password)
24  : IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed()), m_email(email.trimmed()),
25  m_password(password.trimmed())
26  {}
27 
29  {
30  if (hasValidRealName()) { return m_realname % u' ' % getDbKeyAsStringInParentheses(); }
31  else { return getDbKeyAsString(); }
32  }
33 
35  {
36  Q_UNUSED(i18n);
37  if (m_realname.isEmpty()) { return QStringLiteral("<no realname>"); }
38  return m_realname % (this->hasValidDbKey() ? this->getDbKeyAsStringInParentheses(" ") : QString());
39  }
40 
42  {
43  CAuthenticatedUser user;
44  user.setDbKey(json.value("id").toInt(-1));
45  user.setVatsimId(json.value("vatsimId").toInt(-1));
46  user.setRealName(json.value("name").toString());
47  user.setUsername(json.value("username").toString());
48  user.setEmail(json.value("email").toString(""));
49  user.setCountry(CCountry(json.value("country").toString(), json.value("countryname").toString()));
50  user.setEnabled(json.value("enabled").toBool());
51  user.setAuthenticated(json.value("authenticated").toBool());
52  CRoleList roles(CRoleList::fromDatabaseJson(json.value("roles").toArray()));
53  user.setRoles(roles);
54  return user;
55  }
56 
58  {
59  const QString rn(realname.trimmed().simplified());
60  m_realname = rn;
61  }
62 
64  {
65  const QString un(username.trimmed().simplified().toUpper());
66  m_username = un;
67  }
68 
70  {
71  static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
72  CStatusMessageList msgs;
73  // callsign optional
74  if (!this->hasValidDbKey())
75  {
76  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid id"));
77  }
78  if (!this->hasValidRealName())
79  {
80  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid real name"));
81  }
82  if (!this->hasValidCredentials())
83  {
84  msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid credentials"));
85  }
86  return msgs;
87  }
88 
89  bool CAuthenticatedUser::hasAdminRole() const { return this->hasRole("ADMIN"); }
90 
91  bool CAuthenticatedUser::hasMappingAdminRole() const { return this->hasRole("MAPPINGADMIN"); }
92 
93  bool CAuthenticatedUser::hasBulkRole() const { return this->hasRole("BULK"); }
94 
95  bool CAuthenticatedUser::hasBulkAddRole() const { return this->hasRole("BULKADD"); }
96 
97  bool CAuthenticatedUser::isAuthenticated() const { return this->isEnabled() && this->isValid() && m_authenticated; }
98 
99  bool CAuthenticatedUser::canDirectlyWriteModels() const { return this->hasBulkRole() || this->hasBulkAddRole(); }
100 
101  CIcons::IconIndex CAuthenticatedUser::toIcon() const { return CIcons::StandardIconUser16; }
102 
104  {
105  if (index.isMyself()) { return QVariant::fromValue(*this); }
106  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
107  {
108  return IDatastoreObjectWithIntegerKey::propertyByIndex(index);
109  }
110  const auto i = index.frontCasted<ColumnIndex>();
111  switch (i)
112  {
113  case IndexVatsimId: return QVariant::fromValue(m_vatsimId);
114  case IndexEmail: return QVariant::fromValue(m_email);
115  case IndexPassword: return QVariant::fromValue(m_password);
116  case IndexRealName: return QVariant::fromValue(m_realname);
117  case IndexUsername: return QVariant::fromValue(m_username);
118  default: return CValueObject::propertyByIndex(index);
119  }
120  }
121 
123  {
124  if (index.isMyself())
125  {
126  (*this) = variant.value<CAuthenticatedUser>();
127  return;
128  }
129  if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
130  {
131  IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant);
132  return;
133  }
134  const auto i = index.frontCasted<ColumnIndex>();
135  switch (i)
136  {
137  case IndexVatsimId: this->setVatsimId(variant.toInt()); break;
138  case IndexEmail: this->setEmail(variant.value<QString>()); break;
139  case IndexPassword: this->setPassword(variant.value<QString>()); break;
140  case IndexRealName: this->setRealName(variant.value<QString>()); break;
141  case IndexUsername: this->setUsername(variant.value<QString>()); break;
142  default: CValueObject::setPropertyByIndex(index, variant); break;
143  }
144  }
145 } // 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:158
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition: mixinindex.h:165
Value object encapsulating information of an authentiated user.
bool isAuthenticated() const
Authenticated.
CAuthenticatedUser()=default
Default constructor.
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:36
QJsonValue value(QLatin1StringView key) const const
QJsonArray toArray() const const
bool toBool(bool defaultValue) const const
int toInt(int defaultValue) const const
QString toString() const const
bool isEmpty() const const
QString simplified() const const
QString toUpper() const const
QString trimmed() const const
QVariant fromValue(T &&value)
int toInt(bool *ok) const const
T value() const &const
#define SWIFT_DEFINE_VALUEOBJECT_MIXINS(Namespace, Class)
Explicit template definition of mixins for a CValueObject subclass.
Definition: valueobject.h:67