Skip to content

Commit 522f317

Browse files
committed
Merge branch 'master' of github.com:ProspectOne/UserModule into 233-we-need-to-have-ability-to-set-new-password-without-knowing-previous-one
2 parents 8fbef51 + f3053dd commit 522f317

File tree

3 files changed

+59
-32
lines changed

3 files changed

+59
-32
lines changed

src/Service/AuthAdapter.php

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,8 +157,7 @@ public function setPassword($password)
157157
public function authenticate()
158158
{
159159
/** @var User $user */
160-
$user = $this->entityManager->getRepository(User::class)
161-
->findOneByEmail($this->email);
160+
$user = $this->getUserByEmail($this->email);
162161
return $this->validateUser($user);
163162
}
164163

@@ -176,14 +175,14 @@ public function headerAuth()
176175
}
177176

178177
/** @var User $user */
179-
$user = $this->entityManager->getRepository(User::class)
180-
->findOneByToken($this->getAuthHeader());
178+
$user = $this->getUserByToken($this->getAuthHeader());
181179

182180
if (empty($user)) {
183-
throw new LogicException("Invalid user token");
181+
throw new LogicException(LogicException::MESSAGE);
184182
}
185183

186184
if(!empty($user) && $user->getStatus() !== User::STATUS_RETIRED) {
185+
$this->setEmail($user->getEmail());
187186
return $user;
188187
}
189188

@@ -230,6 +229,25 @@ protected function validateUser(?UserInterface $user): Result
230229
null,
231230
['Invalid credentials.']);
232231
}
233-
}
234-
232+
/**
233+
* Find user by password reset token
234+
* @param string $token
235+
* @return mixed
236+
*/
237+
public function getUserByToken(string $token)
238+
{
239+
return $this->entityManager->getRepository(User::class)
240+
->findOneByToken($token);
241+
}
235242

243+
/**
244+
* Find user by Email
245+
* @param string $email
246+
* @return mixed
247+
*/
248+
public function getUserByEmail(string $email)
249+
{
250+
return $this->entityManager->getRepository(User::class)
251+
->findOneByEmail($email);
252+
}
253+
}

src/Service/Factory/AuthManagerFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ class AuthManagerFactory implements FactoryInterface
2525
public function __invoke(ContainerInterface $container, $requestedName, array $options = null)
2626
{
2727
// Instantiate dependencies.
28-
$authenticationService = $container->get(AuthenticationService::class);
2928
$sessionManager = $container->get(SessionManager::class);
29+
$authenticationService = $container->get(AuthenticationService::class);
3030

3131
// Get contents of 'access_filter' config key (the AuthManager service
3232
// will use this data to determine whether to allow currently logged in user

src/Service/UserManager.php

Lines changed: 33 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,18 @@ class UserManager
3434
/**
3535
* @return EntityManager
3636
*/
37-
public function getEntityManager(): EntityManager
38-
{
39-
return $this->entityManager;
40-
}
37+
public function getEntityManager(): EntityManager
38+
{
39+
return $this->entityManager;
40+
}
4141

42-
/**
43-
* @return Bcrypt
44-
*/
45-
public function getBcrypt(): Bcrypt
46-
{
47-
return $this->bcrypt;
48-
}
42+
/**
43+
* @return Bcrypt
44+
*/
45+
public function getBcrypt(): Bcrypt
46+
{
47+
return $this->bcrypt;
48+
}
4949

5050
/**
5151
* UserManager constructor.
@@ -164,22 +164,19 @@ public function createAdminUserIfNotExists()
164164
public function hasRole($email, $roles)
165165
{
166166
/** @var User $user */
167-
$user = $this->entityManager->getRepository(User::class)->findOneByEmail($email);
168-
if(in_array($user->getRoleName(),$roles, true)) {
169-
return true;
170-
}
171-
return false;
167+
$user = $this->getUserByEmail($email);
168+
169+
return in_array($user->getRoleName(),$roles, true);
172170
}
173171

174172
/**
175-
* Checks whether an active user with given email address already exists in the database.
173+
* Checks whether an active user with given email address already exists in the database.
174+
* @param string $email
175+
* @return bool
176176
*/
177-
public function checkUserExists($email) {
178-
179-
$user = $this->entityManager->getRepository(User::class)
180-
->findOneByEmail($email);
181-
182-
return $user !== null;
177+
public function checkUserExists(string $email)
178+
{
179+
return !empty($this->getUserByEmail($email));
183180
}
184181

185182
/**
@@ -264,10 +261,22 @@ public function validatePasswordResetToken($passwordResetToken)
264261
* @param string $passwordResetToken
265262
* @return mixed
266263
*/
267-
public function getUserByPasswordResetToken(string $passwordResetToken) {
264+
public function getUserByPasswordResetToken(string $passwordResetToken)
265+
{
268266
return $this->entityManager->getRepository(User::class)
269267
->findOneByPasswordResetToken($passwordResetToken);
270268
}
269+
270+
/**
271+
* Find user by Email
272+
* @param string $email
273+
* @return mixed
274+
*/
275+
public function getUserByEmail(string $email)
276+
{
277+
return $this->entityManager->getRepository(User::class)
278+
->findOneByEmail($email);
279+
}
271280

272281
/**
273282
* This method sets new password by password reset token.

0 commit comments

Comments
 (0)