1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kunstmaan\AdminBundle\Service; |
4
|
|
|
|
5
|
|
|
use Doctrine\ORM\EntityManagerInterface; |
6
|
|
|
use Doctrine\ORM\EntityRepository; |
7
|
|
|
use Kunstmaan\AdminBundle\Entity\UserInterface; |
8
|
|
|
use Symfony\Component\Security\Core\Encoder\BCryptPasswordEncoder; |
9
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface; |
10
|
|
|
|
11
|
|
|
class UserManager |
12
|
|
|
{ |
13
|
|
|
/** @var EntityManagerInterface */ |
14
|
|
|
protected $em; |
15
|
|
|
|
16
|
|
|
/** @var string */ |
17
|
|
|
private $class; |
18
|
|
|
|
19
|
|
|
/** @var string */ |
20
|
|
|
private $encoderFactory; |
21
|
|
|
|
22
|
|
|
public function __construct(EncoderFactoryInterface $encoderFactory, EntityManagerInterface $em, string $class) |
23
|
|
|
{ |
24
|
|
|
$this->em = $em; |
25
|
|
|
$this->class = $class; |
26
|
|
|
$this->encoderFactory = $encoderFactory; |
|
|
|
|
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function deleteUser(UserInterface $user) |
30
|
|
|
{ |
31
|
|
|
$this->em->remove($user); |
32
|
|
|
$this->em->flush(); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function findUsers() |
36
|
|
|
{ |
37
|
|
|
return $this->getRepository()->findAll(); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
|
protected function getRepository(): EntityRepository |
41
|
|
|
{ |
42
|
|
|
return $this->em->getRepository($this->getClass()); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
public function getClass() |
46
|
|
|
{ |
47
|
|
|
if (false !== strpos($this->class, ':')) { |
48
|
|
|
$metadata = $this->em->getClassMetadata($this->class); |
49
|
|
|
$this->class = $metadata->getName(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
return $this->class; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
public function reloadUser(UserInterface $user) |
56
|
|
|
{ |
57
|
|
|
$this->em->refresh($user); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
public function updateUser(UserInterface $user, $andFlush = true) |
61
|
|
|
{ |
62
|
|
|
$this->updatePassword($user); |
63
|
|
|
|
64
|
|
|
$this->em->persist($user); |
65
|
|
|
if ($andFlush) { |
66
|
|
|
$this->em->flush(); |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
public function updatePassword(UserInterface $user) |
71
|
|
|
{ |
72
|
|
|
$plainPassword = $user->getPlainPassword(); |
73
|
|
|
|
74
|
|
|
if (0 === strlen($plainPassword)) { |
75
|
|
|
return; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
$encoder = $this->encoderFactory->getEncoder($user); |
|
|
|
|
79
|
|
|
|
80
|
|
|
if ($encoder instanceof BCryptPasswordEncoder) { |
81
|
|
|
$user->setSalt(null); |
82
|
|
|
} else { |
83
|
|
|
$salt = rtrim(str_replace('+', '.', base64_encode(random_bytes(32))), '='); |
84
|
|
|
$user->setSalt($salt); |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
$hashedPassword = $encoder->encodePassword($plainPassword, $user->getSalt()); |
88
|
|
|
$user->setPassword($hashedPassword); |
89
|
|
|
$user->eraseCredentials(); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function createUser() |
93
|
|
|
{ |
94
|
|
|
$class = $this->getClass(); |
95
|
|
|
$user = new $class(); |
96
|
|
|
|
97
|
|
|
return $user; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
public function findUserByUsernameOrEmail($usernameOrEmail) |
101
|
|
|
{ |
102
|
|
|
if (preg_match('/^.+\@\S+\.\S+$/', $usernameOrEmail)) { |
103
|
|
|
$user = $this->findUserByEmail($usernameOrEmail); |
104
|
|
|
if (null !== $user) { |
105
|
|
|
return $user; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $this->findUserByUsername($usernameOrEmail); |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
public function findUserByEmail($email) |
113
|
|
|
{ |
114
|
|
|
return $this->findUserBy(['email' => $email]); |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
public function findUserBy(array $criteria) |
118
|
|
|
{ |
119
|
|
|
return $this->getRepository()->findOneBy($criteria); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
public function findUserByUsername($username) |
123
|
|
|
{ |
124
|
|
|
return $this->findUserBy(['username' => $username]); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
public function findUserByConfirmationToken($token) |
128
|
|
|
{ |
129
|
|
|
return $this->findUserBy(['confirmationToken' => $token]); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..