ILIAS  release_9 Revision v9.13-25-g2c18ec4c24f
ProfileChangeMailTokenDBRepository.php
Go to the documentation of this file.
1 <?php
2 
19 declare(strict_types=1);
20 
21 namespace ILIAS\User\Profile;
22 
24 {
25  private const TABLE_NAME = 'usr_change_email_token';
26  private const VALIDITY = 300;
27 
28  public function __construct(
29  private \ilDBInterface $db
30  ) {
31  $this->deleteExpiredEntries();
32  }
33 
34  public function getNewTokenForUser(\ilObjUser $user, string $new_email): string
35  {
36  $token = hash('md5', $user->getId() . '-' . $user->getEmail());
37  $result = $this->db->replace(
38  self::TABLE_NAME,
39  [
40  'token' => ['text', $token]
41  ],
42  [
43  'new_email' => [\ilDBConstants::T_TEXT, $new_email],
44  'valid_until' => [\ilDBConstants::T_INTEGER, time() + self::VALIDITY]
45  ]
46  );
47 
48  if ($result === 1) {
49  return $token;
50  }
51 
52  return '';
53  }
54 
55  public function getNewEmailForUser(\ilObjUser $user, string $received_token): string
56  {
57  if (hash('md5', $user->getId() . '-' . $user->getEmail()) !== $received_token) {
58  return '';
59  }
60 
61  $query = $this->db->queryF(
62  'SELECT `new_email` FROM `' . self::TABLE_NAME . '` WHERE `token` = %s AND `valid_until` >= %s',
64  [$received_token, time()]
65  );
66 
67  $result = $this->db->fetchObject($query);
68 
69  if ($result !== null) {
70  return $result->new_email;
71  }
72 
73  return '';
74  }
75 
76  public function deleteEntryByToken(string $token): void
77  {
78  $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `token` = %s';
79  $this->db->manipulateF($query, [\ilDBConstants::T_TEXT], [$token]);
80  }
81 
82  private function deleteExpiredEntries(): void
83  {
84  $query = 'DELETE FROM `' . self::TABLE_NAME . '` WHERE `valid_until` <= %s';
85  $this->db->manipulateF($query, [\ilDBConstants::T_INTEGER], [time()]);
86  }
87 }
$token
Definition: xapitoken.php:70
getNewEmailForUser(\ilObjUser $user, string $received_token)
This Function will check if the token is actually valid for the given user before returning the new e...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...