1 /*
2    SPDX-FileCopyrightText: 2020-2021 Laurent Montel <montel@kde.org>
3 
4    SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "permission.h"
8 
9 #include "utils.h"
10 
11 Permission::Permission() = default;
12 
parsePermission(const QJsonObject & replyObject,const QVector<RoleInfo> & roleInfo,bool restApi)13 bool Permission::parsePermission(const QJsonObject &replyObject, const QVector<RoleInfo> &roleInfo, bool restApi)
14 {
15     // Don't store settings value.
16     if (!replyObject.value(QLatin1String("settingId")).toString().isEmpty()) {
17         return false;
18     }
19     mIdentifier = replyObject.value(QLatin1String("_id")).toString();
20     if (restApi) {
21         mUpdatedAt = Utils::parseIsoDate(QStringLiteral("_updatedAt"), replyObject);
22     } else {
23         mUpdatedAt = Utils::parseDate(QStringLiteral("_updatedAt"), replyObject);
24     }
25     const QJsonArray roleArray = replyObject.value(QLatin1String("roles")).toArray();
26     mRoles.reserve(roleArray.count());
27     for (int i = 0; i < roleArray.count(); ++i) {
28         const QString role{roleArray.at(i).toString()};
29         mRoles.append(role);
30         for (const RoleInfo &info : roleInfo) {
31             if (role == info.identifier()) {
32                 mRolesStr.append(info.name());
33                 break;
34             }
35         }
36     }
37     return true;
38 }
39 
roles() const40 QStringList Permission::roles() const
41 {
42     return mRoles;
43 }
44 
setRoles(const QStringList & newRoles)45 void Permission::setRoles(const QStringList &newRoles)
46 {
47     mRoles = newRoles;
48 }
49 
updatedAt() const50 qint64 Permission::updatedAt() const
51 {
52     return mUpdatedAt;
53 }
54 
setUpdatedAt(qint64 newUpdatedAt)55 void Permission::setUpdatedAt(qint64 newUpdatedAt)
56 {
57     mUpdatedAt = newUpdatedAt;
58 }
59 
isValid() const60 bool Permission::isValid() const
61 {
62     return mUpdatedAt != -1;
63 }
64 
identifier() const65 const QString &Permission::identifier() const
66 {
67     return mIdentifier;
68 }
69 
setIdentifier(const QString & newIdentifier)70 void Permission::setIdentifier(const QString &newIdentifier)
71 {
72     mIdentifier = newIdentifier;
73 }
74 
rolesStr() const75 const QStringList &Permission::rolesStr() const
76 {
77     return mRolesStr;
78 }
79 
operator ==(const Permission & other) const80 bool Permission::operator==(const Permission &other) const
81 {
82     return other.roles() == roles() && other.updatedAt() == updatedAt() && other.identifier() == identifier();
83 }
84 
operator <<(QDebug d,const Permission & t)85 QDebug operator<<(QDebug d, const Permission &t)
86 {
87     d << "roles : " << t.roles();
88     d << "rolesStr : " << t.rolesStr();
89     d << "mUpdatedAt " << t.updatedAt();
90     d << "mIdentifier " << t.identifier();
91     return d;
92 }
93