1 | <?php |
||
20 | class Authenticate |
||
21 | { |
||
22 | /** |
||
23 | * @var array Login status |
||
24 | */ |
||
25 | private $data = ['user_name'=>'']; |
||
26 | |||
27 | /** |
||
28 | * @var bool Indicate login status, true or false |
||
29 | */ |
||
30 | private $logged = false; |
||
31 | |||
32 | /** |
||
33 | * @var Session Session class |
||
34 | */ |
||
35 | private $sessionInstance; |
||
36 | |||
37 | /** |
||
38 | * @var Password Password class |
||
39 | */ |
||
40 | private $password; |
||
41 | |||
42 | /** |
||
43 | * Class constructor. |
||
44 | * <pre><code class="php">use Linna\Session\Session; |
||
45 | * use Linna\Auth\Password; |
||
46 | * |
||
47 | * $session = new Session(); |
||
48 | * $password = new Password(); |
||
49 | * |
||
50 | * $auth = new Authenticate($session, $password); |
||
51 | * </code></pre> |
||
52 | * |
||
53 | * @param Session $session Session class instance. |
||
54 | * @param Password $password Password class instance. |
||
55 | */ |
||
56 | 10 | public function __construct(Session $session, Password $password) |
|
62 | |||
63 | /** |
||
64 | * Utilize this method for check if an user in the current session, |
||
65 | * is currently logged in. |
||
66 | * <pre><code class="php">$auth = new Authenticate($session, $password); |
||
67 | * |
||
68 | * if ($auth->isLogged()) { |
||
69 | * //do actions |
||
70 | * } |
||
71 | * </code></pre> |
||
72 | * |
||
73 | * @return bool |
||
74 | */ |
||
75 | 6 | public function isLogged() : bool |
|
79 | |||
80 | /** |
||
81 | * Opposite to isLogged() method. |
||
82 | * |
||
83 | * Utilize this method for check if an user in the current session, |
||
84 | * is currently not logged in. |
||
85 | * <pre><code class="php">$auth = new Authenticate($session, $password); |
||
86 | * |
||
87 | * if ($auth->isNotLogged()) { |
||
88 | * //redirect or other action |
||
89 | * } |
||
90 | * |
||
91 | * //do actions |
||
92 | * </code></pre> |
||
93 | * |
||
94 | * @return bool |
||
95 | */ |
||
96 | 3 | public function isNotLogged() : bool |
|
100 | |||
101 | /** |
||
102 | * Return array containing login data. |
||
103 | * <pre><code class="php">$auth = new Authenticate($session, $password); |
||
104 | * |
||
105 | * $data = $auth->getLoginData(); |
||
106 | * |
||
107 | * //var_dump result |
||
108 | * //after session start and login, session data appear like below array: |
||
109 | * //[ |
||
110 | * // 'time' => 1479641396 |
||
111 | * // 'expire' => 1800 |
||
112 | * // 'loginTime' => 1479641395 |
||
113 | * // 'login' => [ |
||
114 | * // 'login' => true |
||
115 | * // 'user_id' => 1 |
||
116 | * // 'user_name' => 'root' |
||
117 | * // ] |
||
118 | * //] |
||
119 | * var_dump($data); |
||
120 | * </code></pre> |
||
121 | * |
||
122 | * @return array |
||
123 | */ |
||
124 | 4 | public function getLoginData() : array |
|
128 | |||
129 | /** |
||
130 | * Try to attemp login with the informations passed by param. |
||
131 | * |
||
132 | * <pre><code class="php">$user = ''; //user from login page form |
||
133 | * $password = ''; //password from login page form |
||
134 | * |
||
135 | * $storedUser = ''; //user from stored informations |
||
136 | * $storedPassword = ''; //password hash from stored informations |
||
137 | * $storedId = ''; //user id from stored informations |
||
138 | * |
||
139 | * $auth = new Authenticate($session, $password); |
||
140 | * $auth->login($user, $password, $storedUser, $storedPassword, $storedId); |
||
141 | * |
||
142 | * //other operation after login |
||
143 | * </code></pre> |
||
144 | * |
||
145 | * @param string $userName |
||
146 | * @param string $password |
||
147 | * @param string $storedUserName |
||
148 | * @param string $storedPassword |
||
149 | * @param int $storedId |
||
150 | * |
||
151 | * @return bool |
||
152 | */ |
||
153 | 6 | public function login(string $userName, string $password, string $storedUserName = '', string $storedPassword = '', int $storedId = 0): bool |
|
173 | |||
174 | /** |
||
175 | * Do logout and delete login information from session. |
||
176 | * <pre><code class="php">$auth = new Authenticate($session, $password); |
||
177 | * $auth->logout(); |
||
178 | * </code></pre> |
||
179 | * |
||
180 | * @return bool |
||
181 | */ |
||
182 | 2 | public function logout(): bool |
|
193 | |||
194 | /** |
||
195 | * Check if user is logged, get login data from session and update it. |
||
196 | * |
||
197 | * @return bool |
||
198 | */ |
||
199 | 10 | private function refresh(): bool |
|
220 | } |
||
221 |