1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Handles user authentication |
4
|
|
|
* |
5
|
|
|
* PHP version 5.5 |
6
|
|
|
* |
7
|
|
|
* @category OpCacheGUI |
8
|
|
|
* @package Auth |
9
|
|
|
* @author Pieter Hordijk <[email protected]> |
10
|
|
|
* @copyright Copyright (c) 2014 Pieter Hordijk <https://github.com/PeeHaa> |
11
|
|
|
* @license http://www.opensource.org/licenses/mit-license.html MIT License |
12
|
|
|
* @version 1.0.0 |
13
|
|
|
*/ |
14
|
|
|
namespace OpCacheGUI\Auth; |
15
|
|
|
|
16
|
|
|
use OpCacheGUI\Storage\Session; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Handles user authentication |
20
|
|
|
* |
21
|
|
|
* @category OpCacheGUI |
22
|
|
|
* @package Auth |
23
|
|
|
* @author Pieter Hordijk <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class User |
26
|
|
|
{ |
27
|
|
|
/** |
28
|
|
|
* @var \OpCacheGUI\Storage\Session The session |
29
|
|
|
*/ |
30
|
|
|
private $sessionStorage; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string The username |
34
|
|
|
*/ |
35
|
|
|
private $username; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @var string The password |
39
|
|
|
*/ |
40
|
|
|
private $password; |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @var \OpCacheGUI\Auth\Whitelist Instance of a IP whitelist |
44
|
|
|
*/ |
45
|
|
|
private $whitelist; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Creates instance |
49
|
|
|
* |
50
|
|
|
* @param \OpCacheGUI\Storage\Session $sessionStorage The session |
51
|
|
|
* @param string $username The username |
52
|
|
|
* @param string $password The password |
53
|
|
|
*/ |
54
|
10 |
|
public function __construct(Session $sessionStorage, $username, $password, Whitelist $whitelist) |
55
|
|
|
{ |
56
|
10 |
|
$this->sessionStorage = $sessionStorage; |
57
|
10 |
|
$this->username = strtolower($username); |
58
|
10 |
|
$this->password = $password; |
59
|
10 |
|
$this->whitelist = $whitelist; |
60
|
10 |
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Checks whether the user requires a login before being able to use the site |
64
|
|
|
* |
65
|
|
|
* @return boolean True when the site requires a login |
66
|
|
|
*/ |
67
|
3 |
|
public function requiresLogin() |
68
|
|
|
{ |
69
|
3 |
|
return $this->username && !$this->isloggedIn(); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Checks whether the user is logged in |
74
|
|
|
* |
75
|
|
|
* @return boolean True when the user is logged in |
76
|
|
|
*/ |
77
|
4 |
|
public function isLoggedIn() |
78
|
|
|
{ |
79
|
4 |
|
return $this->sessionStorage->isKeyValid('user'); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Tries to log the user in |
84
|
|
|
* |
85
|
|
|
* @param string $username The user supplied username |
86
|
|
|
* @param string $password The user supplied password |
87
|
|
|
* @param string $ip The IP of the user |
88
|
|
|
* |
89
|
|
|
* @return boolean True when the user successfully authenticated |
90
|
|
|
*/ |
91
|
5 |
|
public function login($username, $password, $ip) |
92
|
|
|
{ |
93
|
5 |
|
if (!$this->whitelist->isAllowed($ip)) { |
94
|
1 |
|
return false; |
95
|
|
|
} |
96
|
|
|
|
97
|
4 |
|
if (strtolower($username) === $this->username && password_verify($password, $this->password)) { |
98
|
1 |
|
$this->sessionStorage->set('user', $this->username); |
99
|
|
|
|
100
|
1 |
|
return true; |
101
|
|
|
} |
102
|
|
|
|
103
|
3 |
|
return false; |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|