Test Failed
Branch master (46ffa2)
by Sebastian
01:56
created

Authenticate::isNotLogged()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
1
<?php
2
3
/**
4
 * Linna Framework.
5
 *
6
 * @author Sebastian Rapetti <[email protected]>
7
 * @copyright (c) 2017, Sebastian Rapetti
8
 * @license http://opensource.org/licenses/MIT MIT License
9
 */
10
declare(strict_types=1);
11
12
namespace Linna\Auth;
13
14
use Linna\Session\Session;
15
16
/**
17
 * This class provide methods for manage user authentication system.
18
 */
19
class Authenticate
20
{
21
    /**
22
     * @var array Login status
23
     */
24
    private $data = ['user_name'=>''];
25
26
    /**
27
     * @var bool Indicate login status, true or false
28
     */
29
    private $logged = false;
30
31
    /**
32
     * @var Session Session class
33
     */
34
    private $sessionInstance;
35
36
    /**
37
     * @var Password Password class
38
     */
39
    private $password;
40
41
    /**
42
     * Class constructor.
43
     * <pre><code class="php">use Linna\Session\Session;
44
     * use Linna\Auth\Password;
45
     *
46
     * $session = new Session();
47
     * $password = new Password();
48
     *
49
     * $auth = new Authenticate($session, $password);
50
     * </code></pre>
51
     *
52
     * @param Session  $session  Session class instance.
53
     * @param Password $password Password class instance.
54
     */
55 3
    public function __construct(Session $session, Password $password)
56
    {
57 3
        $this->password = $password;
58 3
        $this->sessionInstance = $session;
59 3
        $this->logged = $this->refresh();
60 3
    }
61
62
    /**
63
     * Utilize this method for check if an user in the current session,
64
     * is currently logged in.
65
     * <pre><code class="php">$auth = new Authenticate($session, $password);
66
     *
67
     * if ($auth->isLogged()) {
68
     *     //do actions
69
     * }
70
     * </code></pre>
71
     *
72
     * @return bool
73
     */
74
    public function isLogged() : bool
75
    {
76
        return $this->logged;
77
    }
78
79
    /**
80
     * Opposite to isLogged() method.
81
     *
82
     * Utilize this method for check if an user in the current session,
83
     * is currently not logged in.
84
     * <pre><code class="php">$auth = new Authenticate($session, $password);
85
     *
86
     * if ($auth->isNotLogged()) {
87
     *     //redirect or other action
88
     * }
89
     *
90
     * //do actions
91
     * </code></pre>
92
     *
93
     * @return bool
94
     */
95
    public function isNotLogged() : bool
96
    {
97
        return !$this->logged;
98
    }
99
100
    /**
101
     * Return array containing login data.
102
     * <pre><code class="php">$auth = new Authenticate($session, $password);
103
     *
104
     * $data = $auth->getLoginData();
105
     *
106
     * //var_dump result
107
     * //after session start and login, session data appear like below array:
108
     * //[
109
     * //    'time' => 1479641396
110
     * //    'expire' => 1800
111
     * //    'loginTime' => 1479641395
112
     * //    'login' => [
113
     * //        'login' => true
114
     * //        'user_id' => 1
115
     * //        'user_name' => 'root'
116
     * //    ]
117
     * //]
118
     * var_dump($data);
119
     * </code></pre>
120
     *
121
     * @return array
122
     */
123 3
    public function getLoginData() : array
124
    {
125 3
        return $this->data;
126
    }
127
128
    /**
129
     * Try to attemp login for the informations passed by param.
130
     *
131
     * <pre><code class="php">$user = ''; //user from login page form
132
     * $password = ''; //password from login page form
133
     *
134
     * $storedUser = ''; //user from stored informations
135
     * $storedPassword = ''; //password hash from stored informations
136
     * $storedId = ''; //user id from stored informations
137
     *
138
     * $auth = new Authenticate($session, $password);
139
     * $auth->login($user, $password, $storedUser, $storedPassword, $storedId);
140
     *
141
     * //other operation after login
142
     * </code></pre>
143
     *
144
     * @param string $userName
145
     * @param string $password
146
     * @param string $storedUserName
147
     * @param string $storedPassword
148
     * @param int    $storedId
149
     *
150
     * @return bool
151
     */
152
    public function login(string $userName, string $password, string $storedUserName = '', string $storedPassword = '', int $storedId = 0): bool
153
    {
154
        if ($userName === $storedUserName && $this->password->verify($password, $storedPassword)) {
155
            //write valid login on session
156
            $this->sessionInstance->loginTime = time();
157
            $this->sessionInstance->login = [
158
                'login'     => true,
159
                'user_id'   => $storedId,
160
                'user_name' => $storedUserName,
161
            ];
162
163
            //regenerate session id
164
            $this->sessionInstance->regenerate();
165
            $this->logged = true;
166
167
            return true;
168
        }
169
170
        return false;
171
    }
172
173
    /**
174
     * Do logout and delete login information from session.
175
     * <pre><code class="php">$auth = new Authenticate($session, $password);
176
     * $auth->logout();
177
     * </code></pre>
178
     *
179
     * @return bool
180
     */
181
    public function logout(): bool
182
    {
183
        //remove login data from session
184
        unset($this->sessionInstance->login, $this->sessionInstance->loginTime);
185
186
        //regenerate session id
187
        $this->sessionInstance->regenerate();
188
        $this->logged = false;
189
190
        return true;
191
    }
192
193
    /**
194
     * Check if user is logged, get login data from session and update it.
195
     *
196
     * @return bool
197
     */
198 3
    private function refresh(): bool
199
    {
200
        //check for login data on in current session
201 3
        if (!isset($this->sessionInstance->login)) {
202 3
            return false;
203
        }
204
205
        //take time
206
        $time = time();
207
208
        //check if login expired
209
        if (($this->sessionInstance->loginTime + $this->sessionInstance->expire) < $time) {
210
            return false;
211
        }
212
213
        //update login data
214
        $this->sessionInstance->loginTime = $time;
215
        $this->data = $this->sessionInstance->login;
216
217
        return true;
218
    }
219
}
220