swift
Loading...
Searching...
No Matches
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"
12#include "misc/statusmessage.h"
13
14SWIFT_DEFINE_VALUEOBJECT_MIXINS(swift::misc::network, CAuthenticatedUser)
15
16namespace 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())
31 {
32 return m_realname % u' ' % getDbKeyAsStringInParentheses();
33 }
34 else
35 {
36 return getDbKeyAsString();
37 }
38 }
39
41 {
42 Q_UNUSED(i18n);
43 if (m_realname.isEmpty())
44 {
45 return QStringLiteral("<no realname>");
46 }
47 return m_realname % (this->hasValidDbKey() ? this->getDbKeyAsStringInParentheses(" ") : QString());
48 }
49
51 {
53 user.setDbKey(json.value("id").toInt(-1));
54 user.setVatsimId(json.value("vatsimId").toInt(-1));
55 user.setRealName(json.value("name").toString());
56 user.setUsername(json.value("username").toString());
57 user.setEmail(json.value("email").toString(""));
58 user.setCountry(CCountry(json.value("country").toString(), json.value("countryname").toString()));
59 user.setEnabled(json.value("enabled").toBool());
60 user.setAuthenticated(json.value("authenticated").toBool());
61 CRoleList roles(CRoleList::fromDatabaseJson(json.value("roles").toArray()));
62 user.setRoles(roles);
63 return user;
64 }
65
67 {
68 const QString rn(realname.trimmed().simplified());
69 m_realname = rn;
70 }
71
73 {
74 const QString un(username.trimmed().simplified().toUpper());
75 m_username = un;
76 }
77
79 {
80 static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
82 // callsign optional
83 if (!this->hasValidDbKey())
84 {
86 }
87 if (!this->hasValidRealName())
88 {
89 msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid real name"));
90 }
91 if (!this->hasValidCredentials())
92 {
93 msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid credentials"));
94 }
95 return msgs;
96 }
97
98 bool CAuthenticatedUser::hasAdminRole() const { return this->hasRole("ADMIN"); }
99
100 bool CAuthenticatedUser::hasMappingAdminRole() const { return this->hasRole("MAPPINGADMIN"); }
101
102 bool CAuthenticatedUser::hasBulkRole() const { return this->hasRole("BULK"); }
103
104 bool CAuthenticatedUser::hasBulkAddRole() const { return this->hasRole("BULKADD"); }
105
106 bool CAuthenticatedUser::isAuthenticated() const { return this->isEnabled() && this->isValid() && m_authenticated; }
107
109
110 CIcons::IconIndex CAuthenticatedUser::toIcon() const { return CIcons::StandardIconUser16; }
111
113 {
114 if (index.isMyself())
115 {
116 return QVariant::fromValue(*this);
117 }
118 if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
119 {
120 return IDatastoreObjectWithIntegerKey::propertyByIndex(index);
121 }
122 const auto i = index.frontCasted<ColumnIndex>();
123 switch (i)
124 {
125 case IndexVatsimId: return QVariant::fromValue(m_vatsimId);
126 case IndexEmail: return QVariant::fromValue(m_email);
127 case IndexPassword: return QVariant::fromValue(m_password);
128 case IndexRealName: return QVariant::fromValue(m_realname);
129 case IndexUsername: return QVariant::fromValue(m_username);
130 default: return CValueObject::propertyByIndex(index);
131 }
132 }
133
135 {
136 if (index.isMyself())
137 {
138 (*this) = variant.value<CAuthenticatedUser>();
139 return;
140 }
141 if (IDatastoreObjectWithIntegerKey::canHandleIndex(index))
142 {
143 IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant);
144 return;
145 }
146 const auto i = index.frontCasted<ColumnIndex>();
147 switch (i)
148 {
149 case IndexVatsimId: this->setVatsimId(variant.toInt()); break;
150 case IndexEmail: this->setEmail(variant.value<QString>()); break;
151 case IndexPassword: this->setPassword(variant.value<QString>()); break;
152 case IndexRealName: this->setRealName(variant.value<QString>()); break;
153 case IndexUsername: this->setUsername(variant.value<QString>()); break;
154 default: CValueObject::setPropertyByIndex(index, variant); break;
155 }
156 }
157} // 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:308
Streamable status message, e.g.
static constexpr auto SeverityWarning
Status severities.
Status messages, e.g. from Core -> GUI.
QString getDbKeyAsString() const
DB key as string.
Definition datastore.cpp:33
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:42
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:167
QVariant propertyByIndex(CPropertyIndexRef index) const
Property by index.
Definition mixinindex.h:180
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:41
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