Commit f2c86f83 authored by Leander Schulten's avatar Leander Schulten

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

parent 5ea969e1
......@@ -10,8 +10,9 @@
/**
* @brief Eine ID Klasse, die eindeutige IDs erstellt
*/
class ID{
friend class SyncService;
class ID {
friend class UserManagment;
public:
typedef long long value_type;
private:
......@@ -41,6 +42,4 @@ public:
void writeJsonObject(QJsonObject &o)const;
};
#endif // IDGENERATOR_H
......@@ -2,7 +2,9 @@
#include <QCryptographicHash>
#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
currentOsUserName = qgetenv("USER");
#else
......@@ -25,6 +27,10 @@ void UserManagment::addUser(const QString &name, 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);
if (user->password == hash) {
users.remove_if([=](const auto &p) { return p.get() == user; });
......@@ -40,6 +46,10 @@ bool UserManagment::removeUser(User *user, const QString &password) {
}
bool UserManagment::removeUser(User *user) {
// you cannot remove the default user
if (user == getDefaultUser()) {
return false;
}
if (currentUser->havePermission(Admin) && user != currentUser) {
users.remove_if([=](const auto &p) { return p.get() == user; });
return true;
......@@ -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) {
// the default user has no passwort
if (user == getDefaultUser()) {
return false;
}
const auto hash = QCryptographicHash::hash(password.toUtf8(), QCryptographicHash::Sha3_256);
if (user->password == hash) {
user->password = QCryptographicHash::hash(newPassword.toUtf8(), QCryptographicHash::Sha3_256);
......@@ -84,6 +98,22 @@ bool UserManagment::login(User *user, const QString &password) {
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() {
if (currentOsUserName.isEmpty()) {
return;
......@@ -151,7 +181,10 @@ bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &valu
// USER
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)));
}
}
......@@ -177,7 +210,7 @@ void User::writeJsonObject(QJsonObject &o) const {
names.push_back(i);
}
o.insert(QStringLiteral("autologinUsernames"), names);
IDBase::writeJsonObject(o);
id.writeJsonObject(o);
}
User *UserManagment::getUserByName(const QString &name) const {
......
......@@ -23,14 +23,13 @@ class UserManagment : public QObject {
Q_PROPERTY(QAbstractItemModel *users READ getUserModel CONSTANT)
Q_PROPERTY(QString currentOsUserName READ getCurrentOsUserName CONSTANT)
private:
// User is an incomplete type here
std::unique_ptr<User> defaultUser;
User *currentUser;
QString currentOsUserName;
ModelVector<std::unique_ptr<User>> users;
UserManagment();
~UserManagment() override { currentUser = nullptr; }
friend class User;
static constexpr ID::value_type IdOfDefaultUser = 1000;
public:
const ModelVector<std::unique_ptr<User>> &getUsers() { return users; }
......@@ -39,7 +38,7 @@ public:
User *getUserById(ID id) { return getUserById(id.value()); }
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
* @return
......@@ -116,7 +115,6 @@ public:
* @return true for success, false for failure
*/
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
* @param user the user where the password should be changed
......@@ -132,6 +130,13 @@ public:
* @return true for success, false for failure
*/
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
*/
......@@ -195,7 +200,7 @@ public:
/**
* @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_PROPERTY(QString name READ getUsername NOTIFY usernameChanged)
Q_PROPERTY(QAbstractListModel *permissionModel READ getPermissionModel CONSTANT)
......@@ -204,6 +209,7 @@ class User : public QObject, public IDBase<User> {
friend class UserPermissionModel;
private:
ID id;
QString username;
QByteArray password;
std::set<UserManagment::Permission> permissions;
......@@ -213,6 +219,12 @@ private:
void setPermission(UserManagment::Permission p, bool get = true);
void loadPermissions(const QJsonObject &o);
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);
public:
......@@ -234,6 +246,7 @@ public:
UserPermissionModel *getPermissionModel() { return &permissionModel; }
QAbstractListModel *getAutoLoginUserNameModel() { return &autologinUsernames; }
const std::vector<QString> &getAutoLoginUserNames() { return autologinUsernames.getVector(); }
const ID &getID() const { return id; }
[[nodiscard]] const ModelVector<QString> &getAutoLoginUserNames() const { return autologinUsernames; }
Q_INVOKABLE void removeAutologinUsername(int index) {
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