Commit 5ea969e1 authored by Leander Schulten's avatar Leander Schulten

UserManagment: Do not load deprecated permissions that does not exists anymore.

parent 0da5fec8
......@@ -116,11 +116,12 @@ QVariant UserPermissionModel::data(const QModelIndex &index, int role) const {
if (index.row() < 0 && index.row() >= rowCount(index)) {
return QVariant("Index Out of Range");
}
const auto &me = UserManagment::metaEnum;
switch (role) {
case HavePermissionRole:
case Qt::EditRole: return QVariant(user->havePermission(static_cast<UserManagment::Permission>(index.row())));
case Qt::EditRole: return QVariant(user->havePermission(static_cast<UserManagment::Permission>(me.value(index.row()))));
case Qt::DisplayRole:
case PermissionNameRole: return QVariant(UserManagment::metaEnum.valueToKey(index.row()));
case PermissionNameRole: return QVariant(me.valueToKey(me.value(index.row())));
default: return QVariant("Unknown role!");
}
}
......@@ -129,7 +130,8 @@ bool UserPermissionModel::setData(const QModelIndex &index, const QVariant &valu
if (role == HavePermissionRole) {
if (UserManagment::get()->getCurrentUser()->havePermission(UserManagment::Admin)) {
if (index.row() >= 0 && index.row() < rowCount(index)) {
const auto p = static_cast<UserManagment::Permission>(index.row());
const auto &me = UserManagment::metaEnum;
const auto p = static_cast<UserManagment::Permission>(me.value(index.row()));
// ein admin darf sich nicht selber entmachten:
if (p != UserManagment::Admin || UserManagment::get()->getCurrentUser() != user) {
if (user->havePermission(p) != value.toBool()) {
......@@ -154,11 +156,8 @@ void User::createUser(const QJsonObject &o) {
}
}
User::User(const QJsonObject &o) : QObject(UserManagment::get()), IDBase(o), username(o[QStringLiteral("username")].toString()), password(QByteArray::fromBase64(o[QStringLiteral("password")].toString().toLatin1())), permissionModel(this) {
const auto array = o[QStringLiteral("permissions")].toArray();
for (const auto &i : array) {
permissions.insert(static_cast<UserManagment::Permission>(i.toInt()));
}
User::User(const QJsonObject &o) : QObject(UserManagment::get()), id(o), username(o[QStringLiteral("username")].toString()), password(QByteArray::fromBase64(o[QStringLiteral("password")].toString().toLatin1())), permissionModel(this) {
loadPermissions(o);
const auto arrayNames = o[QStringLiteral("autologinUsernames")].toArray();
for (const auto &i : arrayNames) {
autologinUsernames.push_back(i.toString());
......@@ -211,3 +210,13 @@ void User::setPermission(UserManagment::Permission p, bool get) {
}
}
}
void User::loadPermissions(const QJsonObject &o) {
const auto array = o[QStringLiteral("permissions")].toArray();
for (const auto &i : array) {
auto value = i.toInt(-1);
if (value >= 0 && UserManagment::metaEnum.valueToKey(value)) {
permissions.insert(static_cast<UserManagment::Permission>(value));
}
}
}
......@@ -81,7 +81,6 @@ public:
CHANGE_MODULE_SETTINGS = 26,
MODIFY_THEME = 27,
SAVE_SLIDE_SHOW_SETTINGS = 28,
LAST_PERMISSION = 29,
};
Q_ENUM(Permission)
/**
......@@ -177,7 +176,7 @@ public:
explicit UserPermissionModel(User *user) : user(user) {}
enum GroupModelRoles { HavePermissionRole = Qt::UserRole + 1, PermissionNameRole };
[[nodiscard]] int rowCount(const QModelIndex & /*parent*/) const override { return UserManagment::LAST_PERMISSION; }
[[nodiscard]] int rowCount(const QModelIndex & /*parent*/) const override { return UserManagment::metaEnum.keyCount(); }
[[nodiscard]] QVariant data(const QModelIndex &index, int role) const override;
bool setData(const QModelIndex &index, const QVariant &value, int role) override;
[[nodiscard]] Qt::ItemFlags flags(const QModelIndex &index) const override {
......@@ -212,6 +211,7 @@ private:
UserPermissionModel permissionModel;
void setUsername(const QString &u);
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) {}
explicit User(const QJsonObject &o);
......
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