Completed
Pull Request — master (#2737)
by Jeroen
10:25
created

BaseUser::getAdminLocale()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Entity;
4
5
use Doctrine\Common\Collections\ArrayCollection;
6
use Doctrine\ORM\Mapping as ORM;
7
use FOS\UserBundle\Model\GroupInterface;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Kunstmaan\AdminBundle\Entity\GroupInterface.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
8
use Kunstmaan\AdminBundle\Validator\Constraints\PasswordRestrictions;
9
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
10
use Symfony\Component\Validator\Constraints\Email;
11
use Symfony\Component\Validator\Constraints\NotBlank;
12
use Symfony\Component\Validator\Mapping\ClassMetadata;
13
use DateTime;
14
15
abstract class BaseUser implements UserInterface
16
{
17
    /**
18
     * @ORM\Id
19
     * @ORM\Column(type="integer")
20
     * @ORM\GeneratedValue(strategy="AUTO")
21
     */
22
    protected $id;
23
24
    /**
25
     * @var string
26
     *
27
     * @ORM\Column(type="string", length=180, unique=true)
28
     */
29
    protected $username;
30
31
    /**
32
     * Next Major: Remove attribute
33
     *
34
     * @var string
35
     *
36
     * @ORM\Column(type="string", length=180, unique=true)
37
     */
38
    protected $usernameCanonical;
39
40
    /**
41
     * The doctrine metadata is set dynamically in Kunstmaan\AdminBundle\EventListener\MappingListener
42
     */
43
    protected $groups;
44
45
    /**
46
     * @ORM\Column(type="string", name="admin_locale", length=5, nullable=true)
47
     */
48
    protected $adminLocale;
49
50
    /**
51
     * @ORM\Column(type="boolean", name="password_changed", nullable=true)
52
     */
53
    protected $passwordChanged;
54
55
    /**
56
     * @ORM\Column(name="google_id", type="string", length=255, nullable=true)
57
     */
58
    protected $googleId;
59
60
    /**
61
     * @var string
62
     *
63
     * @ORM\Column(type="string", length=180, unique=true)
64
     */
65
    protected $email;
66
67
    /**
68
     * Next Major: Remove attribute
69
     *
70
     * @var string
71
     *
72
     * @ORM\Column(type="string", length=180, unique=true)
73
     */
74
    protected $emailCanonical;
75
76
    /**
77
     * @var string
78
     *
79
     * @ORM\Column(name="password", type="string", length=100)
80
     */
81
    protected $password;
82
83
    /**
84
     * @var string|null
85
     */
86
    protected $plainPassword;
87
88
    /**
89
     * @var string|null
90
     *
91
     * @ORM\Column(type="string", length=255, nullable=true)
92
     */
93
    protected $confirmationToken;
94
95
    /**
96
     * @var string
97
     *
98
     * @ORM\Column(name="salt", type="string", length=100)
99
     */
100
    protected $salt;
101
102
    /**
103
     * @var \DateTime
104
     *
105
     * @ORM\Column(name="last_login", type="datetime", nullable=true)
106
     */
107
    protected $lastLogin;
108
109
    /**
110
     * @var array
111
     *
112
     * @ORM\Column(name="roles", type="array")
113
     */
114
    protected $roles;
115
116
    /**
117
     * @ORM\Column(name="enabled", type="boolean")
118
     */
119
    protected $enabled;
120
121
    /**
122
     * Construct a new user
123
     */
124 35
    public function __construct()
125
    {
126 35
        $this->groups = new ArrayCollection();
127 35
        $this->roles = [];
128 35
    }
129
130
    /**
131
     * Get id
132
     *
133
     * @return int
134
     */
135 3
    public function getId()
136
    {
137 3
        return $this->id;
138
    }
139
140
    /**
141
     * Set id
142
     *
143
     * @param int $id
144
     *
145
     * @return BaseUser
146
     */
147 8
    public function setId($id)
148
    {
149 8
        $this->id = $id;
150
151 8
        return $this;
152
    }
153
154
    /**
155
     * Gets the groupIds for the user.
156
     *
157
     * @return array
158
     */
159 1
    public function getGroupIds()
160
    {
161 1
        $groups = $this->groups;
162
163 1
        $groupIds = array();
164 1
        if (\count($groups) > 0) {
165
            /* @var $group GroupInterface */
166 1
            foreach ($groups as $group) {
167 1
                $groupIds[] = $group->getId();
168
            }
169
        }
170
171 1
        return $groupIds;
172
    }
173
174
    /**
175
     * Gets the groups the user belongs to.
176
     *
177
     * @return ArrayCollection
178
     */
179 5
    public function getGroups()
180
    {
181 5
        return $this->groups;
182
    }
183
184
    /**
185
     * Get adminLocale
186
     *
187
     * @return string
188
     */
189 1
    public function getAdminLocale()
190
    {
191 1
        return $this->adminLocale;
192
    }
193
194
    /**
195
     * {@inheritdoc}
196
     */
197 2
    public function setEnabled($boolean)
198
    {
199 2
        $this->enabled = (bool) $boolean;
200
201 2
        return $this;
202
    }
203
204
    /**
205
     * Set adminLocale
206
     *
207
     * @param string $adminLocale
208
     *
209
     * @return BaseUser
210
     */
211 2
    public function setAdminLocale($adminLocale)
212
    {
213 2
        $this->adminLocale = $adminLocale;
214
215 2
        return $this;
216
    }
217
218
    /**
219
     * is passwordChanged
220
     *
221
     * @return bool
222
     */
223 1
    public function isPasswordChanged()
224
    {
225 1
        return $this->passwordChanged;
226
    }
227
228 2
    public function setPasswordChanged($passwordChanged)
229
    {
230 2
        $this->passwordChanged = $passwordChanged;
231
232 2
        return $this;
233
    }
234
235
    /**
236
     * @return mixed
237
     */
238 1
    public function getGoogleId()
239
    {
240 1
        return $this->googleId;
241
    }
242
243
    /**
244
     * @param mixed $googleId
245
     */
246 3
    public function setGoogleId($googleId)
247
    {
248 3
        $this->googleId = $googleId;
249 3
    }
250
251
    /**
252
     * @param ClassMetadata $metadata
253
     */
254 1
    public static function loadValidatorMetadata(ClassMetadata $metadata)
255
    {
256 1
        $metadata->addPropertyConstraint('username', new NotBlank());
257 1
        $metadata->addPropertyConstraints(
258 1
            'plainPassword',
259
            array(
260 1
                new NotBlank(array('groups' => array('Registration'))),
261 1
                new PasswordRestrictions(array('groups' => array('Registration', 'Default'))),
262
            )
263
        );
264 1
        $metadata->addPropertyConstraint('email', new NotBlank());
265 1
        $metadata->addPropertyConstraint('email', new Email());
266 1
        $metadata->addConstraint(new UniqueEntity(array(
267 1
            'fields' => 'username',
268
            'message' => 'errors.user.loginexists',
269
        )));
270 1
        $metadata->addConstraint(new UniqueEntity(array(
271 1
            'fields' => 'email',
272
            'message' => 'errors.user.emailexists',
273
        )));
274 1
    }
275
276
    /**
277
     * Return class name of form type used to add & edit users
278
     *
279
     * @return string
280
     */
281
    abstract public function getFormTypeClass();
282
283
    /**
284
     * {@inheritdoc}
285
     */
286 1
    public function isAccountNonLocked()
287
    {
288 1
        return $this->isEnabled();
289
    }
290
291
    /**
292
     * @return string
293
     */
294
    public function getEmail(): string
295
    {
296
        return $this->email;
297
    }
298
299 1
    public function setEmail($email)
300
    {
301 1
        $this->email = $email;
302
303 1
        return $this;
304
    }
305
306
    /**
307
     * @return string
308
     */
309
    public function getPassword(): string
310
    {
311
        return $this->password;
312
    }
313
314
    public function setPassword($password)
315
    {
316
        $this->password = $password;
317
318
        return $this;
319
    }
320
321
    public function getPlainPassword(): ?string
322
    {
323
        return $this->plainPassword;
324
    }
325
326 1
    public function setPlainPassword($plainPassword)
327
    {
328 1
        $this->plainPassword = $plainPassword;
329
330 1
        return $this;
331
    }
332
333
    /**
334
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use string[].

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
335
     */
336 1
    public function getRoles()
337
    {
338 1
        $roles = $this->roles;
339
340 1
        foreach ($this->getGroups() as $group) {
341
            $roles = array_merge($roles, $group->getRoles());
342
        }
343
344
        // we need to make sure to have at least one role
345 1
        $roles[] = static::ROLE_DEFAULT;
346
347 1
        return array_unique($roles);
348
    }
349
350 1
    public function hasRole($role)
351
    {
352 1
        return in_array(strtoupper($role), $this->getRoles(), true);
353
    }
354
355
    /**
356
     * {@inheritdoc}
357
     */
358
    public function setRoles(array $roles)
359
    {
360
        $this->roles = array();
361
362
        foreach ($roles as $role) {
363
            $this->addRole($role);
364
        }
365
366
        return $this;
367
    }
368
369 1
    public function removeRole($role)
370
    {
371 1
        if (false !== $key = array_search(strtoupper($role), $this->roles, true)) {
372 1
            unset($this->roles[$key]);
373 1
            $this->roles = array_values($this->roles);
374
        }
375
376 1
        return $this;
377
    }
378
379
    public function getSalt(): ?string
380
    {
381
        return $this->salt;
382
    }
383
384
    public function setSalt($salt)
385
    {
386
        $this->salt = $salt;
387
388
        return $this;
389
    }
390
391 1
    public function isEnabled()
392
    {
393 1
        return $this->enabled;
394
    }
395
396
    /**
397
     * @return string The username
398
     */
399 7
    public function getUsername(): string
400
    {
401 7
        return $this->username;
402
    }
403
404 8
    public function setUsername($username)
405
    {
406 8
        $this->username = $username;
407
408 8
        return $this;
409
    }
410
411
    /**
412
     * Removes sensitive data from the user.
413
     */
414
    public function eraseCredentials(): void
415
    {
416
        $this->plainPassword = null;
417
    }
418
419
    /**
420
     * {@inheritdoc}
421
     */
422 1
    public function addRole($role)
423
    {
424 1
        $role = strtoupper($role);
425 1
        if ($role === static::ROLE_DEFAULT) {
426
            return $this;
427
        }
428
429 1
        if (!in_array($role, $this->roles, true)) {
430 1
            $this->roles[] = $role;
431
        }
432
433 1
        return $this;
434
    }
435
436
    /**
437
     * {@inheritdoc}
438
     */
439
    public function getGroupNames()
440
    {
441
        $names = array();
442
        foreach ($this->getGroups() as $group) {
443
            $names[] = $group->getName();
444
        }
445
446
        return $names;
447
    }
448
449
    /**
450
     * {@inheritdoc}
451
     */
452
    public function hasGroup($name)
453
    {
454
        return in_array($name, $this->getGroupNames());
455
    }
456
457
    /**
458
     * {@inheritdoc}
459
     */
460 4
    public function addGroup(GroupInterface $group)
461
    {
462 4
        if (!$this->getGroups()->contains($group)) {
463 4
            $this->getGroups()->add($group);
464
        }
465
466 4
        return $this;
467
    }
468
469
    /**
470
     * {@inheritdoc}
471
     */
472
    public function removeGroup(GroupInterface $group)
473
    {
474
        if ($this->getGroups()->contains($group)) {
475
            $this->getGroups()->removeElement($group);
476
        }
477
478
        return $this;
479
    }
480
481
    public function __toString()
482
    {
483
        return (string) $this->getUsername();
484
    }
485
486
    public function getUsernameCanonical()
487
    {
488
        // NEXT_MAJOR remove method
489
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
490
491
        return $this->usernameCanonical;
492
    }
493
494
    public function setUsernameCanonical($usernameCanonical)
495
    {
496
        // NEXT_MAJOR remove method
497
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
498
499
        $this->usernameCanonical = $usernameCanonical;
500
    }
501
502
    public function getEmailCanonical()
503
    {
504
        // NEXT_MAJOR remove method
505
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
506
507
        return $this->emailCanonical;
508
    }
509
510
    public function setEmailCanonical($emailCanonical)
511
    {
512
        // NEXT_MAJOR remove method
513
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
514
515
        $this->emailCanonical = $emailCanonical;
516
    }
517
518 1
    public function isSuperAdmin()
519
    {
520
        // NEXT_MAJOR remove method
521 1
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
522 1
    }
523
524
    public function setSuperAdmin($boolean)
525
    {
526
        // NEXT_MAJOR remove method
527
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
528
    }
529
530
    public function getConfirmationToken()
531
    {
532
        return $this->confirmationToken;
533
    }
534
535
    public function setConfirmationToken($confirmationToken)
536
    {
537
        $this->confirmationToken = $confirmationToken;
538
    }
539
540
    public function setPasswordRequestedAt(DateTime $date = null)
541
    {
542
        // NEXT_MAJOR remove method
543
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
544
    }
545
546
    public function getLastLogin()
547
    {
548
        return $this->lastLogin;
549
    }
550
551
    public function setLastLogin(?DateTime $lastLogin = null)
552
    {
553
        $this->lastLogin = $lastLogin;
554
555
        return $this;
556
    }
557
558
    public function isAccountNonExpired()
559
    {
560
        // NEXT_MAJOR remove method
561
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
562
563
        return true;
564
    }
565
566
    public function isCredentialsNonExpired()
567
    {
568
        // NEXT_MAJOR remove method
569
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
570
571
        return true;
572
    }
573
574
    public function isPasswordRequestNonExpired($ttl)
575
    {
576
        // NEXT_MAJOR remove method
577
        @trigger_error(sprintf('Using method %s from class %s is deprecated since KunstmaanAdminBundle 5.8 and will be removed in KunstmaanAdminBundle 6.0.', __METHOD__, BaseUser::class), E_USER_DEPRECATED);
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition here. This can introduce security issues, and is generally not recommended.

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
578
579
        return false;
580
    }
581
582
    /**
583
     * {@inheritdoc}
584
     */
585
    public function serialize()
586
    {
587
        return serialize(array(
588
            $this->password,
589
            $this->salt,
590
            $this->username,
591
            $this->enabled,
592
            $this->id,
593
            $this->email,
594
        ));
595
    }
596
597
    /**
598
     * {@inheritdoc}
599
     */
600
    public function unserialize($serialized)
601
    {
602
        $data = unserialize($serialized);
603
604
        [
605
            $this->password,
606
            $this->salt,
607
            $this->username,
608
            $this->enabled,
609
            $this->id,
610
            $this->email,
611
            ] = $data;
612
    }
613
}
614