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

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 "permissions.h"
0008 #include "ruqola_debug.h"
0009 
0010 #include <QJsonArray>
0011 #include <QJsonObject>
0012 
0013 Permissions::Permissions() = default;
0014 
0015 QVector<Permission> Permissions::permissions() const
0016 {
0017     return mPermissions;
0018 }
0019 
0020 int Permissions::count() const
0021 {
0022     return mPermissions.count();
0023 }
0024 
0025 void Permissions::clear()
0026 {
0027     mPermissions.clear();
0028 }
0029 
0030 Permission Permissions::at(int index) const
0031 {
0032     if (index < 0 || index > mPermissions.count()) {
0033         qCWarning(RUQOLA_LOG) << "Invalid index " << index;
0034         return {};
0035     }
0036     return mPermissions.at(index);
0037 }
0038 
0039 bool Permissions::isEmpty() const
0040 {
0041     return mPermissions.isEmpty();
0042 }
0043 
0044 void Permissions::setPermissions(const QVector<Permission> &roles)
0045 {
0046     mPermissions = roles;
0047 }
0048 
0049 void Permissions::parsePermissions(const QJsonObject &obj, const QString &str, const QVector<RoleInfo> &roleInfo)
0050 {
0051     mPermissions.clear();
0052 
0053     const QJsonArray permissionArray = obj[str.isEmpty() ? QStringLiteral("update") : str].toArray();
0054     const auto roleArrayCount = permissionArray.count();
0055     mPermissions.reserve(roleArrayCount);
0056     for (auto i = 0; i < roleArrayCount; ++i) {
0057         Permission r;
0058         if (r.parsePermission(permissionArray.at(i).toObject(), roleInfo)) {
0059             if (r.isValid()) {
0060                 mPermissions.append(std::move(r));
0061             } else {
0062                 qCWarning(RUQOLA_LOG) << "Invalid permission: " << permissionArray.at(i).toObject();
0063             }
0064         }
0065     }
0066 }