Kgapi

permission.cpp
1/*
2 SPDX-FileCopyrightText: 2012 Andrius da Costa Ribas <andriusmao@gmail.com>
3
4 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5*/
6
7#include "permission.h"
8#include "permission_p.h"
9#include "utils_p.h"
10
11#include <QJsonDocument>
12
13using namespace KGAPI2;
14using namespace KGAPI2::Drive;
15
16class Q_DECL_HIDDEN Permission::PermissionDetails::Private
17{
18public:
19 Private() = default;
20 Private(const Private &other) = default;
21
22 PermissionDetails::PermissionType permissionType = PermissionType::UndefinedType;
23 Role role;
24 QList<Role> additionalRoles;
25 QString inheritedFrom;
26 bool inherited = false;
27
28 static PermissionType permissionTypeFromName(const QString &typeName);
29 static QString permissionTypeToName(PermissionType permissionType);
30};
31
32Permission::PermissionDetails::PermissionDetails()
33 : d(new Private)
34{
35}
36
37Permission::PermissionDetails::PermissionDetails(const Permission::PermissionDetails &other)
38 : d(new Private(*(other.d)))
39{
40}
41
42Permission::PermissionDetails::~PermissionDetails() = default;
43
44bool Permission::PermissionDetails::operator==(const Permission::PermissionDetails &other) const
45{
46 GAPI_COMPARE(permissionType);
47 GAPI_COMPARE(role);
48 GAPI_COMPARE(additionalRoles);
49 GAPI_COMPARE(inheritedFrom);
50 GAPI_COMPARE(inherited);
51 return true;
52}
53
54QString Permission::PermissionDetails::Private::permissionTypeToName(Permission::PermissionDetails::PermissionType permissionType)
55{
56 switch (permissionType) {
57 case Permission::PermissionDetails::TypeFile:
58 return QStringLiteral("file");
59 case Permission::PermissionDetails::TypeMember:
60 return QStringLiteral("member");
61 default:
62 return QString();
63 }
64}
65
66Permission::PermissionDetails::PermissionType Permission::PermissionDetails::Private::permissionTypeFromName(const QString &typeName)
67{
68 if (typeName == QLatin1StringView("file")) {
69 return Permission::PermissionDetails::TypeFile;
70 } else if (typeName == QLatin1StringView("member")) {
71 return Permission::PermissionDetails::TypeMember;
72 } else {
73 return Permission::PermissionDetails::UndefinedType;
74 }
75}
76
77Permission::PermissionDetails::PermissionType Permission::PermissionDetails::permissionType() const
78{
79 return d->permissionType;
80}
81
83{
84 return d->role;
85}
86
88{
89 return d->additionalRoles;
90}
91
93{
94 return d->inheritedFrom;
95}
96
98{
99 return d->inherited;
100}
101
102Permission::Role Permission::Private::roleFromName(const QString &roleName)
103{
104 if (roleName == QLatin1StringView("owner")) {
105 return Permission::OwnerRole;
106 } else if (roleName == QLatin1StringView("reader")) {
107 return Permission::ReaderRole;
108 } else if (roleName == QLatin1StringView("writer")) {
109 return Permission::WriterRole;
110 } else if (roleName == QLatin1StringView("commenter")) {
111 return Permission::CommenterRole;
112 } else if (roleName == QLatin1StringView("organizer")) {
113 return Permission::OrganizerRole;
114 } else if (roleName == QLatin1StringView("fileOrganizer")) {
115 return Permission::FileOrganizerRole;
116 } else {
117 return Permission::UndefinedRole;
118 }
119}
120
121Permission::Type Permission::Private::typeFromName(const QString &typeName)
122{
123 if (typeName == QLatin1StringView("user")) {
124 return Permission::TypeUser;
125 } else if (typeName == QLatin1StringView("group")) {
126 return Permission::TypeGroup;
127 } else if (typeName == QLatin1StringView("domain")) {
128 return Permission::TypeDomain;
129 } else if (typeName == QLatin1StringView("anyone")) {
130 return Permission::TypeAnyone;
131 } else {
132 return Permission::UndefinedType;
133 }
134}
135
136QString Permission::Private::roleToName(Permission::Role role)
137{
138 switch (role) {
139 case Permission::OwnerRole:
140 return QStringLiteral("owner");
141 case Permission::ReaderRole:
142 return QStringLiteral("reader");
143 case Permission::WriterRole:
144 return QStringLiteral("writer");
145 case Permission::CommenterRole:
146 return QStringLiteral("commenter");
147 case Permission::OrganizerRole:
148 return QStringLiteral("organizerRole");
149 case Permission::FileOrganizerRole:
150 return QStringLiteral("fileOrganizerRole");
151 default:
152 return QString();
153 }
154}
155
156QString Permission::Private::typeToName(Permission::Type type)
157{
158 switch (type) {
159 case Permission::TypeUser:
160 return QStringLiteral("user");
161 case Permission::TypeGroup:
162 return QStringLiteral("group");
163 case Permission::TypeDomain:
164 return QStringLiteral("domain");
165 case Permission::TypeAnyone:
166 return QStringLiteral("anyone");
167 default:
168 return QString();
169 }
170}
171
172PermissionPtr Permission::Private::fromJSON(const QVariantMap &map)
173{
174 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#permission")) {
175 return PermissionPtr();
176 }
177
178 PermissionPtr permission(new Permission());
179 permission->setEtag(map[QStringLiteral("etag")].toString());
180 permission->d->id = map[QStringLiteral("id")].toString();
181 permission->d->selfLink = map[QStringLiteral("selfLink")].toUrl();
182 permission->d->name = map[QStringLiteral("name")].toString();
183
184 permission->d->role = Private::roleFromName(map[QStringLiteral("role")].toString());
185
186 const QStringList additionalRoles = map[QStringLiteral("additionalRoles")].toStringList();
187 for (const QString &additionalRole : additionalRoles) {
188 permission->d->additionalRoles << Private::roleFromName(additionalRole);
189 }
190
191 permission->d->type = Private::typeFromName(map[QStringLiteral("type")].toString());
192 permission->d->authKey = map[QStringLiteral("authKey")].toString();
193 permission->d->withLink = map[QStringLiteral("withLink")].toBool();
194 permission->d->photoLink = map[QStringLiteral("photoLink")].toUrl();
195 permission->d->value = map[QStringLiteral("value")].toString();
196 permission->d->emailAddress = map[QStringLiteral("emailAddress")].toString();
197 permission->d->domain = map[QStringLiteral("domain")].toString();
198 permission->d->expirationDate = QDateTime::fromString(map[QStringLiteral("expirationDate")].toString(), Qt::ISODate);
199 permission->d->deleted = map[QStringLiteral("deleted")].toBool();
200
201 if (map.contains(QStringLiteral("permissionDetails"))) {
202 const QVariantList permissionDetailsList = map[QStringLiteral("permissionDetails")].toList();
203 for (const QVariant &variant : permissionDetailsList) {
204 const QVariantMap permissionDetailsMap = variant.toMap();
206 permissionDetails->d->permissionType =
207 PermissionDetails::Private::permissionTypeFromName(permissionDetailsMap[QStringLiteral("permissionType")].toString());
208 permissionDetails->d->role = Private::roleFromName(permissionDetailsMap[QStringLiteral("role")].toString());
209 const QStringList permissionDetailsAdditionalRoles = permissionDetailsMap[QStringLiteral("additionalRoles")].toStringList();
210 for (const QString &additionalRole : permissionDetailsAdditionalRoles) {
211 permissionDetails->d->additionalRoles << Private::roleFromName(additionalRole);
212 }
213 permissionDetails->d->inheritedFrom = permissionDetailsMap[QStringLiteral("inheritedFrom")].toString();
214 permissionDetails->d->inherited = map[QStringLiteral("inherited")].toBool();
215
216 permission->d->permissionDetails << permissionDetails;
217 }
218 }
219
220 return permission;
221}
222
223Permission::Private::Private()
224 : role(Permission::UndefinedRole)
225 , type(Permission::UndefinedType)
226 , withLink(false)
227{
228}
229
230Permission::Private::Private(const Private &other) = default;
231
232Permission::Permission()
233 : KGAPI2::Object()
234 , d(new Private)
235{
236}
237
238Permission::Permission(const Permission &other)
239 : KGAPI2::Object(other)
240 , d(new Private(*(other.d)))
241{
242}
243
244Permission::~Permission()
245{
246 delete d;
247}
248
249bool Permission::operator==(const Permission &other) const
250{
251 if (!Object::operator==(other)) {
252 return false;
253 }
254 GAPI_COMPARE(id)
255 GAPI_COMPARE(selfLink)
256 GAPI_COMPARE(name)
257 GAPI_COMPARE(role)
258 GAPI_COMPARE(additionalRoles)
259 GAPI_COMPARE(type)
260 GAPI_COMPARE(authKey)
261 GAPI_COMPARE(withLink)
262 GAPI_COMPARE(photoLink)
263 GAPI_COMPARE(value)
264 GAPI_COMPARE(emailAddress)
265 GAPI_COMPARE(domain)
266 GAPI_COMPARE(expirationDate)
267 GAPI_COMPARE(deleted)
268 GAPI_COMPARE(permissionDetails)
269 return true;
270}
271
273{
274 return d->id;
275}
276
278{
279 d->id = id;
280}
281
283{
284 return d->selfLink;
285}
286
288{
289 return d->name;
290}
291
292Permission::Role Permission::role() const
293{
294 return d->role;
295}
296
297void Permission::setRole(Permission::Role role)
298{
299 d->role = role;
300}
301
303{
304 return d->additionalRoles;
305}
306
311
312Permission::Type Permission::type() const
313{
314 return d->type;
315}
316
317void Permission::setType(Permission::Type type)
318{
319 d->type = type;
320}
321
323{
324 return d->authKey;
325}
326
328{
329 return d->withLink;
330}
331
333{
334 d->withLink = withLink;
335}
336
338{
339 return d->photoLink;
340}
341
343{
344 return d->value;
345}
346
348{
349 d->value = value;
350}
351
353{
354 return d->emailAddress;
355}
356
358{
359 return d->domain;
360}
361
363{
364 return d->expirationDate;
365}
366
368{
369 return d->deleted;
370}
371
373{
374 return d->permissionDetails;
375}
376
377PermissionPtr Permission::fromJSON(const QByteArray &jsonData)
378{
379 QJsonDocument document = QJsonDocument::fromJson(jsonData);
380 if (document.isNull()) {
381 return PermissionPtr();
382 }
383 const QVariant json = document.toVariant();
384 const QVariantMap map = json.toMap();
385
386 return Private::fromJSON(map);
387}
388
389PermissionsList Permission::fromJSONFeed(const QByteArray &jsonData)
390{
391 QJsonDocument document = QJsonDocument::fromJson(jsonData);
392 if (document.isNull()) {
393 return PermissionsList();
394 }
395 const QVariant json = document.toVariant();
396 const QVariantMap map = json.toMap();
397 if (!map.contains(QLatin1StringView("kind")) || map[QStringLiteral("kind")].toString() != QLatin1StringView("drive#permissionList")) {
398 return PermissionsList();
399 }
400
401 PermissionsList permissions;
402 const QVariantList items = map[QStringLiteral("items")].toList();
403 for (const QVariant &item : items) {
404 const PermissionPtr permission = Private::fromJSON(item.toMap());
405 if (!permission.isNull()) {
406 permissions << permission;
407 }
408 }
409 return permissions;
410}
411
412QByteArray Permission::toJSON(const PermissionPtr &permission)
413{
414 QVariantMap map;
415
416 if (permission->role() != Permission::UndefinedRole) {
417 map[QStringLiteral("role")] = Private::roleToName(permission->role());
418 }
419 if (permission->type() != Permission::UndefinedType) {
420 map[QStringLiteral("type")] = Private::typeToName(permission->type());
421 }
422
423 QVariantList additionalRoles;
424 const auto roles = permission->additionalRoles();
425 additionalRoles.reserve(roles.size());
426 for (Permission::Role additionalRole : roles) {
427 additionalRoles << Private::roleToName(additionalRole);
428 }
429 if (!additionalRoles.isEmpty()) {
430 map[QStringLiteral("additionalRoles")] = additionalRoles;
431 }
432
433 map[QStringLiteral("withLink")] = permission->withLink();
434
435 if (!permission->value().isEmpty()) {
436 map[QStringLiteral("value")] = permission->value();
437 }
438
440 return document.toJson(QJsonDocument::Compact);
441}
Details of whether the permissions on this shared drive item are inherited or directly on this item.
Definition permission.h:54
PermissionDetails::PermissionType permissionType() const
The permission type for this user.
QList< Permission::Role > additionalRoles() const
Additional roles for this user.
bool inherited() const
Whether this permission is inherited.
QString inheritedFrom() const
The ID of the item from which this permission is inherited.
Permission::Role role() const
The primary role for this user.
Permission contains a permission for a file.
Definition permission.h:34
void setWithLink(bool withLink)
Sets whether the link is required for this permission.
bool withLink() const
Returns whether the link is required for this permission.
Permission::Type type() const
Returns the account type.
QString value() const
Returns the email address or domain name for the entity.
QUrl selfLink() const
Returns a link back to this permission.
void setAdditionalRoles(const QList< Role > &additionalRoles)
Sets additional roles for this user.
QString authKey() const
Returns the authkey parameter required for this permission.
void setId(const QString &id)
Sets the id of the permission.
QString id() const
Returns the id of the permission.
void setRole(Permission::Role role)
Sets the primary role for this user.
QUrl photoLink() const
Returns a link to the profile photo, if available.
bool deleted() const
Whether the account associated with this permission has been deleted.
void setType(Permission::Type type)
Sets the account type.
Permission::Role role() const
Returns the primary role for this user.
Permission::PermissionDetailsList permissionDetails() const
Details of whether the permissions on this shared drive item are inherited or directly on this item.
QString emailAddress() const
The email address of the user or group this permission refers to.
QDateTime expirationDate() const
The time at which this permission will expire.
void setValue(const QString &value)
Sets the email address or domain name for the entity.
QString name() const
Returns the name of this permission.
QList< Role > additionalRoles() const
Returns additional roles for this user.
QString domain() const
The domain name of the entity this permission refers to.
Base class for all objects.
Definition object.h:31
char * toString(const EngineQuery &query)
A job to fetch a single map tile described by a StaticMapUrl.
Definition blog.h:16
QDateTime fromString(QStringView string, QStringView format, QCalendar cal)
QJsonDocument fromJson(const QByteArray &json, QJsonParseError *error)
QJsonDocument fromVariant(const QVariant &variant)
bool isNull() const const
QByteArray toJson(JsonFormat format) const const
QVariant toVariant() const const
bool isEmpty() const const
void reserve(qsizetype size)
QSharedPointer< T > create(Args &&... args)
bool isNull() const const
QFuture< void > map(Iterator begin, Iterator end, MapFunctor &&function)
QMap< QString, QVariant > toMap() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Sat Dec 21 2024 17:00:12 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.