]> git.mxchange.org Git - friendica.git/blob - src/Security/TwoFactor/Repository/TrustedBrowser.php
Add a lot of log-points
[friendica.git] / src / Security / TwoFactor / Repository / TrustedBrowser.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\Security\TwoFactor\Repository;
23
24 use Friendica\Security\TwoFactor;
25 use Friendica\Database\Database;
26 use Psr\Log\LoggerInterface;
27
28 class TrustedBrowser
29 {
30         /** @var Database  */
31         protected $db;
32
33         /** @var LoggerInterface  */
34         protected $logger;
35
36         /** @var TwoFactor\Factory\TrustedBrowser  */
37         protected $factory;
38
39         protected static $table_name = '2fa_trusted_browser';
40
41         public function __construct(Database $database, LoggerInterface $logger, TwoFactor\Factory\TrustedBrowser $factory = null)
42         {
43                 $this->db      = $database;
44                 $this->logger  = $logger;
45                 $this->factory = $factory ?? new TwoFactor\Factory\TrustedBrowser($logger);
46         }
47
48         /**
49          * @param string $cookie_hash
50          *
51          * @return TwoFactor\Model\TrustedBrowser|null
52          *
53          * @throws TwoFactor\Exception\TrustedBrowserPersistenceException
54          * @throws TwoFactor\Exception\TrustedBrowserNotFoundException
55          */
56         public function selectOneByHash(string $cookie_hash): TwoFactor\Model\TrustedBrowser
57         {
58                 try {
59                         $fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
60                 } catch (\Exception $exception) {
61                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Internal server error when retrieving cookie hash \'%s\'', $cookie_hash));
62                 }
63                 if (!$this->db->isResult($fields)) {
64                         throw new TwoFactor\Exception\TrustedBrowserNotFoundException(sprintf('Cookie hash \'%s\' not found', $cookie_hash));
65                 }
66
67                 return $this->factory->createFromTableRow($fields);
68         }
69
70         /**
71          * @param int $uid
72          *
73          * @return TwoFactor\Collection\TrustedBrowsers
74          *
75          * @throws TwoFactor\Exception\TrustedBrowserPersistenceException
76          */
77         public function selectAllByUid(int $uid): TwoFactor\Collection\TrustedBrowsers
78         {
79                 try {
80                         $rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
81
82                         $trustedBrowsers = [];
83                         foreach ($rows as $fields) {
84                                 $trustedBrowsers[] = $this->factory->createFromTableRow($fields);
85                         }
86                         return new TwoFactor\Collection\TrustedBrowsers($trustedBrowsers);
87
88                 } catch (\Exception $exception) {
89                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('selection for uid \'%s\' wasn\'t successful.', $uid));
90                 }
91         }
92
93         /**
94          * @param TwoFactor\Model\TrustedBrowser $trustedBrowser
95          *
96          * @return bool
97          *
98          * @throws TwoFactor\Exception\TrustedBrowserPersistenceException
99          */
100         public function save(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
101         {
102                 try {
103                         return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
104                 } catch (\Exception $exception) {
105                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t save trusted Browser with cookie_hash \'%s\'', $trustedBrowser->cookie_hash));
106                 }
107         }
108
109         /**
110          * @param TwoFactor\Model\TrustedBrowser $trustedBrowser
111          *
112          * @return bool
113          *
114          * @throws TwoFactor\Exception\TrustedBrowserPersistenceException
115          */
116         public function remove(TwoFactor\Model\TrustedBrowser $trustedBrowser): bool
117         {
118                 try {
119                         return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
120                 } catch (\Exception $exception) {
121                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser with cookie hash \'%s\'', $trustedBrowser->cookie_hash));
122                 }
123         }
124
125         /**
126          * @param int    $local_user
127          * @param string $cookie_hash
128          *
129          * @return bool
130          *
131          * @throws TwoFactor\Exception\TrustedBrowserPersistenceException
132          */
133         public function removeForUser(int $local_user, string $cookie_hash): bool
134         {
135                 try {
136                         return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash, 'uid' => $local_user]);
137                 } catch (\Exception $exception) {
138                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browser for user \'%s\' and cookie hash \'%s\'', $local_user, $cookie_hash));
139                 }
140         }
141
142         /**
143          * @param int $local_user
144          * @return bool
145          */
146         public function removeAllForUser(int $local_user): bool
147         {
148                 try {
149                         return $this->db->delete(self::$table_name, ['uid' => $local_user]);
150                 } catch (\Exception $exception) {
151                         throw new TwoFactor\Exception\TrustedBrowserPersistenceException(sprintf('Couldn\'t delete trusted Browsers for user \'%s\'', $local_user));
152                 }
153         }
154 }