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

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