File indexing completed on 2024-05-12 16:27:23

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 "myaccount2faconfigurewidget.h"
0008 #include "ddpapi/ddpclient.h"
0009 #include "myaccount2fadisabletotpwidget.h"
0010 #include "myaccount2fatotpwidget.h"
0011 #include "rocketchataccount.h"
0012 #include <KLocalizedString>
0013 #include <QCheckBox>
0014 #include <QStackedWidget>
0015 #include <QVBoxLayout>
0016 
0017 MyAccount2FaConfigureWidget::MyAccount2FaConfigureWidget(RocketChatAccount *account, QWidget *parent)
0018     : QWidget(parent)
0019     , mActivate2FAViaEmailCheckbox(new QCheckBox(i18n("Activate Two Authentication Factor via Email"), this))
0020     , mActivate2FAViaTOTPCheckbox(new QCheckBox(i18n("Activate Two Authentication Factor via TOTP"), this))
0021     , mRocketChatAccount(account)
0022     , mMyAccount2FaTotpWidget(new MyAccount2FaTotpWidget(account, this))
0023     , mMyAccountDisable2FaTotpWidget(new MyAccount2FaDisableTotpWidget(account, this))
0024     , mMyAccount2FaEmpty(new QWidget(this))
0025     , mStackedWidget(new QStackedWidget(this))
0026 {
0027     auto mainLayout = new QVBoxLayout(this);
0028     mainLayout->setObjectName(QStringLiteral("mainLayout"));
0029 
0030     mActivate2FAViaEmailCheckbox->setObjectName(QStringLiteral("mActivate2FAViaEmailCheckbox"));
0031     mainLayout->addWidget(mActivate2FAViaEmailCheckbox);
0032 
0033     mActivate2FAViaTOTPCheckbox->setObjectName(QStringLiteral("mActivate2FAViaTOTPCheckbox"));
0034     mainLayout->addWidget(mActivate2FAViaTOTPCheckbox);
0035     connect(mActivate2FAViaTOTPCheckbox, &QCheckBox::clicked, this, &MyAccount2FaConfigureWidget::slot2FAViaTOTPActivated);
0036 
0037     mStackedWidget->setObjectName(QStringLiteral("mStackedWidget"));
0038     mainLayout->addWidget(mStackedWidget);
0039 
0040     mMyAccountDisable2FaTotpWidget->setObjectName(QStringLiteral("mMyAccountDisable2FaTotpWidget"));
0041     mMyAccount2FaTotpWidget->setObjectName(QStringLiteral("mMyAccount2FaTotpWidget"));
0042     mMyAccount2FaEmpty->setObjectName(QStringLiteral("mMyAccount2FaEmpty"));
0043 
0044     connect(mMyAccount2FaTotpWidget, &MyAccount2FaTotpWidget::show2FaEnabledWidget, this, [this]() {
0045         mStackedWidget->setCurrentIndex(Enable2FaPage);
0046     });
0047 
0048     connect(mMyAccountDisable2FaTotpWidget, &MyAccount2FaDisableTotpWidget::hide2FaDisableTotpWidget, this, [this]() {
0049         mStackedWidget->setCurrentIndex(EmptyPage);
0050     });
0051 
0052     mStackedWidget->insertWidget(EmptyPage, mMyAccount2FaEmpty);
0053     mStackedWidget->insertWidget(Enable2FaPage, mMyAccount2FaTotpWidget);
0054     mStackedWidget->insertWidget(Disable2FaPage, mMyAccountDisable2FaTotpWidget);
0055     mStackedWidget->setCurrentIndex(EmptyPage);
0056 
0057     mainLayout->addStretch(1);
0058 }
0059 
0060 MyAccount2FaConfigureWidget::~MyAccount2FaConfigureWidget() = default;
0061 
0062 void MyAccount2FaConfigureWidget::slot2FAViaTOTPActivated(bool checked)
0063 {
0064     if (checked) {
0065         if (!mRocketChatAccount->ownUser().servicePassword().totp()) {
0066             mRocketChatAccount->ddp()->enable2fa();
0067         }
0068     } else {
0069         if (mRocketChatAccount->ownUser().servicePassword().totp()) {
0070             mStackedWidget->setCurrentIndex(Disable2FaPage);
0071         }
0072     }
0073 }
0074 
0075 void MyAccount2FaConfigureWidget::load()
0076 {
0077     if (mRocketChatAccount) {
0078         if (mRocketChatAccount->twoFactorAuthenticationByEmailEnabled()) {
0079             mActivate2FAViaEmailCheckbox->setChecked(mRocketChatAccount->ownUser().servicePassword().email2faEnabled());
0080         }
0081         if (mRocketChatAccount->twoFactorAuthenticationByTOTPEnabled()) {
0082             mActivate2FAViaTOTPCheckbox->setChecked(mRocketChatAccount->ownUser().servicePassword().totp());
0083         }
0084     }
0085 }
0086 
0087 void MyAccount2FaConfigureWidget::save()
0088 {
0089     if (mRocketChatAccount) {
0090         if (mRocketChatAccount->twoFactorAuthenticationByEmailEnabled()) {
0091             mRocketChatAccount->enable2FaEmailJob(mActivate2FAViaEmailCheckbox->isChecked());
0092         }
0093         // Not necessary
0094         if (mRocketChatAccount->twoFactorAuthenticationByTOTPEnabled()) {
0095             // TODO
0096         }
0097     }
0098 }
0099 
0100 void MyAccount2FaConfigureWidget::initialize()
0101 {
0102     if (mRocketChatAccount) {
0103         mActivate2FAViaEmailCheckbox->setVisible(mRocketChatAccount->twoFactorAuthenticationByEmailEnabled());
0104         mActivate2FAViaTOTPCheckbox->setVisible(mRocketChatAccount->twoFactorAuthenticationByTOTPEnabled());
0105     }
0106 }
0107 
0108 #include "moc_myaccount2faconfigurewidget.cpp"