Aufgrund einer Wartung wird GitLab am 26.10. zwischen 8:00 und 9:00 Uhr kurzzeitig nicht zur Verfügung stehen. / Due to maintenance, GitLab will be temporarily unavailable on 26.10. between 8:00 and 9:00 am.

Commit f2c86f83 authored by Leander Schulten's avatar Leander Schulten
Browse files

UserManagement: Make the default user a 'normal' user as every other user

parent 5ea969e1
...@@ -10,8 +10,9 @@ ...@@ -10,8 +10,9 @@
/** /**
* @brief Eine ID Klasse, die eindeutige IDs erstellt * @brief Eine ID Klasse, die eindeutige IDs erstellt
*/ */
class ID{ class ID {
friend class SyncService; friend class UserManagment;
public: public:
typedef long long value_type; typedef long long value_type;
private: private:
...@@ -41,6 +42,4 @@ public: ...@@ -41,6 +42,4 @@ public:
void writeJsonObject(QJsonObject &o)const; void writeJsonObject(QJsonObject &o)const;
}; };
#endif // IDGENERATOR_H #endif // IDGENERATOR_H
...@@ -2,7 +2,9 @@ ...@@ -2,7 +2,9 @@
#include <QCryptographicHash> #include <QCryptographicHash>
#include <QJsonArray> #include <QJsonArray>
UserManagment::UserManagment() : defaultUser(new User(QStringLiteral("Default"), "")), currentUser(defaultUser.get()) { UserManagment::UserManagment() {
users.emplace_back(new User(ID(IdOfDefaultUser), QStringLiteral("Default User")));
currentUser = users.begin()->get();
#ifdef Q_OS_UNIX #ifdef Q_OS_UNIX
currentOsUserName = qgetenv("USER"); currentOsUserName = qgetenv("USER");
#else #else
...@@ -25,6 +27,10 @@ void UserManagment::addUser(const QString &name, const QString &password) { ...@@ -25,6 +27,10 @@ void UserManagment::addUser(const QString &name, const QString &password) {
} }
bool UserManagment::removeUser(User *user, const QString &password) { bool UserManagment::removeUser(User *user, const QString &password) {
// you can not remove the default user
if (user == getDefaultUser()) {
return false;
}
const auto hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha3_256); const auto hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha3_256);
if (user->password == hash) { if (user->password == hash) {
users.remove_if([=](const auto &p) { return p.get() == user; }); users.remove_if([=](const auto &p) { return p.get() == user; });
...@@ -40,6 +46,10 @@ bool UserManagment::removeUser(User *user, const QString &password) { ...@@ -40,6 +46,10 @@ bool UserManagment::removeUser(User *user, const QString &password) {
} }
bool UserManagment::removeUser(User *user) { bool UserManagment::removeUser(User *user) {
// you cannot remove the default user
if (user == getDefaultUser()) {
return false;
}
if (currentUser->havePermission(Admin) && user != currentUser) { if (currentUser->havePermission(Admin) && user != currentUser) {
users.remove_if([=](const auto &p) { return p.get() == user; }); users.remove_if([=](const auto &p) { return p.get() == user; });
return true; return true;
...@@ -63,6 +73,10 @@ bool UserManagment::changeUserName(User *user, const QString &newName, const QSt ...@@ -63,6 +73,10 @@ bool UserManagment::changeUserName(User *user, const QString &newName, const QSt
} }
bool UserManagment::changeUserPasswort(User *user, const QString &password, const QString &newPassword) { bool UserManagment::changeUserPasswort(User *user, const QString &password, const QString &newPassword) {
// the default user has no passwort
if (user == getDefaultUser()) {
return false;
}
const auto hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha3_256); const auto hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha3_256);
if (user->password == hash) { if (user->password == hash) {
user->password = QCryptographicHash::hash(newPassword.toUtf8(), QCryptographicHash::Sha3_256); user->password = QCryptographicHash::hash(newPassword.toUtf8(), QCryptographicHash::Sha3_256);
...@@ -84,6 +98,22 @@ bool UserManagment::login(User *user, const QString &password) { ...@@ -84,6 +98,22 @@ bool UserManagment::login(User *user, const QString &password) {
return false; return false;
} }
bool UserManagment::login(User *user) {
if (user == getDefaultUser()) {
currentUser = user;
emit currentUserChanged();
return true;
}
for (const auto &name : user->getAutoLoginUserNames()) {
if (name == currentOsUserName) {
currentUser = user;
emit currentUserChanged();
return true;
}
}
return false;
}
void UserManagment::autoLoginUser() { void UserManagment::autoLoginUser() {
if (currentOsUserName.isEmpty()) { if (currentOsUserName.isEmpty()) {
return; return;
...@@ -151,7 +181,10 @@ bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &valu ...@@ -151,7 +181,10 @@ bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &valu
// USER // USER
void User::createUser(const QJsonObject &o) { void User::createUser(const QJsonObject &o) {
if (o[QStringLiteral("password")].toString().length() != 0) { // we dont want a user without a password // handle the default user
if (ID(o) == UserManagment::IdOfDefaultUser) {
UserManagment::get()->getDefaultUser()->loadPermissions(o);
} else if (o[QStringLiteral("password")].toString().length() != 0) { // we dont want a user without a password
UserManagment::get()->users.push_back(std::unique_ptr<User>(new User(o))); UserManagment::get()->users.push_back(std::unique_ptr<User>(new User(o)));
} }
} }
...@@ -177,7 +210,7 @@ void User::writeJsonObject(QJsonObject &o) const { ...@@ -177,7 +210,7 @@ void User::writeJsonObject(QJsonObject &o) const {
names.push_back(i); names.push_back(i);
} }
o.insert(QStringLiteral("autologinUsernames"), names); o.insert(QStringLiteral("autologinUsernames"), names);
IDBase::writeJsonObject(o); id.writeJsonObject(o);
} }
User *UserManagment::getUserByName(const QString &name) const { User *UserManagment::getUserByName(const QString &name) const {
......
...@@ -23,14 +23,13 @@ class UserManagment : public QObject { ...@@ -23,14 +23,13 @@ class UserManagment : public QObject {
Q_PROPERTY(QAbstractItemModel *users READ getUserModel CONSTANT) Q_PROPERTY(QAbstractItemModel *users READ getUserModel CONSTANT)
Q_PROPERTY(QString currentOsUserName READ getCurrentOsUserName CONSTANT) Q_PROPERTY(QString currentOsUserName READ getCurrentOsUserName CONSTANT)
private: private:
// User is an incomplete type here
std::unique_ptr<User> defaultUser;
User *currentUser; User *currentUser;
QString currentOsUserName; QString currentOsUserName;
ModelVector<std::unique_ptr<User>> users; ModelVector<std::unique_ptr<User>> users;
UserManagment(); UserManagment();
~UserManagment() override { currentUser = nullptr; } ~UserManagment() override { currentUser = nullptr; }
friend class User; friend class User;
static constexpr ID::value_type IdOfDefaultUser = 1000;
public: public:
const ModelVector<std::unique_ptr<User>> &getUsers() { return users; } const ModelVector<std::unique_ptr<User>> &getUsers() { return users; }
...@@ -39,7 +38,7 @@ public: ...@@ -39,7 +38,7 @@ public:
User *getUserById(ID id) { return getUserById(id.value()); } User *getUserById(ID id) { return getUserById(id.value()); }
User *getUserById(ID::value_type id); User *getUserById(ID::value_type id);
[[nodiscard]] User *getDefaultUser() const { return defaultUser.get(); } [[nodiscard]] User *getDefaultUser() const { return users.begin()->get(); }
/** /**
* @brief get Return the Singletone of the UserManagment * @brief get Return the Singletone of the UserManagment
* @return * @return
...@@ -116,7 +115,6 @@ public: ...@@ -116,7 +115,6 @@ public:
* @return true for success, false for failure * @return true for success, false for failure
*/ */
Q_INVOKABLE bool changeUserName(User *user, const QString &newName, const QString &password); Q_INVOKABLE bool changeUserName(User *user, const QString &newName, const QString &password);
// Q_INVOKABLE bool changeUserPermission(User * user, Permission newPermission,const QString &password);
/** /**
* @brief changeUserPasswort change the passwort of a user * @brief changeUserPasswort change the passwort of a user
* @param user the user where the password should be changed * @param user the user where the password should be changed
...@@ -132,6 +130,13 @@ public: ...@@ -132,6 +130,13 @@ public:
* @return true for success, false for failure * @return true for success, false for failure
*/ */
Q_INVOKABLE bool login(User *user, const QString &passwort); Q_INVOKABLE bool login(User *user, const QString &passwort);
/**
* @brief login tries to login a user without a passwort. A user can be logged in without a password if the user is
* the default user or if the current os username is in the autologinUsernames list of the user that should be logged in
* @param user the user that should be logged in
* @return true if the log in was successful, otherwise false
*/
Q_INVOKABLE bool login(User *user);
/** /**
* @brief autoLoginUser checks if the autologin user name of one user matches the currentOsUserName and login the user where the name match * @brief autoLoginUser checks if the autologin user name of one user matches the currentOsUserName and login the user where the name match
*/ */
...@@ -195,7 +200,7 @@ public: ...@@ -195,7 +200,7 @@ public:
/** /**
* @brief The User class represents a user. A user have a name and a password and have permissions * @brief The User class represents a user. A user have a name and a password and have permissions
*/ */
class User : public QObject, public IDBase<User> { class User : public QObject {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString name READ getUsername NOTIFY usernameChanged) Q_PROPERTY(QString name READ getUsername NOTIFY usernameChanged)
Q_PROPERTY(QAbstractListModel *permissionModel READ getPermissionModel CONSTANT) Q_PROPERTY(QAbstractListModel *permissionModel READ getPermissionModel CONSTANT)
...@@ -204,6 +209,7 @@ class User : public QObject, public IDBase<User> { ...@@ -204,6 +209,7 @@ class User : public QObject, public IDBase<User> {
friend class UserPermissionModel; friend class UserPermissionModel;
private: private:
ID id;
QString username; QString username;
QByteArray password; QByteArray password;
std::set<UserManagment::Permission> permissions; std::set<UserManagment::Permission> permissions;
...@@ -213,6 +219,12 @@ private: ...@@ -213,6 +219,12 @@ private:
void setPermission(UserManagment::Permission p, bool get = true); void setPermission(UserManagment::Permission p, bool get = true);
void loadPermissions(const QJsonObject &o); void loadPermissions(const QJsonObject &o);
User(QString name, QByteArray password) : username(std::move(name)), password(std::move(password)), permissionModel(this) {} User(QString name, QByteArray password) : username(std::move(name)), password(std::move(password)), permissionModel(this) {}
/**
* @brief User creates a user with a given id and no passwort. This constructor should be only used for the default
* user that exists before the settings file is loaded, so the ID can't change
* @param name The name of the user
*/
User(ID id, QString name) : id(id), username(std::move(name)), permissionModel(this) {}
explicit User(const QJsonObject &o); explicit User(const QJsonObject &o);
public: public:
...@@ -234,6 +246,7 @@ public: ...@@ -234,6 +246,7 @@ public:
UserPermissionModel *getPermissionModel() { return &permissionModel; } UserPermissionModel *getPermissionModel() { return &permissionModel; }
QAbstractListModel *getAutoLoginUserNameModel() { return &autologinUsernames; } QAbstractListModel *getAutoLoginUserNameModel() { return &autologinUsernames; }
const std::vector<QString> &getAutoLoginUserNames() { return autologinUsernames.getVector(); } const std::vector<QString> &getAutoLoginUserNames() { return autologinUsernames.getVector(); }
const ID &getID() const { return id; }
[[nodiscard]] const ModelVector<QString> &getAutoLoginUserNames() const { return autologinUsernames; } [[nodiscard]] const ModelVector<QString> &getAutoLoginUserNames() const { return autologinUsernames; }
Q_INVOKABLE void removeAutologinUsername(int index) { Q_INVOKABLE void removeAutologinUsername(int index) {
if (index >= 0 && index < autologinUsernames.ssize()) { if (index >= 0 && index < autologinUsernames.ssize()) {
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment