]> git.mxchange.org Git - friendica.git/blob - src/Module/Settings/TwoFactor/Trusted.php
Merge pull request #12208 from MrPetovan/bug/12059-display-not-found
[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\Core\Session\Capability\IHandleUserSessions;
29 use Friendica\DI;
30 use Friendica\Module\BaseSettings;
31 use Friendica\Module\Response;
32 use Friendica\Security\TwoFactor;
33 use Friendica\Util\DateTimeFormat;
34 use Friendica\Util\Profiler;
35 use Friendica\Util\Temporal;
36 use Psr\Log\LoggerInterface;
37 use UAParser\Parser;
38
39 /**
40  * Manages users' two-factor trusted browsers in the 2fa_trusted_browsers table
41  */
42 class Trusted extends BaseSettings
43 {
44         /** @var IManagePersonalConfigValues */
45         protected $pConfig;
46         /** @var TwoFactor\Repository\TrustedBrowser */
47         protected $trustedBrowserRepo;
48
49         public function __construct(IManagePersonalConfigValues $pConfig, TwoFactor\Repository\TrustedBrowser $trustedBrowserRepo, IHandleUserSessions $session, App\Page $page, L10n $l10n, App\BaseURL $baseUrl, App\Arguments $args, LoggerInterface $logger, Profiler $profiler, Response $response, array $server, array $parameters = [])
50         {
51                 parent::__construct($session, $page, $l10n, $baseUrl, $args, $logger, $profiler, $response, $server, $parameters);
52
53                 $this->pConfig            = $pConfig;
54                 $this->trustedBrowserRepo = $trustedBrowserRepo;
55
56                 if (!DI::userSession()->getLocalUserId()) {
57                         return;
58                 }
59
60                 $verified = $this->pConfig->get(DI::userSession()->getLocalUserId(), '2fa', 'verified');
61
62                 if (!$verified) {
63                         $this->baseUrl->redirect('settings/2fa');
64                 }
65
66                 if (!self::checkFormSecurityToken('settings_2fa_password', 't')) {
67                         DI::sysmsg()->addNotice($this->t('Please enter your password to access this page.'));
68                         $this->baseUrl->redirect('settings/2fa');
69                 }
70         }
71
72         protected function post(array $request = [])
73         {
74                 if (!DI::userSession()->getLocalUserId()) {
75                         return;
76                 }
77
78                 if (!empty($_POST['action'])) {
79                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
80
81                         switch ($_POST['action']) {
82                                 case 'remove_all':
83                                         $this->trustedBrowserRepo->removeAllForUser(DI::userSession()->getLocalUserId());
84                                         DI::sysmsg()->addInfo($this->t('Trusted browsers successfully removed.'));
85                                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
86                                         break;
87                         }
88                 }
89
90                 if (!empty($_POST['remove_id'])) {
91                         self::checkFormSecurityTokenRedirectOnError('settings/2fa/trusted', 'settings_2fa_trusted');
92
93                         if ($this->trustedBrowserRepo->removeForUser(DI::userSession()->getLocalUserId(), $_POST['remove_id'])) {
94                                 DI::sysmsg()->addInfo($this->t('Trusted browser successfully removed.'));
95                         }
96
97                         $this->baseUrl->redirect('settings/2fa/trusted?t=' . self::getFormSecurityToken('settings_2fa_password'));
98                 }
99         }
100
101
102         protected function content(array $request = []): string
103         {
104                 parent::content();
105
106                 $trustedBrowsers = $this->trustedBrowserRepo->selectAllByUid(DI::userSession()->getLocalUserId());
107
108                 $parser = Parser::create();
109
110                 $trustedBrowserDisplay = array_map(function (TwoFactor\Model\TrustedBrowser $trustedBrowser) use ($parser) {
111                         $dates = [
112                                 'created_ago'     => Temporal::getRelativeDate($trustedBrowser->created),
113                                 'created_utc'     => DateTimeFormat::utc($trustedBrowser->created, 'c'),
114                                 'created_local'   => DateTimeFormat::local($trustedBrowser->created, 'r'),
115                                 'last_used_ago'   => Temporal::getRelativeDate($trustedBrowser->last_used),
116                                 'last_used_utc'   => $trustedBrowser->last_used ? DateTimeFormat::utc($trustedBrowser->last_used, 'c') : '',
117                                 'last_used_local' => $trustedBrowser->last_used ? DateTimeFormat::local($trustedBrowser->last_used, 'r') : '',
118                         ];
119
120                         $result = $parser->parse($trustedBrowser->user_agent);
121
122                         $uaData = [
123                                 'os'              => $result->os->family,
124                                 'device'          => $result->device->family,
125                                 'browser'         => $result->ua->family,
126                                 'trusted_labeled' => $trustedBrowser->trusted ? $this->t('Yes') : $this->t('No'),
127                         ];
128
129                         return $trustedBrowser->toArray() + $dates + $uaData;
130                 }, $trustedBrowsers->getArrayCopy());
131
132                 return Renderer::replaceMacros(Renderer::getMarkupTemplate('settings/twofactor/trusted_browsers.tpl'), [
133                         '$form_security_token'     => self::getFormSecurityToken('settings_2fa_trusted'),
134                         '$password_security_token' => self::getFormSecurityToken('settings_2fa_password'),
135
136                         '$title'            => $this->t('Two-factor Trusted Browsers'),
137                         '$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.'),
138                         '$device_label'     => $this->t('Device'),
139                         '$os_label'         => $this->t('OS'),
140                         '$browser_label'    => $this->t('Browser'),
141                         '$trusted_label'    => $this->t('Trusted'),
142                         '$created_label'    => $this->t('Created At'),
143                         '$last_used_label'  => $this->t('Last Use'),
144                         '$remove_label'     => $this->t('Remove'),
145                         '$remove_all_label' => $this->t('Remove All'),
146
147                         '$trusted_browsers' => $trustedBrowserDisplay,
148                 ]);
149         }
150 }