forked from KnpLabs/KnpUserBundle
/
UserManager.php
222 lines (186 loc) · 5.59 KB
/
UserManager.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
<?php
/*
* This file is part of the FOSUserBundle package.
*
* (c) FriendsOfSymfony <http://friendsofsymfony.github.com/>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace FOS\UserBundle\Propel;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManager as BaseUserManager;
use FOS\UserBundle\Propel\User;
use FOS\UserBundle\Util\CanonicalizerInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserInterface as SecurityUserInterface;
use Symfony\Component\Validator\Constraint;
class UserManager extends BaseUserManager
{
protected $class;
protected $modelClass;
/**
* Constructor.
*
* @param EncoderFactoryInterface $encoderFactory
* @param CanonicalizerInterface $usernameCanonicalizer
* @param CanonicalizerInterface $emailCanonicalizer
* @param string $proxyClass
* @param string $modelClass
*/
public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, $proxyClass, $modelClass)
{
parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer);
$this->class = $proxyClass;
$this->modelClass = $modelClass;
}
/**
* {@inheritDoc}
*/
public function deleteUser(UserInterface $user)
{
if (!$user instanceof UserProxy) {
throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
}
$user->delete();
}
/**
* Returns an empty user instance
*
* @return UserInterface
*/
public function createUser()
{
$class = $this->modelClass;
$user = new $class();
return $this->proxyfy($user);
}
public function getModelClass()
{
return $this->modelClass;
}
/**
* {@inheritDoc}
*/
public function getClass()
{
return $this->class;
}
/**
* {@inheritDoc}
*/
public function findUserBy(array $criteria)
{
$query = $this->createQuery();
foreach ($criteria as $field => $value) {
$method = 'filterBy'.ucfirst($field);
$query->$method($value);
}
$user = $query->findOne();
if ($user) {
$user = $this->proxyfy($user);
}
return $user;
}
/**
* {@inheritDoc}
*/
public function findUsers()
{
return $this->createQuery()->find();
}
/**
* {@inheritDoc}
*/
public function reloadUser(UserInterface $user)
{
if (!$user instanceof UserProxy) {
throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
}
$user->reload();
}
/**
* Updates a user.
*
* @param UserInterface $user
*/
public function updateUser(UserInterface $user)
{
if (!$user instanceof UserProxy) {
throw new \InvalidArgumentException('This user instance is not supported by the Propel UserManager implementation');
}
$this->updateCanonicalFields($user);
$this->updatePassword($user);
$user->save();
}
/**
* {@inheritDoc}
*/
public function validateUnique(UserInterface $value, Constraint $constraint)
{
// Since we probably want to validate the canonical fields,
// we'd better make sure we have them.
$this->updateCanonicalFields($value);
$fields = array_map('trim', explode(',', $constraint->property));
$users = $this->findConflictualUsers($value, $fields);
// there is no conflictual user
if (empty($users)) {
return true;
}
// there is no conflictual user which is not the same as the value
if ($this->anyIsUser($value, $users)) {
return true;
}
return false;
}
/**
* Indicates whether the given user and all compared objects correspond to the same record.
*
* @param UserInterface $user
* @param array $comparisons
* @return Boolean
*/
protected function anyIsUser($user, array $comparisons)
{
foreach ($comparisons as $comparison) {
foreach ($comparison as $field => $value) {
if ($user->{'get'.$field}() !== $value) {
return false;
}
}
}
return true;
}
/**
* Gets conflictual users for the given user and constraint.
*
* @param UserInterface $value
* @param array $fields
* @return array
*/
protected function findConflictualUsers($value, array $fields)
{
$query = $this->createQuery();
foreach ($fields as $field) {
$method = 'get'.ucfirst($field);
$query->filterBy(ucfirst($field), $value->$method());
}
return $query->find()->toArray();
}
/**
* Create the propel query class corresponding to your queryclass
*
* @return \ModelCriteria the queryClass
*/
protected function createQuery()
{
return \PropelQuery::from($this->modelClass);
}
protected function proxyfy($user)
{
$proxyClass = $this->getClass();
$proxy = new $proxyClass($user);
return $proxy;
}
}