Completed
Push — master ( d4e81f...550fff )
by Chad
05:10
created

LdapUser::setLdapGuid()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
/**
3
 * This file is part of the LdapToolsBundle package.
4
 *
5
 * (c) Chad Sikorra <[email protected]>
6
 *
7
 * For the full copyright and license information, please view the LICENSE
8
 * file that was distributed with this source code.
9
 */
10
11
namespace LdapTools\Bundle\LdapToolsBundle\Security\User;
12
13
use LdapTools\Object\LdapObject;
14
use Symfony\Component\Security\Core\User\AdvancedUserInterface;
15
16
/**
17
 * Represents a user from LDAP.
18
 *
19
 * @author Chad Sikorra <[email protected]>
20
 */
21
class LdapUser extends LdapObject implements LdapUserInterface, AdvancedUserInterface, \Serializable
22
{
23
    /**
24
     * @var array The Symfony roles for this user.
25
     */
26
    protected $roles = [];
27
28
    public function __construct()
29
    {
30
        parent::__construct([]);
31
    }
32
33
    /**
34
     * {@inheritdoc}
35
     */
36
    public function getSalt()
37
    {
38
        return null;
39
    }
40
41
    /**
42
     * {@inheritdoc}
43
     */
44
    public function getPassword()
45
    {
46
        return null;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function eraseCredentials()
53
    {
54
        return null;
55
    }
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    public function getRoles()
61
    {
62
        return $this->roles;
63
    }
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    public function setRoles(array $roles)
69
    {
70
        $this->roles = [];
71
        foreach ($roles as $role) {
72
            $this->addRole($role);
73
        }
74
75
        return $this;
76
    }
77
78
    /**
79
     * {@inheritdoc}
80
     */
81 View Code Duplication
    public function addRole($role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
82
    {
83
        $role = strtoupper($role);
84
85
        if (!in_array($role, $this->roles)) {
86
            $this->roles[] = $role;
87
        }
88
89
        return $this;
90
    }
91
92
    /**
93
     * {@inheritdoc}
94
     */
95 View Code Duplication
    public function removeRole($role)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
96
    {
97
        $role = strtoupper($role);
98
99
        if (in_array($role, $this->roles)) {
100
            $this->roles = array_diff($this->roles, [$role]);
101
        }
102
103
        return $this;
104
    }
105
106
    /**
107
     * {@inheritdoc}
108
     */
109
    public function getUsername()
110
    {
111
        return $this->get('username');
112
    }
113
114
    /**
115
     * {@inheritdoc}
116
     */
117
    public function setUsername($username)
118
    {
119
        return $this->set('username', $username);
120
    }
121
122
    /**
123
     * {@inheritdoc}
124
     */
125
    public function isAccountNonExpired()
126
    {
127
        if (!$this->has('accountExpirationDate') || $this->get('accountExpirationDate') === false) {
128
            $result = true;
129
        } elseif ($this->get('accountExpirationDate') instanceof \DateTime) {
130
            $result = ($this->get('accountExpirationDate') > new \DateTime());
131
        } else {
132
            $result = (bool) $this->get('accountExpirationDate');
133
        }
134
135
        return $result;
136
    }
137
138
    /**
139
     * {@inheritdoc}
140
     */
141
    public function isAccountNonLocked()
142
    {
143
        return $this->has('locked') ? !$this->get('locked') : true;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function isCredentialsNonExpired()
150
    {
151
        return $this->has('passwordMustChange') ? !$this->get('passwordMustChange') : true;
152
    }
153
154
    /**
155
     * {@inheritdoc}
156
     */
157
    public function isEnabled()
158
    {
159
        return $this->has('enabled') ? $this->get('enabled') : true;
160
    }
161
162
    /**
163
     * {@inheritdoc}
164
     */
165
    public function getLdapGuid()
166
    {
167
        return $this->get('guid');
168
    }
169
170
    /**
171
     * {@inheritdoc}
172
     */
173
    public function setLdapGuid($guid)
174
    {
175
        return $this->set('guid', $guid);
176
    }
177
178
    /**
179
     * {@inheritdoc}
180
     */
181
    public function getGroups()
182
    {
183
        return $this->has('groups') ? $this->get('groups') : [];
184
    }
185
186
    /**
187
     * {@inheritdoc}
188
     */
189
    public function serialize()
190
    {
191
        return serialize([
192
            $this->attributes,
193
            $this->type,
194
            $this->roles
195
        ]);
196
    }
197
198
    /**
199
     * {@inheritdoc}
200
     */
201
    public function unserialize($serialized)
202
    {
203
        list($this->attributes, $this->type, $this->roles) = unserialize($serialized);
204
    }
205
206
    /**
207
     * @return string
208
     */
209
    public function __toString()
210
    {
211
        return $this->getUsername();
212
    }
213
}
214