]> git.mxchange.org Git - friendica.git/blobdiff - src/Module/Settings/TwoFactor/AppSpecific.php
Introduce `Response` for Modules to create a testable way for module responses
[friendica.git] / src / Module / Settings / TwoFactor / AppSpecific.php
index fe7423c5d214ea7a6bc1d9e1bff5ee78962b3fe2..94dfc6d4121df6ada787f62fc069c996c3021c15 100644 (file)
@@ -1,44 +1,72 @@
 <?php
-
+/**
+ * @copyright Copyright (C) 2010-2021, the Friendica project
+ *
+ * @license GNU AGPL version 3 or any later version
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Affero General Public License for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ *
+ */
 
 namespace Friendica\Module\Settings\TwoFactor;
 
-
+use Friendica\App;
 use Friendica\Core\L10n;
-use Friendica\Core\PConfig;
+use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
 use Friendica\Core\Renderer;
-use Friendica\Model\TwoFactor\AppSpecificPassword;
-use Friendica\Module\BaseSettingsModule;
+use Friendica\Module\Response;
+use Friendica\Security\TwoFactor\Model\AppSpecificPassword;
+use Friendica\Module\BaseSettings;
 use Friendica\Module\Security\Login;
+use Friendica\Util\Profiler;
+use Psr\Log\LoggerInterface;
 
 /**
  * // Page 5: 2FA enabled, app-specific password generation
  *
  * @package Friendica\Module\TwoFactor
  */
-class AppSpecific extends BaseSettingsModule
+class AppSpecific extends BaseSettings
 {
-       private static $appSpecificPassword = null;
+       private $appSpecificPassword = null;
 
-       public static function init(array $parameters = [])
+       /** @var IManagePersonalConfigValues */
+       protected $pConfig;
+
+       public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, array $server, array $parameters = [])
        {
+               parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
+
+               $this->pConfig = $pConfig;
+
                if (!local_user()) {
                        return;
                }
 
-               $verified = PConfig::get(local_user(), '2fa', 'verified');
+               $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
 
                if (!$verified) {
-                       self::getApp()->internalRedirect('settings/2fa');
+                       $this->baseUrl->redirect('settings/2fa');
                }
 
                if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
-                       notice(L10n::t('Please enter your password to access this page.'));
-                       self::getApp()->internalRedirect('settings/2fa');
+                       notice($this->t('Please enter your password to access this page.'));
+                       $this->baseUrl->redirect('settings/2fa');
                }
        }
 
-       public static function post(array $parameters = [])
+       protected function post(array $request = [], array $post = [])
        {
                if (!local_user()) {
                        return;
@@ -51,21 +79,21 @@ class AppSpecific extends BaseSettingsModule
                                case 'generate':
                                        $description = $_POST['description'] ?? '';
                                        if (empty($description)) {
-                                               notice(L10n::t('App-specific password generation failed: The description is empty.'));
-                                               self::getApp()->internalRedirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                                               notice($this->t('App-specific password generation failed: The description is empty.'));
+                                               $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
                                        } elseif (AppSpecificPassword::checkDuplicateForUser(local_user(), $description)) {
-                                               notice(L10n::t('App-specific password generation failed: This description already exists.'));
-                                               self::getApp()->internalRedirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                                               notice($this->t('App-specific password generation failed: This description already exists.'));
+                                               $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
                                        } else {
-                                               self::$appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
-                                               notice(L10n::t('New app-specific password generated.'));
+                                               $this->appSpecificPassword = AppSpecificPassword::generateForUser(local_user(), $_POST['description'] ?? '');
+                                               info($this->t('New app-specific password generated.'));
                                        }
 
                                        break;
                                case 'revoke_all' :
                                        AppSpecificPassword::deleteAllForUser(local_user());
-                                       notice(L10n::t('App-specific passwords successfully revoked.'));
-                                       self::getApp()->internalRedirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                                       info($this->t('App-specific passwords successfully revoked.'));
+                                       $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
                                        break;
                        }
                }
@@ -74,20 +102,20 @@ class AppSpecific extends BaseSettingsModule
                        self::checkFormSecurityTokenRedirectOnError('settings/2fa/app_specific', 'settings_2fa_app_specific');
 
                        if (AppSpecificPassword::deleteForUser(local_user(), $_POST['revoke_id'])) {
-                               notice(L10n::t('App-specific password successfully revoked.'));
+                               info($this->t('App-specific password successfully revoked.'));
                        }
 
-                       self::getApp()->internalRedirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
+                       $this->baseUrl->redirect('settings/2fa/app_specific?t=' . self::getFormSecurityToken('settings_2fa_password'));
                }
        }
 
-       public static function content(array $parameters = [])
+       protected function content(array $request = []): string
        {
                if (!local_user()) {
                        return Login::form('settings/2fa/app_specific');
                }
 
-               parent::content($parameters);
+               parent::content();
 
                $appSpecificPasswords = AppSpecificPassword::getListForUser(local_user());
 
@@ -95,22 +123,22 @@ class AppSpecific extends BaseSettingsModule
                        '$form_security_token'     => self::getFormSecurityToken('settings_2fa_app_specific'),
                        '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
 
-                       '$title'                  => L10n::t('Two-factor app-specific passwords'),
-                       '$help_label'             => L10n::t('Help'),
-                       '$message'                => L10n::t('<p>App-specific passwords are randomly generated passwords used instead your regular password to authenticate your account on third-party applications that don\'t support two-factor authentication.</p>'),
-                       '$generated_message'      => L10n::t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
-                       '$generated_app_specific_password' => self::$appSpecificPassword,
+                       '$title'                  => $this->t('Two-factor app-specific passwords'),
+                       '$help_label'             => $this->t('Help'),
+                       '$message'                => $this->t('<p>App-specific passwords are randomly generated passwords used instead your regular password to authenticate your account on third-party applications that don\'t support two-factor authentication.</p>'),
+                       '$generated_message'      => $this->t('Make sure to copy your new app-specific password now. You won’t be able to see it again!'),
+                       '$generated_app_specific_password' => $this->appSpecificPassword,
 
-                       '$description_label'      => L10n::t('Description'),
-                       '$last_used_label'        => L10n::t('Last Used'),
-                       '$revoke_label'           => L10n::t('Revoke'),
-                       '$revoke_all_label'       => L10n::t('Revoke All'),
+                       '$description_label'      => $this->t('Description'),
+                       '$last_used_label'        => $this->t('Last Used'),
+                       '$revoke_label'           => $this->t('Revoke'),
+                       '$revoke_all_label'       => $this->t('Revoke All'),
 
                        '$app_specific_passwords' => $appSpecificPasswords,
-                       '$generate_message'       => L10n::t('When you generate a new app-specific password, you must use it right away, it will be shown to you once after you generate it.'),
-                       '$generate_title'         => L10n::t('Generate new app-specific password'),
-                       '$description_placeholder_label' => L10n::t('Friendiqa on my Fairphone 2...'),
-                       '$generate_label' => L10n::t('Generate'),
+                       '$generate_message'       => $this->t('When you generate a new app-specific password, you must use it right away, it will be shown to you once after you generate it.'),
+                       '$generate_title'         => $this->t('Generate new app-specific password'),
+                       '$description_placeholder_label' => $this->t('Friendiqa on my Fairphone 2...'),
+                       '$generate_label' => $this->t('Generate'),
                ]);
        }
 }