UserProvider::refreshUser()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
1
<?php
2
3
/*
4
 * This file is part of the CRUDlexUser package.
5
 *
6
 * (c) Philip Lehmann-Böhm <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace CRUDlex;
13
14
use Symfony\Component\Security\Core\User\UserProviderInterface;
15
use Symfony\Component\Security\Core\User\UserInterface;
16
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
17
use CRUDlex\AbstractData;
18
use CRUDlex\User;
19
20
/**
21
 * The implementation of the UserProviderInterface to work with the CRUDlex API.
22
 */
23
class UserProvider implements UserProviderInterface
24
{
25
26
    /**
27
     * The Entity fieldname of the username.
28
     */
29
    protected $usernameField;
30
31
    /**
32
     * The fieldname of the password (hash).
33
     */
34
    protected $passwordField;
35
36
    /**
37
     * The fieldname of the password hash salt.
38
     */
39
    protected $saltField;
40
41
    /**
42
     * Holds the AbstractData instance to grab the user data from.
43
     */
44
    protected $userData;
45
46
    /**
47
     * Holds the AbstractData instance or the field of the many-to-many relationship to grab the user role data from.
48
     */
49
    protected $userRoleData;
50
51
    /**
52
     * Holds the AbstractData instance or the field of the many-to-many relationship to grab the user role data from.
53
     */
54
    protected $userRoleIdentifier;
55
56
    /**
57
     * Loads the roles of an user via an AbstractData instance.
58
     *
59
     * @param mixed $userId
60
     * the id of the user
61
     *
62
     * @return string[]
63
     * the roles of the user
64
     */
65
    protected function loadUserRolesViaData($userId)
66
    {
67
        $crudRoles = $this->userRoleIdentifier->listEntries(['user' => $userId], ['user' => '=']);
0 ignored issues
show
Bug introduced by
The method listEntries cannot be called on $this->userRoleIdentifier (of type string).

Methods can only be called on objects. This check looks for methods being called on variables that have been inferred to never be objects.

Loading history...
68
        $roles = ['ROLE_USER'];
69
        if ($crudRoles !== null) {
70
            foreach ($crudRoles as $crudRole) {
71
                $role = $crudRole->get('role');
72
                $roles[] = $role['name'];
73
            }
74
        }
75
        return $roles;
76
    }
77
78
    /**
79
     * Loads the roles of an user via a many-to-many relationship
80
     *
81
     * @param Entity $user
82
     * the id of the user
83
     *
84
     * @return string[]
85
     * the roles of the user
86
     */
87
    protected function loadUserRolesViaManyToMany($user)
88
    {
89
        $roles = ['ROLE_USER'];
90
        if (is_string($this->userRoleIdentifier)) {
91
            foreach ($user->get($this->userRoleIdentifier) as $role) {
92
                $roles[] = $role['name'];
93
            }
94
        }
95
        return $roles;
96
    }
97
98
    /**
99
     * Constructor for data structures connecting users and roles via a many-to-many relationship on the user.
100
     *
101
     * @param Service $service
102
     * the Service instance to take the AbstractData of the users from
103
     *
104
     * @param string $userIdentifier
105
     * the identifier to of the user AbstractData
106
     *
107
     * @param string $userRoleIdentifier
108
     * the field of the many-to-many relationship to grab the user role data from or the AbstractData if its an own entity
109
     *
110
     * @param string $usernameField
111
     * the Entity fieldname of the username
112
     *
113
     * @param string $passwordField
114
     * the Entity fieldname of the password hash
115
     *
116
     * @param string $saltField
117
     * the Entity fieldname of the password hash salt
118
     */
119
    public function __construct(Service $service, $userIdentifier = 'user', $userRoleIdentifier = 'roles', $usernameField = 'username', $passwordField = 'password', $saltField = 'salt')
120
    {
121
        $this->userData = $service->getData($userIdentifier);
122
        $this->userRoleIdentifier = $userRoleIdentifier;
123
        $this->usernameField = $usernameField;
124
        $this->passwordField = $passwordField;
125
        $this->saltField = $saltField;
126
    }
127
128
    /**
129
     * Loads and returns an user by username.
130
     * Throws an UsernameNotFoundException on not existing username.
131
     *
132
     * @param string $username
133
     * the username
134
     *
135
     * @return User
136
     * the loaded user
137
     */
138
    public function loadUserByUsername($username)
139
    {
140
141
        $users = $this->userData->listEntries([$this->usernameField => $username], [$this->usernameField => '='], 0, 1);
142
        if (count($users) === 0) {
143
            throw new UsernameNotFoundException();
144
        }
145
146
        $user = $users[0];
147
        $roles = is_string($this->userRoleIdentifier) ? $this->loadUserRolesViaManyToMany($user) : $this->loadUserRolesViaData($user->get('id'));
148
149
        $userObj = new User($this->usernameField, $this->passwordField, $this->saltField, $user, $roles);
150
        return $userObj;
151
    }
152
153
    /**
154
     * Reloads and returns the given user.
155
     * Throws an UsernameNotFoundException if the user ceased to exist meanwhile.
156
     *
157
     * @param UserInterface $user
158
     * the user to reload
159
     *
160
     * @return User
161
     * the reloaded user
162
     */
163
    public function refreshUser(UserInterface $user)
164
    {
165
        $refreshedUser = $this->loadUserByUsername($user->getUsername());
166
        return $refreshedUser;
167
    }
168
169
    /**
170
     * Tests whether the given user class is supported by this UserProvider.
171
     *
172
     * @param string $class
173
     * the user class name to test
174
     *
175
     * @return boolean
176
     * true if the class is "CRUDlex\User"
177
     */
178
    public function supportsClass($class)
179
    {
180
        return $class === 'CRUDlex\User';
181
    }
182
183
}
184