File indexing completed on 2024-12-22 04:44:57
0001 /* 0002 SPDX-FileCopyrightText: 2020-2024 Laurent Montel <montel@kde.org> 0003 0004 SPDX-License-Identifier: LGPL-2.0-or-later 0005 */ 0006 0007 #include "user2fasendemailcodejob.h" 0008 #include "restapimethod.h" 0009 #include "rocketchatqtrestapi_debug.h" 0010 0011 #include <QJsonDocument> 0012 #include <QJsonObject> 0013 #include <QNetworkReply> 0014 using namespace RocketChatRestApi; 0015 User2FASendEmailCodeJob::User2FASendEmailCodeJob(QObject *parent) 0016 : RestApiAbstractJob(parent) 0017 { 0018 } 0019 0020 User2FASendEmailCodeJob::~User2FASendEmailCodeJob() = default; 0021 0022 bool User2FASendEmailCodeJob::start() 0023 { 0024 if (!canStart()) { 0025 deleteLater(); 0026 return false; 0027 } 0028 addStartRestApiInfo("User2FASendEmailCodeJob::start"); 0029 submitPostRequest(json()); 0030 return true; 0031 } 0032 0033 void User2FASendEmailCodeJob::onPostRequestResponse(const QString &replyErrorString, const QJsonDocument &replyJson) 0034 { 0035 // TODO it reports only email. 0036 0037 addLoggerInfo(QByteArrayLiteral("User2FASendEmailCodeJob: success: ") + replyJson.toJson(QJsonDocument::Indented)); 0038 Q_EMIT sendEmailCodeDone(); 0039 0040 // const QJsonObject replyObject = replyJson.object(); 0041 // qDebug() << "replyObject "<< replyObject; 0042 // if (replyObject[QLatin1String("success")].toBool()) { 0043 // } else { 0044 // emitFailedMessage(replyObject, reply); 0045 // addLoggerWarning(QByteArrayLiteral("User2FASendEmailCodeJob: Problem: ") + replyJson.toJson(QJsonDocument::Indented)); 0046 // } 0047 } 0048 0049 QString User2FASendEmailCodeJob::usernameOrEmail() const 0050 { 0051 return mUsernameOrEmail; 0052 } 0053 0054 void User2FASendEmailCodeJob::setUsernameOrEmail(const QString &usernameOrEmail) 0055 { 0056 mUsernameOrEmail = usernameOrEmail; 0057 } 0058 0059 bool User2FASendEmailCodeJob::requireHttpAuthentication() const 0060 { 0061 return false; 0062 } 0063 0064 bool User2FASendEmailCodeJob::canStart() const 0065 { 0066 if (!RestApiAbstractJob::canStart()) { 0067 return false; 0068 } 0069 if (mUsernameOrEmail.isEmpty()) { 0070 qCWarning(ROCKETCHATQTRESTAPI_LOG) << "mUsernameOrEmail is empty"; 0071 return false; 0072 } 0073 return true; 0074 } 0075 0076 QNetworkRequest User2FASendEmailCodeJob::request() const 0077 { 0078 const QUrl url = mRestApiMethod->generateUrl(RestApiUtil::RestApiUrlType::Users2FASendEmailCode); 0079 QNetworkRequest request(url); 0080 addAuthRawHeader(request); 0081 addRequestAttribute(request); 0082 return request; 0083 } 0084 0085 QJsonDocument User2FASendEmailCodeJob::json() const 0086 { 0087 QJsonObject jsonObj; 0088 jsonObj[QLatin1String("emailOrUsername")] = mUsernameOrEmail; 0089 const QJsonDocument postData = QJsonDocument(jsonObj); 0090 return postData; 0091 } 0092 0093 #include "moc_user2fasendemailcodejob.cpp"