forked from FriendsOfSymfony/FOSUserBundle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUserManager.php
173 lines (149 loc) · 4.96 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
<?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\Document;
use Doctrine\ODM\MongoDB\DocumentManager;
use Doctrine\ODM\MongoDB\Proxy\Proxy;
use FOS\UserBundle\Model\UserInterface;
use FOS\UserBundle\Model\UserManager as BaseUserManager;
use FOS\UserBundle\Util\CanonicalizerInterface;
use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
use Symfony\Component\Validator\Constraint;
class UserManager extends BaseUserManager
{
protected $dm;
protected $repository;
protected $class;
/**
* Constructor.
*
* @param EncoderFactoryInterface $encoderFactory
* @param CanonicalizerInterface $usernameCanonicalizer
* @param CanonicalizerInterface $emailCanonicalizer
* @param DocumentManager $dm
* @param string $class
*/
public function __construct(EncoderFactoryInterface $encoderFactory, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, DocumentManager $dm, $class)
{
parent::__construct($encoderFactory, $usernameCanonicalizer, $emailCanonicalizer);
$this->dm = $dm;
$this->repository = $dm->getRepository($class);
$metadata = $dm->getClassMetadata($class);
$this->class = $metadata->name;
}
/**
* {@inheritDoc}
*/
public function deleteUser(UserInterface $user)
{
$this->dm->remove($user);
$this->dm->flush();
}
/**
* {@inheritDoc}
*/
public function getClass()
{
return $this->class;
}
/**
* {@inheritDoc}
*/
public function findUserBy(array $criteria)
{
return $this->repository->findOneBy($criteria);
}
/**
* {@inheritDoc}
*/
public function findUsers()
{
return $this->repository->findAll();
}
/**
* {@inheritDoc}
*/
public function reloadUser(UserInterface $user)
{
$this->dm->refresh($user);
}
/**
* Updates a user.
*
* @param UserInterface $user
* @param Boolean $andFlush Whether to flush the changes (default true)
*/
public function updateUser(UserInterface $user, $andFlush = true)
{
$this->updateCanonicalFields($user);
$this->updatePassword($user);
$this->dm->persist($user);
if ($andFlush) {
$this->dm->flush();
}
}
/**
* {@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);
$classMetadata = $this->dm->getClassMetadata($this->class);
// TODO: ODM seems to be missing handling for multiple properties
// $fields = array_map('trim', explode(',', $constraint->property));
$query = $this->getQueryArray($classMetadata, $value, $constraint->property);
$document = $this->findUserBy($query);
if (null === $document) {
return true;
}
// check if document in mongodb is the same document as the checked one
if ($document->isUser($value)) {
return true;
}
// check if returned document is proxy and initialize the minimum identifier if needed
if ($document instanceof Proxy) {
$classMetadata->setIdentifierValue($document, $document->__identifier);
}
// check if document has the same identifier as the current one
if ($classMetadata->getIdentifierValue($document) === $classMetadata->getIdentifierValue($value)) {
return true;
}
return false;
}
protected function getQueryArray($classMetadata, $value, $fieldName)
{
$field = $this->getFieldNameFromPropertyPath($fieldName);
if (!isset($classMetadata->fieldMappings[$field])) {
throw new \LogicException("Mapping for '$fieldName' doesn't exist for " . $this->class);
}
$mapping = $classMetadata->fieldMappings[$field];
if (isset($mapping['reference']) && $mapping['reference']) {
throw new \LogicException('Cannot determine uniqueness of referenced document values');
}
$criteria[$field] = $value instanceOf UserInterface ? $classMetadata->getFieldValue($value, $field) : $value;
return $criteria;
}
/**
* Returns the actual document field value.
*
* E.g. document.someVal -> document
* user.emails -> user
* username -> username
*
* @param string $field
* @return string
*/
protected function getFieldNameFromPropertyPath($field)
{
$pieces = explode('.', $field);
return $pieces[0];
}
}