File indexing completed on 2024-12-22 04:45:23
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 "permissionupdatejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 0011 #include <QJsonArray> 0012 #include <QJsonDocument> 0013 #include <QJsonObject> 0014 #include <QNetworkReply> 0015 using namespace RocketChatRestApi; 0016 PermissionUpdateJob::PermissionUpdateJob(QObject *parent) 0017 : RestApiAbstractJob(parent) 0018 { 0019 } 0020 0021 PermissionUpdateJob::~PermissionUpdateJob() = default; 0022 0023 bool PermissionUpdateJob::start() 0024 { 0025 if (!canStart()) { 0026 deleteLater(); 0027 return false; 0028 } 0029 addStartRestApiInfo("PermissionUpdateJob::start"); 0030 submitPostRequest(json()); 0031 0032 return true; 0033 } 0034 0035 void PermissionUpdateJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0036 { 0037 const QJsonObject replyObject = replyJson.object(); 0038 if (replyObject[QLatin1String("success")].toBool()) { 0039 addLoggerInfo(QByteArrayLiteral("PermissionUpdateJob success: ") + replyJson.toJson(QJsonDocument::Indented)); 0040 Q_EMIT permissionUpdateDone(replyObject); 0041 } else { 0042 emitFailedMessage(replyErrorString, replyObject); 0043 addLoggerWarning(QByteArrayLiteral("PermissionUpdateJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0044 } 0045 } 0046 0047 const QMap<QString, QStringList> &PermissionUpdateJob::permissions() const 0048 { 0049 return mPermissions; 0050 } 0051 0052 void PermissionUpdateJob::setPermissions(const QMap<QString, QStringList> &newPermissions) 0053 { 0054 mPermissions = newPermissions; 0055 } 0056 0057 bool PermissionUpdateJob::requireHttpAuthentication() const 0058 { 0059 return true; 0060 } 0061 0062 bool PermissionUpdateJob::canStart() const 0063 { 0064 if (!RestApiAbstractJob::canStart()) { 0065 return false; 0066 } 0067 if (mPermissions.isEmpty()) { 0068 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "PermissionUpdateJob: mPermissions is empty"; 0069 return false; 0070 } 0071 return true; 0072 } 0073 0074 QNetworkRequest PermissionUpdateJob::request() const 0075 { 0076 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::PermissionsUpdate); 0077 QNetworkRequest request(url); 0078 addAuthRawHeader(request); 0079 addRequestAttribute(request); 0080 return request; 0081 } 0082 0083 QJsonDocument PermissionUpdateJob::json() const 0084 { 0085 QJsonObject jsonObj; 0086 QJsonArray array; 0087 QMapIterator<QString, QStringList> i(mPermissions); 0088 while (i.hasNext()) { 0089 i.next(); 0090 QJsonObject obj; 0091 obj[QLatin1String("_id")] = i.key(); 0092 obj[QLatin1String("roles")] = QJsonArray::fromStringList(i.value()); 0093 array.append(std::move(obj)); 0094 } 0095 jsonObj[QLatin1String("permissions")] = array; 0096 const QJsonDocument postData = QJsonDocument(jsonObj); 0097 return postData; 0098 } 0099 0100 #include "moc_permissionupdatejob.cpp"