File indexing completed on 2024-12-08 04:33:21

0001 /*
0002    SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "permissionmanager.h"
0008 #include "ruqola_debug.h"
0009 #include <QDebug>
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 PermissionManager::PermissionManager() = default;
0013 
0014 PermissionManager::~PermissionManager() = default;
0015 
0016 void PermissionManager::parsePermissions(const QJsonObject &replyObject)
0017 {
0018     const QJsonArray removeArray = replyObject[QLatin1String("remove")].toArray();
0019     if (!removeArray.isEmpty()) {
0020         parseRemovePermission(removeArray);
0021     }
0022     const QJsonArray updateArray = replyObject[QLatin1String("update")].toArray();
0023     parseUpdatePermission(updateArray);
0024     // qDebug() << "mMapPermissions  " << mMapPermissions;
0025 }
0026 
0027 const Permission PermissionManager::permission(const QString &permissionId) const
0028 {
0029     return mMapPermissions.value(permissionId);
0030 }
0031 
0032 QVector<Permission> PermissionManager::permissions() const
0033 {
0034     return mMapPermissions.values().toVector();
0035 }
0036 
0037 bool PermissionManager::contains(const QString &permissionId) const
0038 {
0039     return mMapPermissions.contains(permissionId);
0040 }
0041 
0042 QStringList PermissionManager::roles(const QString &permissionId) const
0043 {
0044     const Permission p = mMapPermissions.value(permissionId);
0045     if (p.isValid()) {
0046         return p.roles();
0047     }
0048     return {};
0049 }
0050 
0051 int PermissionManager::permissionCount() const
0052 {
0053     return mMapPermissions.count();
0054 }
0055 
0056 void PermissionManager::parseRemovePermission(const QJsonArray &removeArray)
0057 {
0058     qCWarning(RUQOLA_LOG) << " void PermissionManager::parseRemovePermission(const QJsonArray &removeArray) not implemented yet" << removeArray;
0059 }
0060 
0061 bool PermissionManager::storePermission(const QString &id)
0062 {
0063     return id == QLatin1String("mail-messages") || id == QLatin1String("pin-message") || id == QLatin1String("delete-own-message")
0064         || id == QLatin1String("create-invite-links") || id == QLatin1String("edit-message") || id == QLatin1String("delete-message")
0065         || id == QLatin1String("edit-room") || id == QLatin1String("clean-channel-history") || id == QLatin1String("auto-translate")
0066         || id == QLatin1String("snippet-message") || id == QLatin1String("force-delete-message") || id == QLatin1String("edit-room-avatar")
0067         || id == QLatin1String("create-c") || id == QLatin1String("add-user-to-joined-room") || id == QLatin1String("leave-c")
0068         || id == QLatin1String("create-p") || id == QLatin1String("create-d") || id == QLatin1String("leave-p") || id == QLatin1String("add-user-to-any-c-room")
0069         || id == QLatin1String("add-user-to-any-p-room") || id == QLatin1String("view-logs") || id == QLatin1String("unarchive-room")
0070         || id == QLatin1String("archive-room") || id == QLatin1String("mute-user") || id == QLatin1String("remove-user") || id == QLatin1String("view-c-room")
0071         || id == QLatin1String("view-d-room") || id == QLatin1String("view-outside-room") || id == QLatin1String("view-statistics")
0072         || id == QLatin1String("post-readonly") || id == QLatin1String("manage-sounds") || id == QLatin1String("set-moderator")
0073         || id == QLatin1String("set-leader") || id == QLatin1String("set-owner") || id == QLatin1String("delete-user")
0074         || id == QLatin1String("edit-room-retention-policy") || id == QLatin1String("access-setting-permissions") || id == QLatin1String("start-discussion")
0075         || id == QLatin1String("start-discussion-other-user") || id == QLatin1String("force-delete-message") || id == QLatin1String("create-team")
0076         || id == QLatin1String("convert-team") || id == QLatin1String("delete-team") || id == QLatin1String("edit-team")
0077         || id == QLatin1String("add-team-member") || id == QLatin1String("edit-team-member") || id == QLatin1String("add-team-channel")
0078         || id == QLatin1String("edit-team-channel") || id == QLatin1String("remove-team-channel") || id == QLatin1String("view-all-team-channels")
0079         || id == QLatin1String("view-all-teams") || id == QLatin1String("access-permissions") || id == QLatin1String("edit-other-user-e2ee")
0080         || id == QLatin1String("edit-other-user-totp") || id == QLatin1String("assign-admin-role") || id == QLatin1String("edit-other-user-info")
0081         || id == QLatin1String("edit-other-user-active-status") || id == QLatin1String("logout-device-management")
0082         || id == QLatin1String("view-device-management") || id == QLatin1String("create-personal-access-tokens") || id == QLatin1String("manage-oauth-apps")
0083         || id == QLatin1String("create-user") || id == QLatin1String("bulk-register-user")
0084         || id == QLatin1String("view-broadcast-member-list") /* TODO implement view-broadcast-member-list */
0085         || id == QLatin1String("videoconf-ring-users") || id == QLatin1String("edit-other-user-password") || id == QLatin1String("view-moderation-console")
0086         || id == QLatin1String("manage-moderation-actions");
0087 }
0088 
0089 void PermissionManager::parseUpdatePermission(const QJsonArray &updateArray)
0090 {
0091     for (int i = 0; i < updateArray.count(); ++i) {
0092         const QJsonObject obj = updateArray.at(i).toObject();
0093         const QString id = obj[QLatin1String("_id")].toString();
0094         if (storePermission(id)) {
0095             Permission p;
0096             p.parsePermission(obj /* add roles? */);
0097             // qDebug() << "obj " << obj;
0098             if (p.isValid()) {
0099                 mMapPermissions.insert(id, p);
0100             }
0101         } else {
0102             qCDebug(RUQOLA_LOG) << "Permission id not stored: " << id;
0103         }
0104     }
0105     // qDebug() << "mMapPermissions " << mMapPermissions;
0106 }
0107 
0108 bool PermissionManager::updatePermission(const QJsonArray &updateArray)
0109 {
0110     bool updatedPermission = false;
0111     if (updateArray.count() == 2) {
0112         if (updateArray.at(0).toString() == QLatin1String("updated")) {
0113             const QJsonObject obj = updateArray.at(1).toObject();
0114             const QString id = obj[QLatin1String("_id")].toString();
0115             if (storePermission(id)) {
0116                 Permission p;
0117                 p.parsePermission(obj, {} /* add roles? */, false); // We use date from ddpclient not restapi
0118                 if (p.isValid()) {
0119                     mMapPermissions.insert(id, p);
0120                     updatedPermission = true;
0121                 }
0122             }
0123         }
0124     } else {
0125         qCWarning(RUQOLA_LOG) << " PermissionManager::updatePermission invalid updateArray count " << updateArray.count();
0126     }
0127     return updatedPermission;
0128     // QJsonObject({"args":["updated",{"_id":"access-mailer","_updatedAt":{"$date":1634569746270},"roles":["admin","vFXCWG9trXLti6xQm"]}],"eventName":"permissions-changed"}
0129 }