]> git.mxchange.org Git - friendica.git/blob - src/Security/TwoFactor/Repository/TrustedBrowser.php
Merge pull request #10976 from nupplaphil/feat/api_tests
[friendica.git] / src / Security / TwoFactor / Repository / TrustedBrowser.php
1 <?php
2 /**
3  * @copyright Copyright (C) 2010-2021, 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\Security\TwoFactor\Repository;
23
24 use Friendica\Security\TwoFactor\Model;
25 use Friendica\Security\TwoFactor\Collection\TrustedBrowsers;
26 use Friendica\Database\Database;
27 use Friendica\Network\HTTPException\NotFoundException;
28 use Psr\Log\LoggerInterface;
29
30 class TrustedBrowser
31 {
32         /** @var Database  */
33         protected $db;
34
35         /** @var LoggerInterface  */
36         protected $logger;
37
38         /** @var \Friendica\Security\TwoFactor\Factory\TrustedBrowser  */
39         protected $factory;
40
41         protected static $table_name = '2fa_trusted_browser';
42
43         public function __construct(Database $database, LoggerInterface $logger, \Friendica\Security\TwoFactor\Factory\TrustedBrowser $factory = null)
44         {
45                 $this->db = $database;
46                 $this->logger = $logger;
47                 $this->factory = $factory ?? new \Friendica\Security\TwoFactor\Factory\TrustedBrowser($logger);
48         }
49
50         /**
51          * @param string $cookie_hash
52          * @return Model\TrustedBrowser|null
53          * @throws \Exception
54          */
55         public function selectOneByHash(string $cookie_hash): Model\TrustedBrowser
56         {
57                 $fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
58                 if (!$this->db->isResult($fields)) {
59                         throw new NotFoundException('');
60                 }
61
62                 return $this->factory->createFromTableRow($fields);
63         }
64
65         public function selectAllByUid(int $uid): TrustedBrowsers
66         {
67                 $rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
68
69                 $trustedBrowsers = [];
70                 foreach ($rows as $fields) {
71                         $trustedBrowsers[] = $this->factory->createFromTableRow($fields);
72                 }
73
74                 return new TrustedBrowsers($trustedBrowsers);
75         }
76
77         /**
78          * @param Model\TrustedBrowser $trustedBrowser
79          * @return bool
80          * @throws \Exception
81          */
82         public function save(Model\TrustedBrowser $trustedBrowser): bool
83         {
84                 return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
85         }
86
87         /**
88          * @param Model\TrustedBrowser $trustedBrowser
89          * @return bool
90          * @throws \Exception
91          */
92         public function remove(Model\TrustedBrowser $trustedBrowser): bool
93         {
94                 return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
95         }
96
97         /**
98          * @param int    $local_user
99          * @param string $cookie_hash
100          * @return bool
101          * @throws \Exception
102          */
103         public function removeForUser(int $local_user, string $cookie_hash): bool
104         {
105                 return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash,'uid' => $local_user]);
106         }
107
108         /**
109          * @param int $local_user
110          * @return bool
111          * @throws \Exception
112          */
113         public function removeAllForUser(int $local_user): bool
114         {
115                 return $this->db->delete(self::$table_name, ['uid' => $local_user]);
116         }
117 }