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

0001 /*
0002    SPDX-FileCopyrightText: 2019-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "roles.h"
0008 #include "ruqola_debug.h"
0009 
0010 #include <QJsonArray>
0011 
0012 Roles::Roles() = default;
0013 
0014 QVector<Role> Roles::roles() const
0015 {
0016     return mRoles;
0017 }
0018 
0019 bool Roles::isEmpty() const
0020 {
0021     return mRoles.isEmpty();
0022 }
0023 
0024 void Roles::setRoles(const QVector<Role> &roles)
0025 {
0026     mRoles = roles;
0027 }
0028 
0029 void Roles::updateRoles(const QJsonObject &obj)
0030 {
0031     const QString type = obj[QLatin1String("type")].toString();
0032     const QString id = obj[QLatin1String("_id")].toString();
0033     const QString userId = obj[QLatin1String("u")].toObject().value(QLatin1String("_id")).toString();
0034     bool foundUser = false;
0035     // qDebug() << " type " << type << " id " << id << " userId" << userId;
0036     if (type == QLatin1String("added")) {
0037         for (int i = 0, total = mRoles.count(); i < total; ++i) {
0038             if (mRoles.at(i).userId() == userId) {
0039                 Role &r = mRoles[i];
0040                 r.updateRole(id, true);
0041                 foundUser = true;
0042                 break;
0043             }
0044         }
0045         if (!foundUser) {
0046             Role r;
0047             r.setUserId(userId);
0048             r.updateRole(id, true);
0049             mRoles.append(std::move(r));
0050         }
0051     } else if (type == QLatin1String("removed")) {
0052         for (int i = 0, total = mRoles.count(); i < total; ++i) {
0053             if (mRoles.at(i).userId() == userId) {
0054                 Role r = mRoles.takeAt(i);
0055                 r.updateRole(id, false);
0056                 if (r.hasARole()) {
0057                     mRoles.append(std::move(r));
0058                 }
0059                 foundUser = true;
0060                 break;
0061             }
0062         }
0063         if (!foundUser) {
0064             qCWarning(RUQOLA_LOG) << "Problem you want to remove role for an not existing role! it seems to be a bug ";
0065         }
0066     } else {
0067         qCWarning(RUQOLA_LOG) << "Unknown change role type " << type;
0068     }
0069 }
0070 
0071 int Roles::count() const
0072 {
0073     return mRoles.count();
0074 }
0075 
0076 Role Roles::at(int index) const
0077 {
0078     if (index < 0 || index > mRoles.count()) {
0079         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0080         return {};
0081     }
0082     return mRoles.at(index);
0083 }
0084 
0085 void Roles::parseRole(const QJsonObject &obj)
0086 {
0087     mRoles.clear();
0088 
0089     const QJsonArray roleArray = obj[QLatin1String("roles")].toArray();
0090     const auto roleArrayCount = roleArray.count();
0091     mRoles.reserve(roleArrayCount);
0092     for (auto i = 0; i < roleArrayCount; ++i) {
0093         Role r;
0094         r.parseRole(roleArray.at(i).toObject());
0095         if (r.isValid()) {
0096             mRoles.append(std::move(r));
0097         } else {
0098             qCWarning(RUQOLA_LOG) << "Invalid role" << roleArray.at(i).toObject();
0099         }
0100     }
0101 }
0102 
0103 Role Roles::findRoleByUserId(const QString &userId) const
0104 {
0105     for (const Role &r : std::as_const(mRoles)) {
0106         if (r.userId() == userId) {
0107             return r;
0108         }
0109     }
0110     return {};
0111 }
0112 
0113 QDebug operator<<(QDebug d, const Roles &t)
0114 {
0115     d << " count " << t.count();
0116     for (int i = 0; i < t.count(); i++) {
0117         d << t.at(i) << "\n";
0118     }
0119     return d;
0120 }