Completed
Push — EZP-31044-site-access-provider ( 6f7695...16abcc )
by
unknown
14:57
created

User::isAccountNonExpired()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the User class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\MVC\Symfony\Security;
10
11
use eZ\Publish\API\Repository\Values\User\User as APIUser;
12
use eZ\Publish\Core\Repository\Values\User\UserReference;
13
use Symfony\Component\Security\Core\User\UserInterface as BaseUserInterface;
14
use Symfony\Component\Security\Core\User\EquatableInterface;
15
16
class User implements ReferenceUserInterface, EquatableInterface
17
{
18
    /** @var \eZ\Publish\API\Repository\Values\User\User */
19
    private $user;
20
21
    /** @var \eZ\Publish\API\Repository\Values\User\UserReference */
22
    private $reference;
23
24
    /** @var array */
25
    private $roles;
26
27
    public function __construct(APIUser $user = null, array $roles = [])
28
    {
29
        $this->user = $user;
30
        $this->reference = new UserReference($user ? $user->getUserId() : null);
31
        $this->roles = $roles;
32
    }
33
34
    /**
35
     * Returns the roles granted to the user.
36
     *
37
     * <code>
38
     * public function getRoles()
39
     * {
40
     *     return array( 'ROLE_USER' );
41
     * }
42
     * </code>
43
     *
44
     * Alternatively, the roles might be stored on a ``roles`` property,
45
     * and populated in any number of different ways when the user object
46
     * is created.
47
     *
48
     * @return Role[] The user roles
49
     */
50
    public function getRoles()
51
    {
52
        return $this->roles;
53
    }
54
55
    /**
56
     * Returns the password used to authenticate the user.
57
     *
58
     * This should be the encoded password. On authentication, a plain-text
59
     * password will be salted, encoded, and then compared to this value.
60
     *
61
     * @return string The password
62
     */
63
    public function getPassword()
64
    {
65
        return $this->getAPIUser()->passwordHash;
66
    }
67
68
    /**
69
     * Returns the salt that was originally used to encode the password.
70
     *
71
     * This can return null if the password was not encoded using a salt.
72
     *
73
     * @return string The salt
74
     */
75
    public function getSalt()
76
    {
77
        return null;
78
    }
79
80
    /**
81
     * Returns the username used to authenticate the user.
82
     *
83
     * @return string The username
84
     */
85
    public function getUsername()
86
    {
87
        return $this->getAPIUser()->login;
88
    }
89
90
    /**
91
     * Removes sensitive data from the user.
92
     *
93
     * This is important if, at any given point, sensitive information like
94
     * the plain-text password is stored on this object.
95
     */
96
    public function eraseCredentials()
97
    {
98
    }
99
100
    /**
101
     * @return \eZ\Publish\API\Repository\Values\User\UserReference
102
     */
103
    public function getAPIUserReference()
104
    {
105
        return $this->reference;
106
    }
107
108
    /**
109
     * @return \eZ\Publish\API\Repository\Values\User\User
110
     */
111
    public function getAPIUser()
112
    {
113
        if (!$this->user instanceof APIUser) {
114
            throw new \LogicException(
115
                'Attempts to get APIUser before it has been set by UserProvider, APIUser is not serialized to session'
116
            );
117
        }
118
119
        return $this->user;
120
    }
121
122
    /**
123
     * @param \eZ\Publish\API\Repository\Values\User\User $user
124
     */
125
    public function setAPIUser(APIUser $user)
126
    {
127
        $this->user = $user;
128
        $this->reference = new UserReference($user->getUserId());
129
    }
130
131
    public function isEqualTo(BaseUserInterface $user)
132
    {
133
        // Check for the lighter ReferenceUserInterface first
134
        if ($user instanceof ReferenceUserInterface) {
135
            return $user->getAPIUserReference()->getUserId() === $this->reference->getUserId();
136
        } elseif ($user instanceof UserInterface) {
137
            return $user->getAPIUser()->getUserId() === $this->reference->getUserId();
138
        }
139
140
        return false;
141
    }
142
143
    public function __toString()
144
    {
145
        return $this->getAPIUser()->contentInfo->name;
146
    }
147
148
    /**
149
     * Make sure we don't serialize the whole API user object given it's a full fledged api content object. We set
150
     * (& either way refresh) the user object in \eZ\Publish\Core\MVC\Symfony\Security\User\Provider->refreshUser()
151
     * when object wakes back up from session.
152
     *
153
     * @return array
154
     */
155
    public function __sleep()
156
    {
157
        return ['reference', 'roles'];
158
    }
159
}
160