1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of Jitamin. |
5
|
|
|
* |
6
|
|
|
* Copyright (C) Jitamin Team |
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 Jitamin\Auth; |
13
|
|
|
|
14
|
|
|
use Jitamin\Foundation\Base; |
15
|
|
|
use Jitamin\Foundation\Security\PasswordAuthenticationProviderInterface; |
16
|
|
|
use Jitamin\Foundation\Security\SessionCheckProviderInterface; |
17
|
|
|
use Jitamin\Model\UserModel; |
18
|
|
|
use Jitamin\Services\Identity\DatabaseUserProvider; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Database Authentication Provider. |
22
|
|
|
*/ |
23
|
|
|
class DatabaseAuth extends Base implements PasswordAuthenticationProviderInterface, SessionCheckProviderInterface |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* User properties. |
27
|
|
|
* |
28
|
|
|
* @var array |
29
|
|
|
*/ |
30
|
|
|
protected $userInfo = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Username. |
34
|
|
|
* |
35
|
|
|
* @var string |
36
|
|
|
*/ |
37
|
|
|
protected $username = ''; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Password. |
41
|
|
|
* |
42
|
|
|
* @var string |
43
|
|
|
*/ |
44
|
|
|
protected $password = ''; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Get authentication provider name. |
48
|
|
|
* |
49
|
|
|
* @return string |
50
|
|
|
*/ |
51
|
|
|
public function getName() |
52
|
|
|
{ |
53
|
|
|
return 'Database'; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* Authenticate the user. |
58
|
|
|
* |
59
|
|
|
* @return bool |
60
|
|
|
*/ |
61
|
|
|
public function authenticate() |
62
|
|
|
{ |
63
|
|
|
$user = $this->db |
|
|
|
|
64
|
|
|
->table(UserModel::TABLE) |
65
|
|
|
->columns('id', 'password') |
66
|
|
|
->eq(strpos($this->username, '@') === false ? 'username' : 'email', $this->username) |
67
|
|
|
->eq('disable_login_form', 0) |
68
|
|
|
->eq('is_ldap_user', 0) |
69
|
|
|
->eq('is_active', 1) |
70
|
|
|
->findOne(); |
71
|
|
|
|
72
|
|
|
if (!empty($user) && password_verify($this->password, $user['password'])) { |
73
|
|
|
$this->userInfo = $user; |
74
|
|
|
|
75
|
|
|
return true; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return false; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Check if the user session is valid. |
83
|
|
|
* |
84
|
|
|
* @return bool |
85
|
|
|
*/ |
86
|
|
|
public function isValidSession() |
87
|
|
|
{ |
88
|
|
|
return $this->userModel->isActive($this->userSession->getId()); |
|
|
|
|
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* Get user object. |
93
|
|
|
* |
94
|
|
|
* @return \Jitamin\Services\User\DatabaseUserProvider |
95
|
|
|
*/ |
96
|
|
|
public function getUser() |
97
|
|
|
{ |
98
|
|
|
if (empty($this->userInfo)) { |
99
|
|
|
return; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
return new DatabaseUserProvider($this->userInfo); |
|
|
|
|
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Set username. |
107
|
|
|
* |
108
|
|
|
* @param string $username |
109
|
|
|
*/ |
110
|
|
|
public function setUsername($username) |
111
|
|
|
{ |
112
|
|
|
$this->username = $username; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* Set password. |
117
|
|
|
* |
118
|
|
|
* @param string $password |
119
|
|
|
*/ |
120
|
|
|
public function setPassword($password) |
121
|
|
|
{ |
122
|
|
|
$this->password = $password; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Since your code implements the magic getter
_get
, this function will be called for any read access on an undefined variable. You can add the@property
annotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.