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