File indexing completed on 2024-12-22 04:45:26

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "updateadminsettingsjob.h"
0008 #include "restapimethod.h"
0009 
0010 #include <KLocalizedString>
0011 
0012 #include <QJsonDocument>
0013 #include <QJsonObject>
0014 #include <QNetworkReply>
0015 using namespace RocketChatRestApi;
0016 UpdateAdminSettingsJob::UpdateAdminSettingsJob(QObject *parent)
0017     : RestApiAbstractJob(parent)
0018 {
0019 }
0020 
0021 UpdateAdminSettingsJob::~UpdateAdminSettingsJob() = default;
0022 
0023 bool UpdateAdminSettingsJob::start()
0024 {
0025     if (!canStart()) {
0026         deleteLater();
0027         return false;
0028     }
0029     addStartRestApiInfo("UpdateAdminSettingsJob::start");
0030     submitPostRequest(json());
0031     return true;
0032 }
0033 
0034 void UpdateAdminSettingsJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0035 {
0036     const QJsonObject replyObject = replyJson.object();
0037     if (replyObject[QLatin1String("success")].toBool()) {
0038         addLoggerInfo(QByteArrayLiteral("UpdateAdminSettingsJob: success: ") + replyJson.toJson(QJsonDocument::Indented));
0039         Q_EMIT updateAdminSettingsDone(replyObject);
0040     } else {
0041         emitFailedMessage(replyErrorString, replyObject);
0042         addLoggerWarning(QByteArrayLiteral("UpdateAdminSettingsJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0043     }
0044 }
0045 
0046 const UpdateAdminSettingsJob::UpdateAdminSettingsInfo &UpdateAdminSettingsJob::updateAdminSettingsInfo() const
0047 {
0048     return mInfo;
0049 }
0050 
0051 void UpdateAdminSettingsJob::setUpdateAdminSettingsInfo(const UpdateAdminSettingsInfo &newInfo)
0052 {
0053     mInfo = newInfo;
0054 }
0055 
0056 bool UpdateAdminSettingsJob::requireHttpAuthentication() const
0057 {
0058     return true;
0059 }
0060 
0061 bool UpdateAdminSettingsJob::canStart() const
0062 {
0063     if (!RestApiAbstractJob::canStart()) {
0064         return false;
0065     }
0066     if (!mInfo.canStart()) {
0067         return false;
0068     }
0069     return true;
0070 }
0071 
0072 QNetworkRequest UpdateAdminSettingsJob::request() const
0073 {
0074     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UpdateAdminSettings, mInfo.settingName);
0075     QNetworkRequest request(url);
0076     addAuthRawHeader(request);
0077     addRequestAttribute(request);
0078     return request;
0079 }
0080 
0081 QJsonDocument UpdateAdminSettingsJob::json() const
0082 {
0083     QJsonObject jsonObj;
0084     switch (mInfo.valueType) {
0085     case UpdateAdminSettingsInfo::ValueType::Unknown:
0086         break;
0087     case UpdateAdminSettingsInfo::ValueType::Boolean:
0088         jsonObj[QLatin1String("value")] = mInfo.settingsValue.toBool();
0089         break;
0090     case UpdateAdminSettingsInfo::ValueType::Integer:
0091         jsonObj[QLatin1String("value")] = mInfo.settingsValue.toInt();
0092         break;
0093     case UpdateAdminSettingsInfo::ValueType::String:
0094         jsonObj[QLatin1String("value")] = mInfo.settingsValue.toString();
0095         break;
0096     }
0097     const QJsonDocument postData = QJsonDocument(jsonObj);
0098     return postData;
0099 }
0100 
0101 bool UpdateAdminSettingsJob::UpdateAdminSettingsInfo::isValid() const
0102 {
0103     return !settingName.isEmpty() && valueType != UpdateAdminSettingsInfo::ValueType::Unknown;
0104 }
0105 
0106 bool UpdateAdminSettingsJob::UpdateAdminSettingsInfo::canStart() const
0107 {
0108     return isValid();
0109 }
0110 
0111 bool UpdateAdminSettingsJob::requireTwoFactorAuthentication() const
0112 {
0113     return true;
0114 }
0115 
0116 #include "moc_updateadminsettingsjob.cpp"