Changeset 873 for trunk/Modules/User/UserModel.php
- Timestamp:
- Apr 6, 2020, 11:17:40 PM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/User/UserModel.php
r828 r873 30 30 function Hash($Password, $Salt) 31 31 { 32 return (sha1(sha1($Password).$Salt));32 return (sha1(sha1($Password).$Salt)); 33 33 } 34 34 35 35 function Verify($Password, $Salt, $StoredHash) 36 36 { 37 return ($this->Hash($Password, $Salt) == $StoredHash);37 return ($this->Hash($Password, $Salt) == $StoredHash); 38 38 } 39 39 … … 71 71 // Lookup user record 72 72 $Query = $this->Database->select('UserOnline', '*', 'SessionId="'.$SID.'"'); 73 if ($Query->num_rows > 0)73 if ($Query->num_rows > 0) 74 74 { 75 75 // Refresh time of last access … … 81 81 82 82 // Logged permanently? 83 if (array_key_exists('LoginHash', $_COOKIE))83 if (array_key_exists('LoginHash', $_COOKIE)) 84 84 { 85 85 $DbResult = $this->Database->query('SELECT * FROM `UserOnline` WHERE `User`='.$_COOKIE['LoginUserId']. 86 86 ' AND `StayLogged`=1 AND SessionId!="'.$SID.'"'); 87 if ($DbResult->num_rows > 0)87 if ($DbResult->num_rows > 0) 88 88 { 89 89 $DbRow = $DbResult->fetch_assoc(); 90 if (sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash'])90 if (sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash']) 91 91 { 92 92 $this->Database->query('DELETE FROM `UserOnline` WHERE `SessionId`="'.$SID.'"'); … … 99 99 $Query = $this->Database->select('UserOnline', '*', '`SessionId`="'.$SID.'"'); 100 100 $Row = $Query->fetch_assoc(); 101 if ($Row['User'] != '')101 if ($Row['User'] != '') 102 102 { 103 103 $Query = $this->Database->query('SELECT `User`.*, `UserCustomerRel`.`Customer` AS `Member` FROM `User` '. … … 114 114 // Remove nonactive users 115 115 $DbResult = $this->Database->select('UserOnline', '`Id`, `User`', '(`ActivityTime` < DATE_SUB(NOW(), INTERVAL '.$this->OnlineStateTimeout.' SECOND)) AND (`StayLogged` = 0)'); 116 while ($DbRow = $DbResult->fetch_array())116 while ($DbRow = $DbResult->fetch_array()) 117 117 { 118 118 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']); 119 if ($DbRow['User'] != null) $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout');119 if ($DbRow['User'] != null) $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout'); 120 120 } 121 121 //$this->LoadPermission($this->User['Role']); … … 127 127 function Register($Login, $Password, $Password2, $Email, $Name) 128 128 { 129 if (($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING;130 else if ($Password != $Password2) $Result = PASSWORDS_UNMATCHED;129 if (($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING; 130 else if ($Password != $Password2) $Result = PASSWORDS_UNMATCHED; 131 131 else 132 132 { 133 133 // Is user registred yet? 134 134 $Query = $this->Database->select('User', '*', 'Login = "'.$Login.'"'); 135 if ($Query->num_rows > 0) $Result = LOGIN_USED;135 if ($Query->num_rows > 0) $Result = LOGIN_USED; 136 136 else 137 137 { 138 138 $Query = $this->Database->select('User', '*', 'Name = "'.$Name.'"'); 139 if ($Query->num_rows > 0) $Result = NAME_USED;139 if ($Query->num_rows > 0) $Result = NAME_USED; 140 140 else 141 141 { 142 142 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"'); 143 if ($Query->num_rows > 0) $Result = EMAIL_USED;143 if ($Query->num_rows > 0) $Result = EMAIL_USED; 144 144 else 145 145 { … … 177 177 } 178 178 } 179 return ($Result);179 return ($Result); 180 180 } 181 181 … … 183 183 { 184 184 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id); 185 if ($DbResult->num_rows > 0)185 if ($DbResult->num_rows > 0) 186 186 { 187 187 $Row = $DbResult->fetch_array(); 188 188 $NewPassword = substr(sha1(strtoupper($Row['Login'])), 0, 7); 189 if ($Hash == $NewPassword)189 if ($Hash == $NewPassword) 190 190 { 191 191 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0)); … … 195 195 } else $Output = PASSWORDS_UNMATCHED; 196 196 } else $Output = USER_NOT_FOUND; 197 return ($Output);197 return ($Output); 198 198 } 199 199 200 200 function Login($Login, $Password, $StayLogged = false) 201 201 { 202 if ($StayLogged) $StayLogged = 1; else $StayLogged = 0;202 if ($StayLogged) $StayLogged = 1; else $StayLogged = 0; 203 203 $SID = session_id(); 204 204 $Query = $this->Database->select('User', '*', 'Login="'.$Login.'"'); 205 if ($Query->num_rows > 0)205 if ($Query->num_rows > 0) 206 206 { 207 207 $Row = $Query->fetch_assoc(); 208 208 $PasswordHash = new PasswordHash(); 209 if (!$PasswordHash->Verify($Password, $Row['Salt'], $Row['Password'])) $Result = BAD_PASSWORD;210 else if ($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED;209 if (!$PasswordHash->Verify($Password, $Row['Salt'], $Row['Password'])) $Result = BAD_PASSWORD; 210 else if ($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED; 211 211 else 212 212 { … … 217 217 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array( 218 218 'User' => $Row['Id'], 'StayLogged' => $StayLogged, 'StayLoggedHash' => $StayLoggedSalt)); 219 if ($StayLogged)219 if ($StayLogged) 220 220 { 221 221 setcookie('LoginUserId', $Row['Id'], time()+365*24*60*60, $this->System->Link('/')); … … 231 231 } 232 232 } else $Result = USER_NOT_REGISTRED; 233 return ($Result);233 return ($Result); 234 234 } 235 235 … … 240 240 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']); 241 241 $this->Check(); 242 return (USER_LOGGED_OUT);242 return (USER_LOGGED_OUT); 243 243 } 244 244 … … 247 247 $this->Roles = array(); 248 248 $DbResult = $this->Database->select('UserRole', '*'); 249 while ($DbRow = $DbResult->fetch_array())249 while ($DbRow = $DbResult->fetch_array()) 250 250 $this->Roles[] = $DbRow; 251 251 } … … 255 255 $this->User['Permission'] = array(); 256 256 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description` FROM `UserRolePermission` JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` WHERE `UserRolePermission`.`Role` = '.$Role); 257 if ($DbResult->num_rows > 0)258 while ($DbRow = $DbResult->fetch_array())257 if ($DbResult->num_rows > 0) 258 while ($DbRow = $DbResult->fetch_array()) 259 259 $this->User['Permission'][$DbRow['Operation']] = $DbRow; 260 260 } … … 264 264 $Result = array(); 265 265 $DbResult = $this->Database->query('SELECT `UserRolePermission`.*, `PermissionOperation`.`Description`, `UserRole`.`Title` FROM `UserRolePermission` LEFT JOIN `PermissionOperation` ON `PermissionOperation`.`Id` = `UserRolePermission`.`Operation` LEFT JOIN `UserRole` ON `UserRole`.`Id` = `UserRolePermission`.`Role`'); 266 while ($DbRow = $DbResult->fetch_array())266 while ($DbRow = $DbResult->fetch_array()) 267 267 { 268 268 $Value = ''; 269 if ($DbRow['Read']) $Value .= 'R';270 if ($DbRow['Write']) $Value .= 'W';269 if ($DbRow['Read']) $Value .= 'R'; 270 if ($DbRow['Write']) $Value .= 'W'; 271 271 $Result[$DbRow['Description']][$DbRow['Title']] = $Value; 272 272 } 273 return ($Result);273 return ($Result); 274 274 } 275 275 … … 278 278 $PermissionExists = false; 279 279 // First try to check cache group-group relation 280 if (array_key_exists($GroupId, $this->PermissionGroupCache))280 if (array_key_exists($GroupId, $this->PermissionGroupCache)) 281 281 { 282 282 $PermissionExists = true; … … 287 287 '") AND (`AssignedGroup` IS NOT NULL)'); 288 288 $DbRow = array(); 289 while ($DbRow[] = $DbResult->fetch_array());289 while ($DbRow[] = $DbResult->fetch_array()); 290 290 $this->PermissionGroupCache[$GroupId] = $DbRow; 291 291 $PermissionExists = true; 292 292 } 293 if ($PermissionExists)294 { 295 foreach ($this->PermissionGroupCache[$GroupId] as $DbRow)296 { 297 if ($DbRow['AssignedGroup'] != '')298 if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);293 if ($PermissionExists) 294 { 295 foreach ($this->PermissionGroupCache[$GroupId] as $DbRow) 296 { 297 if ($DbRow['AssignedGroup'] != '') 298 if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return (true); 299 299 } 300 300 } 301 301 302 302 // Check group-operation relation 303 if (array_key_exists($GroupId.','.$OperationId, $this->PermissionGroupCacheOp))303 if (array_key_exists($GroupId.','.$OperationId, $this->PermissionGroupCacheOp)) 304 304 { 305 305 $PermissionExists = true; … … 308 308 // If no permission combination exists in cache, do new check of database items 309 309 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `AssignedOperation`="'.$OperationId.'"'); 310 if ($DbResult->num_rows > 0) $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = true;310 if ($DbResult->num_rows > 0) $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = true; 311 311 else $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = false; 312 312 $PermissionExists = true; 313 313 } 314 if ($PermissionExists)315 { 316 return ($this->PermissionGroupCacheOp[$GroupId.','.$OperationId]);317 } 318 return (false);314 if ($PermissionExists) 315 { 316 return ($this->PermissionGroupCacheOp[$GroupId.','.$OperationId]); 317 } 318 return (false); 319 319 } 320 320 … … 323 323 // Get module id 324 324 $DbResult = $this->Database->select('Module', 'Id', '`Name`="'.$Module.'"'); 325 if ($DbResult->num_rows > 0)325 if ($DbResult->num_rows > 0) 326 326 { 327 327 $DbRow = $DbResult->fetch_assoc(); 328 328 $ModuleId = $DbRow['Id']; 329 } else return (false);329 } else return (false); 330 330 331 331 // First try to check cache 332 if (in_array(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache))332 if (in_array(array($Module, $Operation, $ItemType, $ItemType), $this->PermissionCache)) 333 333 { 334 334 $OperationId = array_search(array($Module, $Operation, $ItemType, $ItemIndex), $this->PermissionCache); … … 339 339 $DbResult = $this->Database->select('PermissionOperation', 'Id', '(`Module`="'.$ModuleId. 340 340 '") AND (`Item`="'.$ItemType.'") AND (`ItemId`='.$ItemIndex.') AND (`Operation`="'.$Operation.'")'); 341 if ($DbResult->num_rows > 0)341 if ($DbResult->num_rows > 0) 342 342 { 343 343 $DbRow = $DbResult->fetch_array(); … … 352 352 } 353 353 354 if ($PermissionExists)355 { 356 if ($this->User['Id'] == null) $UserCondition = '(`User` IS NULL)';354 if ($PermissionExists) 355 { 356 if ($this->User['Id'] == null) $UserCondition = '(`User` IS NULL)'; 357 357 else $UserCondition = '(`User`="'.$this->User['Id'].'")'; 358 358 // Check user-operation relation 359 359 $DbResult = $this->Database->select('PermissionUserAssignment', '*', $UserCondition.' AND (`AssignedOperation`="'.$OperationId.'")'); 360 if ($DbResult->num_rows > 0) return(true);360 if ($DbResult->num_rows > 0) return (true); 361 361 362 362 // Check user-group relation 363 363 $DbResult = $this->Database->select('PermissionUserAssignment', 'AssignedGroup', $UserCondition); 364 while ($DbRow = $DbResult->fetch_array())365 { 366 if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return(true);367 } 368 return (false);369 } else return (false);364 while ($DbRow = $DbResult->fetch_array()) 365 { 366 if ($this->CheckGroupPermission($DbRow['AssignedGroup'], $OperationId) == true) return (true); 367 } 368 return (false); 369 } else return (false); 370 370 } 371 371 … … 373 373 { 374 374 $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"'); 375 if ($DbResult->num_rows > 0)375 if ($DbResult->num_rows > 0) 376 376 { 377 377 $Row = $DbResult->fetch_array(); … … 394 394 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email); 395 395 } else $Output = USER_PASSWORD_RECOVERY_FAIL; 396 return ($Output);396 return ($Output); 397 397 } 398 398 … … 400 400 { 401 401 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id); 402 if ($DbResult->num_rows > 0)402 if ($DbResult->num_rows > 0) 403 403 { 404 404 $Row = $DbResult->fetch_array(); 405 405 $NewPassword2 = substr(sha1(strtoupper($Row['Login'])), 0, 7); 406 if (($NewPassword == $NewPassword2) and ($Hash == $Row['Password']))406 if (($NewPassword == $NewPassword2) and ($Hash == $Row['Password'])) 407 407 { 408 408 $PasswordHash = new PasswordHash(); … … 414 414 } else $Output = PASSWORDS_UNMATCHED; 415 415 } else $Output = USER_NOT_FOUND; 416 return ($Output);416 return ($Output); 417 417 } 418 418 … … 420 420 { 421 421 $DbResult = $this->Database->select('APIToken', 'User', '`Token`="'.$Token.'"'); 422 if ($DbResult->num_rows > 0)422 if ($DbResult->num_rows > 0) 423 423 { 424 424 $DbRow = $DbResult->fetch_assoc(); 425 425 $User = new User($this->System); 426 426 $User->User = array('Id' => $DbRow['User']); 427 return ($User->CheckPermission($Module, $Operation));428 } else return (false);427 return ($User->CheckPermission($Module, $Operation)); 428 } else return (false); 429 429 } 430 430 }
Note:
See TracChangeset
for help on using the changeset viewer.