Completed
Push — master ( 6eae1b...9354c8 )
by Philip
02:11
created

UserProvider::loadUserByUsername()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 15
rs 9.4285
cc 2
eloc 10
nc 2
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\Data;
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
     * The CRUDEntity fieldname of the username.
27
     */
28
    protected $usernameField;
29
30
    /**
31
     * The fieldname of the password (hash).
32
     */
33
    protected $passwordField;
34
35
    /**
36
     * The fieldname of the password hash salt.
37
     */
38
    protected $saltField;
39
40
    /**
41
     * Holds the CRUDData instance to grab the user data from.
42
     */
43
    protected $userData;
44
45
    /**
46
     * Holds the CRUDData instance to grab the user role data from.
47
     */
48
    protected $userRoleData;
49
50
    /**
51
     * Loads the roles of an user.
52
     *
53
     * @param mixed $userId
54
     * the id of the user
55
     *
56
     * @return string[]
57
     * the roles of the user
58
     */
59
    protected function loadUserRoles($userId) {
60
        $crudRoles = $this->userRoleData->listEntries(array('user' => $userId), array('user' => '='));
61
        $this->userRoleData->fetchReferences($crudRoles);
62
        $roles = array('ROLE_USER');
63
        if ($crudRoles !== null) {
64
            foreach ($crudRoles as $crudRole) {
65
                $role = $crudRole->get('role');
66
                $roles[] = $role['name'];
67
            }
68
        }
69
        return $roles;
70
    }
71
72
    /**
73
     * Constructor.
74
     *
75
     * @param Data $userData
76
     * the Data instance to grab the user data from
77
     *
78
     * @param Data $userRoleData
79
     * the Data instance to grab the user role data from
80
     *
81
     * @param string $usernameField
82
     * the Entity fieldname of the username
83
     *
84
     * @param string $passwordField
85
     * the Entity fieldname of the password hash
86
     *
87
     * @param string $saltField
88
     * the Entity fieldname of the password hash salt
89
     */
90
    public function __construct(Data $userData, Data $userRoleData, $usernameField = 'username', $passwordField = 'password', $saltField = 'salt') {
91
        $this->userData = $userData;
92
        $this->userRoleData = $userRoleData;
93
        $this->usernameField = $usernameField;
94
        $this->passwordField = $passwordField;
95
        $this->saltField = $saltField;
96
    }
97
98
    /**
99
     * Loads and returns an user by username.
100
     * Throws an UsernameNotFoundException on not existing username.
101
     *
102
     * @param string $username
103
     * the username
104
     *
105
     * @return User
106
     * the loaded user
107
     */
108
    public function loadUserByUsername($username) {
109
110
        $users = $this->userData->listEntries(array($this->usernameField => $username), array($this->usernameField => '='), 0, 1);
111
        if (count($users) === 0) {
112
            throw new UsernameNotFoundException();
113
        }
114
115
        $user = $users[0];
116
        $password = $user->get($this->passwordField);
117
        $salt = $user->get($this->saltField);
118
        $roles = $this->loadUserRoles($user->get('id'));
119
120
        $userObj = new User($username, $password, $salt, $roles);
121
        return $userObj;
122
    }
123
124
    /**
125
     * Reloads and returns the given user.
126
     * Throws an UsernameNotFoundException if the user ceased to exist meanwhile.
127
     *
128
     * @param UserInterface $user
129
     * the user to reload
130
     *
131
     * @return User
132
     * the reloaded user
133
     */
134
    public function refreshUser(UserInterface $user) {
135
        $refreshedUser = $this->loadUserByUsername($user->getUsername());
136
        return $refreshedUser;
137
    }
138
139
    /**
140
     * Tests whether the given user class is supported by this UserProvider.
141
     *
142
     * @param string $class
143
     * the user class name to test
144
     *
145
     * @return boolean
146
     * true if the class is "CRUDlex\User"
147
     */
148
    public function supportsClass($class) {
149
        return $class === 'CRUDlex\User';
150
    }
151
152
}
153