|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SumoCoders\FrameworkMultiUserBundle\User; |
|
4
|
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
|
6
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Entity\BaseUser; |
|
7
|
|
|
use SumoCoders\FrameworkMultiUserBundle\Security\PasswordResetToken; |
|
8
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\User; |
|
9
|
|
|
use SumoCoders\FrameworkMultiUserBundle\User\Interfaces\UserRepository as UserRepositoryInterface; |
|
10
|
|
|
use Symfony\Component\Security\Core\Encoder\EncoderFactory; |
|
11
|
|
|
|
|
12
|
|
|
class InMemoryBaseUserRepository implements UserRepositoryInterface |
|
13
|
|
|
{ |
|
14
|
|
|
/** @var BaseUser[] */ |
|
15
|
|
|
private $users = []; |
|
16
|
|
|
|
|
17
|
|
|
public function __construct(EncoderFactory $encoderFactory) |
|
18
|
|
|
{ |
|
19
|
|
|
$user = new BaseUser( |
|
20
|
|
|
'wouter', |
|
21
|
|
|
'test', |
|
22
|
|
|
'Wouter Sioen', |
|
23
|
|
|
'[email protected]', |
|
24
|
|
|
new ArrayCollection(['ROLE_USER']), |
|
25
|
|
|
1 |
|
26
|
|
|
); |
|
27
|
|
|
$user->encodePassword($encoderFactory->getEncoder($user)); |
|
28
|
|
|
|
|
29
|
|
|
$this->users[] = $user; |
|
30
|
|
|
|
|
31
|
|
|
$passwordResetUser = new BaseUser( |
|
32
|
|
|
'reset', |
|
33
|
|
|
'reset', |
|
34
|
|
|
'reset', |
|
35
|
|
|
'[email protected]', |
|
36
|
|
|
new ArrayCollection(['ROLE_USER']), |
|
37
|
|
|
2, |
|
38
|
|
|
PasswordResetToken::generate() |
|
39
|
|
|
); |
|
40
|
|
|
$passwordResetUser->encodePassword($encoderFactory->getEncoder($passwordResetUser)); |
|
41
|
|
|
|
|
42
|
|
|
$this->users[] = $passwordResetUser; |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function findByUsername(string $username): ?User |
|
46
|
|
|
{ |
|
47
|
|
|
foreach ($this->users as $user) { |
|
48
|
|
|
if ($user->getUsername() === $username) { |
|
49
|
|
|
return $user; |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return null; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
public function findByEmailAddress(string $emailAddress): ?User |
|
57
|
|
|
{ |
|
58
|
|
|
foreach ($this->users as $user) { |
|
59
|
|
|
if ($user->getEmail() === $emailAddress) { |
|
60
|
|
|
return $user; |
|
61
|
|
|
} |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
return null; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @param int $id |
|
69
|
|
|
* |
|
70
|
|
|
* @return null|User |
|
71
|
|
|
*/ |
|
72
|
|
|
public function find($id): ?User |
|
73
|
|
|
{ |
|
74
|
|
|
foreach ($this->users as $user) { |
|
75
|
|
|
if ($user->getId() === $id) { |
|
76
|
|
|
return $user; |
|
77
|
|
|
} |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
return null; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
public function supportsClass(string $class): bool |
|
84
|
|
|
{ |
|
85
|
|
|
return $class === BaseUser::class; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
public function findByPasswordResetToken(PasswordResetToken $token): ?User |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->findByUsername('reset'); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function add(User $user): void |
|
94
|
|
|
{ |
|
95
|
|
|
$this->users[] = $user; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
/** |
|
99
|
|
|
* This does nothing here since the objects get updated by reference when changing them in the tests |
|
100
|
|
|
*/ |
|
101
|
|
|
public function save(User $user): void |
|
102
|
|
|
{ |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
public function delete(User $user): void |
|
106
|
|
|
{ |
|
107
|
|
|
foreach ($this->users as $key => $row) { |
|
108
|
|
|
if ($row->getUserName() === $user->getUserName()) { |
|
109
|
|
|
unset($this->users[$key]); |
|
110
|
|
|
break; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|