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 "permission.h"
0008 
0009 #include "utils.h"
0010 
0011 Permission::Permission() = default;
0012 
0013 bool Permission::parsePermission(const QJsonObject &replyObject, const QVector<RoleInfo> &roleInfo, bool restApi)
0014 {
0015     // Don't store settings value.
0016     if (!replyObject.value(QLatin1String("settingId")).toString().isEmpty()) {
0017         return false;
0018     }
0019     mIdentifier = replyObject.value(QLatin1String("_id")).toString();
0020     if (restApi) {
0021         mUpdatedAt = Utils::parseIsoDate(QStringLiteral("_updatedAt"), replyObject);
0022     } else {
0023         mUpdatedAt = Utils::parseDate(QStringLiteral("_updatedAt"), replyObject);
0024     }
0025     const QJsonArray roleArray = replyObject.value(QLatin1String("roles")).toArray();
0026     const auto roleArrayCount{roleArray.count()};
0027     mRoles.reserve(roleArrayCount);
0028     for (int i = 0; i < roleArrayCount; ++i) {
0029         const QString role{roleArray.at(i).toString()};
0030         mRoles.append(role);
0031         if (roleInfo.isEmpty()) {
0032             mRolesStr.append(role);
0033         } else {
0034             auto index = std::find_if(roleInfo.begin(), roleInfo.end(), [role](const RoleInfo &info) {
0035                 return (role == info.identifier());
0036             });
0037             if (index != roleInfo.end()) {
0038                 mRolesStr.append((*index).name());
0039             }
0040         }
0041     }
0042     return true;
0043 }
0044 
0045 QStringList Permission::roles() const
0046 {
0047     return mRoles;
0048 }
0049 
0050 void Permission::setRoles(const QStringList &newRoles)
0051 {
0052     mRoles = newRoles;
0053 }
0054 
0055 qint64 Permission::updatedAt() const
0056 {
0057     return mUpdatedAt;
0058 }
0059 
0060 void Permission::setUpdatedAt(qint64 newUpdatedAt)
0061 {
0062     mUpdatedAt = newUpdatedAt;
0063 }
0064 
0065 bool Permission::isValid() const
0066 {
0067     return mUpdatedAt != -1;
0068 }
0069 
0070 const QString &Permission::identifier() const
0071 {
0072     return mIdentifier;
0073 }
0074 
0075 void Permission::setIdentifier(const QString &newIdentifier)
0076 {
0077     mIdentifier = newIdentifier;
0078 }
0079 
0080 const QStringList &Permission::rolesStr() const
0081 {
0082     return mRolesStr;
0083 }
0084 
0085 bool Permission::operator==(const Permission &other) const
0086 {
0087     return other.roles() == roles() && other.updatedAt() == updatedAt() && other.identifier() == identifier();
0088 }
0089 
0090 QDebug operator<<(QDebug d, const Permission &t)
0091 {
0092     d << "roles : " << t.roles();
0093     d << "rolesStr : " << t.rolesStr();
0094     d << "mUpdatedAt " << t.updatedAt();
0095     d << "mIdentifier " << t.identifier();
0096     return d;
0097 }