Passed
Push — master ( d74986...20c13f )
by Sebastian
02:50
created

User::setPassword()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 1
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 1
crap 1
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2018, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Authentication;
13
14
use Linna\DataMapper\DomainObjectAbstract;
15
16
/**
17
 * Provide a basic user for authentication system.
18
 * This class is thinked to be used with PHP PDO,
19
 * but should be instantiated like a normal class.
20
 *
21
 * <pre><code class="php">$password = new Password();
22
 *
23
 * $userId = 1;
24
 *
25
 * $pdos = $pdo->prepare('SELECT user_id AS objectId,
26
 *     uuid,
27
 *     name,
28
 *     email,
29
 *     description,
30
 *     password,
31
 *     active,
32
 *     created,
33
 *     last_update AS lastUpdate
34
 *     FROM user WHERE user_id = :id');
35
 *
36
 * $pdos->bindParam(':id', $userId, \PDO::PARAM_INT);
37
 * $pdos->execute();
38
 *
39
 * //pdo return an User class instance
40
 * $user = $pdos->fetchObject('\Linna\Auth\User', [$password]);
41
 *
42
 * //var_dump result
43
 * //root
44
 * var_dump($user->name);
45
 * </code></pre>
46
 */
47
class User extends DomainObjectAbstract
48
{
49
    /**
50
     * @var string Universal unique identifier.
51
     */
52
    public $uuid = '';
53
54
    /**
55
     * @var string User name.
56
     */
57
    public $name = '';
58
59
    /**
60
     * @var string User description.
61
     */
62
    public $description = '';
63
64
    /**
65
     * @var string User e-mail.
66
     */
67
    public $email = '';
68
69
    /**
70
     * @var string User hashed password.
71
     */
72
    public $password = '';
73
74
    /**
75
     * @var int It say if user is active or not.
76
     */
77
    public $active = 0;
78
79
    /**
80
     * @var string User creation date.
81
     */
82
    public $created = '';
83
84
    /**
85
     * @var string Last update.
86
     */
87
    public $lastUpdate = '';
88
89
    /**
90
     * @var Password Password class for manage password.
91
     */
92
    private $passwordUtility;
93
94
    /**
95
     * Class Constructor.
96
     *
97
     * <pre><code class="php">$password = new Password();
98
     *
99
     * $user = new User($password);
100
     * </code></pre>
101
     *
102
     * @param Password $password
103
     */
104 227
    public function __construct(Password $password)
105
    {
106 227
        $this->passwordUtility = $password;
107
108
        //set required type
109
        //do type conversion because PDO doesn't return any original type from db :(.
110 227
        \settype($this->rId, 'integer');
111 227
        \settype($this->objectId, 'integer');
112 227
        \settype($this->active, 'integer');
113 227
    }
114
115
    /**
116
     * Set new user password without do any check.
117
     *
118
     * <pre><code class="php">$password = new Password();
119
     *
120
     * $user = new User($password);
121
     *
122
     * $user->setPassword('newPassword');
123
     * </code></pre>
124
     *
125
     * @param string $newPassword
126
     *
127
     * @return void
128
     */
129 5
    public function setPassword(string $newPassword): void
130
    {
131
        //hash provided password
132 5
        $this->password = $this->passwordUtility->hash($newPassword);
133 5
    }
134
135
    /**
136
     * Change user password only after check old password.
137
     *
138
     * <pre><code class="php">$password = new Password();
139
     *
140
     * $user = new User($password);
141
     *
142
     * $user->changePassword('newPassword', 'oldPassword');
143
     * </code></pre>
144
     *
145
     * @param string $newPassword
146
     * @param string $oldPassword
147
     *
148
     * @return bool
149
     */
150 1
    public function changePassword(string $newPassword, string $oldPassword): bool
151
    {
152
        //verfy password match
153 1
        if ($this->passwordUtility->verify($oldPassword, $this->password)) {
154
155
            //if match set new password
156 1
            $this->password = $this->passwordUtility->hash($newPassword);
157
158 1
            return true;
159
        }
160
161 1
        return false;
162
    }
163
}
164