mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-11 23:05:34 +08:00
Use nested namespaces (C++17 feature)
This commit is contained in:
@@ -17,144 +17,141 @@
|
||||
#include <QStringBuilder>
|
||||
#include <QtGlobal>
|
||||
|
||||
namespace BlackMisc
|
||||
namespace BlackMisc::Network
|
||||
{
|
||||
namespace Network
|
||||
CAuthenticatedUser::CAuthenticatedUser()
|
||||
{ }
|
||||
|
||||
CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname)
|
||||
: IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed())
|
||||
{ }
|
||||
|
||||
CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname, const QString &email, const QString &password)
|
||||
: IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed()), m_email(email.trimmed()), m_password(password.trimmed())
|
||||
{ }
|
||||
|
||||
QString CAuthenticatedUser::getRealNameAndId() const
|
||||
{
|
||||
CAuthenticatedUser::CAuthenticatedUser()
|
||||
{ }
|
||||
|
||||
CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname)
|
||||
: IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed())
|
||||
{ }
|
||||
|
||||
CAuthenticatedUser::CAuthenticatedUser(int id, const QString &realname, const QString &email, const QString &password)
|
||||
: IDatastoreObjectWithIntegerKey(id), m_realname(realname.trimmed()), m_email(email.trimmed()), m_password(password.trimmed())
|
||||
{ }
|
||||
|
||||
QString CAuthenticatedUser::getRealNameAndId() const
|
||||
if (hasValidRealName())
|
||||
{
|
||||
if (hasValidRealName())
|
||||
{
|
||||
return m_realname % u' ' % getDbKeyAsStringInParentheses();
|
||||
}
|
||||
else
|
||||
{
|
||||
return getDbKeyAsString();
|
||||
}
|
||||
return m_realname % u' ' % getDbKeyAsStringInParentheses();
|
||||
}
|
||||
|
||||
QString CAuthenticatedUser::convertToQString(bool i18n) const
|
||||
else
|
||||
{
|
||||
Q_UNUSED(i18n);
|
||||
if (m_realname.isEmpty()) { return QStringLiteral("<no realname>"); }
|
||||
return m_realname % (this->hasValidDbKey() ? this->getDbKeyAsStringInParentheses(" ") : QString());
|
||||
return getDbKeyAsString();
|
||||
}
|
||||
}
|
||||
|
||||
CAuthenticatedUser CAuthenticatedUser::fromDatabaseJson(const QJsonObject &json)
|
||||
{
|
||||
CAuthenticatedUser user;
|
||||
user.setDbKey(json.value("id").toInt(-1));
|
||||
user.setVatsimId(json.value("vatsimId").toInt(-1));
|
||||
user.setRealName(json.value("name").toString());
|
||||
user.setUsername(json.value("username").toString());
|
||||
user.setEmail(json.value("email").toString(""));
|
||||
user.setCountry(CCountry(json.value("country").toString(), json.value("countryname").toString()));
|
||||
user.setEnabled(json.value("enabled").toBool());
|
||||
user.setAuthenticated(json.value("authenticated").toBool());
|
||||
CRoleList roles(CRoleList::fromDatabaseJson(json.value("roles").toArray()));
|
||||
user.setRoles(roles);
|
||||
return user;
|
||||
}
|
||||
QString CAuthenticatedUser::convertToQString(bool i18n) const
|
||||
{
|
||||
Q_UNUSED(i18n);
|
||||
if (m_realname.isEmpty()) { return QStringLiteral("<no realname>"); }
|
||||
return m_realname % (this->hasValidDbKey() ? this->getDbKeyAsStringInParentheses(" ") : QString());
|
||||
}
|
||||
|
||||
void CAuthenticatedUser::setRealName(const QString &realname)
|
||||
{
|
||||
const QString rn(realname.trimmed().simplified());
|
||||
m_realname = rn;
|
||||
}
|
||||
CAuthenticatedUser CAuthenticatedUser::fromDatabaseJson(const QJsonObject &json)
|
||||
{
|
||||
CAuthenticatedUser user;
|
||||
user.setDbKey(json.value("id").toInt(-1));
|
||||
user.setVatsimId(json.value("vatsimId").toInt(-1));
|
||||
user.setRealName(json.value("name").toString());
|
||||
user.setUsername(json.value("username").toString());
|
||||
user.setEmail(json.value("email").toString(""));
|
||||
user.setCountry(CCountry(json.value("country").toString(), json.value("countryname").toString()));
|
||||
user.setEnabled(json.value("enabled").toBool());
|
||||
user.setAuthenticated(json.value("authenticated").toBool());
|
||||
CRoleList roles(CRoleList::fromDatabaseJson(json.value("roles").toArray()));
|
||||
user.setRoles(roles);
|
||||
return user;
|
||||
}
|
||||
|
||||
void CAuthenticatedUser::setUsername(const QString &username)
|
||||
{
|
||||
const QString un(username.trimmed().simplified().toUpper());
|
||||
m_username = un;
|
||||
}
|
||||
void CAuthenticatedUser::setRealName(const QString &realname)
|
||||
{
|
||||
const QString rn(realname.trimmed().simplified());
|
||||
m_realname = rn;
|
||||
}
|
||||
|
||||
CStatusMessageList CAuthenticatedUser::validate() const
|
||||
{
|
||||
static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
|
||||
CStatusMessageList msgs;
|
||||
// callsign optional
|
||||
if (!this->hasValidDbKey()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid id"));}
|
||||
if (!this->hasValidRealName()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid real name"));}
|
||||
if (!this->hasValidCredentials()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid credentials"));}
|
||||
return msgs;
|
||||
}
|
||||
void CAuthenticatedUser::setUsername(const QString &username)
|
||||
{
|
||||
const QString un(username.trimmed().simplified().toUpper());
|
||||
m_username = un;
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::hasAdminRole() const
|
||||
{
|
||||
return this->hasRole("ADMIN");
|
||||
}
|
||||
CStatusMessageList CAuthenticatedUser::validate() const
|
||||
{
|
||||
static const CLogCategoryList cats(CLogCategoryList(this).withValidation());
|
||||
CStatusMessageList msgs;
|
||||
// callsign optional
|
||||
if (!this->hasValidDbKey()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid id"));}
|
||||
if (!this->hasValidRealName()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid real name"));}
|
||||
if (!this->hasValidCredentials()) { msgs.push_back(CStatusMessage(cats, CStatusMessage::SeverityWarning, u"Invalid credentials"));}
|
||||
return msgs;
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::hasMappingAdminRole() const
|
||||
{
|
||||
return this->hasRole("MAPPINGADMIN");
|
||||
}
|
||||
bool CAuthenticatedUser::hasAdminRole() const
|
||||
{
|
||||
return this->hasRole("ADMIN");
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::hasBulkRole() const
|
||||
{
|
||||
return this->hasRole("BULK");
|
||||
}
|
||||
bool CAuthenticatedUser::hasMappingAdminRole() const
|
||||
{
|
||||
return this->hasRole("MAPPINGADMIN");
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::hasBulkAddRole() const
|
||||
{
|
||||
return this->hasRole("BULKADD");
|
||||
}
|
||||
bool CAuthenticatedUser::hasBulkRole() const
|
||||
{
|
||||
return this->hasRole("BULK");
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::isAuthenticated() const
|
||||
{
|
||||
return this->isEnabled() && this->isValid() && m_authenticated;
|
||||
}
|
||||
bool CAuthenticatedUser::hasBulkAddRole() const
|
||||
{
|
||||
return this->hasRole("BULKADD");
|
||||
}
|
||||
|
||||
bool CAuthenticatedUser::canDirectlyWriteModels() const
|
||||
{
|
||||
return this->hasBulkRole() || this->hasBulkAddRole();
|
||||
}
|
||||
bool CAuthenticatedUser::isAuthenticated() const
|
||||
{
|
||||
return this->isEnabled() && this->isValid() && m_authenticated;
|
||||
}
|
||||
|
||||
CIcons::IconIndex CAuthenticatedUser::toIcon() const
|
||||
{
|
||||
return CIcons::StandardIconUser16;
|
||||
}
|
||||
bool CAuthenticatedUser::canDirectlyWriteModels() const
|
||||
{
|
||||
return this->hasBulkRole() || this->hasBulkAddRole();
|
||||
}
|
||||
|
||||
QVariant CAuthenticatedUser::propertyByIndex(BlackMisc::CPropertyIndexRef index) const
|
||||
{
|
||||
if (index.isMyself()) { return QVariant::fromValue(*this); }
|
||||
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { return IDatastoreObjectWithIntegerKey::propertyByIndex(index); }
|
||||
const ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexVatsimId: return QVariant::fromValue(m_vatsimId);
|
||||
case IndexEmail: return QVariant::fromValue(m_email);
|
||||
case IndexPassword: return QVariant::fromValue(m_password);
|
||||
case IndexRealName: return QVariant::fromValue(m_realname);
|
||||
case IndexUsername: return QVariant::fromValue(m_username);
|
||||
default: return CValueObject::propertyByIndex(index);
|
||||
}
|
||||
}
|
||||
CIcons::IconIndex CAuthenticatedUser::toIcon() const
|
||||
{
|
||||
return CIcons::StandardIconUser16;
|
||||
}
|
||||
|
||||
void CAuthenticatedUser::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
|
||||
QVariant CAuthenticatedUser::propertyByIndex(BlackMisc::CPropertyIndexRef index) const
|
||||
{
|
||||
if (index.isMyself()) { return QVariant::fromValue(*this); }
|
||||
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { return IDatastoreObjectWithIntegerKey::propertyByIndex(index); }
|
||||
const ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
if (index.isMyself()) { (*this) = variant.value<CAuthenticatedUser>(); return; }
|
||||
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant); return; }
|
||||
const ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexVatsimId: this->setVatsimId(variant.toInt()); break;
|
||||
case IndexEmail: this->setEmail(variant.value<QString>()); break;
|
||||
case IndexPassword: this->setPassword(variant.value<QString>()); break;
|
||||
case IndexRealName: this->setRealName(variant.value<QString>()); break;
|
||||
case IndexUsername: this->setUsername(variant.value<QString>()); break;
|
||||
default: CValueObject::setPropertyByIndex(index, variant); break;
|
||||
}
|
||||
case IndexVatsimId: return QVariant::fromValue(m_vatsimId);
|
||||
case IndexEmail: return QVariant::fromValue(m_email);
|
||||
case IndexPassword: return QVariant::fromValue(m_password);
|
||||
case IndexRealName: return QVariant::fromValue(m_realname);
|
||||
case IndexUsername: return QVariant::fromValue(m_username);
|
||||
default: return CValueObject::propertyByIndex(index);
|
||||
}
|
||||
} // namespace
|
||||
}
|
||||
|
||||
void CAuthenticatedUser::setPropertyByIndex(CPropertyIndexRef index, const QVariant &variant)
|
||||
{
|
||||
if (index.isMyself()) { (*this) = variant.value<CAuthenticatedUser>(); return; }
|
||||
if (IDatastoreObjectWithIntegerKey::canHandleIndex(index)) { IDatastoreObjectWithIntegerKey::setPropertyByIndex(index, variant); return; }
|
||||
const ColumnIndex i = index.frontCasted<ColumnIndex>();
|
||||
switch (i)
|
||||
{
|
||||
case IndexVatsimId: this->setVatsimId(variant.toInt()); break;
|
||||
case IndexEmail: this->setEmail(variant.value<QString>()); break;
|
||||
case IndexPassword: this->setPassword(variant.value<QString>()); break;
|
||||
case IndexRealName: this->setRealName(variant.value<QString>()); break;
|
||||
case IndexUsername: this->setUsername(variant.value<QString>()); break;
|
||||
default: CValueObject::setPropertyByIndex(index, variant); break;
|
||||
}
|
||||
}
|
||||
} // namespace
|
||||
|
||||
Reference in New Issue
Block a user