More work on multiuser

This commit is contained in:
Michael Zanetti 2022-01-14 09:44:37 +01:00
parent 30d148931d
commit 92327bb6ec
6 changed files with 38 additions and 25 deletions

View File

@ -6,7 +6,7 @@
UserInfo::UserInfo(QObject *parent): UserInfo::UserInfo(QObject *parent):
QObject(parent) QObject(parent)
{ {
qRegisterMetaType<UserInfo::PermissionScopes>("UserInfo.PermissionScopes");
} }
UserInfo::UserInfo(const QString &username, QObject *parent): UserInfo::UserInfo(const QString &username, QObject *parent):

View File

@ -17,7 +17,6 @@ public:
PermissionScopeConfigureRules = 0x0030, PermissionScopeConfigureRules = 0x0030,
PermissionScopeAdmin = 0xFFFF, PermissionScopeAdmin = 0xFFFF,
}; };
Q_ENUM(PermissionScope)
Q_DECLARE_FLAGS(PermissionScopes, PermissionScope) Q_DECLARE_FLAGS(PermissionScopes, PermissionScope)
Q_FLAG(PermissionScopes) Q_FLAG(PermissionScopes)
@ -43,4 +42,7 @@ private:
}; };
Q_DECLARE_METATYPE(UserInfo::PermissionScope)
Q_DECLARE_METATYPE(UserInfo::PermissionScopes)
#endif // USERINFO_H #endif // USERINFO_H

View File

@ -70,14 +70,15 @@ Users *UserManager::users() const
return m_users; return m_users;
} }
int UserManager::createUser(const QString &username, const QString &password, UserInfo::PermissionScopes scopes) int UserManager::createUser(const QString &username, const QString &password, int permissionScopes)
{ {
QVariantMap params; QVariantMap params;
params.insert("username", username); params.insert("username", username);
params.insert("password", password); params.insert("password", password);
if (m_engine->jsonRpcClient()->ensureServerVersion("5.6")) { if (m_engine->jsonRpcClient()->ensureServerVersion("5.6")) {
params.insert("scopes", UserInfo::scopesToList(scopes)); params.insert("scopes", UserInfo::scopesToList((UserInfo::PermissionScopes)permissionScopes));
} }
qCDebug(dcUserManager()) << "Creating user" << username << permissionScopes;
return m_engine->jsonRpcClient()->sendCommand("Users.CreateUser", params, this, "createUserResponse"); return m_engine->jsonRpcClient()->sendCommand("Users.CreateUser", params, this, "createUserResponse");
} }
@ -105,6 +106,15 @@ int UserManager::removeUser(const QString &username)
return m_engine->jsonRpcClient()->sendCommand("Users.RemoveUser", params, this, "removeUserResponse"); return m_engine->jsonRpcClient()->sendCommand("Users.RemoveUser", params, this, "removeUserResponse");
} }
int UserManager::setUserScopes(const QString &username, int scopes)
{
QVariantMap params;
params.insert("username", username);
params.insert("scopes", UserInfo::scopesToList((UserInfo::PermissionScopes)scopes));
qCDebug(dcUserManager()) << "Setting new permission scopes for user" << username << scopes << (int)scopes;
return m_engine->jsonRpcClient()->sendCommand("Users.SetUserScopes", params, this, "setUserScopesResponse");
}
void UserManager::notificationReceived(const QVariantMap &data) void UserManager::notificationReceived(const QVariantMap &data)
{ {
qCDebug(dcUserManager()) << "Users notification" << data; qCDebug(dcUserManager()) << "Users notification" << data;
@ -211,15 +221,6 @@ void UserManager::setUserScopesResponse(int commandId, const QVariantMap &params
emit setUserScopesReply(commandId, error); emit setUserScopesReply(commandId, error);
} }
int UserManager::setUserScopes(const QString &username, UserInfo::PermissionScopes scopes)
{
QVariantMap params;
params.insert("username", username);
params.insert("scopes", UserInfo::scopesToList(scopes));
qCDebug(dcUserManager()) << "Setting new permission scopes for user" << username << scopes << (int)scopes;
return m_engine->jsonRpcClient()->sendCommand("Users.SetUserScopes", params, this, "setUserScopesResponse");
}
Users::Users(QObject *parent): QAbstractListModel(parent) Users::Users(QObject *parent): QAbstractListModel(parent)
{ {

View File

@ -45,11 +45,13 @@ public:
TokenInfos* tokenInfos() const; TokenInfos* tokenInfos() const;
Users *users() const; Users *users() const;
Q_INVOKABLE int createUser(const QString &username, const QString &password, UserInfo::PermissionScopes scopes = UserInfo::PermissionScopeAdmin); // NOTE: Q_FLAG from another QObject (UserInfo::PermissionScopes) doesn't seem to work in certain Qt versions. Using int instead
Q_INVOKABLE int createUser(const QString &username, const QString &password, int permissionScopes = UserInfo::PermissionScopeAdmin);
Q_INVOKABLE int changePassword(const QString &newPassword); Q_INVOKABLE int changePassword(const QString &newPassword);
Q_INVOKABLE int removeToken(const QUuid &id); Q_INVOKABLE int removeToken(const QUuid &id);
Q_INVOKABLE int removeUser(const QString &username); Q_INVOKABLE int removeUser(const QString &username);
Q_INVOKABLE int setUserScopes(const QString &username, UserInfo::PermissionScopes scopes); // NOTE: Q_FLAG from another QObject (UserInfo::PermissionScopes) doesn't seem to work in certain Qt versions. Using int instead
Q_INVOKABLE int setUserScopes(const QString &username, int permissionScopes);
signals: signals:
void engineChanged(); void engineChanged();

View File

@ -61,13 +61,17 @@ Page {
var message; var message;
switch (error) { switch (error) {
case "UserErrorInvalidUserId": case "UserErrorInvalidUserId":
message = qsTr("The email you've entered isn't valid.") if (engine.jsonRpcClient.ensureServerVersion("7.0")) {
message = qsTr("The email you've entered isn't valid.")
} else {
message = qsTr("The username you've entered isn't valid.")
}
break; break;
case "UserErrorDuplicateUserId": case "UserErrorDuplicateUserId":
message = qsTr("The email you've entered is already used.") message = qsTr("The username you've entered is already used.")
break; break;
case "UserErrorBadPassword": case "UserErrorBadPassword":
message = qsTr("The password you've chose is too weak.") message = qsTr("The password you've chosen is too weak.")
break; break;
case "UserErrorBackendError": case "UserErrorBackendError":
message = qsTr("An error happened with the user storage. Please make sure your %1 system is installed correctly.").arg(Configuration.systemName) message = qsTr("An error happened with the user storage. Please make sure your %1 system is installed correctly.").arg(Configuration.systemName)
@ -102,7 +106,7 @@ Page {
Label { Label {
Layout.fillWidth: true Layout.fillWidth: true
text: engine.jsonRpcClient.initialSetupRequired ? text: engine.jsonRpcClient.initialSetupRequired ?
qsTr("In order to use your %1 system, please enter your email address and set a password for it.").arg(Configuration.systemName) qsTr("In order to use your %1 system, please create an account.").arg(Configuration.systemName)
: qsTr("In order to use your %1 system, please log in.").arg(Configuration.systemName) : qsTr("In order to use your %1 system, please log in.").arg(Configuration.systemName)
wrapMode: Text.WordWrap wrapMode: Text.WordWrap
} }
@ -116,14 +120,16 @@ Page {
columnSpacing: app.margins columnSpacing: app.margins
Label { Label {
text: qsTr("Your e-mail address:") text: engine.jsonRpcClient.ensureServerVersion("7.0") ? qsTr("Username:") : qsTr("Your e-mail address:")
Layout.fillWidth: true Layout.fillWidth: true
Layout.minimumWidth: implicitWidth Layout.minimumWidth: implicitWidth
} }
TextField { TextField {
id: usernameTextField id: usernameTextField
Layout.fillWidth: true Layout.fillWidth: true
inputMethodHints: Qt.ImhEmailCharactersOnly | Qt.ImhNoAutoUppercase inputMethodHints: engine.jsonRpcClient.ensureServerVersion("7.0")
? Qt.ImhEmailCharactersOnly | Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
: Qt.ImhNoAutoUppercase | Qt.ImhNoPredictiveText
// placeholderText: "john.smith@cooldomain.com" // placeholderText: "john.smith@cooldomain.com"
} }
Label { Label {
@ -146,7 +152,7 @@ Page {
Layout.fillWidth: true Layout.fillWidth: true
Layout.leftMargin: app.margins; Layout.rightMargin: app.margins; Layout.bottomMargin: app.margins Layout.leftMargin: app.margins; Layout.rightMargin: app.margins; Layout.bottomMargin: app.margins
text: qsTr("OK") text: qsTr("OK")
enabled: passwordTextField.isValid enabled: usernameTextField.displayText.length >= 3 && passwordTextField.isValid
onClicked: { onClicked: {
if (engine.jsonRpcClient.initialSetupRequired) { if (engine.jsonRpcClient.initialSetupRequired) {
print("create user") print("create user")

View File

@ -240,8 +240,8 @@ SettingsPageBase {
Layout.fillWidth: true Layout.fillWidth: true
text: model.text text: model.text
checked: (userDetailsPage.userInfo.scopes & model.scope) === model.scope checked: (userDetailsPage.userInfo.scopes & model.scope) === model.scope
enabled: model.scope === UserInfo.ScopeAdmin || enabled: model.scope === UserInfo.PermissionScopeAdmin ||
(userDetailsPage.userInfo.scopes & UserInfo.ScopeAdmin) == 0 ((userDetailsPage.userInfo.scopes & UserInfo.PermissionScopeAdmin) !== UserInfo.PermissionScopeAdmin)
onClicked: { onClicked: {
print("scopes:", userDetailsPage.userInfo.scopes) print("scopes:", userDetailsPage.userInfo.scopes)
var scopes = userDetailsPage.userInfo.scopes var scopes = userDetailsPage.userInfo.scopes
@ -251,6 +251,8 @@ SettingsPageBase {
scopes &= ~model.scope scopes &= ~model.scope
scopes |= model.resetOnUnset scopes |= model.resetOnUnset
} }
print("username:", userDetailsPage.userInfo.username)
print("new scopes:", scopes, UserInfo.PermissionScopeAdmin)
userManager.setUserScopes(userDetailsPage.userInfo.username, scopes) userManager.setUserScopes(userDetailsPage.userInfo.username, scopes)
} }
} }
@ -331,7 +333,7 @@ SettingsPageBase {
Layout.fillWidth: true Layout.fillWidth: true
Layout.leftMargin: Style.margins Layout.leftMargin: Style.margins
Layout.rightMargin: Style.margins Layout.rightMargin: Style.margins
enabled: usernameTextField.displayText.length > 3 && passwordTextField.isValid enabled: usernameTextField.displayText.length >= 3 && passwordTextField.isValid
onClicked: { onClicked: {
userManager.createUser(usernameTextField.displayText, passwordTextField.password, createUserPage.permissionScopes) userManager.createUser(usernameTextField.displayText, passwordTextField.password, createUserPage.permissionScopes)
} }