File indexing completed on 2024-05-12 16:25:56

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "rolesmanager.h"
0008 #include "ruqola_debug.h"
0009 #include <QJsonArray>
0010 RolesManager::RolesManager(QObject *parent)
0011     : QObject{parent}
0012 {
0013 }
0014 
0015 RolesManager::~RolesManager() = default;
0016 
0017 void RolesManager::parseRoles(const QJsonObject &obj)
0018 {
0019     const QJsonArray array = obj[QLatin1String("roles")].toArray();
0020 
0021     mRoleInfo.reserve(array.count());
0022     for (const QJsonValue &current : array) {
0023         const QJsonObject roleObject = current.toObject();
0024         RoleInfo info;
0025         info.parseRoleInfo(roleObject);
0026         mRoleInfo.append(std::move(info));
0027     }
0028 }
0029 
0030 void RolesManager::updateRoles(const QJsonArray &contents)
0031 {
0032     bool wasChanged = false;
0033     for (const QJsonValue &current : contents) {
0034         const QJsonObject roleObject = current.toObject();
0035         const QString type = roleObject.value(QStringLiteral("type")).toString();
0036         const QString identifier = roleObject.value(QStringLiteral("_id")).toString();
0037         if (type == QLatin1String("removed")) {
0038             for (int i = 0; i < mRoleInfo.count(); ++i) {
0039                 if (mRoleInfo.at(i).identifier() == identifier) {
0040                     mRoleInfo.removeAt(i);
0041                     wasChanged = true;
0042                     break;
0043                 }
0044             }
0045         } else if (type == QLatin1String("changed")) {
0046             bool found = false;
0047             RoleInfo info;
0048             info.parseRoleInfo(roleObject);
0049             for (int i = 0, total = mRoleInfo.count(); i < total; ++i) {
0050                 if (mRoleInfo.at(i).identifier() == identifier) {
0051                     mRoleInfo.removeAt(i);
0052                     mRoleInfo.append(info);
0053                     found = true;
0054                     wasChanged = true;
0055                     break;
0056                 }
0057             }
0058             if (!found) { // Insert it.
0059                 mRoleInfo.append(info);
0060                 wasChanged = true;
0061             }
0062         } else {
0063             qCWarning(RUQOLA_LOG) << " No defined type" << type;
0064         }
0065     }
0066     if (wasChanged) {
0067         Q_EMIT rolesChanged();
0068     }
0069     // QJsonObject({"args":[{"_id":"vFXCWG9trXLti6xQm","name":"vFXCWG9trXLti6xQm","type":"removed"}],"eventName":"roles"})
0070     // QJsonObject({"args":[{"_id":"hiafuM2enNapgD2mg","_updatedAt":{"$date":1634588706596},"description":"","mandatory2fa":false,"name":"test4","protected":false,"scope":"Users","type":"changed"}],"eventName":"roles"})
0071 }
0072 
0073 const QVector<RoleInfo> &RolesManager::roleInfo() const
0074 {
0075     return mRoleInfo;
0076 }
0077 
0078 #include "moc_rolesmanager.cpp"