]> git.mxchange.org Git - friendica.git/blob - src/Security/TwoFactor/Repository/TrustedBrowser.php
Merge remote-tracking branch 'upstream/develop' into more-q
[friendica.git] / src / Security / TwoFactor / Repository / TrustedBrowser.php
1 <?php
2
3 namespace Friendica\Security\TwoFactor\Repository;
4
5 use Friendica\Security\TwoFactor\Model;
6 use Friendica\Security\TwoFactor\Collection\TrustedBrowsers;
7 use Friendica\Database\Database;
8 use Friendica\Network\HTTPException\NotFoundException;
9 use Psr\Log\LoggerInterface;
10
11 class TrustedBrowser
12 {
13         /** @var Database  */
14         protected $db;
15
16         /** @var LoggerInterface  */
17         protected $logger;
18
19         /** @var \Friendica\Security\TwoFactor\Factory\TrustedBrowser  */
20         protected $factory;
21
22         protected static $table_name = '2fa_trusted_browser';
23
24         public function __construct(Database $database, LoggerInterface $logger, \Friendica\Security\TwoFactor\Factory\TrustedBrowser $factory = null)
25         {
26                 $this->db = $database;
27                 $this->logger = $logger;
28                 $this->factory = $factory ?? new \Friendica\Security\TwoFactor\Factory\TrustedBrowser($logger);
29         }
30
31         /**
32          * @param string $cookie_hash
33          * @return Model\TrustedBrowser|null
34          * @throws \Exception
35          */
36         public function selectOneByHash(string $cookie_hash): Model\TrustedBrowser
37         {
38                 $fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
39                 if (!$this->db->isResult($fields)) {
40                         throw new NotFoundException('');
41                 }
42
43                 return $this->factory->createFromTableRow($fields);
44         }
45
46         public function selectAllByUid(int $uid): TrustedBrowsers
47         {
48                 $rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
49
50                 $trustedBrowsers = [];
51                 foreach ($rows as $fields) {
52                         $trustedBrowsers[] = $this->factory->createFromTableRow($fields);
53                 }
54
55                 return new TrustedBrowsers($trustedBrowsers);
56         }
57
58         /**
59          * @param Model\TrustedBrowser $trustedBrowser
60          * @return bool
61          * @throws \Exception
62          */
63         public function save(Model\TrustedBrowser $trustedBrowser): bool
64         {
65                 return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
66         }
67
68         /**
69          * @param Model\TrustedBrowser $trustedBrowser
70          * @return bool
71          * @throws \Exception
72          */
73         public function remove(Model\TrustedBrowser $trustedBrowser): bool
74         {
75                 return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
76         }
77
78         /**
79          * @param int    $local_user
80          * @param string $cookie_hash
81          * @return bool
82          * @throws \Exception
83          */
84         public function removeForUser(int $local_user, string $cookie_hash): bool
85         {
86                 return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash,'uid' => $local_user]);
87         }
88
89         /**
90          * @param int $local_user
91          * @return bool
92          * @throws \Exception
93          */
94         public function removeAllForUser(int $local_user): bool
95         {
96                 return $this->db->delete(self::$table_name, ['uid' => $local_user]);
97         }
98 }