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

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 "removepersonalaccesstokenjob.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 
0017 using namespace RocketChatRestApi;
0018 RemovePersonalAccessTokenJob::RemovePersonalAccessTokenJob(QObject *parent)
0019     : RestApiAbstractJob(parent)
0020 {
0021 }
0022 
0023 RemovePersonalAccessTokenJob::~RemovePersonalAccessTokenJob() = default;
0024 
0025 bool RemovePersonalAccessTokenJob::start()
0026 {
0027     if (!canStart()) {
0028         deleteLater();
0029         return false;
0030     }
0031     addStartRestApiInfo("RemovePersonalAccessTokenJob::start");
0032     submitPostRequest(json());
0033 
0034     return true;
0035 }
0036 
0037 void RemovePersonalAccessTokenJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson)
0038 {
0039     const QJsonObject replyObject = replyJson.object();
0040 
0041     if (replyObject[QLatin1String("success")].toBool()) {
0042         addLoggerInfo(QByteArrayLiteral("RemovePersonalAccessTokenJob success: ") + replyJson.toJson(QJsonDocument::Indented));
0043         Q_EMIT removeTokenDone(replyObject);
0044     } else {
0045         emitFailedMessage(replyErrorString, replyObject);
0046         addLoggerWarning(QByteArrayLiteral("RemovePersonalAccessTokenJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented));
0047     }
0048 }
0049 
0050 const QString &RemovePersonalAccessTokenJob::tokenName() const
0051 {
0052     return mTokenName;
0053 }
0054 
0055 void RemovePersonalAccessTokenJob::setTokenName(const QString &newTokenName)
0056 {
0057     mTokenName = newTokenName;
0058 }
0059 
0060 bool RemovePersonalAccessTokenJob::requireHttpAuthentication() const
0061 {
0062     return true;
0063 }
0064 
0065 bool RemovePersonalAccessTokenJob::canStart() const
0066 {
0067     if (!RestApiAbstractJob::canStart()) {
0068         return false;
0069     }
0070     if (mTokenName.isEmpty()) {
0071         qCWarning(ROCKETCHATQTRESTAPI_LOG) << "RemovePersonalAccessTokenJob: mTokenName is empty";
0072         return false;
0073     }
0074     return true;
0075 }
0076 
0077 QNetworkRequest RemovePersonalAccessTokenJob::request() const
0078 {
0079     const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::UsersRemovePersonalAccessToken);
0080     QNetworkRequest request(url);
0081     addAuthRawHeader(request);
0082     addRequestAttribute(request);
0083     return request;
0084 }
0085 
0086 QJsonDocument RemovePersonalAccessTokenJob::json() const
0087 {
0088     QJsonObject jsonObj;
0089     jsonObj[QLatin1String("tokenName")] = mTokenName;
0090     const QJsonDocument postData = QJsonDocument(jsonObj);
0091     return postData;
0092 }
0093 
0094 bool RemovePersonalAccessTokenJob::requireTwoFactorAuthentication() const
0095 {
0096     return true;
0097 }
0098 
0099 #include "moc_removepersonalaccesstokenjob.cpp"