[ Index ] |
|
Code source de PRADO 3.0.6 |
1 <?php 2 /** 3 * TUserManager class 4 * 5 * @author Qiang Xue <qiang.xue@gmail.com> 6 * @link http://www.pradosoft.com/ 7 * @copyright Copyright © 2005 PradoSoft 8 * @license http://www.pradosoft.com/license/ 9 * @version $Id: TUserManager.php 1397 2006-09-07 07:55:53Z wei $ 10 * @package System.Security 11 */ 12 13 /** 14 * Using TUser class 15 */ 16 Prado::using('System.Security.TUser'); 17 18 /** 19 * TUserManager class 20 * 21 * TUserManager manages a static list of users {@link TUser}. 22 * The user information is specified via module configuration using the following XML syntax, 23 * <code> 24 * <module id="users" class="System.Security.TUserManager" PasswordMode="Clear"> 25 * <user name="Joe" password="demo" /> 26 * <user name="John" password="demo" /> 27 * <role name="Administrator" users="John" /> 28 * <role name="Writer" users="Joe,John" /> 29 * </module> 30 * </code> 31 * 32 * In addition, user information can also be loaded from an external file 33 * specified by {@link setUserFile UserFile} property. Note, the property 34 * only accepts a file path in namespace format. The user file format is 35 * similar to the above sample. 36 * 37 * The user passwords may be specified as clear text, SH1 or MD5 hashed by setting 38 * {@link setPasswordMode PasswordMode} as <b>Clear</b>, <b>SHA1</b> or <b>MD5</b>. 39 * The default name for a guest user is <b>Guest</b>. It may be changed 40 * by setting {@link setGuestName GuestName} property. 41 * 42 * TUserManager may be used together with {@link TAuthManager} which manages 43 * how users are authenticated and authorized in a Prado application. 44 * 45 * @author Qiang Xue <qiang.xue@gmail.com> 46 * @version $Id: TUserManager.php 1397 2006-09-07 07:55:53Z wei $ 47 * @package System.Security 48 * @since 3.0 49 */ 50 class TUserManager extends TModule implements IUserManager 51 { 52 /** 53 * extension name to the user file 54 */ 55 const USER_FILE_EXT='.xml'; 56 57 /** 58 * @var array list of users managed by this module 59 */ 60 private $_users=array(); 61 /** 62 * @var array list of roles managed by this module 63 */ 64 private $_roles=array(); 65 /** 66 * @var string guest name 67 */ 68 private $_guestName='Guest'; 69 /** 70 * @var TUserManagerPasswordMode password mode 71 */ 72 private $_passwordMode=TUserManagerPasswordMode::MD5; 73 /** 74 * @var boolean whether the module has been initialized 75 */ 76 private $_initialized=false; 77 /** 78 * @var string user/role information file 79 */ 80 private $_userFile=null; 81 82 /** 83 * Initializes the module. 84 * This method is required by IModule and is invoked by application. 85 * It loads user/role information from the module configuration. 86 * @param TXmlElement module configuration 87 */ 88 public function init($config) 89 { 90 $this->loadUserData($config); 91 if($this->_userFile!==null) 92 { 93 $dom=new TXmlDocument; 94 $dom->loadFromFile($this->_userFile); 95 $this->loadUserData($dom); 96 } 97 $this->_initialized=true; 98 } 99 100 /** 101 * Loads user/role information from an XML node. 102 * @param TXmlElement the XML node containing the user information 103 */ 104 private function loadUserData($xmlNode) 105 { 106 foreach($xmlNode->getElementsByTagName('user') as $node) 107 { 108 $name=strtolower($node->getAttribute('name')); 109 $this->_users[$name]=$node->getAttribute('password'); 110 if(($roles=trim($node->getAttribute('roles')))!=='') 111 { 112 foreach(explode(',',$roles) as $role) 113 $this->_roles[$name][]=$role; 114 } 115 } 116 foreach($xmlNode->getElementsByTagName('role') as $node) 117 { 118 foreach(explode(',',$node->getAttribute('users')) as $user) 119 { 120 if(($user=trim($user))!=='') 121 $this->_roles[strtolower($user)][]=$node->getAttribute('name'); 122 } 123 } 124 } 125 126 /** 127 * Returns an array of all users. 128 * Each array element represents a single user. 129 * The array key is the username in lower case, and the array value is the 130 * corresponding user password. 131 * @return array list of users 132 */ 133 public function getUsers() 134 { 135 return $this->_users; 136 } 137 138 /** 139 * Returns an array of user role information. 140 * Each array element represents the roles for a single user. 141 * The array key is the username in lower case, and the array value is 142 * the roles (represented as an array) that the user is in. 143 * @return array list of user role information 144 */ 145 public function getRoles() 146 { 147 return $this->_roles; 148 } 149 150 /** 151 * @return string the full path to the file storing user/role information 152 */ 153 public function getUserFile() 154 { 155 return $this->_userFile; 156 } 157 158 /** 159 * @param string user/role data file path (in namespace form). The file format is XML 160 * whose content is similar to that user/role block in application configuration. 161 * @throws TInvalidOperationException if the module is already initialized 162 * @throws TConfigurationException if the file is not in proper namespace format 163 */ 164 public function setUserFile($value) 165 { 166 if($this->_initialized) 167 throw new TInvalidOperationException('usermanager_userfile_unchangeable'); 168 else if(($this->_userFile=Prado::getPathOfNamespace($value,self::USER_FILE_EXT))===null || !is_file($this->_userFile)) 169 throw new TConfigurationException('usermanager_userfile_invalid',$value); 170 } 171 172 /** 173 * @return string guest name, defaults to 'Guest' 174 */ 175 public function getGuestName() 176 { 177 return $this->_guestName; 178 } 179 180 /** 181 * @param string name to be used for guest users. 182 */ 183 public function setGuestName($value) 184 { 185 $this->_guestName=$value; 186 } 187 188 /** 189 * @return TUserManagerPasswordMode how password is stored, clear text, or MD5 or SHA1 hashed. Default to TUserManagerPasswordMode::MD5. 190 */ 191 public function getPasswordMode() 192 { 193 return $this->_passwordMode; 194 } 195 196 /** 197 * @param TUserManagerPasswordMode how password is stored, clear text, or MD5 or SHA1 hashed. 198 */ 199 public function setPasswordMode($value) 200 { 201 $this->_passwordMode=TPropertyValue::ensureEnum($value,'TUserManagerPasswordMode'); 202 } 203 204 /** 205 * Validates if the username and password are correct. 206 * @param string user name 207 * @param string password 208 * @return boolean true if validation is successful, false otherwise. 209 */ 210 public function validateUser($username,$password) 211 { 212 if($this->_passwordMode===TUserManagerPasswordMode::MD5) 213 $password=md5($password); 214 else if($this->_passwordMode===TUserManagerPasswordMode::SHA1) 215 $password=sha1($password); 216 $username=strtolower($username); 217 return (isset($this->_users[$username]) && $this->_users[$username]===$password); 218 } 219 220 /** 221 * Returns a user instance given the user name. 222 * @param string user name, null if it is a guest. 223 * @return TUser the user instance, null if the specified username is not in the user database. 224 */ 225 public function getUser($username=null) 226 { 227 if($username===null) 228 { 229 $user=new TUser($this); 230 $user->setIsGuest(true); 231 return $user; 232 } 233 else 234 { 235 $username=strtolower($username); 236 if(isset($this->_users[$username])) 237 { 238 $user=new TUser($this); 239 $user->setName($username); 240 $user->setIsGuest(false); 241 if(isset($this->_roles[$username])) 242 $user->setRoles($this->_roles[$username]); 243 return $user; 244 } 245 else 246 return null; 247 } 248 } 249 250 /** 251 * Sets a user as a guest. 252 * User name is changed as guest name, and roles are emptied. 253 * @param TUser the user to be changed to a guest. 254 */ 255 public function switchToGuest($user) 256 { 257 $user->setIsGuest(true); 258 } 259 } 260 261 /** 262 * TUserManagerPasswordMode class. 263 * TUserManagerPasswordMode defines the enumerable type for the possible modes 264 * that user passwords can be specified for a {@link TUserManager}. 265 * 266 * The following enumerable values are defined: 267 * - Clear: the password is in plain text 268 * - MD5: the password is recorded as the MD5 hash value of the original password 269 * - SHA1: the password is recorded as the SHA1 hash value of the original password 270 * 271 * @author Qiang Xue <qiang.xue@gmail.com> 272 * @version $Id: TUserManager.php 1397 2006-09-07 07:55:53Z wei $ 273 * @package System.Security 274 * @since 3.0.4 275 */ 276 class TUserManagerPasswordMode extends TEnumerable 277 { 278 const Clear='Clear'; 279 const MD5='MD5'; 280 const SHA1='SHA1'; 281 } 282 283 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Sun Feb 25 21:07:04 2007 | par Balluche grâce à PHPXref 0.7 |