diff --git a/libnymea-app/types/userinfo.cpp b/libnymea-app/types/userinfo.cpp index b4b81ca9..2a8afdd1 100644 --- a/libnymea-app/types/userinfo.cpp +++ b/libnymea-app/types/userinfo.cpp @@ -6,7 +6,7 @@ UserInfo::UserInfo(QObject *parent): QObject(parent) { - + qRegisterMetaType("UserInfo.PermissionScopes"); } UserInfo::UserInfo(const QString &username, QObject *parent): diff --git a/libnymea-app/types/userinfo.h b/libnymea-app/types/userinfo.h index 5d88502b..6aaf4109 100644 --- a/libnymea-app/types/userinfo.h +++ b/libnymea-app/types/userinfo.h @@ -17,7 +17,6 @@ public: PermissionScopeConfigureRules = 0x0030, PermissionScopeAdmin = 0xFFFF, }; - Q_ENUM(PermissionScope) Q_DECLARE_FLAGS(PermissionScopes, PermissionScope) Q_FLAG(PermissionScopes) @@ -43,4 +42,7 @@ private: }; +Q_DECLARE_METATYPE(UserInfo::PermissionScope) +Q_DECLARE_METATYPE(UserInfo::PermissionScopes) + #endif // USERINFO_H diff --git a/libnymea-app/usermanager.cpp b/libnymea-app/usermanager.cpp index ce9cf456..044943a5 100644 --- a/libnymea-app/usermanager.cpp +++ b/libnymea-app/usermanager.cpp @@ -70,14 +70,15 @@ Users *UserManager::users() const 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; params.insert("username", username); params.insert("password", password); 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"); } @@ -105,6 +106,15 @@ int UserManager::removeUser(const QString &username) 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) { qCDebug(dcUserManager()) << "Users notification" << data; @@ -211,15 +221,6 @@ void UserManager::setUserScopesResponse(int commandId, const QVariantMap ¶ms 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) { diff --git a/libnymea-app/usermanager.h b/libnymea-app/usermanager.h index 8923f5a1..cae16a96 100644 --- a/libnymea-app/usermanager.h +++ b/libnymea-app/usermanager.h @@ -45,11 +45,13 @@ public: TokenInfos* tokenInfos() 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 removeToken(const QUuid &id); 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: void engineChanged(); diff --git a/nymea-app/ui/connection/LoginPage.qml b/nymea-app/ui/connection/LoginPage.qml index 433e27ed..529f958c 100644 --- a/nymea-app/ui/connection/LoginPage.qml +++ b/nymea-app/ui/connection/LoginPage.qml @@ -61,13 +61,17 @@ Page { var message; switch (error) { 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; case "UserErrorDuplicateUserId": - message = qsTr("The email you've entered is already used.") + message = qsTr("The username you've entered is already used.") break; case "UserErrorBadPassword": - message = qsTr("The password you've chose is too weak.") + message = qsTr("The password you've chosen is too weak.") break; case "UserErrorBackendError": 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 { Layout.fillWidth: true 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) wrapMode: Text.WordWrap } @@ -116,14 +120,16 @@ Page { columnSpacing: app.margins Label { - text: qsTr("Your e-mail address:") + text: engine.jsonRpcClient.ensureServerVersion("7.0") ? qsTr("Username:") : qsTr("Your e-mail address:") Layout.fillWidth: true Layout.minimumWidth: implicitWidth } TextField { id: usernameTextField 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" } Label { @@ -146,7 +152,7 @@ Page { Layout.fillWidth: true Layout.leftMargin: app.margins; Layout.rightMargin: app.margins; Layout.bottomMargin: app.margins text: qsTr("OK") - enabled: passwordTextField.isValid + enabled: usernameTextField.displayText.length >= 3 && passwordTextField.isValid onClicked: { if (engine.jsonRpcClient.initialSetupRequired) { print("create user") diff --git a/nymea-app/ui/system/UsersSettingsPage.qml b/nymea-app/ui/system/UsersSettingsPage.qml index fbb419e7..db92aa52 100644 --- a/nymea-app/ui/system/UsersSettingsPage.qml +++ b/nymea-app/ui/system/UsersSettingsPage.qml @@ -240,8 +240,8 @@ SettingsPageBase { Layout.fillWidth: true text: model.text checked: (userDetailsPage.userInfo.scopes & model.scope) === model.scope - enabled: model.scope === UserInfo.ScopeAdmin || - (userDetailsPage.userInfo.scopes & UserInfo.ScopeAdmin) == 0 + enabled: model.scope === UserInfo.PermissionScopeAdmin || + ((userDetailsPage.userInfo.scopes & UserInfo.PermissionScopeAdmin) !== UserInfo.PermissionScopeAdmin) onClicked: { print("scopes:", userDetailsPage.userInfo.scopes) var scopes = userDetailsPage.userInfo.scopes @@ -251,6 +251,8 @@ SettingsPageBase { scopes &= ~model.scope scopes |= model.resetOnUnset } + print("username:", userDetailsPage.userInfo.username) + print("new scopes:", scopes, UserInfo.PermissionScopeAdmin) userManager.setUserScopes(userDetailsPage.userInfo.username, scopes) } } @@ -331,7 +333,7 @@ SettingsPageBase { Layout.fillWidth: true Layout.leftMargin: Style.margins Layout.rightMargin: Style.margins - enabled: usernameTextField.displayText.length > 3 && passwordTextField.isValid + enabled: usernameTextField.displayText.length >= 3 && passwordTextField.isValid onClicked: { userManager.createUser(usernameTextField.displayText, passwordTextField.password, createUserPage.permissionScopes) }