|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* TDbUserManager class |
|
5
|
|
|
* |
|
6
|
|
|
* @author Qiang Xue <[email protected]> |
|
7
|
|
|
* @link https://github.com/pradosoft/prado |
|
8
|
|
|
* @license https://github.com/pradosoft/prado/blob/master/LICENSE |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace Prado\Security; |
|
12
|
|
|
|
|
13
|
|
|
use Prado\Data\TDataSourceConfig; |
|
14
|
|
|
use Prado\Exceptions\TConfigurationException; |
|
15
|
|
|
use Prado\Exceptions\TInvalidDataTypeException; |
|
16
|
|
|
use Prado\Prado; |
|
17
|
|
|
use Prado\Util\IDbModule; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* TDbUserManager class |
|
21
|
|
|
* |
|
22
|
|
|
* TDbUserManager manages user accounts that are stored in a database. |
|
23
|
|
|
* TDbUserManager is mainly designed to be used together with {@see \Prado\Security\TAuthManager} |
|
24
|
|
|
* which manages how users are authenticated and authorized in a Prado application. |
|
25
|
|
|
* |
|
26
|
|
|
* To use TDbUserManager together with TAuthManager, configure them in |
|
27
|
|
|
* the application configuration like following: |
|
28
|
|
|
* ```xml |
|
29
|
|
|
* <module id="db" |
|
30
|
|
|
* class="Prado\Data\TDataSourceConfig" ..../> |
|
31
|
|
|
* <module id="users" |
|
32
|
|
|
* class="Prado\Security\TDbUserManager" |
|
33
|
|
|
* UserClass="Path\To\MyUserClass" |
|
34
|
|
|
* ConnectionID="db" /> |
|
35
|
|
|
* <module id="auth" |
|
36
|
|
|
* class="Prado\Security\TAuthManager" |
|
37
|
|
|
* UserManager="users" LoginPage="Path\To\LoginPage" /> |
|
38
|
|
|
* ``` |
|
39
|
|
|
* |
|
40
|
|
|
* In the above, {@see setUserClass UserClass} specifies what class will be used |
|
41
|
|
|
* to create user instance. The class must extend from {@see \Prado\Security\TDbUser}. |
|
42
|
|
|
* {@see setConnectionID ConnectionID} refers to the ID of a {@see \Prado\Data\TDataSourceConfig} module |
|
43
|
|
|
* which specifies how to establish database connection to retrieve user information. |
|
44
|
|
|
* |
|
45
|
|
|
* @author Qiang Xue <[email protected]> |
|
46
|
|
|
* @since 3.1.0 |
|
47
|
|
|
*/ |
|
48
|
|
|
class TDbUserManager extends \Prado\TModule implements IUserManager, IDbModule |
|
49
|
|
|
{ |
|
50
|
|
|
private $_connID = ''; |
|
51
|
|
|
private $_conn; |
|
52
|
|
|
private $_guestName = 'Guest'; |
|
53
|
|
|
private $_userClass = ''; |
|
54
|
|
|
private $_userFactory; |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Initializes. |
|
58
|
|
|
* @param array|\Prado\Xml\TXmlElement $config module configuration |
|
59
|
|
|
*/ |
|
60
|
|
|
public function init($config) |
|
61
|
|
|
{ |
|
62
|
|
|
if ($this->_userClass === '') { |
|
63
|
|
|
throw new TConfigurationException('dbusermanager_userclass_required'); |
|
64
|
|
|
} |
|
65
|
|
|
$this->_userFactory = Prado::createComponent($this->_userClass, $this); |
|
66
|
|
|
if (!($this->_userFactory instanceof TDbUser)) { |
|
67
|
|
|
throw new TInvalidDataTypeException('dbusermanager_userclass_invalid', $this->_userClass); |
|
68
|
|
|
} |
|
69
|
|
|
parent::init($config); |
|
|
|
|
|
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @return string the user class name in namespace format. Defaults to empty string, meaning not set. |
|
74
|
|
|
*/ |
|
75
|
|
|
public function getUserClass() |
|
76
|
|
|
{ |
|
77
|
|
|
return $this->_userClass; |
|
78
|
|
|
} |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* @param string $value the user class name in namespace format. The user class must extend from {@see \Prado\Security\TDbUser}. |
|
82
|
|
|
*/ |
|
83
|
|
|
public function setUserClass($value) |
|
84
|
|
|
{ |
|
85
|
|
|
$this->_userClass = $value; |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string guest name, defaults to 'Guest' |
|
90
|
|
|
*/ |
|
91
|
|
|
public function getGuestName() |
|
92
|
|
|
{ |
|
93
|
|
|
return $this->_guestName; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @param string $value name to be used for guest users. |
|
98
|
|
|
*/ |
|
99
|
|
|
public function setGuestName($value) |
|
100
|
|
|
{ |
|
101
|
|
|
$this->_guestName = $value; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Validates if the username and password are correct. |
|
106
|
|
|
* @param string $username user name |
|
107
|
|
|
* @param string $password password |
|
108
|
|
|
* @return bool true if validation is successful, false otherwise. |
|
109
|
|
|
*/ |
|
110
|
|
|
public function validateUser($username, #[\SensitiveParameter] $password) |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->_userFactory->validateUser($username, $password); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Returns a user instance given the user name. |
|
117
|
|
|
* @param null|string $username user name, null if it is a guest. |
|
118
|
|
|
* @return TUser the user instance, null if the specified username is not in the user database. |
|
119
|
|
|
*/ |
|
120
|
|
|
public function getUser($username = null) |
|
121
|
|
|
{ |
|
122
|
|
|
if ($username === null) { |
|
123
|
|
|
$user = Prado::createComponent($this->_userClass, $this); |
|
124
|
|
|
$user->setIsGuest(true); |
|
125
|
|
|
return $user; |
|
126
|
|
|
} else { |
|
127
|
|
|
return $this->_userFactory->createUser($username); |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function getConnectionID() |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->_connID; |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Sets the ID of a TDataSourceConfig module. |
|
141
|
|
|
* The datasource module will be used to establish the DB connection |
|
142
|
|
|
* that will be used by the user manager. |
|
143
|
|
|
* @param string $value module ID. |
|
144
|
|
|
*/ |
|
145
|
|
|
public function setConnectionID($value) |
|
146
|
|
|
{ |
|
147
|
|
|
$this->_connID = $value; |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
/** |
|
151
|
|
|
* @return \Prado\Data\TDbConnection the database connection that may be used to retrieve user data. |
|
152
|
|
|
*/ |
|
153
|
|
|
public function getDbConnection() |
|
154
|
|
|
{ |
|
155
|
|
|
if ($this->_conn === null) { |
|
156
|
|
|
$this->_conn = $this->createDbConnection($this->_connID); |
|
157
|
|
|
$this->_conn->setActive(true); |
|
158
|
|
|
} |
|
159
|
|
|
return $this->_conn; |
|
160
|
|
|
} |
|
161
|
|
|
|
|
162
|
|
|
/** |
|
163
|
|
|
* Creates the DB connection. |
|
164
|
|
|
* @param string $connectionID the module ID for TDataSourceConfig |
|
165
|
|
|
* @throws TConfigurationException if module ID is invalid or empty |
|
166
|
|
|
* @return \Prado\Data\TDbConnection the created DB connection |
|
167
|
|
|
*/ |
|
168
|
|
|
protected function createDbConnection($connectionID) |
|
169
|
|
|
{ |
|
170
|
|
|
if ($connectionID !== '') { |
|
171
|
|
|
$conn = $this->getApplication()->getModule($connectionID); |
|
172
|
|
|
if ($conn instanceof TDataSourceConfig) { |
|
173
|
|
|
return $conn->getDbConnection(); |
|
174
|
|
|
} else { |
|
175
|
|
|
throw new TConfigurationException('dbusermanager_connectionid_invalid', $connectionID); |
|
176
|
|
|
} |
|
177
|
|
|
} else { |
|
178
|
|
|
throw new TConfigurationException('dbusermanager_connectionid_required'); |
|
179
|
|
|
} |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
/** |
|
183
|
|
|
* Returns a user instance according to auth data stored in a cookie. |
|
184
|
|
|
* @param \Prado\Web\THttpCookie $cookie the cookie storing user authentication information |
|
185
|
|
|
* @return TDbUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data. |
|
186
|
|
|
* @since 3.1.1 |
|
187
|
|
|
*/ |
|
188
|
|
|
public function getUserFromCookie($cookie) |
|
189
|
|
|
{ |
|
190
|
|
|
return $this->_userFactory->createUserFromCookie($cookie); |
|
191
|
|
|
} |
|
192
|
|
|
|
|
193
|
|
|
/** |
|
194
|
|
|
* Saves user auth data into a cookie. |
|
195
|
|
|
* @param \Prado\Web\THttpCookie $cookie the cookie to receive the user auth data. |
|
196
|
|
|
* @since 3.1.1 |
|
197
|
|
|
*/ |
|
198
|
|
|
public function saveUserToCookie($cookie) |
|
199
|
|
|
{ |
|
200
|
|
|
$user = $this->getApplication()->getUser(); |
|
201
|
|
|
if ($user instanceof TDbUser) { |
|
202
|
|
|
$user->saveUserToCookie($cookie); |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|