Completed
Push — master ( d998d0...abb41b )
by Kamil
38:34
created

User::getOAuthAccounts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Sylius package.
5
 *
6
 * (c) Paweł Jędrzejewski
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * This component was inspired by FOS User-Bundle
12
 */
13
14
namespace Sylius\Component\User\Model;
15
16
use Doctrine\Common\Collections\ArrayCollection;
17
use Doctrine\Common\Collections\Collection;
18
use Sylius\Component\Resource\Model\SoftDeletableTrait;
19
use Sylius\Component\Resource\Model\TimestampableTrait;
20
use Sylius\Component\Resource\Model\ToggleableTrait;
21
22
/**
23
 * @author Paweł Jędrzejewski <[email protected]>
24
 * @author Łukasz Chruściel <[email protected]>
25
 * @author Michał Marcinkowski <[email protected]>
26
 */
27
class User implements UserInterface
28
{
29
    use SoftDeletableTrait, TimestampableTrait, ToggleableTrait;
30
31
    /**
32
     * @var mixed
33
     */
34
    protected $id;
35
36
    /**
37
     * @var CustomerInterface
38
     */
39
    protected $customer;
40
41
    /**
42
     * @var string
43
     */
44
    protected $username;
45
46
    /**
47
     * Normalized representation of a username.
48
     *
49
     * @var string
50
     */
51
    protected $usernameCanonical;
52
53
    /**
54
     * Random data that is used as an additional input to a function that hashes a password.
55
     *
56
     * @var string
57
     */
58
    protected $salt;
59
60
    /**
61
     * Encrypted password. Must be persisted.
62
     *
63
     * @var string
64
     */
65
    protected $password;
66
67
    /**
68
     * Password before encryption. Used for model validation. Must not be persisted.
69
     *
70
     * @var string
71
     */
72
    protected $plainPassword;
73
74
    /**
75
     * @var \DateTime
76
     */
77
    protected $lastLogin;
78
79
    /**
80
     * Random string sent to the user email address in order to verify it
81
     *
82
     * @var string
83
     */
84
    protected $confirmationToken;
85
86
    /**
87
     * @var \DateTime
88
     */
89
    protected $passwordRequestedAt;
90
91
    /**
92
     * @var boolean
93
     */
94
    protected $locked = false;
95
96
    /**
97
     * @var \DateTime
98
     */
99
    protected $expiresAt;
100
101
    /**
102
     * @var \DateTime
103
     */
104
    protected $credentialsExpireAt;
105
106
    /**
107
     * We need at least one role to be able to authenticate
108
     *
109
     * @var array
110
     */
111
    protected $roles = array(UserInterface::DEFAULT_ROLE);
112
113
    /**
114
     * @var Collection|UserOAuth[]
115
     */
116
    protected $oauthAccounts;
117
118
    public function __construct()
119
    {
120
        $this->salt = base_convert(sha1(uniqid(mt_rand(), true)), 16, 36);
121
        $this->oauthAccounts = new ArrayCollection();
122
        $this->createdAt = new \DateTime();
123
124
        // Set here to overwrite default value from trait
125
        $this->enabled = false;
126
    }
127
128
    /**
129
     * {@inheritdoc}
130
     */
131
    public function getId()
132
    {
133
        return $this->id;
134
    }
135
136
    /**
137
     * {@inheritdoc}
138
     */
139
    public function getCustomer()
140
    {
141
        return $this->customer;
142
    }
143
144
    /**
145
     * {@inheritdoc}
146
     */
147
    public function setCustomer(CustomerInterface $customer = null)
148
    {
149
        if ($this->customer !== $customer) {
150
            $this->customer = $customer;
151
            $this->assignUser($customer);
152
        }
153
    }
154
155
    /**
156
     * {@inheritdoc}
157
     */
158
    public function getUsername()
159
    {
160
        return $this->username;
161
    }
162
163
    /**
164
     * {@inheritdoc}
165
     */
166
    public function setUsername($username)
167
    {
168
        $this->username = $username;
169
    }
170
171
    /**
172
     * {@inheritdoc}
173
     */
174
    public function getUsernameCanonical()
175
    {
176
        return $this->usernameCanonical;
177
    }
178
179
    /**
180
     * {@inheritdoc}
181
     */
182
    public function setUsernameCanonical($usernameCanonical)
183
    {
184
        $this->usernameCanonical = $usernameCanonical;
185
    }
186
187
    /**
188
     * {@inheritdoc}
189
     */
190
    public function getSalt()
191
    {
192
        return $this->salt;
193
    }
194
195
    /**
196
     * {@inheritdoc}
197
     */
198
    public function getPlainPassword()
199
    {
200
        return $this->plainPassword;
201
    }
202
203
    /**
204
     * {@inheritdoc}
205
     */
206
    public function setPlainPassword($password)
207
    {
208
        $this->plainPassword = $password;
209
    }
210
211
    /**
212
     * {@inheritdoc}
213
     */
214
    public function getPassword()
215
    {
216
        return $this->password;
217
    }
218
219
    /**
220
     * {@inheritdoc}
221
     */
222
    public function setPassword($password)
223
    {
224
        $this->password = $password;
225
    }
226
227
    /**
228
     * {@inheritdoc}
229
     */
230
    public function getExpiresAt()
231
    {
232
        return $this->expiresAt;
233
    }
234
235
    /**
236
     * @param \DateTime $date
237
     */
238
    public function setExpiresAt(\DateTime $date = null)
239
    {
240
        $this->expiresAt = $date;
241
    }
242
243
    /**
244
     * {@inheritdoc}
245
     */
246
    public function getCredentialsExpireAt()
247
    {
248
        return $this->credentialsExpireAt;
249
    }
250
251
    /**
252
     * {@inheritdoc}
253
     */
254
    public function setCredentialsExpireAt(\DateTime $date = null)
255
    {
256
        $this->credentialsExpireAt = $date;
257
    }
258
259
    /**
260
     * {@inheritdoc}
261
     */
262
    public function getLastLogin()
263
    {
264
        return $this->lastLogin;
265
    }
266
267
    /**
268
     * {@inheritdoc}
269
     */
270
    public function setLastLogin(\DateTime $time = null)
271
    {
272
        $this->lastLogin = $time;
273
    }
274
275
    /**
276
     * {@inheritdoc}
277
     */
278
    public function getConfirmationToken()
279
    {
280
        return $this->confirmationToken;
281
    }
282
283
    /**
284
     * {@inheritdoc}
285
     */
286
    public function setConfirmationToken($confirmationToken)
287
    {
288
        $this->confirmationToken = $confirmationToken;
289
    }
290
291
    /**
292
     * {@inheritdoc}
293
     */
294
    public function isCredentialsNonExpired()
295
    {
296
        return !$this->hasExpired($this->credentialsExpireAt);
297
    }
298
299
    /**
300
     * {@inheritdoc}
301
     */
302
    public function isAccountNonExpired()
303
    {
304
        return !$this->hasExpired($this->expiresAt);
305
    }
306
307
    /**
308
     * {@inheritdoc}
309
     */
310
    public function setLocked($locked)
311
    {
312
        $this->locked = $locked;
313
    }
314
315
    /**
316
     * {@inheritdoc}
317
     */
318
    public function isAccountNonLocked()
319
    {
320
        return !$this->locked;
321
    }
322
323
    /**
324
     * {@inheritdoc}
325
     */
326
    public function hasRole($role)
327
    {
328
        return in_array(strtoupper($role), $this->getRoles(), true);
329
    }
330
331
    /**
332
     * {@inheritdoc}
333
     */
334
    public function addRole($role)
335
    {
336
        $role = strtoupper($role);
337
        if (!in_array($role, $this->roles, true)) {
338
            $this->roles[] = $role;
339
        }
340
    }
341
342
    /**
343
     * {@inheritdoc}
344
     */
345
    public function removeRole($role)
346
    {
347
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
348
            unset($this->roles[$key]);
349
            $this->roles = array_values($this->roles);
350
        }
351
    }
352
353
    /**
354
     * {@inheritdoc}
355
     */
356
    public function getRoles()
357
    {
358
        return $this->roles;
359
    }
360
361
    /**
362
     * {@inheritdoc}
363
     */
364
    public function setRoles(array $roles)
365
    {
366
        $this->roles = array();
367
368
        foreach ($roles as $role) {
369
            $this->addRole($role);
370
        }
371
    }
372
373
    /**
374
     * {@inheritdoc}
375
     */
376
    public function getEmail()
377
    {
378
        return $this->customer->getEmail();
379
    }
380
381
    /**
382
     * {@inheritdoc}
383
     */
384
    public function setEmail($email)
385
    {
386
        $this->customer->setEmail($email);
387
    }
388
389
    /**
390
     * {@inheritdoc}
391
     */
392
    public function getEmailCanonical()
393
    {
394
        return $this->customer->getEmailCanonical();
395
    }
396
397
    /**
398
     * {@inheritdoc}
399
     */
400
    public function setEmailCanonical($emailCanonical)
401
    {
402
        $this->customer->setEmailCanonical($emailCanonical);
403
    }
404
405
    /**
406
     * {@inheritdoc}
407
     */
408
    public function isPasswordRequestNonExpired(\DateInterval $ttl)
409
    {
410
        return null !== $this->passwordRequestedAt && new \DateTime() <= $this->passwordRequestedAt->add($ttl);
411
    }
412
413
    /**
414
     * Gets the timestamp that the user requested a password reset.
415
     *
416
     * @return null|\DateTime
417
     */
418
    public function getPasswordRequestedAt()
419
    {
420
        return $this->passwordRequestedAt;
421
    }
422
423
    /**
424
     * {@inheritdoc}
425
     */
426
    public function setPasswordRequestedAt(\DateTime $date = null)
427
    {
428
        $this->passwordRequestedAt = $date;
429
    }
430
431
    /**
432
     * {@inheritdoc}
433
     */
434
    public function eraseCredentials()
435
    {
436
        $this->plainPassword = null;
437
    }
438
439
    /**
440
     * {@inheritdoc}
441
     */
442
    public function getOAuthAccounts()
443
    {
444
        return $this->oauthAccounts;
445
    }
446
447
    /**
448
     * {@inheritdoc}
449
     */
450
    public function getOAuthAccount($provider)
451
    {
452
        if ($this->oauthAccounts->isEmpty()) {
453
            return null;
454
        }
455
456
        $filtered = $this->oauthAccounts->filter(function (UserOAuthInterface $oauth) use ($provider) {
457
            return $provider === $oauth->getProvider();
458
        });
459
460
        if ($filtered->isEmpty()) {
461
            return null;
462
        }
463
464
        return $filtered->current();
465
    }
466
467
    /**
468
     * {@inheritdoc}
469
     */
470
    public function addOAuthAccount(UserOAuthInterface $oauth)
471
    {
472
        if (!$this->oauthAccounts->contains($oauth)) {
473
            $this->oauthAccounts->add($oauth);
474
            $oauth->setUser($this);
475
        }
476
    }
477
478
    /**
479
     * Returns username.
480
     *
481
     * @return string
482
     */
483
    public function __toString()
484
    {
485
        return (string) $this->getUsername();
486
    }
487
488
    /**
489
     * The serialized data have to contain the fields used by the equals method and the username.
490
     *
491
     * @return string
492
     */
493
    public function serialize()
494
    {
495
        return serialize(array(
496
            $this->password,
497
            $this->salt,
498
            $this->usernameCanonical,
499
            $this->username,
500
            $this->locked,
501
            $this->enabled,
502
            $this->id,
503
        ));
504
    }
505
506
    /**
507
     * @param string $serialized
508
     */
509
    public function unserialize($serialized)
510
    {
511
        $data = unserialize($serialized);
512
        // add a few extra elements in the array to ensure that we have enough keys when unserializing
513
        // older data which does not include all properties.
514
        $data = array_merge($data, array_fill(0, 2, null));
515
516
        list(
517
            $this->password,
518
            $this->salt,
519
            $this->usernameCanonical,
520
            $this->username,
521
            $this->locked,
522
            $this->enabled,
523
            $this->id
524
        ) = $data;
525
    }
526
527
    /**
528
     * @param CustomerInterface $customer
529
     */
530
    protected function assignUser(CustomerInterface $customer = null)
531
    {
532
        if (null !== $customer) {
533
            $customer->setUser($this);
534
        }
535
    }
536
537
    /**
538
     * @param \DateTime $date
539
     * @return bool
540
     */
541
    protected function hasExpired(\DateTime $date = null)
542
    {
543
        return null !== $date && new \DateTime() >= $date;
544
    }
545
}
546