[ Index ] |
|
Code source de LifeType 1.2.4 |
1 <?php 2 3 lt_include( PLOG_CLASS_PATH."class/dao/userdata/baseuserdataprovider.class.php" ); 4 lt_include( PLOG_CLASS_PATH."class/dao/userinfo.class.php" ); 5 lt_include( PLOG_CLASS_PATH."class/dao/userstatus.class.php" ); 6 lt_include( PLOG_CLASS_PATH."class/database/db.class.php" ); 7 8 /** 9 * Model representing the users in our application. Provides the methods such as 10 * authentication and querying for users. 11 * 12 * \ingroup User_Data_Providers 13 */ 14 class LifeTypeUserDataProvider extends BaseUserDataProvider 15 { 16 17 /** 18 * Initializes the model 19 */ 20 function LifeTypeUserDataProvider( $providerConfig ) 21 { 22 $this->BaseUserDataProvider( $providerConfig ); 23 24 $this->table = $this->getPrefix()."users"; 25 } 26 27 /** 28 * Returns true if the user is in the database and the username 29 * and password match 30 * 31 * @param user Username of the user who we'd like to authenticate 32 * @param pass Password of the user 33 * @return true if user and password correct or false otherwise. 34 */ 35 function authenticateUser( $user, $pass ) 36 { 37 $user = $this->getUserInfoFromUsername( $user ); 38 if( $user ) { 39 return( $user->getPassword() == md5($pass)); 40 } 41 else { 42 return( false ); 43 } 44 } 45 46 /** 47 * Returns all the information associated to the user given 48 * 49 * @param user Username of the user from who we'd like to get the information 50 * @param pass Password of the user we'd like to get the information 51 * @return Returns a UserInfo object with the requested information, or false otherwise. 52 */ 53 function getUserInfo( $user, $pass ) 54 { 55 $userInfo = $this->getUserInfoFromUsername( $user ); 56 if ( $userInfo && ($userInfo->getPassword() == md5($pass)) ) { 57 return $userInfo; 58 } else { 59 return false; 60 } 61 } 62 63 /** 64 * Retrieves the user information but given only a username 65 * 66 * @param username The username of the user 67 * @return Returns a UserInfo object with the requested information, or false otherwise. 68 */ 69 function getUserInfoFromUsername( $username ) 70 { 71 $user = $this->get( "user", $username, CACHE_USERIDBYNAME, Array( CACHE_USERINFO => "getId" )); 72 if( $user ) { 73 if( $user->getUsername() != $username ) { 74 $user = false; 75 } 76 } 77 78 return( $user ); 79 } 80 81 /** 82 * Retrieves the user infromation but given only a userid 83 * 84 * @param userId User ID of the user from whom we'd like to get the information 85 * @return Returns a UserInfo object with the requested information, or false otherwise. 86 */ 87 function getUserInfoFromId( $userid ) 88 { 89 return( $this->get( "id", $userid, CACHE_USERINFO, Array( CACHE_USERIDBYNAME => "getUsername" ))); 90 } 91 92 /** 93 * Returns an array with all the users available in the database 94 * 95 * @param status 96 * @param searchTerms 97 * @param orderBy 98 * @param page 99 * @param itemsPerPage 100 * @return An array containing all the users. 101 */ 102 function getAllUsers( $status = USER_STATUS_ALL, 103 $searchTerms = "", 104 $orderBy = "", 105 $page = DEFAULT_PAGING_ENABLED, 106 $itemsPerPage = DEFAULT_ITEMS_PER_PAGE ) 107 { 108 $where = ""; 109 110 if( $status != USER_STATUS_ALL ) 111 $where = "status = '".Db::qstr($status)."'"; 112 113 if( $searchTerms != "" ) { 114 if( $where != "" ) 115 $where .= " AND "; 116 $where = $this->getSearchConditions( $searchTerms ); 117 } 118 if( $where != "" ) 119 $where = "WHERE $where"; 120 121 if( $orderBy == "" ) 122 $orderBy = "id ASC"; 123 124 $query = "SELECT * FROM ".$this->getPrefix()."users $where ORDER BY $orderBy"; 125 126 $result = $this->Execute( $query, $page, $itemsPerPage ); 127 128 $users = Array(); 129 130 if( !$result ) 131 return $users; 132 133 while ($row = $result->FetchRow()) { 134 $user = $this->mapRow( $row ); 135 $users[] = $user; 136 // cache the data for later use 137 $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user ); 138 $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user ); 139 } 140 $result->Close(); 141 142 return $users; 143 } 144 145 /** 146 * @see Model::buildSearchCondition 147 */ 148 function buildSearchCondition( $searchTerms ) 149 { 150 $searchTerms = trim( $searchTerms ); 151 $searchCond = "(user LIKE '%".Db::qstr($searchTerms)."%' 152 OR full_name LIKE '%".Db::qstr($searchTerms)."%' OR 153 email LIKE '%".Db::qstr($searchTerms)."%')"; 154 155 return( $searchCond ); 156 } 157 158 /** 159 * Updates the information related to a user 160 * 161 * @param userInfo An UserInfo object containing the <b>already udpated</b> information of the 162 * user we would like to update. 163 * @return Returns true if ok or false otherwise. 164 */ 165 function updateUser( $user ) 166 { 167 $result = $this->update( $user ); 168 169 if( $result ) { 170 // remove the old data 171 $this->_cache->removeData( $user->getId(), CACHE_USERINFO ); 172 $this->_cache->removeData( $user->getUsername(), CACHE_USERIDBYNAME ); 173 } 174 175 BaseUserDataProvider::updateUser( $user ); 176 177 return $result; 178 } 179 180 /** 181 * Adds a user to the database. 182 * 183 * @param user An UserInfo object with the necessary information 184 * @return Returns the identifier assigned to the user, or false if there was any error. It will also modify the 185 * UserInfo object passed by parameter and set its database id. 186 */ 187 function addUser( &$user ) 188 { 189 $userId = $this->add( $user ); 190 191 if( $userId ) { 192 // 1. We need to set the password again in this initial UserInfo object, because 193 // current password is plain password. Through setPassword() we can encrpyt the password 194 // and make the UserInfo object right, then we can cache it. Or user can not login even 195 // we addUser() successfully. 196 // 2. Another easy way to solve this is remove the cache code below, don't cache the UserInfo 197 // Object in the first time. Let it cache later. 198 $user->setMD5Password( $user->getPassword() ); 199 $this->_cache->setData( $user->getId(), CACHE_USERINFO, $user ); 200 $this->_cache->setData( $user->getUsername(), CACHE_USERIDBYNAME, $user ); 201 } 202 203 return( $userId ); 204 } 205 206 /** 207 * Removes users from the database 208 * 209 * @param userId The identifier of the user we are trying to remove 210 */ 211 function deleteUser( $userId ) 212 { 213 // first, delete all of his/her permissions 214 $user = $this->getUserInfoFromId( $userId ); 215 if( $this->delete( "id", $userId )) { 216 lt_include( PLOG_CLASS_PATH."class/dao/userpermissions.class.php" ); 217 $perms = new UserPermissions(); 218 $perms->revokeUserPermissions( $userId ); 219 $this->_cache->removeData( $userId, CACHE_USERINFO ); 220 $this->_cache->removeData( $user->getUsername(), CACHE_USERIDBYNAME ); 221 return true; 222 } 223 224 return false; 225 } 226 227 /** 228 * returns the total number of users 229 * 230 * @return total number of users 231 */ 232 function getNumUsers( $status = USER_STATUS_ALL, $searchTerms = "" ) 233 { 234 $table = $this->getPrefix()."users"; 235 236 $where = ""; 237 if( $status != USER_STATUS_ALL ) 238 $where = "status = '".Db::qstr($status)."'"; 239 240 if( $searchTerms != "" ) { 241 if( $where != "" ) 242 $where .= " AND "; 243 $where = $this->getSearchConditions( $searchTerms ); 244 } 245 246 return( $this->getNumItems( $table, $where )); 247 } 248 249 /** 250 * check if the email account has been registered 251 * @return true if the email account has been registered 252 */ 253 function emailExists($email) 254 { 255 $query = "SELECT email 256 FROM ".$this->getPrefix()."users 257 WHERE email = '".Db::qstr($email)."'"; 258 259 $result = $this->Execute($query); 260 261 if(!$result) 262 return false; 263 264 $count = $result->RecordCount(); 265 $result->Close(); 266 return ($count >= 1); 267 } 268 269 /** 270 * @see Model::getSearchConditions 271 */ 272 function getSearchConditions( $searchTerms ) 273 { 274 lt_include( PLOG_CLASS_PATH."class/dao/searchengine.class.php" ); 275 // prepare the query string 276 $searchTerms = SearchEngine::adaptSearchString( $searchTerms ); 277 278 return( "(user LIKE '%".$searchTerms."%' OR full_name LIKE '%".$searchTerms."%')"); 279 } 280 } 281 ?>
titre
Description
Corps
titre
Description
Corps
titre
Description
Corps
titre
Corps
Généré le : Mon Nov 26 21:04:15 2007 | par Balluche grâce à PHPXref 0.7 |
![]() |