‪TYPO3CMS  11.5
FrontendUserRepository.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
25 
30 {
34  protected ‪$connection;
35 
39  protected ‪$context;
40 
44  protected ‪$userService;
45 
46  public function ‪__construct(
49  ) {
50  $this->userService = ‪$userService;
51  $this->connection = GeneralUtility::makeInstance(ConnectionPool::class)->getConnectionForTable($this->‪getTable());
52  $this->context = ‪$context;
53  }
54 
55  public function ‪getTable(): string
56  {
57  return $this->userService->getFeUserTable();
58  }
59 
65  public function ‪updatePasswordAndInvalidateHash(string $forgotPasswordHash, string $hashedPassword): void
66  {
67  $queryBuilder = $this->connection->createQueryBuilder();
68 
69  $currentTimestamp = $this->context->getPropertyFromAspect('date', 'timestamp');
70  $query = $queryBuilder
71  ->update($this->‪getTable())
72  ->set('password', $hashedPassword)
73  ->set('felogin_forgotHash', $this->connection->quote(''), false)
74  ->set('tstamp', $currentTimestamp)
75  ->where(
76  $queryBuilder->expr()->eq('felogin_forgotHash', $queryBuilder->createNamedParameter($forgotPasswordHash))
77  )
78  ;
79  $query->executeStatement();
80  }
81 
85  public function ‪existsUserWithHash(string $hash): bool
86  {
87  $queryBuilder = $this->connection->createQueryBuilder();
88 
89  $query = $queryBuilder
90  ->count('uid')
91  ->from($this->‪getTable())
92  ->where(
93  $queryBuilder->expr()->eq(
94  'felogin_forgotHash',
95  $queryBuilder->createNamedParameter($hash)
96  )
97  )
98  ;
99 
100  return (bool)$query->executeQuery()->fetchOne();
101  }
102 
106  public function ‪updateForgotHashForUserByEmail(string $emailAddress, string $hash): void
107  {
108  $queryBuilder = $this->connection->createQueryBuilder();
109  $query = $queryBuilder
110  ->update($this->‪getTable())
111  ->where(
112  $queryBuilder->expr()->eq(
113  'email',
114  $queryBuilder->createNamedParameter($emailAddress)
115  )
116  )
117  ->set('felogin_forgotHash', $hash)
118  ;
119  $query->executeStatement();
120  }
121 
126  public function ‪fetchUserInformationByEmail(string $emailAddress): array
127  {
128  $queryBuilder = $this->connection->createQueryBuilder();
129 
130  $query = $queryBuilder
131  ->select('uid', 'username', 'email', 'first_name', 'middle_name', 'last_name')
132  ->from($this->‪getTable())
133  ->where(
134  $queryBuilder->expr()->eq(
135  'email',
136  $queryBuilder->createNamedParameter($emailAddress)
137  )
138  )
139  ;
140  $result = $query->executeQuery()->fetchAssociative();
141  if (!is_array($result)) {
142  $result = [];
143  }
144  return $result;
145  }
146 
147  public function ‪findEmailByUsernameOrEmailOnPages(string $usernameOrEmail, array $pages = []): ?string
148  {
149  if ($usernameOrEmail === '') {
150  return null;
151  }
152 
153  $queryBuilder = $this->connection->createQueryBuilder();
154  $query = $queryBuilder
155  ->select('email')
156  ->from($this->‪getTable())
157  ->where(
158  $queryBuilder->expr()->orX(
159  $queryBuilder->expr()->eq('username', $queryBuilder->createNamedParameter($usernameOrEmail)),
160  $queryBuilder->expr()->eq('email', $queryBuilder->createNamedParameter($usernameOrEmail))
161  )
162  )
163  ;
164 
165  if (!empty($pages)) {
166  // respect storage pid
167  $query->andWhere($queryBuilder->expr()->in('pid', $pages));
168  }
169 
170  $column = $query->executeQuery()->fetchOne();
171  return $column === false || $column === '' ? null : (string)$column;
172  }
173 
174  public function ‪findOneByForgotPasswordHash(string $hash): ?array
175  {
176  if ($hash === '') {
177  return null;
178  }
179 
180  $queryBuilder = $this->connection->createQueryBuilder();
181  $query = $queryBuilder
182  ->select('*')
183  ->from($this->‪getTable())
184  ->where(
185  $queryBuilder->expr()->eq(
186  'felogin_forgotHash',
187  $queryBuilder->createNamedParameter($hash)
188  )
189  )
190  ->setMaxResults(1)
191  ;
192 
193  $row = $query->executeQuery()->fetchAssociative();
194  return is_array($row) ? $row : null;
195  }
196 
197  public function ‪findRedirectIdPageByUserId(int $uid): ?int
198  {
199  $queryBuilder = $this->connection->createQueryBuilder();
200  $queryBuilder->getRestrictions()->removeAll();
201  $query = $queryBuilder
202  ->select('felogin_redirectPid')
203  ->from($this->‪getTable())
204  ->where(
205  $queryBuilder->expr()->neq(
206  'felogin_redirectPid',
207  $this->connection->quote('')
208  ),
209  $queryBuilder->expr()->eq(
210  $this->userService->getFeUserIdColumn(),
211  $queryBuilder->createNamedParameter($uid, ‪Connection::PARAM_INT)
212  )
213  )
214  ->setMaxResults(1)
215  ;
216 
217  $column = $query->executeQuery()->fetchOne();
218  return $column === false ? null : (int)$column;
219  }
220 }
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\updatePasswordAndInvalidateHash
‪updatePasswordAndInvalidateHash(string $forgotPasswordHash, string $hashedPassword)
Definition: FrontendUserRepository.php:62
‪TYPO3\CMS\Core\Database\Connection\PARAM_INT
‪const PARAM_INT
Definition: Connection.php:49
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\$connection
‪Connection $connection
Definition: FrontendUserRepository.php:33
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository
Definition: FrontendUserRepository.php:30
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\fetchUserInformationByEmail
‪fetchUserInformationByEmail(string $emailAddress)
Definition: FrontendUserRepository.php:123
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\findOneByForgotPasswordHash
‪findOneByForgotPasswordHash(string $hash)
Definition: FrontendUserRepository.php:171
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\findEmailByUsernameOrEmailOnPages
‪findEmailByUsernameOrEmailOnPages(string $usernameOrEmail, array $pages=[])
Definition: FrontendUserRepository.php:144
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\__construct
‪__construct(UserService $userService, Context $context)
Definition: FrontendUserRepository.php:43
‪TYPO3\CMS\Core\Context\Context
Definition: Context.php:53
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\updateForgotHashForUserByEmail
‪updateForgotHashForUserByEmail(string $emailAddress, string $hash)
Definition: FrontendUserRepository.php:103
‪TYPO3\CMS\FrontendLogin\Service\UserService
Definition: UserService.php:26
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\existsUserWithHash
‪existsUserWithHash(string $hash)
Definition: FrontendUserRepository.php:82
‪TYPO3\CMS\Core\Database\Connection
Definition: Connection.php:38
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\$context
‪Context $context
Definition: FrontendUserRepository.php:37
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\findRedirectIdPageByUserId
‪findRedirectIdPageByUserId(int $uid)
Definition: FrontendUserRepository.php:194
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\$userService
‪UserService $userService
Definition: FrontendUserRepository.php:41
‪TYPO3\CMS\Core\Database\ConnectionPool
Definition: ConnectionPool.php:46
‪TYPO3\CMS\Core\Utility\GeneralUtility
Definition: GeneralUtility.php:50
‪TYPO3\CMS\FrontendLogin\Domain\Repository\FrontendUserRepository\getTable
‪getTable()
Definition: FrontendUserRepository.php:52
‪TYPO3\CMS\FrontendLogin\Domain\Repository
Definition: FrontendUserGroupRepository.php:18