1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace DoS\UserBundle\Model; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Sylius\Component\Rbac\Model\Role; |
7
|
|
|
use Sylius\Component\User\Model\User as BaseUser; |
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
9
|
|
|
use Sylius\Component\Media\Model\ImageInterface; |
10
|
|
|
|
11
|
|
|
class User extends BaseUser implements UserInterface |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var string |
15
|
|
|
*/ |
16
|
|
|
protected $locale; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* @var string |
20
|
|
|
*/ |
21
|
|
|
protected $displayname; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $confirmationType; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var \DateTime |
30
|
|
|
*/ |
31
|
|
|
protected $confirmedAt; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @var ArrayCollection|Role[] |
35
|
|
|
*/ |
36
|
|
|
protected $authorizationRoles; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var ImageInterface |
40
|
|
|
*/ |
41
|
|
|
protected $picture; |
42
|
|
|
|
43
|
|
|
public function __construct() |
44
|
|
|
{ |
45
|
|
|
parent::__construct(); |
46
|
|
|
|
47
|
|
|
$this->authorizationRoles = new ArrayCollection(); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function getAuthorizationRoles() |
54
|
|
|
{ |
55
|
|
|
return $this->authorizationRoles; |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* {@inheritdoc} |
60
|
|
|
*/ |
61
|
|
|
public function resizeSecurityRoles() |
62
|
|
|
{ |
63
|
|
|
$this->roles = array(self::DEFAULT_ROLE); |
64
|
|
|
|
65
|
|
|
foreach($this->authorizationRoles as $role) { |
66
|
|
|
foreach($role->getSecurityRoles() as $r) { |
67
|
|
|
$this->roles[] = $r; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* @return bool |
74
|
|
|
*/ |
75
|
|
|
public function isLocked() |
76
|
|
|
{ |
77
|
|
|
return $this->locked; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return string |
82
|
|
|
*/ |
83
|
|
|
public function getDisplayName() |
84
|
|
|
{ |
85
|
|
|
$customer = $this->getCustomer(); |
86
|
|
|
|
87
|
|
|
return $this->displayname ?: |
88
|
|
|
( |
89
|
|
|
$customer && trim($customer->getFullName()) |
90
|
|
|
? $customer->getFullName() |
91
|
|
|
: $this->username |
92
|
|
|
) |
93
|
|
|
; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* @param null|string $displayname |
98
|
|
|
*/ |
99
|
|
|
public function setDisplayName($displayname = null) |
100
|
|
|
{ |
101
|
|
|
$this->displayname = $displayname; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* @return string |
106
|
|
|
*/ |
107
|
|
|
public function getLocale() |
108
|
|
|
{ |
109
|
|
|
return $this->locale; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* @param string $locale |
114
|
|
|
*/ |
115
|
|
|
public function setLocale($locale) |
116
|
|
|
{ |
117
|
|
|
$this->locale = $locale; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @return string|void |
122
|
|
|
*/ |
123
|
|
|
public function getLang() |
124
|
|
|
{ |
125
|
|
|
if ($this->locale) { |
126
|
|
|
if (preg_match('/_([a-z]{2})/i', $this->locale, $match)) { |
127
|
|
|
return strtolower($match[1]); |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
return; |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* @inheritdoc |
136
|
|
|
*/ |
137
|
|
|
public function getMediaPath() |
138
|
|
|
{ |
139
|
|
|
return '/user/' . $this->usernameCanonical; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* {@inheritdoc} |
144
|
|
|
*/ |
145
|
|
|
public function setPicture(ImageInterface $picture = null) |
146
|
|
|
{ |
147
|
|
|
$this->picture = $picture; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* {@inheritdoc} |
152
|
|
|
*/ |
153
|
|
|
public function getPicture() |
154
|
|
|
{ |
155
|
|
|
return $this->picture; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* {@inheritdoc} |
160
|
|
|
*/ |
161
|
|
|
public function getProfilePicture() |
162
|
|
|
{ |
163
|
|
|
if ($this->picture) { |
164
|
|
|
return $this->picture->getMediaId(); |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
foreach ($this->oauthAccounts as $account) { |
168
|
|
|
if ($avatar = $account->getProfilePicture()) { |
169
|
|
|
return $avatar; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return; |
174
|
|
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* @inheritdoc |
178
|
|
|
*/ |
179
|
|
|
public function getConfirmedAt() |
180
|
|
|
{ |
181
|
|
|
return $this->confirmedAt; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* @inheritdoc |
186
|
|
|
*/ |
187
|
|
|
public function setConfirmedAt(\DateTime $confirmedAt = null) |
188
|
|
|
{ |
189
|
|
|
$this->confirmedAt = $confirmedAt; |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* @inheritdoc |
194
|
|
|
*/ |
195
|
|
|
public function confirmed(\DateTime $confirmedAt = null) |
196
|
|
|
{ |
197
|
|
|
$this->setConfirmedAt($confirmedAt ?: new \DateTime()); |
198
|
|
|
$this->setEnabled(true); |
199
|
|
|
$this->setConfirmationToken(null); |
200
|
|
|
$this->setPasswordRequestedAt(null); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @inheritdoc |
205
|
|
|
*/ |
206
|
|
|
public function isConfirmed() |
207
|
|
|
{ |
208
|
|
|
return $this->confirmedAt || $this->enabled; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* {@inheritdoc} |
213
|
|
|
*/ |
214
|
|
|
public function setEnabled($boolean) |
215
|
|
|
{ |
216
|
|
|
$this->enabled = (Boolean)$boolean; |
217
|
|
|
|
218
|
|
|
if (!$this->isConfirmed()) { |
219
|
|
|
$this->enabled = false; |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
return $this; |
223
|
|
|
} |
224
|
|
|
|
225
|
|
|
/** |
226
|
|
|
* {@inheritdoc} |
227
|
|
|
*/ |
228
|
|
|
public function getConfirmationChannel($propertyPath) |
229
|
|
|
{ |
230
|
|
|
$accessor = PropertyAccess::createPropertyAccessor(); |
231
|
|
|
|
232
|
|
|
return $accessor->getValue($this, $propertyPath); |
233
|
|
|
} |
234
|
|
|
|
235
|
|
|
/** |
236
|
|
|
* {@inheritdoc} |
237
|
|
|
*/ |
238
|
|
|
public function getConfirmationRequestedAt() |
239
|
|
|
{ |
240
|
|
|
return $this->getPasswordRequestedAt(); |
241
|
|
|
} |
242
|
|
|
|
243
|
|
|
/** |
244
|
|
|
* {@inheritdoc} |
245
|
|
|
*/ |
246
|
|
|
public function setConfirmationRequestedAt(\DateTime $dateTime = null) |
247
|
|
|
{ |
248
|
|
|
$this->setPasswordRequestedAt($dateTime); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* {@inheritdoc} |
253
|
|
|
*/ |
254
|
|
|
public function getConfirmationConfirmedAt() |
255
|
|
|
{ |
256
|
|
|
return $this->getConfirmedAt(); |
257
|
|
|
} |
258
|
|
|
|
259
|
|
|
/** |
260
|
|
|
* {@inheritdoc} |
261
|
|
|
*/ |
262
|
|
|
public function setConfirmationConfirmedAt(\DateTime $dateTime = null) |
263
|
|
|
{ |
264
|
|
|
$this->setConfirmedAt($dateTime); |
265
|
|
|
} |
266
|
|
|
|
267
|
|
|
/** |
268
|
|
|
* {@inheritdoc} |
269
|
|
|
*/ |
270
|
|
|
public function isConfirmationConfirmed() |
271
|
|
|
{ |
272
|
|
|
return $this->isConfirmed(); |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
/** |
276
|
|
|
* {@inheritdoc} |
277
|
|
|
*/ |
278
|
|
|
public function confirmationRequest($token) |
279
|
|
|
{ |
280
|
|
|
$this->setConfirmationToken($token); |
281
|
|
|
$this->setConfirmationRequestedAt(new \DateTime()); |
282
|
|
|
$this->setEnabled(false); |
283
|
|
|
} |
284
|
|
|
|
285
|
|
|
/** |
286
|
|
|
* {@inheritdoc} |
287
|
|
|
*/ |
288
|
|
|
public function confirmationConfirm() |
289
|
|
|
{ |
290
|
|
|
$this->confirmed(); |
291
|
|
|
} |
292
|
|
|
|
293
|
|
|
/** |
294
|
|
|
* {@inheritdoc} |
295
|
|
|
*/ |
296
|
|
|
public function getConfirmationType() |
297
|
|
|
{ |
298
|
|
|
return $this->confirmationType; |
299
|
|
|
} |
300
|
|
|
|
301
|
|
|
/** |
302
|
|
|
* {@inheritdoc} |
303
|
|
|
*/ |
304
|
|
|
public function setConfirmationType($confirmationType) |
305
|
|
|
{ |
306
|
|
|
$this->confirmationType = $confirmationType; |
307
|
|
|
} |
308
|
|
|
|
309
|
|
|
/** |
310
|
|
|
* {@inheritdoc} |
311
|
|
|
*/ |
312
|
|
|
public function confirmationDisableAccess() |
313
|
|
|
{ |
314
|
|
|
$this->enabled = false; |
315
|
|
|
} |
316
|
|
|
|
317
|
|
|
/** |
318
|
|
|
* {@inheritdoc} |
319
|
|
|
*/ |
320
|
|
|
public function confirmationEnableAccess() |
321
|
|
|
{ |
322
|
|
|
$this->enabled = true; |
323
|
|
|
} |
324
|
|
|
} |
325
|
|
|
|