5 * @author Qiang Xue <qiang.xue@gmail.com>
6 * @link http://www.pradosoft.com/
7 * @copyright Copyright © 2005-2013 PradoSoft
8 * @license http://www.pradosoft.com/license/
9 * @version $Id: TDbUserManager.php 3245 2013-01-07 20:23:32Z ctrlaltca $
10 * @package System.Security
14 * Using IUserManager interface
16 Prado::using('System.Security.IUserManager');
17 Prado::using('System.Data.TDataSourceConfig');
18 Prado::using('System.Security.TUser');
21 * TDbUserManager class
23 * TDbUserManager manages user accounts that are stored in a database.
24 * TDbUserManager is mainly designed to be used together with {@link TAuthManager}
25 * which manages how users are authenticated and authorized in a Prado application.
27 * To use TDbUserManager together with TAuthManager, configure them in
28 * the application configuration like following:
31 * class="System.Data.TDataSourceConfig" ..../>
33 * class="System.Security.TDbUserManager"
34 * UserClass="Path.To.MyUserClass"
35 * ConnectionID="db" />
37 * class="System.Security.TAuthManager"
38 * UserManager="users" LoginPage="Path.To.LoginPage" />
41 * In the above, {@link setUserClass UserClass} specifies what class will be used
42 * to create user instance. The class must extend from {@link TDbUser}.
43 * {@link setConnectionID ConnectionID} refers to the ID of a {@link TDataSourceConfig} module
44 * which specifies how to establish database connection to retrieve user information.
46 * @author Qiang Xue <qiang.xue@gmail.com>
47 * @version $Id: TDbUserManager.php 3245 2013-01-07 20:23:32Z ctrlaltca $
48 * @package System.Security
51 class TDbUserManager extends TModule implements IUserManager
55 private $_guestName='Guest';
56 private $_userClass='';
57 private $_userFactory;
60 * Initializes the module.
61 * This method is required by IModule and is invoked by application.
62 * @param TXmlElement module configuration
64 public function init($config)
66 if($this->_userClass==='')
67 throw new TConfigurationException('dbusermanager_userclass_required');
68 $this->_userFactory=Prado::createComponent($this->_userClass,$this);
69 if(!($this->_userFactory instanceof TDbUser))
70 throw new TInvalidDataTypeException('dbusermanager_userclass_invalid',$this->_userClass);
74 * @return string the user class name in namespace format. Defaults to empty string, meaning not set.
76 public function getUserClass()
78 return $this->_userClass;
82 * @param string the user class name in namespace format. The user class must extend from {@link TDbUser}.
84 public function setUserClass($value)
86 $this->_userClass=$value;
90 * @return string guest name, defaults to 'Guest'
92 public function getGuestName()
94 return $this->_guestName;
98 * @param string name to be used for guest users.
100 public function setGuestName($value)
102 $this->_guestName=$value;
106 * Validates if the username and password are correct.
107 * @param string user name
108 * @param string password
109 * @return boolean true if validation is successful, false otherwise.
111 public function validateUser($username,$password)
113 return $this->_userFactory->validateUser($username,$password);
117 * Returns a user instance given the user name.
118 * @param string user name, null if it is a guest.
119 * @return TUser the user instance, null if the specified username is not in the user database.
121 public function getUser($username=null)
125 $user=Prado::createComponent($this->_userClass,$this);
126 $user->setIsGuest(true);
130 return $this->_userFactory->createUser($username);
134 * @return string the ID of a TDataSourceConfig module. Defaults to empty string, meaning not set.
136 public function getConnectionID()
138 return $this->_connID;
142 * Sets the ID of a TDataSourceConfig module.
143 * The datasource module will be used to establish the DB connection
144 * that will be used by the user manager.
145 * @param string module ID.
147 public function setConnectionID($value)
149 $this->_connID=$value;
153 * @return TDbConnection the database connection that may be used to retrieve user data.
155 public function getDbConnection()
157 if($this->_conn===null)
159 $this->_conn=$this->createDbConnection($this->_connID);
160 $this->_conn->setActive(true);
166 * Creates the DB connection.
167 * @param string the module ID for TDataSourceConfig
168 * @return TDbConnection the created DB connection
169 * @throws TConfigurationException if module ID is invalid or empty
171 protected function createDbConnection($connectionID)
173 if($connectionID!=='')
175 $conn=$this->getApplication()->getModule($connectionID);
176 if($conn instanceof TDataSourceConfig)
177 return $conn->getDbConnection();
179 throw new TConfigurationException('dbusermanager_connectionid_invalid',$connectionID);
182 throw new TConfigurationException('dbusermanager_connectionid_required');
186 * Returns a user instance according to auth data stored in a cookie.
187 * @param THttpCookie the cookie storing user authentication information
188 * @return TDbUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data.
191 public function getUserFromCookie($cookie)
193 return $this->_userFactory->createUserFromCookie($cookie);
197 * Saves user auth data into a cookie.
198 * @param THttpCookie the cookie to receive the user auth data.
201 public function saveUserToCookie($cookie)
203 $user=$this->getApplication()->getUser();
204 if($user instanceof TDbUser)
205 $user->saveUserToCookie($cookie);
213 * TDbUser is the base user class for using together with {@link TDbUserManager}.
214 * Two methods are declared and must be implemented in the descendant classes:
215 * - {@link validateUser()}: validates if username and password are correct entries.
216 * - {@link createUser()}: creates a new user instance given the username
218 * @author Qiang Xue <qiang.xue@gmail.com>
219 * @version $Id: TDbUserManager.php 3245 2013-01-07 20:23:32Z ctrlaltca $
220 * @package System.Security
223 abstract class TDbUser extends TUser
225 private $_connection;
228 * Returns a database connection that may be used to retrieve data from database.
230 * @return TDbConnection database connection that may be used to retrieve data from database
232 public function getDbConnection()
234 if($this->_connection===null)
236 $userManager=$this->getManager();
237 if($userManager instanceof TDbUserManager)
239 $connection=$userManager->getDbConnection();
240 if($connection instanceof TDbConnection)
242 $connection->setActive(true);
243 $this->_connection=$connection;
246 if($this->_connection===null)
247 throw new TConfigurationException('dbuser_dbconnection_invalid');
249 return $this->_connection;
253 * Validates if username and password are correct entries.
254 * Usually, this is accomplished by checking if the user database
255 * contains this (username, password) pair.
256 * You may use {@link getDbConnection DbConnection} to deal with database.
257 * @param string username (case-sensitive)
258 * @param string password
259 * @return boolean whether the validation succeeds
261 abstract public function validateUser($username,$password);
264 * Creates a new user instance given the username.
265 * This method usually needs to retrieve necessary user information
266 * (e.g. role, name, rank, etc.) from the user database according to
267 * the specified username. The newly created user instance should be
268 * initialized with these information.
270 * If the username is invalid (not found in the user database), null
271 * should be returned.
273 * You may use {@link getDbConnection DbConnection} to deal with database.
275 * @param string username (case-sensitive)
276 * @return TDbUser the newly created and initialized user instance
278 abstract public function createUser($username);
281 * Creates a new user instance given the cookie containing auth data.
283 * This method is invoked when {@link TAuthManager::setAllowAutoLogin AllowAutoLogin} is set true.
284 * The default implementation simply returns null, meaning no user instance can be created
285 * from the given cookie.
287 * If you want to support automatic login (remember login), you should override this method.
288 * Typically, you obtain the username and a unique token from the cookie's value.
289 * You then verify the token is valid and use the username to create a user instance.
291 * @param THttpCookie the cookie storing user authentication information
292 * @return TDbUser the user instance generated based on the cookie auth data, null if the cookie does not have valid auth data.
293 * @see saveUserToCookie
296 public function createUserFromCookie($cookie)
302 * Saves necessary auth data into a cookie.
303 * This method is invoked when {@link TAuthManager::setAllowAutoLogin AllowAutoLogin} is set true.
304 * The default implementation does nothing, meaning auth data is not stored in the cookie
305 * (and thus automatic login is not supported.)
307 * If you want to support automatic login (remember login), you should override this method.
308 * Typically, you generate a unique token according to the current login information
309 * and save it together with the username in the cookie's value.
310 * You should avoid revealing the password in the generated token.
312 * @param THttpCookie the cookie to store the user auth information
313 * @see createUserFromCookie
316 public function saveUserToCookie($cookie)