Completed
Push — master ( 70a03c...6eae1b )
by Philip
02:14
created

UserProvider::refreshUser()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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