1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace App\Users\Service; |
6
|
|
|
|
7
|
|
|
use App\Users\Entity\ApiToken; |
8
|
|
|
use App\Users\Entity\User; |
9
|
|
|
use App\Users\Repository\UserRepository; |
10
|
|
|
use App\Users\Request\AuthUserRequest; |
11
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
12
|
|
|
use Symfony\Component\Security\Core\Encoder\UserPasswordEncoderInterface; |
13
|
|
|
use Symfony\Component\Security\Core\Exception\BadCredentialsException; |
14
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
15
|
|
|
|
16
|
|
|
class AuthService |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @var EntityManagerInterface |
20
|
|
|
*/ |
21
|
|
|
private $entityManager; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var UserRepository |
25
|
|
|
*/ |
26
|
|
|
private $userRepository; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var TranslatorInterface |
30
|
|
|
*/ |
31
|
|
|
private $translator; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var UserPasswordEncoderInterface |
35
|
|
|
*/ |
36
|
|
|
private $passwordEncoder; |
37
|
|
|
|
38
|
8 |
|
public function __construct( |
39
|
|
|
EntityManagerInterface $entityManager, |
40
|
|
|
UserRepository $userRepository, |
41
|
|
|
TranslatorInterface $translator, |
42
|
|
|
UserPasswordEncoderInterface $passwordEncoder |
43
|
|
|
) { |
44
|
8 |
|
$this->entityManager = $entityManager; |
45
|
8 |
|
$this->userRepository = $userRepository; |
46
|
8 |
|
$this->translator = $translator; |
47
|
8 |
|
$this->passwordEncoder = $passwordEncoder; |
48
|
8 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param AuthUserRequest $request |
52
|
|
|
* |
53
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
54
|
|
|
* |
55
|
|
|
* @return ApiToken |
56
|
|
|
*/ |
57
|
8 |
|
public function getTokenByRequest(AuthUserRequest $request): ApiToken |
58
|
|
|
{ |
59
|
8 |
|
$credentials = $request->get('credentials'); |
60
|
|
|
|
61
|
8 |
|
return $this->getTokenByCredentials($credentials['username'], $credentials['password']); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $username |
66
|
|
|
* @param string $password |
67
|
|
|
* |
68
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
69
|
|
|
* |
70
|
|
|
* @return ApiToken |
71
|
|
|
*/ |
72
|
8 |
|
public function getTokenByCredentials(string $username, string $password): ApiToken |
73
|
|
|
{ |
74
|
8 |
|
$user = $this->findUserByCredentials($username, $password); |
75
|
4 |
|
$apiToken = $this->createApiTokenForUser($user); |
76
|
|
|
|
77
|
4 |
|
return $apiToken; |
78
|
|
|
} |
79
|
|
|
|
80
|
4 |
|
private function createApiTokenForUser(User $user): ApiToken |
81
|
|
|
{ |
82
|
4 |
|
$apiToken = new ApiToken($user); |
83
|
|
|
|
84
|
4 |
|
$this->entityManager->persist($apiToken); |
85
|
4 |
|
$this->entityManager->flush(); |
86
|
|
|
|
87
|
4 |
|
return $apiToken; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @param string $username |
92
|
|
|
* @param string $password |
93
|
|
|
* |
94
|
|
|
* @throws \Doctrine\ORM\NonUniqueResultException |
95
|
|
|
* |
96
|
|
|
* @return User |
97
|
|
|
*/ |
98
|
8 |
|
private function findUserByCredentials(string $username, string $password): User |
99
|
|
|
{ |
100
|
8 |
|
$user = $this->userRepository->loadUserByUsername($username); |
101
|
|
|
|
102
|
8 |
|
if ($user === null) { |
103
|
2 |
|
throw new BadCredentialsException( |
104
|
2 |
|
$this->translator->trans('user_with_this_username_not_exist', [ |
105
|
2 |
|
'username' => $username, |
106
|
2 |
|
], 'users') |
107
|
|
|
); |
108
|
|
|
} |
109
|
|
|
|
110
|
6 |
|
if ($user->isPasswordValid($password, $this->passwordEncoder) === false) { |
111
|
2 |
|
throw new BadCredentialsException( |
112
|
2 |
|
$this->translator->trans('wrong_password', [], 'users') |
113
|
|
|
); |
114
|
|
|
} |
115
|
|
|
|
116
|
4 |
|
return $user; |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|