Changeset 650 for trunk/Modules/User/User.php
- Timestamp:
- Mar 27, 2014, 10:53:03 PM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/User/User.php
r634 r650 35 35 return(sha1(sha1($Password).$Salt)); 36 36 } 37 37 38 38 function Verify($Password, $Salt, $StoredHash) 39 39 { 40 40 return($this->Hash($Password, $Salt) == $StoredHash); 41 41 } 42 42 43 43 function GetSalt() 44 44 { … … 48 48 } 49 49 50 // TODO: Make User class more general without dependencies to System, Mail, Log 50 // TODO: Make User class more general without dependencies to System, Mail, Log 51 51 52 52 class User extends Model … … 60 60 /** @var Password */ 61 61 var $PasswordHash; 62 62 63 63 function __construct($System) 64 64 { … … 77 77 // Refresh time of last access 78 78 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('ActivityTime' => 'NOW()')); 79 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 80 'User' => null, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 81 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 79 } else $this->Database->insert('UserOnline', array('SessionId' => $SID, 80 'User' => null, 'LoginTime' => 'NOW()', 'ActivityTime' => 'NOW()', 81 'IpAddress' => GetRemoteAddress(), 'HostName' => gethostbyaddr(GetRemoteAddress()), 82 82 'ScriptName' => $_SERVER['PHP_SELF'])); 83 83 … … 91 91 $DbRow = $DbResult->fetch_assoc(); 92 92 if(sha1($_COOKIE['LoginUserId'].$DbRow['StayLoggedHash']) == $_COOKIE['LoginHash']) 93 { 93 { 94 94 $this->Database->query('DELETE FROM `UserOnline` WHERE `SessionId`="'.$SID.'"'); 95 95 $this->Database->query('UPDATE `UserOnline` SET `SessionId`="'.$SID.'" WHERE `Id`='.$DbRow['Id']); … … 97 97 } 98 98 } 99 99 100 100 // Check login 101 101 $Query = $this->Database->select('UserOnline', '*', '`SessionId`="'.$SID.'"'); 102 102 $Row = $Query->fetch_assoc(); 103 if($Row['User'] != '') 103 if($Row['User'] != '') 104 104 { 105 105 $Query = $this->Database->query('SELECT `User`.*, `UserCustomerRel`.`Customer` AS `Member` FROM `User` '. 106 ' LEFT JOIN `UserCustomerRel` ON `UserCustomerRel`.`User`=`User`.`Id` WHERE `User`.`Id`='.$Row['User']); 106 ' LEFT JOIN `UserCustomerRel` ON `UserCustomerRel`.`User`=`User`.`Id` WHERE `User`.`Id`='.$Row['User']); 107 107 $this->User = $Query->fetch_assoc(); 108 108 $Result = USER_LOGGED; 109 } else 109 } else 110 110 { 111 111 $Query = $this->Database->select('User', '*', 'Id IS NULL'); … … 141 141 if($Query->num_rows > 0) $Result = NAME_USED; 142 142 else 143 { 143 { 144 144 $Query = $this->Database->select('User', '*', 'Email = "'.$Email.'"'); 145 145 if($Query->num_rows > 0) $Result = EMAIL_USED; … … 148 148 $PasswordHash = new PasswordHash(); 149 149 $Salt = $PasswordHash->GetSalt(); 150 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 151 'Password' => $PasswordHash->Hash($Password, $Salt), 'Salt' => $Salt, 152 'Email' => $Email, 'RegistrationTime' => 'NOW()', 150 $this->Database->insert('User', array('Name' => $Name, 'Login' => $Login, 151 'Password' => $PasswordHash->Hash($Password, $Salt), 'Salt' => $Salt, 152 'Email' => $Email, 'RegistrationTime' => 'NOW()', 153 153 'Locked' => 1, 'PhoneNumber' => $PhoneNumber, 'ICQ' => $ICQ)); 154 154 $UserId = $this->Database->insert_id; 155 $this->Database->insert('PermissionUserAssignment', array('User' => $UserId, 155 $this->Database->insert('PermissionUserAssignment', array('User' => $UserId, 156 156 'AssignedGroup' => 2)); 157 157 158 158 $NewPassword = substr(sha1(strtoupper($Login)), 0, 7); 159 159 160 160 // Send activation mail to user email 161 161 $ServerURL = 'http://'.$this->System->Config['Web']['Host'].$this->System->Config['Web']['RootFolder']; … … 172 172 $Mail->From = $this->System->Config['Web']['Title'].' <noreplay@zdechov.net>'; 173 173 $Mail->Send(); 174 174 175 175 $Result = USER_REGISTRATED; 176 176 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'NewRegistration', $Login); … … 210 210 if(!$PasswordHash->Verify($Password, $Row['Salt'], $Row['Password'])) $Result = BAD_PASSWORD; 211 211 else if($Row['Locked'] == 1) $Result = ACCOUNT_LOCKED; 212 else 213 { 214 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 212 else 213 { 214 $this->Database->update('User', 'Id='.$Row['Id'], array('LastLoginTime' => 'NOW()', 215 215 'LastIpAddress' => GetRemoteAddress())); 216 $Hash = new PasswordHash(); 216 $Hash = new PasswordHash(); 217 217 $StayLoggedSalt = $Hash->GetSalt(); 218 218 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array( 219 219 'User' => $Row['Id'], 'StayLogged' => $StayLogged, 'StayLoggedHash' => $StayLoggedSalt)); 220 if($StayLogged) 220 if($StayLogged) 221 221 { 222 setcookie('LoginUserId', $Row['Id'], time()+365*24*60*60 );223 setcookie('LoginHash', sha1($Row['Id'].$StayLoggedSalt), time()+365*24*60*60 );222 setcookie('LoginUserId', $Row['Id'], time()+365*24*60*60, $this->System->Link('/')); 223 setcookie('LoginHash', sha1($Row['Id'].$StayLoggedSalt), time()+365*24*60*60, $this->System->Link('/')); 224 224 } else { 225 setcookie('LoginUserId', '', time() - 3600 );226 setcookie('LoginHash', '', time() - 3600 );225 setcookie('LoginUserId', '', time() - 3600, $this->System->Link('/')); 226 setcookie('LoginHash', '', time() - 3600, $this->System->Link('/')); 227 227 } 228 228 229 229 $Result = USER_LOGGED_IN; 230 230 $this->Check(); … … 281 281 if(array_key_exists($GroupId, $this->PermissionGroupCache)) 282 282 { 283 $PermissionExists = true; 284 } else 285 { 286 // If no permission combination exists in cache, do new check of database items 287 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" AND `AssignedGroup` IS NOT NULL'); 283 $PermissionExists = true; 284 } else 285 { 286 // If no permission combination exists in cache, do new check of database items 287 $DbResult = $this->Database->select('PermissionGroupAssignment', '*', '`Group`="'.$GroupId.'" 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 $PermissionExists = true; 291 $PermissionExists = true; 292 292 } 293 293 if($PermissionExists) … … 303 303 if(array_key_exists($GroupId.','.$OperationId, $this->PermissionGroupCacheOp)) 304 304 { 305 $PermissionExists = true; 306 } else 307 { 308 // If no permission combination exists in cache, do new check of database items 305 $PermissionExists = true; 306 } else 307 { 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 310 if($DbResult->num_rows > 0) $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = true; 311 311 else $this->PermissionGroupCacheOp[$GroupId.','.$OperationId] = false; 312 $PermissionExists = true; 312 $PermissionExists = true; 313 313 } 314 314 if($PermissionExists) … … 328 328 $ModuleId = $DbRow['Id']; 329 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); 335 335 $PermissionExists = is_numeric($OperationId); 336 } else 337 { 336 } else 337 { 338 338 // If no permission combination exists in cache, do new check of database items 339 339 $DbResult = $this->Database->select('PermissionOperation', 'Id', '`Module`="'.$ModuleId.'" AND `Item`="'.$ItemType.'" AND `ItemId`='.$ItemIndex.' AND `Operation`="'.$Operation.'"'); … … 344 344 $this->PermissionCache[$DbRow['Id']] = array($Module, $Operation, $ItemType, $ItemIndex); 345 345 $PermissionExists = true; 346 } else 347 { 348 $this->PermissionCache[count($this->PermissionCache).'_'] = array($Module, $Operation, $ItemType, $ItemIndex); 346 } else 347 { 348 $this->PermissionCache[count($this->PermissionCache).'_'] = array($Module, $Operation, $ItemType, $ItemIndex); 349 349 $PermissionExists = false; 350 350 } 351 } 351 } 352 352 353 353 if($PermissionExists) … … 356 356 else $UserCondition = '(`User`="'.$this->User['Id'].'")'; 357 357 // Check user-operation relation 358 $DbResult = $this->Database->select('PermissionUserAssignment', '*', $UserCondition.' AND (`AssignedOperation`="'.$OperationId.'")'); 358 $DbResult = $this->Database->select('PermissionUserAssignment', '*', $UserCondition.' AND (`AssignedOperation`="'.$OperationId.'")'); 359 359 if($DbResult->num_rows > 0) return(true); 360 360 … … 389 389 "Po přihlášení si prosím změňte heslo na nové.\n\n<br><br>Na tento email neodpovídejte.", 'text/html'); 390 390 $Mail->Send(); 391 391 392 392 $Output = USER_PASSWORD_RECOVERY_SUCCESS; 393 393 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email); … … 407 407 $PasswordHash = new PasswordHash(); 408 408 $Salt = $PasswordHash->GetSalt(); 409 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => $PasswordHash->Hash($NewPassword, $Salt), 409 $this->Database->update('User', 'Id='.$Row['Id'], array('Password' => $PasswordHash->Hash($NewPassword, $Salt), 410 410 'Salt' => $Salt, 'Locked' => 0)); 411 411 $Output = USER_PASSWORD_RECOVERY_CONFIRMED; … … 420 420 { 421 421 var $UserPanel; 422 422 423 423 function __construct($System) 424 424 { … … 431 431 $this->Dependencies = array(); 432 432 $this->UserPanel = array(); 433 } 433 } 434 434 435 435 function DoInstall() … … 470 470 PRIMARY KEY (`Id`) 471 471 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"); 472 472 473 473 $this->Database->query("CREATE TABLE IF NOT EXISTS `PermissionGroupAssignment` ( 474 474 `Id` int(11) NOT NULL AUTO_INCREMENT, … … 481 481 KEY `AssignedOperation` (`AssignedOperation`) 482 482 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"); 483 483 484 484 $this->Database->query("CREATE TABLE IF NOT EXISTS `PermissionOperation` ( 485 485 `Id` int(11) NOT NULL AUTO_INCREMENT, … … 494 494 KEY `ItemId` (`ItemId`) 495 495 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"); 496 496 497 497 $this->Database->query("CREATE TABLE IF NOT EXISTS `PermissionUserAssignment` ( 498 498 `Id` int(11) NOT NULL AUTO_INCREMENT, … … 505 505 KEY `AssignedOperation` (`AssignedOperation`) 506 506 ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;"); 507 507 508 508 $this->Database->query("ALTER TABLE `PermissionGroupAssignment` 509 509 ADD CONSTRAINT `PermissionGroupAssignment_ibfk_1` FOREIGN KEY (`Group`) REFERENCES `PermissionGroup` (`Id`), 510 510 ADD CONSTRAINT `PermissionGroupAssignment_ibfk_2` FOREIGN KEY (`AssignedGroup`) REFERENCES `PermissionGroup` (`Id`), 511 511 ADD CONSTRAINT `PermissionGroupAssignment_ibfk_3` FOREIGN KEY (`AssignedOperation`) REFERENCES `PermissionOperation` (`Id`);"); 512 512 513 513 $this->Database->query("ALTER TABLE `PermissionOperation` 514 514 ADD CONSTRAINT `PermissionOperation_ibfk_1` FOREIGN KEY (`Module`) REFERENCES `Module` (`Id`);"); 515 515 516 516 $this->Database->query("ALTER TABLE `PermissionUserAssignment` 517 517 ADD CONSTRAINT `PermissionUserAssignment_ibfk_2` FOREIGN KEY (`AssignedGroup`) REFERENCES `PermissionGroup` (`Id`), … … 519 519 ADD CONSTRAINT `PermissionUserAssignment_ibfk_4` FOREIGN KEY (`User`) REFERENCES `User` (`Id`);"); 520 520 } 521 521 522 522 function DoUninstall() 523 { 523 { 524 524 $this->Database->query('DROP TABLE `PermissionUserAssignment`'); 525 525 $this->Database->query('DROP TABLE `PermissionGroupAssignment`'); … … 529 529 $this->Database->query('DROP TABLE `User`'); 530 530 } 531 531 532 532 function DoStart() 533 533 { … … 583 583 'Email' => array('Type' => 'String', 'Caption' => 'E-mail', 'Default' => ''), 584 584 ), 585 )); 585 )); 586 586 $this->System->FormManager->RegisterClass('User', array( 587 587 'Title' => 'Uživatelé', … … 642 642 ), 643 643 )); 644 645 } 646 644 645 } 646 647 647 function DoStop() 648 { 649 } 650 648 { 649 } 650 651 651 function TopBarCallback() 652 { 652 { 653 653 if($this->System->User->User['Id'] == null) 654 654 $Output = '<a href="'.$this->System->Link('/user/?Action=LoginForm').'">Přihlášení</a> '. 655 '<a href="'.$this->System->Link('/user/?Action=UserRegister').'">Registrace</a>'; 655 '<a href="'.$this->System->Link('/user/?Action=UserRegister').'">Registrace</a>'; 656 656 else $Output = $this->System->User->User['Name']. 657 657 ' <a href="'.$this->System->Link('/user/?Action=UserMenu').'">Nabídka</a>'. 658 658 ' <a href="'.$this->System->Link('/user/?Action=Logout').'">Odhlásit</a>'; 659 659 // <a href="'.$this->System->Link('/?Action=UserOptions').'">Nastavení</a>'; 660 return($Output); 660 return($Output); 661 661 } 662 662 }
Note:
See TracChangeset
for help on using the changeset viewer.