|
@@ -0,0 +1,189 @@
|
|
1
|
+<?php
|
|
2
|
+
|
|
3
|
+/*
|
|
4
|
+ *
|
|
5
|
+ */
|
|
6
|
+
|
|
7
|
+namespace Muzich\UserBundle\Entity;
|
|
8
|
+
|
|
9
|
+use Doctrine\ORM\EntityManager;
|
|
10
|
+use FOS\UserBundle\Model\UserInterface;
|
|
11
|
+use FOS\UserBundle\Model\UserManager as BaseUserManager;
|
|
12
|
+use FOS\UserBundle\Util\CanonicalizerInterface;
|
|
13
|
+use Symfony\Component\Security\Core\Encoder\EncoderFactoryInterface;
|
|
14
|
+use Symfony\Component\Validator\Constraint;
|
|
15
|
+
|
|
16
|
+class UserManager extends BaseUserManager
|
|
17
|
+{
|
|
18
|
+ protected $em;
|
|
19
|
+ protected $class;
|
|
20
|
+ protected $repository;
|
|
21
|
+
|
|
22
|
+ /**
|
|
23
|
+ * Constructor.
|
|
24
|
+ *
|
|
25
|
+ * @param EncoderFactoryInterface $encoderFactory
|
|
26
|
+ * @param string $algorithm
|
|
27
|
+ * @param CanonicalizerInterface $usernameCanonicalizer
|
|
28
|
+ * @param CanonicalizerInterface $emailCanonicalizer
|
|
29
|
+ * @param EntityManager $em
|
|
30
|
+ * @param string $class
|
|
31
|
+ */
|
|
32
|
+ public function __construct(EncoderFactoryInterface $encoderFactory, $algorithm, CanonicalizerInterface $usernameCanonicalizer, CanonicalizerInterface $emailCanonicalizer, EntityManager $em, $class)
|
|
33
|
+ {
|
|
34
|
+ parent::__construct($encoderFactory, $algorithm, $usernameCanonicalizer, $emailCanonicalizer);
|
|
35
|
+
|
|
36
|
+ $this->em = $em;
|
|
37
|
+ $this->repository = $em->getRepository($class);
|
|
38
|
+
|
|
39
|
+ $metadata = $em->getClassMetadata($class);
|
|
40
|
+ $this->class = $metadata->name;
|
|
41
|
+
|
|
42
|
+ // Slug stuff
|
|
43
|
+ $evm = new \Doctrine\Common\EventManager();
|
|
44
|
+ // ORM and ODM
|
|
45
|
+ $sluggableListener = new \Gedmo\Sluggable\SluggableListener();
|
|
46
|
+ $evm->addEventSubscriber($sluggableListener);
|
|
47
|
+ // now this event manager should be passed to entity manager constructor
|
|
48
|
+ $this->em->getEventManager()->addEventSubscriber($sluggableListener);
|
|
49
|
+ }
|
|
50
|
+
|
|
51
|
+ /**
|
|
52
|
+ * {@inheritDoc}
|
|
53
|
+ */
|
|
54
|
+ public function deleteUser(UserInterface $user)
|
|
55
|
+ {
|
|
56
|
+ $this->em->remove($user);
|
|
57
|
+ $this->em->flush();
|
|
58
|
+ }
|
|
59
|
+
|
|
60
|
+ /**
|
|
61
|
+ * {@inheritDoc}
|
|
62
|
+ */
|
|
63
|
+ public function getClass()
|
|
64
|
+ {
|
|
65
|
+ return $this->class;
|
|
66
|
+ }
|
|
67
|
+
|
|
68
|
+ /**
|
|
69
|
+ * {@inheritDoc}
|
|
70
|
+ */
|
|
71
|
+ public function findUserBy(array $criteria)
|
|
72
|
+ {
|
|
73
|
+ return $this->repository->findOneBy($criteria);
|
|
74
|
+ }
|
|
75
|
+
|
|
76
|
+ /**
|
|
77
|
+ * {@inheritDoc}
|
|
78
|
+ */
|
|
79
|
+ public function findUsers()
|
|
80
|
+ {
|
|
81
|
+ return $this->repository->findAll();
|
|
82
|
+ }
|
|
83
|
+
|
|
84
|
+ /**
|
|
85
|
+ * {@inheritDoc}
|
|
86
|
+ */
|
|
87
|
+ public function reloadUser(UserInterface $user)
|
|
88
|
+ {
|
|
89
|
+ $this->em->refresh($user);
|
|
90
|
+ }
|
|
91
|
+
|
|
92
|
+ /**
|
|
93
|
+ * Updates a user.
|
|
94
|
+ *
|
|
95
|
+ * @param UserInterface $user
|
|
96
|
+ * @param Boolean $andFlush Whether to flush the changes (default true)
|
|
97
|
+ */
|
|
98
|
+ public function updateUser(UserInterface $user, $andFlush = true)
|
|
99
|
+ {
|
|
100
|
+ $this->updateCanonicalFields($user);
|
|
101
|
+ $this->updatePassword($user);
|
|
102
|
+
|
|
103
|
+ $this->em->persist($user);
|
|
104
|
+ if ($andFlush) {
|
|
105
|
+ $this->em->flush();
|
|
106
|
+ }
|
|
107
|
+ }
|
|
108
|
+
|
|
109
|
+ /**
|
|
110
|
+ * {@inheritDoc}
|
|
111
|
+ */
|
|
112
|
+ public function validateUnique(UserInterface $value, Constraint $constraint)
|
|
113
|
+ {
|
|
114
|
+ // Since we probably want to validate the canonical fields,
|
|
115
|
+ // we'd better make sure we have them.
|
|
116
|
+ $this->updateCanonicalFields($value);
|
|
117
|
+
|
|
118
|
+ $fields = array_map('trim', explode(',', $constraint->property));
|
|
119
|
+ $users = $this->findConflictualUsers($value, $fields);
|
|
120
|
+
|
|
121
|
+ // there is no conflictual user
|
|
122
|
+ if (empty($users)) {
|
|
123
|
+ return true;
|
|
124
|
+ }
|
|
125
|
+
|
|
126
|
+ // there is no conflictual user which is not the same as the value
|
|
127
|
+ if ($this->anyIsUser($value, $users)) {
|
|
128
|
+ return true;
|
|
129
|
+ }
|
|
130
|
+
|
|
131
|
+ return false;
|
|
132
|
+ }
|
|
133
|
+
|
|
134
|
+ /**
|
|
135
|
+ * Indicates whether the given user and all compared objects correspond to the same record.
|
|
136
|
+ *
|
|
137
|
+ * @param UserInterface $user
|
|
138
|
+ * @param array $comparisons
|
|
139
|
+ * @return Boolean
|
|
140
|
+ */
|
|
141
|
+ protected function anyIsUser($user, array $comparisons)
|
|
142
|
+ {
|
|
143
|
+ foreach ($comparisons as $comparison) {
|
|
144
|
+ if (!$user->isUser($comparison)) {
|
|
145
|
+ return false;
|
|
146
|
+ }
|
|
147
|
+ }
|
|
148
|
+
|
|
149
|
+ return true;
|
|
150
|
+ }
|
|
151
|
+
|
|
152
|
+ /**
|
|
153
|
+ * Gets conflictual users for the given user and constraint.
|
|
154
|
+ *
|
|
155
|
+ * @param UserInterface $value
|
|
156
|
+ * @param array $fields
|
|
157
|
+ * @return array
|
|
158
|
+ */
|
|
159
|
+ protected function findConflictualUsers($value, array $fields)
|
|
160
|
+ {
|
|
161
|
+ return $this->repository->findBy($this->getCriteria($value, $fields));
|
|
162
|
+ }
|
|
163
|
+
|
|
164
|
+ /**
|
|
165
|
+ * Gets the criteria used to find conflictual entities.
|
|
166
|
+ *
|
|
167
|
+ * @param UserInterface $value
|
|
168
|
+ * @param array $constraint
|
|
169
|
+ * @return array
|
|
170
|
+ */
|
|
171
|
+ protected function getCriteria($value, array $fields)
|
|
172
|
+ {
|
|
173
|
+ $classMetadata = $this->em->getClassMetadata($this->class);
|
|
174
|
+
|
|
175
|
+ $criteria = array();
|
|
176
|
+ foreach ($fields as $field) {
|
|
177
|
+ if (!$classMetadata->hasField($field)) {
|
|
178
|
+ throw new \InvalidArgumentException(sprintf('The "%s" class metadata does not have any "%s" field or association mapping.', $this->class, $field));
|
|
179
|
+ }
|
|
180
|
+
|
|
181
|
+ $criteria[$field] = $classMetadata->getFieldValue($value, $field);
|
|
182
|
+ }
|
|
183
|
+
|
|
184
|
+ return $criteria;
|
|
185
|
+ }
|
|
186
|
+}
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+?>
|