ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
PasswordAssistanceDbRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
30
31class PasswordAssistanceDbRepository implements RepositoryInterface
32{
33 private const DEFAULT_LIFETIME_IN_SECONDS = 3600;
34
35 public function __construct(
36 private readonly \ilDBInterface $db,
37 private readonly ClockInterface $clock
38 ) {
39 }
40
42 {
43 do {
44 $hash = bin2hex(\ilPasswordUtils::getBytes(32));
45
46 $query = 'SELECT EXISTS(SELECT 1 FROM usr_pwassist WHERE pwassist_id = %s) AS hit';
47
48 $exists = (
49 (int) ($this->db->fetchAssoc(
50 $this->db->queryF(
51 $query,
53 [$hash]
54 )
55 )['hit'] ?? 0) === 1
56 );
57 } while ($exists);
58
59 return new PasswordAssistanceHash($hash);
60 }
61
62 public function getSessionByUsrId(ObjectId $usr_id): Result
63 {
64 $query = 'SELECT * FROM usr_pwassist WHERE user_id = ' . $this->db->quote(
65 $usr_id->toInt(),
67 );
68 $result = $this->db->query($query);
69 $row = $this->db->fetchAssoc($result);
70 if ($row === null) {
71 return new Error(sprintf('No session found for usr_id %s', $usr_id->toInt()));
72 }
73
74 return new Result\Ok(
76 new PasswordAssistanceHash($row['pwassist_id']),
77 $usr_id,
78 new \DateTimeImmutable('@' . (int) $row['ctime']),
79 new \DateTimeImmutable('@' . (int) $row['expires']),
80 )
81 );
82 }
83
85 {
86 $query = 'SELECT * FROM usr_pwassist WHERE pwassist_id = ' . $this->db->quote(
87 $hash->value(),
89 );
90 $result = $this->db->query($query);
91 $row = $this->db->fetchAssoc($result);
92 if ($row === null) {
93 return new Error(sprintf('No session found for hash %s', $hash->value()));
94 }
95
96 return new Result\Ok(
98 $hash,
99 new ObjectId((int) $row['user_id']),
100 new \DateTimeImmutable('@' . (int) $row['ctime']),
101 new \DateTimeImmutable('@' . (int) $row['expires']),
102 )
103 );
104 }
105
107 {
108 $query = 'DELETE FROM usr_pwassist ' .
109 'WHERE pwassist_id = ' . $this->db->quote($hash->value(), \ilDBConstants::T_TEXT) . ' ' .
110 'OR user_id = ' . $this->db->quote($usr_id->toInt(), \ilDBConstants::T_INTEGER);
111 $this->db->manipulate($query);
112
113 $session = (new PasswordAssistanceSession($hash, $usr_id))
114 ->withCreationDateTime(
115 $this->clock->now()
116 )->withExpirationDateTime(
117 $this->clock->now()->add(new \DateInterval('PT' . self::DEFAULT_LIFETIME_IN_SECONDS . 'S'))
118 );
119
120 $this->db->manipulateF(
121 'INSERT INTO usr_pwassist (pwassist_id, expires, user_id, ctime) VALUES (%s, %s, %s, %s)',
122 [
127 ],
128 [
129 $session->hash()->value(),
130 $session->expirationDateTime()->getTimestamp(),
131 $session->usrId()->toInt(),
132 $session->creationDateTime()->getTimestamp()
133 ]
134 );
135
136 return $session;
137 }
138
139 public function deleteSession(PasswordAssistanceSession $session): void
140 {
141 $query = 'DELETE FROM usr_pwassist WHERE pwassist_id = ' . $this->db->quote(
142 $session->hash()->value(),
144 );
145 $this->db->manipulate($query);
146 }
147}
__construct(private readonly \ilDBInterface $db, private readonly ClockInterface $clock)
static getBytes(int $length)
Generate random bytes using OpenSSL or Mcrypt and mt_rand() as fallback.
A result encapsulates a value or an error and simplifies the handling of those.
Definition: Result.php:29
Interface ilDBInterface.