ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DBRepository.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23class DBRepository implements Repository
24{
25 public const TABLE_NAME = 'usr_change_email_token';
26
27 public function __construct(
28 private readonly \ilDBInterface $db,
29 private readonly \ilSetting $settings
30 ) {
31 }
32
33 public function getNewTokenForUser(
34 \ilObjUser $user,
35 string $new_email,
36 int $now
37 ): Token {
38 $token = new Token(
39 $user->getId(),
40 $user->getEmail(),
41 $new_email,
42 $now
43 );
44
45 $this->storeToken($token);
46 return $token;
47 }
48
49 public function hasUserValidEmailConfirmationToken(\ilObjUser $user): bool
50 {
51 $query = $this->db->queryF(
52 'SELECT count(*) as cnt FROM `' . self::TABLE_NAME . '`' . PHP_EOL
53 . 'WHERE `usr_id` = %s' . PHP_EOL
54 . 'AND `status` = %s' . PHP_EOL
55 . 'AND `created_ts` >= %s',
56 [
60 ],
61 [
62 $user->getId(),
63 Status::EmailConfirmation->value,
64 time() - Status::EmailConfirmation->getValidity($this->settings)
65 ]
66 );
67
68 $result = $this->db->fetchObject($query);
69
70 if ($result->cnt > 0) {
71 return true;
72 }
73
74 return false;
75 }
76
77 public function getTokenForTokenString(string $token_string, \ilObjUser $user): ?Token
78 {
79 $query = $this->db->queryF(
80 'SELECT * FROM `' . self::TABLE_NAME . '` WHERE `token` = %s',
82 [$token_string]
83 );
84
85 $result = $this->db->fetchObject($query);
86
87 if ($result === null) {
88 return null;
89 }
90
91 $token = new Token(
92 $user->getId(),
93 $user->getEmail(),
94 $result->new_email,
95 $result->created_ts,
96 Status::from($result->status),
97 $result->token
98 );
99
100 if (!$token->isTokenValidForCurrentStatus($this->settings)) {
101 return null;
102 }
103
104 return $token;
105 }
106
107 public function moveToNextStep(Token $token, int $now): Token
108 {
109 $new_token = new Token(
110 $token->getUserId(),
111 $token->getCurrentEmail(),
112 $token->getNewEmail(),
113 $now,
114 $token->getStatus()->next()
115 );
116 $this->deleteEntryByToken($token->getToken());
117 $this->storeToken($new_token);
118 return $new_token;
119 }
120
121 public function deleteEntryByToken(string $token): void
122 {
123 $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `token` = %s';
124 $this->db->manipulateF($query, [\ilDBConstants::T_TEXT], [$token]);
125 }
126
127 public function deleteExpiredEntries(): void
128 {
129 $validity = max(
130 Status::Login->getValidity($this->settings),
131 Status::EmailConfirmation->getValidity($this->settings)
132 );
133 $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `created_ts` < %s';
134 $this->db->manipulateF($query, [\ilDBConstants::T_INTEGER], [time() - $validity]);
135 }
136
137 private function storeToken(Token $token): void
138 {
139 $this->db->replace(
140 self::TABLE_NAME,
141 [
142 'token' => ['text', $token->getToken()]
143 ],
144 [
145 'usr_id' => [\ilDBConstants::T_TEXT, $token->getUserId()],
146 'new_email' => [\ilDBConstants::T_TEXT, $token->getNewEmail()],
147 'status' => [\ilDBConstants::T_INTEGER, $token->getStatus()->value],
148 'created_ts' => [\ilDBConstants::T_INTEGER, $token->getCreatedTimestamp()]
149 ]
150 );
151 }
152}
__construct(private readonly \ilDBInterface $db, private readonly \ilSetting $settings)
hasUserValidEmailConfirmationToken(\ilObjUser $user)
getNewTokenForUser(\ilObjUser $user, string $new_email, int $now)
getTokenForTokenString(string $token_string, \ilObjUser $user)
This Function will check if the token is actually valid for the given user before returning the new e...
User class.
ILIAS Setting Class.
Interface ilDBInterface.
Token
The string representation of these tokens must not occur in the names of metadata elements.
Definition: Token.php:28
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
$token
Definition: xapitoken.php:70