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\TDbConnection; |
||||
14 | use Prado\Exceptions\TConfigurationException; |
||||
15 | |||||
16 | /** |
||||
17 | * TDbUser class |
||||
18 | * |
||||
19 | * TDbUser is the base user class for using together with {@see \Prado\Security\TDbUserManager}. |
||||
20 | * Two methods are declared and must be implemented in the descendant classes: |
||||
21 | * - {@see validateUser()}: validates if username and password are correct entries. |
||||
22 | * - {@see createUser()}: creates a new user instance given the username |
||||
23 | * |
||||
24 | * @author Qiang Xue <[email protected]> |
||||
25 | * @since 3.1.0 |
||||
26 | */ |
||||
27 | abstract class TDbUser extends TUser |
||||
28 | { |
||||
29 | private $_connection; |
||||
30 | |||||
31 | /** |
||||
32 | * Returns a database connection that may be used to retrieve data from database. |
||||
33 | * |
||||
34 | * @return \Prado\Data\TDbConnection database connection that may be used to retrieve data from database |
||||
35 | */ |
||||
36 | public function getDbConnection() |
||||
37 | { |
||||
38 | if ($this->_connection === null) { |
||||
39 | $userManager = $this->getManager(); |
||||
40 | if ($userManager instanceof TDbUserManager) { |
||||
41 | $connection = $userManager->getDbConnection(); |
||||
42 | if ($connection instanceof TDbConnection) { |
||||
0 ignored issues
–
show
introduced
by
![]() |
|||||
43 | $connection->setActive(true); |
||||
44 | $this->_connection = $connection; |
||||
45 | } |
||||
46 | } |
||||
47 | if ($this->_connection === null) { |
||||
48 | throw new TConfigurationException('dbuser_dbconnection_invalid'); |
||||
49 | } |
||||
50 | } |
||||
51 | return $this->_connection; |
||||
52 | } |
||||
53 | |||||
54 | /** |
||||
55 | * Validates if username and password are correct entries. |
||||
56 | * Usually, this is accomplished by checking if the user database |
||||
57 | * contains this (username, password) pair. |
||||
58 | * You may use {@see getDbConnection DbConnection} to deal with database. |
||||
59 | * @param string $username username (case-sensitive) |
||||
60 | * @param string $password password |
||||
61 | * @return bool whether the validation succeeds |
||||
62 | */ |
||||
63 | abstract public function validateUser($username, #[\SensitiveParameter] $password); |
||||
64 | |||||
65 | /** |
||||
66 | * Creates a new user instance given the username. |
||||
67 | * This method usually needs to retrieve necessary user information |
||||
68 | * (e.g. role, name, rank, etc.) from the user database according to |
||||
69 | * the specified username. The newly created user instance should be |
||||
70 | * initialized with these information. |
||||
71 | * |
||||
72 | * If the username is invalid (not found in the user database), null |
||||
73 | * should be returned. |
||||
74 | * |
||||
75 | * You may use {@see getDbConnection DbConnection} to deal with database. |
||||
76 | * |
||||
77 | * @param string $username username (case-sensitive) |
||||
78 | * @return TDbUser the newly created and initialized user instance |
||||
79 | */ |
||||
80 | abstract public function createUser($username); |
||||
81 | |||||
82 | /** |
||||
83 | * Creates a new user instance given the cookie containing auth data. |
||||
84 | * |
||||
85 | * This method is invoked when {@see \Prado\Security\TAuthManager::setAllowAutoLogin AllowAutoLogin} is set true. |
||||
86 | * The default implementation simply returns null, meaning no user instance can be created |
||||
87 | * from the given cookie. |
||||
88 | * |
||||
89 | * If you want to support automatic login (remember login), you should override this method. |
||||
90 | * Typically, you obtain the username and a unique token from the cookie's value. |
||||
91 | * You then verify the token is valid and use the username to create a user instance. |
||||
92 | * |
||||
93 | * @param \Prado\Web\THttpCookie $cookie the cookie storing user authentication information |
||||
94 | * @return TDbUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data. |
||||
95 | * @see saveUserToCookie |
||||
96 | * @since 3.1.1 |
||||
97 | */ |
||||
98 | public function createUserFromCookie($cookie) |
||||
0 ignored issues
–
show
The parameter
$cookie is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
99 | { |
||||
100 | return null; |
||||
101 | } |
||||
102 | |||||
103 | /** |
||||
104 | * Saves necessary auth data into a cookie. |
||||
105 | * This method is invoked when {@see \Prado\Security\TAuthManager::setAllowAutoLogin AllowAutoLogin} is set true. |
||||
106 | * The default implementation does nothing, meaning auth data is not stored in the cookie |
||||
107 | * (and thus automatic login is not supported.) |
||||
108 | * |
||||
109 | * If you want to support automatic login (remember login), you should override this method. |
||||
110 | * Typically, you generate a unique token according to the current login information |
||||
111 | * and save it together with the username in the cookie's value. |
||||
112 | * You should avoid revealing the password in the generated token. |
||||
113 | * |
||||
114 | * @param \Prado\Web\THttpCookie $cookie the cookie to store the user auth information |
||||
115 | * @see createUserFromCookie |
||||
116 | * @since 3.1.1 |
||||
117 | */ |
||||
118 | public function saveUserToCookie($cookie) |
||||
0 ignored issues
–
show
The parameter
$cookie is not used and could be removed.
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for parameters that have been defined for a function or method, but which are not used in the method body. ![]() |
|||||
119 | { |
||||
120 | } |
||||
121 | } |
||||
122 |