Changeset 887 for trunk/Modules/User
- Timestamp:
- Nov 20, 2020, 12:08:12 AM (4 years ago)
- Location:
- trunk/Modules/User
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Modules/User/User.php
r874 r887 7 7 class ModuleUser extends AppModule 8 8 { 9 var $UserPanel; 10 11 function __construct($System) 9 public array $UserPanel; 10 public User $User; 11 12 function __construct(System $System) 12 13 { 13 14 parent::__construct($System); … … 19 20 $this->Dependencies = array(); 20 21 $this->UserPanel = array(); 21 } 22 23 function DoInstall() 22 $this->User = new User($System); 23 } 24 25 function DoInstall(): void 24 26 { 25 27 $this->Database->query("CREATE TABLE IF NOT EXISTS `User` ( … … 108 110 } 109 111 110 function DoUninstall() 112 function DoUninstall(): void 111 113 { 112 114 $this->Database->query('DROP TABLE `PermissionUserAssignment`'); … … 118 120 } 119 121 120 function DoUpgrade() 122 function DoUpgrade(): void 121 123 { 122 124 /* … … 129 131 } 130 132 131 function DoStart() 132 { 133 $this->System->User = new User($this->System); 134 if (isset($_SERVER['REMOTE_ADDR'])) $this->System->User->Check(); 135 $this->System->RegisterPage('userlist', 'PageUserList'); 136 $this->System->RegisterPage('user', 'PageUser'); 133 function DoStart(): void 134 { 135 if (isset($_SERVER['REMOTE_ADDR'])) $this->User->Check(); 136 $this->System->RegisterPage(['userlist'], 'PageUserList'); 137 $this->System->RegisterPage(['user'], 'PageUser'); 137 138 $this->System->RegisterPageBarItem('Top', 'User', array($this, 'TopBarCallback')); 138 139 $this->System->FormManager->RegisterClass('UserLogin', array( … … 272 273 'Filter' => '1', 273 274 )); 274 $this->System->ModuleManager->Modules['IS']->RegisterDashboardItem('User',275 array('ModuleUser', 'ShowDashboardItem'));276 } 277 278 function ShowDashboardItem() 275 ModuleIS::Cast($this->System->GetModule('IS'))->RegisterDashboardItem('User', 276 array($this, 'ShowDashboardItem')); 277 } 278 279 function ShowDashboardItem(): string 279 280 { 280 281 $DbResult = $this->Database->select('User', 'COUNT(*)', '1'); … … 284 285 } 285 286 286 function DoStop() 287 { 288 } 289 290 function TopBarCallback() 291 { 292 if ($this-> System->User->User['Id'] == null)287 function DoStop(): void 288 { 289 } 290 291 function TopBarCallback(): string 292 { 293 if ($this->User->User['Id'] == null) 293 294 { 294 295 $Output = '<a href="'.$this->System->Link('/user/?Action=LoginForm').'">Přihlášení</a> '. … … 296 297 } else 297 298 { 298 $Output = $this-> System->User->User['Name'].299 $Output = $this->User->User['Name']. 299 300 ' <a href="'.$this->System->Link('/user/?Action=UserMenu').'">Nabídka</a>'. 300 301 ' <a href="'.$this->System->Link('/user/?Action=Logout').'">Odhlásit</a>'; … … 303 304 return $Output; 304 305 } 306 307 static function Cast(AppModule $AppModule): ModuleUser 308 { 309 if ($AppModule instanceof ModuleUser) 310 { 311 return $AppModule; 312 } 313 throw new Exception('Expected ModuleUser type but got '.gettype($AppModule)); 314 } 305 315 } -
trunk/Modules/User/UserList.php
r874 r887 3 3 class PageUserList extends Page 4 4 { 5 var $FullTitle = 'Seznam registrovaných uživatelů'; 6 var $ShortTitle = 'Seznam uživatelů'; 7 var $ParentClass = 'PagePortal'; 5 function __construct(System $System) 6 { 7 parent::__construct($System); 8 $this->FullTitle = 'Seznam registrovaných uživatelů'; 9 $this->ShortTitle = 'Seznam uživatelů'; 10 $this->ParentClass = 'PagePortal'; 11 } 8 12 9 function Show() 13 function Show(): string 10 14 { 11 if (! $this->System->User->CheckPermission('User', 'ShowList'))15 if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('User', 'ShowList')) 12 16 return 'Nemáte oprávnění'; 13 17 -
trunk/Modules/User/UserModel.php
r878 r887 28 28 class PasswordHash 29 29 { 30 function Hash( $Password, $Salt)30 function Hash(string $Password, string $Salt): string 31 31 { 32 32 return sha1(sha1($Password).$Salt); 33 33 } 34 34 35 function Verify( $Password, $Salt, $StoredHash)35 function Verify(string $Password, string $Salt, string $StoredHash): bool 36 36 { 37 37 return $this->Hash($Password, $Salt) == $StoredHash; 38 38 } 39 39 40 function GetSalt() 40 function GetSalt(): string 41 41 { 42 42 mt_srand(microtime(true) * 100000 + memory_get_usage(true)); … … 49 49 class User extends Model 50 50 { 51 var $Roles = array(); 52 var $User = array(); 53 var $OnlineStateTimeout; 54 var $PermissionCache = array(); 55 var $PermissionGroupCache = array(); 56 var $PermissionGroupCacheOp = array(); 57 /** @var Password */ 58 var $PasswordHash; 59 60 function __construct($System) 51 public array $Roles = array(); 52 public array $User = array(); 53 public int $OnlineStateTimeout; 54 public array $PermissionCache = array(); 55 public array $PermissionGroupCache = array(); 56 public array $PermissionGroupCacheOp = array(); 57 public PasswordHash $PasswordHash; 58 59 function __construct(System $System) 61 60 { 62 61 parent::__construct($System); … … 66 65 } 67 66 68 function Check() 67 function Check(): void 69 68 { 70 69 $SID = session_id(); … … 117 116 { 118 117 $this->Database->delete('UserOnline', 'Id='.$DbRow['Id']); 119 if ($DbRow['User'] != null) $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout');118 if ($DbRow['User'] != null) ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'Logout'); 120 119 } 121 120 //$this->LoadPermission($this->User['Role']); … … 125 124 } 126 125 127 function Register( $Login, $Password, $Password2, $Email, $Name)126 function Register(string $Login, string $Password, string $Password2, string $Email, string $Name): string 128 127 { 129 128 if (($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '') || ($Name == '')) $Result = DATA_MISSING; … … 172 171 173 172 $Result = USER_REGISTRATED; 174 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);173 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'NewRegistration', $Login); 175 174 } 176 175 } … … 180 179 } 181 180 182 function RegisterConfirm( $Id, $Hash)181 function RegisterConfirm(string $Id, string $Hash): string 183 182 { 184 183 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id); … … 191 190 $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0)); 192 191 $Output = USER_REGISTRATION_CONFIRMED; 193 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'RegisterConfirm', 'Login='.192 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'RegisterConfirm', 'Login='. 194 193 $Row['Login'].', Id='.$Row['Id']); 195 194 } else $Output = PASSWORDS_UNMATCHED; … … 198 197 } 199 198 200 function Login( $Login, $Password, $StayLogged = false)199 function Login(string $Login, string $Password, bool $StayLogged = false): string 201 200 { 202 201 if ($StayLogged) $StayLogged = 1; else $StayLogged = 0; … … 228 227 $Result = USER_LOGGED_IN; 229 228 $this->Check(); 230 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress()));229 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'Login', 'Login='.$Login.',Host='.gethostbyaddr(GetRemoteAddress())); 231 230 } 232 231 } else $Result = USER_NOT_REGISTRED; … … 234 233 } 235 234 236 function Logout() 235 function Logout(): string 237 236 { 238 237 $SID = session_id(); 239 238 $this->Database->update('UserOnline', 'SessionId="'.$SID.'"', array('User' => null)); 240 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Logout', $this->User['Login']);239 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'Logout', $this->User['Login']); 241 240 $this->Check(); 242 241 return USER_LOGGED_OUT; … … 248 247 $DbResult = $this->Database->select('UserRole', '*'); 249 248 while ($DbRow = $DbResult->fetch_array()) 249 { 250 250 $this->Roles[] = $DbRow; 251 } 251 252 } 252 253 … … 257 258 if ($DbResult->num_rows > 0) 258 259 while ($DbRow = $DbResult->fetch_array()) 260 { 259 261 $this->User['Permission'][$DbRow['Operation']] = $DbRow; 260 } 261 262 function PermissionMatrix() 262 } 263 } 264 265 function PermissionMatrix(): array 263 266 { 264 267 $Result = array(); … … 274 277 } 275 278 276 function CheckGroupPermission( $GroupId, $OperationId)279 function CheckGroupPermission(string $GroupId, string $OperationId): bool 277 280 { 278 281 $PermissionExists = false; … … 322 325 } 323 326 324 function CheckPermission( $Module, $Operation, $ItemType = '', $ItemIndex = 0)327 function CheckPermission(string $Module, string $Operation, string $ItemType = '', int $ItemIndex = 0): bool 325 328 { 326 329 // Get module id … … 373 376 } 374 377 375 function PasswordRecoveryRequest( $Login, $Email)378 function PasswordRecoveryRequest(string $Login, string $Email): string 376 379 { 377 380 $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"'); … … 395 398 396 399 $Output = USER_PASSWORD_RECOVERY_SUCCESS; 397 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email);400 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'PasswordRecoveryRequest', 'Login='.$Login.',Email='.$Email); 398 401 } else $Output = USER_PASSWORD_RECOVERY_FAIL; 399 402 return $Output; 400 403 } 401 404 402 function PasswordRecoveryConfirm( $Id, $Hash, $NewPassword)405 function PasswordRecoveryConfirm(string $Id, string $Hash, string $NewPassword): string 403 406 { 404 407 $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id); … … 414 417 'Salt' => $Salt, 'Locked' => 0)); 415 418 $Output = USER_PASSWORD_RECOVERY_CONFIRMED; 416 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']);419 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'PasswordRecoveryConfirm', 'Login='.$Row['Login']); 417 420 } else $Output = PASSWORDS_UNMATCHED; 418 421 } else $Output = USER_NOT_FOUND; … … 420 423 } 421 424 422 function CheckToken( $Module, $Operation, $Token)425 function CheckToken(string $Module, string $Operation, string $Token): bool 423 426 { 424 427 $DbResult = $this->Database->select('APIToken', 'User', '`Token`="'.$Token.'"'); -
trunk/Modules/User/UserPage.php
r874 r887 3 3 class PageUser extends Page 4 4 { 5 var $FullTitle = 'Uživatel'; 6 var $ShortTitle = 'Uživatel'; 7 var $ParentClass = 'PagePortal'; 8 9 function Panel($Title, $Content, $Menu = array()) 5 function __construct(System $System) 6 { 7 parent::__construct($System); 8 $this->FullTitle = 'Uživatel'; 9 $this->ShortTitle = 'Uživatel'; 10 $this->ParentClass = 'PagePortal'; 11 } 12 13 function Panel(string $Title, string $Content, array $Menu = array()): string 10 14 { 11 15 if (count($Menu) > 0) … … 15 19 } 16 20 17 function ShowContacts() 21 function ShowContacts(): string 18 22 { 19 23 $Query = 'SELECT `Contact`.`Value`, `Contact`.`Description`, (SELECT `Name` FROM `ContactCategory` WHERE `ContactCategory`.`Id` = `Contact`.`Category`) AS `Category` '. 20 24 'FROM `Contact` WHERE `User` = '. 21 $this->System->User->User['Id'];25 ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id']; 22 26 $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$Query.') AS T'); 23 27 $DbRow = $DbResult->fetch_row(); … … 53 57 } 54 58 55 function ShowUserPanel() 56 { 59 function ShowUserPanel(): string 60 { 61 $User = &ModuleUser::Cast($this->System->GetModule('User'))->User; 57 62 $Output = ''; 58 if ($ this->System->User->User['Id'] != null)63 if ($User->User['Id'] != null) 59 64 { 60 65 $Actions = ''; 61 foreach ( $this->System->ModuleManager->Modules['User']->UserPanel as $Action)66 foreach (ModuleUser::Cast($this->System->GetModule('User'))->UserPanel as $Action) 62 67 { 63 68 if (is_string($Action[0])) … … 71 76 $Output .= $this->Panel('Nabídka uživatele', $Actions); 72 77 $Output .= '</td><td style="vertical-align:top;">'; 73 if ($ this->System->User->User['Id'] != null)78 if ($User->User['Id'] != null) 74 79 { 75 80 $Form = new Form($this->System->FormManager); 76 81 $Form->SetClass('UserOptions'); 77 $Form->LoadValuesFromDatabase($ this->System->User->User['Id']);82 $Form->LoadValuesFromDatabase($User->User['Id']); 78 83 $Form->OnSubmit = '?Action=UserOptionsSave'; 79 84 $Output .= $Form->ShowViewForm(); … … 88 93 } 89 94 90 function Show() 91 { 95 function Show(): string 96 { 97 $User = &ModuleUser::Cast($this->System->GetModule('User'))->User; 92 98 $Output = ''; 93 99 if (array_key_exists('Action', $_GET)) … … 112 118 if (array_key_exists('StayLogged', $_POST) and ($_POST['StayLogged'] == 'on')) $StayLogged = true; 113 119 else $StayLogged = false; 114 $Result = $ this->System->User->Login($_POST['Username'], $_POST['Password'], $StayLogged);120 $Result = $User->Login($_POST['Username'], $_POST['Password'], $StayLogged); 115 121 $Output .= $this->SystemMessage('Přihlášení', $Result); 116 122 if ($Result <> USER_LOGGED_IN) … … 130 136 if ($Action == 'Logout') 131 137 { 132 if ($ this->System->User->User['Id'] != null)133 { 134 $Output .= $this->SystemMessage('Odhlášení', $ this->System->User->Logout());138 if ($User->User['Id'] != null) 139 { 140 $Output .= $this->SystemMessage('Odhlášení', $User->Logout()); 135 141 } else $Output .= $this->SystemMessage('Nastavení uživatele', 'Nejste přihlášen'); 136 142 } else 137 143 if ($Action == 'UserOptions') 138 144 { 139 if ($ this->System->User->User['Id'] != null)145 if ($User->User['Id'] != null) 140 146 { 141 147 $Form = new Form($this->System->FormManager); 142 148 $Form->SetClass('UserOptions'); 143 $Form->LoadValuesFromDatabase($ this->System->User->User['Id']);149 $Form->LoadValuesFromDatabase($User->User['Id']); 144 150 $Form->OnSubmit = '?Action=UserOptionsSave'; 145 151 $Output .= $Form->ShowEditForm(); … … 151 157 $Form->SetClass('UserOptions'); 152 158 $Form->LoadValuesFromForm(); 153 $Form->SaveValuesToDatabase($ this->System->User->User['Id']);159 $Form->SaveValuesToDatabase($User->User['Id']); 154 160 $Output .= $this->SystemMessage('Nastavení', 'Nastavení uloženo.'); 155 $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'Nastavení uživatele změněno', $Form->Values['Name']);156 $Form->LoadValuesFromDatabase($ this->System->User->User['Id']);161 ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'Nastavení uživatele změněno', $Form->Values['Name']); 162 $Form->LoadValuesFromDatabase($User->User['Id']); 157 163 $Form->OnSubmit = '?Action=UserOptionsSave'; 158 164 $Output .= $Form->ShowEditForm(); … … 169 175 { 170 176 $Output .= $this->SystemMessage('Potvrzení registrace', 171 $ this->System->User->RegisterConfirm($_GET['User'], $_GET['H']));177 $User->RegisterConfirm($_GET['User'], $_GET['H'])); 172 178 } else 173 179 if ($Action == 'PasswordRecovery') … … 183 189 $Form->SetClass('PasswordRecovery'); 184 190 $Form->LoadValuesFromForm(); 185 $Result = $ this->System->User->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']);191 $Result = $User->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']); 186 192 $Output .= $this->SystemMessage('Obnova hesla', $Result); 187 193 if ($Result <> USER_PASSWORD_RECOVERY_SUCCESS) … … 192 198 if ($Action == 'PasswordRecoveryConfirm') 193 199 { 194 $Output .= $this->SystemMessage('Obnova hesla', $ this->System->User->PasswordRecoveryConfirm($_GET['User'], $_GET['H'], $_GET['P']));200 $Output .= $this->SystemMessage('Obnova hesla', $User->PasswordRecoveryConfirm($_GET['User'], $_GET['H'], $_GET['P'])); 195 201 } else 196 202 if ($Action == 'UserRegisterSave') … … 199 205 $Form->SetClass('UserRegister'); 200 206 $Form->LoadValuesFromForm(); 201 $Result = $ this->System->User->Register($Form->Values['Login'], $Form->Values['Password'],207 $Result = $User->Register($Form->Values['Login'], $Form->Values['Password'], 202 208 $Form->Values['Password2'], $Form->Values['Email'], $Form->Values['Name']); 203 209 $Output .= $this->SystemMessage('Registrace nového účtu', $Result); … … 216 222 } 217 223 218 function ShowMain() 224 function ShowMain(): string 219 225 { 220 226 $Output = 'Nebyla vybrána akce';
Note:
See TracChangeset
for help on using the changeset viewer.