File indexing completed on 2024-12-22 04:45:24
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 "rolecreatejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 0011 #include <KLocalizedString> 0012 0013 #include <QJsonDocument> 0014 #include <QJsonObject> 0015 #include <QNetworkReply> 0016 using namespace RocketChatRestApi; 0017 RoleCreateJob::RoleCreateJob(QObject *parent) 0018 : RestApiAbstractJob(parent) 0019 { 0020 } 0021 0022 RoleCreateJob::~RoleCreateJob() = default; 0023 0024 bool RoleCreateJob::start() 0025 { 0026 if (!canStart()) { 0027 deleteLater(); 0028 return false; 0029 } 0030 addStartRestApiInfo("RoleCreateJob::start"); 0031 submitPostRequest(json()); 0032 return true; 0033 } 0034 0035 void RoleCreateJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0036 { 0037 const QJsonObject replyObject = replyJson.object(); 0038 if (replyObject[QLatin1String("success")].toBool()) { 0039 addLoggerInfo(QByteArrayLiteral("RoleCreateJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0040 Q_EMIT createRoleDone(); 0041 } else { 0042 emitFailedMessage(replyErrorString, replyObject); 0043 addLoggerWarning(QByteArrayLiteral("RoleCreateJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0044 } 0045 } 0046 0047 RoleCreateJob::RoleCreateInfo RoleCreateJob::createRoleInfo() const 0048 { 0049 return mRoleCreateInfo; 0050 } 0051 0052 void RoleCreateJob::setCreateRoleInfo(const RoleCreateInfo &statusCreateInfo) 0053 { 0054 mRoleCreateInfo = statusCreateInfo; 0055 } 0056 0057 bool RoleCreateJob::requireHttpAuthentication() const 0058 { 0059 return true; 0060 } 0061 0062 bool RoleCreateJob::canStart() const 0063 { 0064 if (!RestApiAbstractJob::canStart()) { 0065 return false; 0066 } 0067 if (!mRoleCreateInfo.isValid()) { 0068 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RoleCreateJob: mRoleCreateInfo is not valid."; 0069 return false; 0070 } 0071 return true; 0072 } 0073 0074 QNetworkRequest RoleCreateJob::request() const 0075 { 0076 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::RolesCreate); 0077 QNetworkRequest request(url); 0078 addAuthRawHeader(request); 0079 addRequestAttribute(request); 0080 return request; 0081 } 0082 0083 QJsonDocument RoleCreateJob::json() const 0084 { 0085 QJsonObject jsonObj; 0086 jsonObj[QLatin1String("name")] = mRoleCreateInfo.name; 0087 jsonObj[QLatin1String("description")] = mRoleCreateInfo.description; 0088 jsonObj[QLatin1String("scope")] = mRoleCreateInfo.scope; 0089 jsonObj[QLatin1String("mandatory2fa")] = mRoleCreateInfo.mandatory2fa; 0090 0091 const QJsonDocument postData = QJsonDocument(jsonObj); 0092 return postData; 0093 } 0094 0095 QString RoleCreateJob::errorMessage(const QString &str, const QJsonObject &detail) 0096 { 0097 if (str == QLatin1String("error-action-not-allowed")) { 0098 return i18n("This is an enterprise feature"); 0099 } 0100 0101 return RestApiAbstractJob::errorMessage(str, detail); 0102 } 0103 0104 #include "moc_rolecreatejob.cpp"