3 namespace Friendica\Security\TwoFactor\Repository;
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;
16 /** @var LoggerInterface */
19 /** @var \Friendica\Security\TwoFactor\Factory\TrustedBrowser */
22 protected static $table_name = '2fa_trusted_browser';
24 public function __construct(Database $database, LoggerInterface $logger, \Friendica\Security\TwoFactor\Factory\TrustedBrowser $factory = null)
26 $this->db = $database;
27 $this->logger = $logger;
28 $this->factory = $factory ?? new \Friendica\Security\TwoFactor\Factory\TrustedBrowser($logger);
32 * @param string $cookie_hash
33 * @return Model\TrustedBrowser|null
36 public function selectOneByHash(string $cookie_hash): Model\TrustedBrowser
38 $fields = $this->db->selectFirst(self::$table_name, [], ['cookie_hash' => $cookie_hash]);
39 if (!$this->db->isResult($fields)) {
40 throw new NotFoundException('');
43 return $this->factory->createFromTableRow($fields);
46 public function selectAllByUid(int $uid): TrustedBrowsers
48 $rows = $this->db->selectToArray(self::$table_name, [], ['uid' => $uid]);
50 $trustedBrowsers = [];
51 foreach ($rows as $fields) {
52 $trustedBrowsers[] = $this->factory->createFromTableRow($fields);
55 return new TrustedBrowsers($trustedBrowsers);
59 * @param Model\TrustedBrowser $trustedBrowser
63 public function save(Model\TrustedBrowser $trustedBrowser): bool
65 return $this->db->insert(self::$table_name, $trustedBrowser->toArray(), $this->db::INSERT_UPDATE);
69 * @param Model\TrustedBrowser $trustedBrowser
73 public function remove(Model\TrustedBrowser $trustedBrowser): bool
75 return $this->db->delete(self::$table_name, ['cookie_hash' => $trustedBrowser->cookie_hash]);
79 * @param int $local_user
80 * @param string $cookie_hash
84 public function removeForUser(int $local_user, string $cookie_hash): bool
86 return $this->db->delete(self::$table_name, ['cookie_hash' => $cookie_hash,'uid' => $local_user]);
90 * @param int $local_user
94 public function removeAllForUser(int $local_user): bool
96 return $this->db->delete(self::$table_name, ['uid' => $local_user]);