]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Trusted.php
Merge pull request #11150 from annando/user-banner
[friendica.git] / src / Module / Settings / TwoFactor / Trusted.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2022, the Friendica project
4  *
5  * @license GNU AGPL version 3 or any later version
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU Affero General Public License as
9  * published by the Free Software Foundation, either version 3 of the
10  * License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU Affero General Public License for more details.
16  *
17  * You should have received a copy of the GNU Affero General Public License
18  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19  *
20  */
21
22 namespace Friendica\Module\Settings\TwoFactor;
23
24 use Friendica\App;
25 use Friendica\Core\L10n;
26 use Friendica\Core\PConfig\Capability\IManagePersonalConfigValues;
27 use Friendica\Core\Renderer;
28 use Friendica\Module\BaseSettings;
29 use Friendica\Module\Response;
30 use Friendica\Security\TwoFactor;
31 use Friendica\Util\DateTimeFormat;
32 use Friendica\Util\Profiler;
33 use Friendica\Util\Temporal;
34 use Psr\Log\LoggerInterface;
35 use UAParser\Parser;
36
37 /**
38  * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
39  */
40 class Trusted extends BaseSettings
41 {
42         /** @var IManagePersonalConfigValues */
43         protected $pConfig;
44         /** @var TwoFactor\Repository\TrustedBrowser */
45         protected $trustedBrowserRepo;
46
47         public function __construct(L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, array $server, array $parameters = [])
48         {
49                 parent::__construct($l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
50
51                 $this->pConfig            = $pConfig;
52                 $this->trustedBrowserRepo = $trustedBrowserRepo;
53
54                 if (!local_user()) {
55                         return;
56                 }
57
58                 $verified = $this->pConfig->get(local_user(), '2fa', 'verified');
59
60                 if (!$verified) {
61                         $this->baseUrl->redirect('settings/2fa');
62                 }
63
64                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
65                         notice($this->t('Please enter your password to access this page.'));
66                         $this->baseUrl->redirect('settings/2fa');
67                 }
68         }
69
70         protected function post(array $request = [])
71         {
72                 if (!local_user()) {
73                         return;
74                 }
75
76                 if (!empty($_POST['action'])) {
77                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
78
79                         switch ($_POST['action']) {
80                                 case 'remove_all' :
81                                         $this->trustedBrowserRepo->removeAllForUser(local_user());
82                                         info($this->t('Trusted browsers successfully removed.'));
83                                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
84                                         break;
85                         }
86                 }
87
88                 if (!empty($_POST['remove_id'])) {
89                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
90
91                         if ($this->trustedBrowserRepo->removeForUser(local_user(), $_POST['remove_id'])) {
92                                 info($this->t('Trusted browser successfully removed.'));
93                         }
94
95                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
96                 }
97         }
98
99
100         protected function content(array $request = []): string
101         {
102                 parent::content();
103
104                 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(local_user());
105
106                 $parser = Parser::create();
107
108                 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
109                         $dates = [
110                                 'created_ago'     => Temporal::getRelativeDate($trustedBrowser->created),
111                                 'created_utc'     => DateTimeFormat::utc($trustedBrowser->created, 'c'),
112                                 'created_local'   => DateTimeFormat::local($trustedBrowser->created, 'r'),
113                                 'last_used_ago'   => Temporal::getRelativeDate($trustedBrowser->last_used),
114                                 'last_used_utc'   => $trustedBrowser->last_used ? DateTimeFormat::utc($trustedBrowser->last_used, 'c') : '',
115                                 'last_used_local' => $trustedBrowser->last_used ? DateTimeFormat::local($trustedBrowser->last_used, 'r') : '',
116                         ];
117
118                         $result = $parser->parse($trustedBrowser->user_agent);
119
120                         $uaData = [
121                                 'os' => $result->os->family,
122                                 'device' => $result->device->family,
123                                 'browser' => $result->ua->family,
124                         ];
125
126                         return $trustedBrowser->toArray() + $dates + $uaData;
127                 }, $trustedBrowsers->getArrayCopy());
128
129                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
130                         '$form_security_token' => self::getFormSecurityToken('settings_2fa_trusted'),
131                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
132
133                         '$title'               => $this->t('Two-factor Trusted Browsers'),
134                         '$message'             => $this->t('Trusted browsers are individual browsers you chose to skip two-factor authentication to access Friendica. Please use this feature sparingly, as it can negate the benefit of two-factor authentication.'),
135                         '$device_label'        => $this->t('Device'),
136                         '$os_label'            => $this->t('OS'),
137                         '$browser_label'       => $this->t('Browser'),
138                         '$created_label'       => $this->t('Trusted'),
139                         '$last_used_label'     => $this->t('Last Use'),
140                         '$remove_label'        => $this->t('Remove'),
141                         '$remove_all_label'    => $this->t('Remove All'),
142
143                         '$trusted_browsers'    => $trustedBrowserDisplay,
144                 ]);
145         }
146 }