Completed
Push — master ( 62be60...d8ccc4 )
by Philip
02:20
created

User::getUserData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
eloc 2
nc 1
nop 0
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\UserInterface;
15
use CRUDlex\Entity;
16
17
/**
18
 * The UserInterface implementation for the UserProvider.
19
 */
20
class User implements UserInterface {
21
22
    /**
23
     * Holds the actual user data.
24
     */
25
    private $userData;
26
27
    /**
28
     * The CRUDEntity fieldname of the username.
29
     */
30
    protected $usernameField;
31
32
    /**
33
     * The fieldname of the password (hash).
34
     */
35
    protected $passwordField;
36
37
    /**
38
     * The fieldname of the password hash salt.
39
     */
40
    protected $saltField;
41
42
    /**
43
     * Hold password hash salt.
44
     */
45
    private $roles;
46
47
    /**
48
     * Constructor.
49
     *
50
     * @param string $usernameField
51
     * the username
52
     *
53
     * @param string $passwordField
54
     * the password (hash)
55
     *
56
     * @param string $saltField
57
     * the password hash salt
58
     *
59
     * @param Entity $userEntity
60
     * the actual user data
61
     *
62
     * @param array $roles
63
     * the roles
64
     */
65
    public function __construct($usernameField, $passwordField, $saltField, Entity $userEntity, array $roles) {
66
        $this->usernameField = $usernameField;
67
        $this->passwordField = $passwordField;
68
        $this->saltField = $saltField;
69
        // We have to copy it over as symfony/security wants something serializable.
70
        $this->userData = array();
71
        foreach ($userEntity->getDefinition()->getFieldNames() as $field) {
72
            $this->userData[$field] = $userEntity->get($field);
73
        }
74
        $this->roles = $roles;
75
    }
76
77
    /**
78
     * Gets the roles.
79
     *
80
     * @return array
81
     * the roles
82
     */
83
    public function getRoles() {
84
        return $this->roles;
85
    }
86
87
    /**
88
     * Gets the password (hash).
89
     *
90
     * @return string
91
     * the password
92
     */
93
    public function getPassword() {
94
        return $this->userData[$this->passwordField];
95
    }
96
97
    /**
98
     * Gets the password hash salt.
99
     *
100
     * @return string
101
     * the salt
102
     */
103
    public function getSalt() {
104
        return $this->userData[$this->saltField];
105
    }
106
107
    /**
108
     * Gets the username.
109
     *
110
     * @return string
111
     * the username
112
     */
113
    public function getUsername() {
114
        return $this->userData[$this->usernameField];
115
    }
116
117
    /**
118
     * Should erase some crucial data if needed. But nothing to do here in this
119
     * implementation.
120
     */
121
    public function eraseCredentials() {
122
    }
123
124
    /**
125
     * Gets the user data.
126
     *
127
     * @return array
128
     * the user data
129
     */
130
    public function getUserData() {
131
        return $this->userData;
132
    }
133
134
}
135