|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* YAWIK |
|
4
|
|
|
* |
|
5
|
|
|
* @filesource |
|
6
|
|
|
* @copyright (c) 2013 - 2016 Cross Solution (http://cross-solution.de) |
|
7
|
|
|
* @license MIT |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Auth\Repository; |
|
11
|
|
|
|
|
12
|
|
|
use \Auth\Entity\Info; |
|
13
|
|
|
use Auth\Entity\UserInterface; |
|
14
|
|
|
use Core\Repository\AbstractRepository; |
|
15
|
|
|
use Doctrine\ODM\MongoDB\Event\LifecycleEventArgs; |
|
16
|
|
|
use Doctrine\ODM\MongoDB\Events; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* class for accessing a user |
|
20
|
|
|
*/ |
|
21
|
|
|
class User extends AbstractRepository |
|
22
|
|
|
{ |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* {@inheritDoc} |
|
26
|
|
|
*/ |
|
27
|
|
View Code Duplication |
public function findBy(array $criteria, array $sort = null, $limit = null, $skip = null) |
|
|
|
|
|
|
28
|
|
|
{ |
|
29
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
30
|
|
|
$criteria['isDraft'] = false; |
|
31
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
32
|
|
|
unset($criteria['isDraft']); |
|
33
|
|
|
} |
|
34
|
|
|
return parent::findBy($criteria, $sort, $limit, $skip); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritDoc} |
|
39
|
|
|
* @return null | UserInterface |
|
|
|
|
|
|
40
|
|
|
*/ |
|
41
|
|
View Code Duplication |
public function findOneBy(array $criteria) |
|
|
|
|
|
|
42
|
|
|
{ |
|
43
|
|
|
if (!array_key_exists('isDraft', $criteria)) { |
|
44
|
|
|
$criteria['isDraft'] = false; |
|
45
|
|
|
} elseif (null === $criteria['isDraft']) { |
|
46
|
|
|
unset($criteria['isDraft']); |
|
47
|
|
|
} |
|
48
|
|
|
return parent::findOneBy($criteria); |
|
49
|
|
|
} |
|
50
|
|
|
|
|
51
|
|
|
/** |
|
52
|
|
|
* {@inheritDoc} |
|
53
|
|
|
*/ |
|
54
|
|
View Code Duplication |
public function createQueryBuilder($findDrafts = false) |
|
|
|
|
|
|
55
|
|
|
{ |
|
56
|
|
|
$qb = parent::createQueryBuilder(); |
|
57
|
|
|
if (null !== $findDrafts) { |
|
58
|
|
|
$qb->field('isDraft')->equals($findDrafts); |
|
59
|
|
|
} |
|
60
|
|
|
return $qb; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Creates a User |
|
65
|
|
|
* |
|
66
|
|
|
* @see \Core\Repository\AbstractRepository::create() |
|
67
|
|
|
* @return UserInterface |
|
|
|
|
|
|
68
|
|
|
*/ |
|
69
|
|
|
public function create(array $data = null) |
|
70
|
|
|
{ |
|
71
|
|
|
$entity = parent::create($data); |
|
72
|
|
|
|
|
73
|
|
|
$eventArgs = new LifecycleEventArgs($entity, $this->dm); |
|
74
|
|
|
$this->dm->getEventManager()->dispatchEvent( |
|
75
|
|
|
Events::postLoad, |
|
76
|
|
|
$eventArgs |
|
77
|
|
|
); |
|
78
|
|
|
return $entity; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Finds user by profile identifier |
|
83
|
|
|
* |
|
84
|
|
|
* @param string $identifier |
|
85
|
|
|
* @param string $provider |
|
86
|
|
|
* @return UserInterface |
|
|
|
|
|
|
87
|
|
|
*/ |
|
88
|
|
|
public function findByProfileIdentifier($identifier, $provider) |
|
89
|
|
|
{ |
|
90
|
|
|
return $this->findOneBy(array('profiles.' . $provider . '.auth.identifier' => $identifier)) ?: $this->findOneBy(array('profile.identifier' => $identifier)); |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Returns true if profile is already assigned to anotherUser |
|
95
|
|
|
* |
|
96
|
|
|
* @param int $curentUserId |
|
97
|
|
|
* @param string $identifier |
|
98
|
|
|
* @param string $provider |
|
99
|
|
|
* @return bool |
|
100
|
|
|
*/ |
|
101
|
|
|
public function isProfileAssignedToAnotherUser($curentUserId, $identifier, $provider) |
|
102
|
|
|
{ |
|
103
|
|
|
$qb = $this->createQueryBuilder(null); |
|
|
|
|
|
|
104
|
|
|
$qb->field('_id')->notEqual($curentUserId) |
|
105
|
|
|
->addAnd( |
|
106
|
|
|
$qb->expr() |
|
107
|
|
|
->addOr($qb->expr()->field('profiles.' . $provider . '.auth.identifier' )->equals($identifier)) |
|
108
|
|
|
->addOr($qb->expr()->field('profile.identifier')->equals($identifier)) |
|
109
|
|
|
); |
|
110
|
|
|
|
|
111
|
|
|
return $qb->count() |
|
112
|
|
|
->getQuery() |
|
113
|
|
|
->execute() > 0; |
|
114
|
|
|
} |
|
115
|
|
|
|
|
116
|
|
|
/** |
|
117
|
|
|
* Finds user by login name |
|
118
|
|
|
* |
|
119
|
|
|
* @param string $login |
|
120
|
|
|
* @return UserInterface |
|
|
|
|
|
|
121
|
|
|
*/ |
|
122
|
|
|
public function findByLogin($login) |
|
123
|
|
|
{ |
|
124
|
|
|
$entity = $this->findOneBy(array('login' => $login)); |
|
125
|
|
|
return $entity; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
/** |
|
129
|
|
|
* @param $email |
|
130
|
|
|
* @param bool $isDraft |
|
131
|
|
|
* |
|
132
|
|
|
* @return UserInterface|null |
|
|
|
|
|
|
133
|
|
|
*/ |
|
134
|
|
|
public function findByEmail($email, $isDraft = false) |
|
135
|
|
|
{ |
|
136
|
|
|
$entity = $this->findOneBy( |
|
137
|
|
|
array( |
|
138
|
|
|
'$or' => array( |
|
139
|
|
|
array('email' => $email), |
|
140
|
|
|
array('info.email' => $email), |
|
141
|
|
|
), |
|
142
|
|
|
'isDraft' => $isDraft, |
|
143
|
|
|
) |
|
144
|
|
|
); |
|
145
|
|
|
|
|
146
|
|
|
return $entity; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* Finds user by login name or email |
|
151
|
|
|
* |
|
152
|
|
|
* @param string $identity |
|
153
|
|
|
* @param string $suffix |
|
154
|
|
|
* |
|
155
|
|
|
* @return UserInterface|null |
|
|
|
|
|
|
156
|
|
|
*/ |
|
157
|
|
|
public function findByLoginOrEmail($identity, $suffix = '') |
|
158
|
|
|
{ |
|
159
|
|
|
return $this->findOneBy( |
|
160
|
|
|
array( |
|
161
|
|
|
'$or' => array( |
|
162
|
|
|
array('login' => $identity . $suffix), |
|
163
|
|
|
array('info.email' => $identity) |
|
164
|
|
|
) |
|
165
|
|
|
) |
|
166
|
|
|
); |
|
167
|
|
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* Find an user by a token hash. |
|
171
|
|
|
* |
|
172
|
|
|
* @param string $tokenHash |
|
173
|
|
|
* |
|
174
|
|
|
* @return UserInterface|null |
|
|
|
|
|
|
175
|
|
|
*/ |
|
176
|
|
|
public function findByToken($tokenHash) |
|
177
|
|
|
{ |
|
178
|
|
|
$criteria = array( |
|
179
|
|
|
'isDraft' => null, |
|
180
|
|
|
'tokens.hash' => $tokenHash |
|
181
|
|
|
); |
|
182
|
|
|
|
|
183
|
|
|
return $this->findOneBy($criteria); |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Finds user by internal id |
|
188
|
|
|
* |
|
189
|
|
|
* @param array $ids |
|
190
|
|
|
* @return \MongoCursor |
|
|
|
|
|
|
191
|
|
|
*/ |
|
192
|
|
|
public function findByIds(array $ids) |
|
193
|
|
|
{ |
|
194
|
|
|
return $this->findBy( |
|
195
|
|
|
array( |
|
196
|
|
|
'_id' => array('$in' => $ids) |
|
197
|
|
|
) |
|
198
|
|
|
); |
|
199
|
|
|
} |
|
200
|
|
|
|
|
201
|
|
|
/** |
|
202
|
|
|
* Find user by query |
|
203
|
|
|
* |
|
204
|
|
|
* @param String $query |
|
205
|
|
|
* @deprecated since 0.19 not used anymore and probably broken. |
|
206
|
|
|
* @return object |
|
207
|
|
|
*/ |
|
208
|
|
|
public function findByQuery($query) |
|
209
|
|
|
{ |
|
210
|
|
|
$qb = $this->createQueryBuilder(); |
|
211
|
|
|
$parts = explode(' ', trim($query)); |
|
212
|
|
|
|
|
213
|
|
|
foreach ($parts as $q) { |
|
214
|
|
|
$regex = new \MongoRegex('/^' . $query . '/i'); |
|
215
|
|
|
$qb->addOr($qb->expr()->field('info.firstName')->equals($regex)); |
|
216
|
|
|
$qb->addOr($qb->expr()->field('info.lastName')->equals($regex)); |
|
217
|
|
|
$qb->addOr($qb->expr()->field('info.email')->equals($regex)); |
|
218
|
|
|
} |
|
219
|
|
|
$qb->sort(array('info.lastName' => 1)) |
|
220
|
|
|
->sort(array('info.email' => 1)); |
|
221
|
|
|
|
|
222
|
|
|
return $qb->getQuery()->execute(); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Copy user info into the applications info Entity |
|
227
|
|
|
* |
|
228
|
|
|
* @param \Auth\Entity\Info $info |
|
229
|
|
|
*/ |
|
230
|
|
|
public function copyUserInfo(Info $info) |
|
231
|
|
|
{ |
|
232
|
|
|
$contact = new Info(); |
|
233
|
|
|
$contact->fromArray(Info::toArray($info)); |
|
|
|
|
|
|
234
|
|
|
} |
|
235
|
|
|
} |
|
236
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.