Changeset 887 for trunk/Modules/User


Ignore:
Timestamp:
Nov 20, 2020, 12:08:12 AM (4 years ago)
Author:
chronos
Message:
  • Added: Static types added to almost all classes, methods and function. Supported by PHP 7.4.
  • Fixed: Various found code issues.
Location:
trunk/Modules/User
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/Modules/User/User.php

    r874 r887  
    77class ModuleUser extends AppModule
    88{
    9   var $UserPanel;
    10 
    11   function __construct($System)
     9  public array $UserPanel;
     10  public User $User;
     11
     12  function __construct(System $System)
    1213  {
    1314    parent::__construct($System);
     
    1920    $this->Dependencies = array();
    2021    $this->UserPanel = array();
    21   }
    22 
    23   function DoInstall()
     22    $this->User = new User($System);
     23  }
     24
     25  function DoInstall(): void
    2426  {
    2527    $this->Database->query("CREATE TABLE IF NOT EXISTS `User` (
     
    108110  }
    109111
    110   function DoUninstall()
     112  function DoUninstall(): void
    111113  {
    112114    $this->Database->query('DROP TABLE `PermissionUserAssignment`');
     
    118120  }
    119121
    120   function DoUpgrade()
     122  function DoUpgrade(): void
    121123  {
    122124    /*
     
    129131  }
    130132
    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');
    137138    $this->System->RegisterPageBarItem('Top', 'User', array($this, 'TopBarCallback'));
    138139    $this->System->FormManager->RegisterClass('UserLogin', array(
     
    272273      'Filter' => '1',
    273274    ));
    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
    279280  {
    280281    $DbResult = $this->Database->select('User', 'COUNT(*)', '1');
     
    284285  }
    285286
    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)
    293294    {
    294295      $Output = '<a href="'.$this->System->Link('/user/?Action=LoginForm').'">Přihlášení</a> '.
     
    296297    } else
    297298    {
    298       $Output = $this->System->User->User['Name'].
     299      $Output = $this->User->User['Name'].
    299300        ' <a href="'.$this->System->Link('/user/?Action=UserMenu').'">Nabídka</a>'.
    300301        ' <a href="'.$this->System->Link('/user/?Action=Logout').'">Odhlásit</a>';
     
    303304    return $Output;
    304305  }
     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  }
    305315}
  • trunk/Modules/User/UserList.php

    r874 r887  
    33class PageUserList extends Page
    44{
    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  }
    812
    9   function Show()
     13  function Show(): string
    1014  {
    11     if (!$this->System->User->CheckPermission('User', 'ShowList'))
     15    if (!ModuleUser::Cast($this->System->GetModule('User'))->User->CheckPermission('User', 'ShowList'))
    1216      return 'Nemáte oprávnění';
    1317
  • trunk/Modules/User/UserModel.php

    r878 r887  
    2828class PasswordHash
    2929{
    30   function Hash($Password, $Salt)
     30  function Hash(string $Password, string $Salt): string
    3131  {
    3232    return sha1(sha1($Password).$Salt);
    3333  }
    3434
    35   function Verify($Password, $Salt, $StoredHash)
     35  function Verify(string $Password, string $Salt, string $StoredHash): bool
    3636  {
    3737    return $this->Hash($Password, $Salt) == $StoredHash;
    3838  }
    3939
    40   function GetSalt()
     40  function GetSalt(): string
    4141  {
    4242    mt_srand(microtime(true) * 100000 + memory_get_usage(true));
     
    4949class User extends Model
    5050{
    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)
    6160  {
    6261    parent::__construct($System);
     
    6665  }
    6766
    68   function Check()
     67  function Check(): void
    6968  {
    7069    $SID = session_id();
     
    117116    {
    118117      $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');
    120119    }
    121120    //$this->LoadPermission($this->User['Role']);
     
    125124  }
    126125
    127   function Register($Login, $Password, $Password2, $Email, $Name)
     126  function Register(string $Login, string $Password, string $Password2, string $Email, string $Name): string
    128127  {
    129128    if (($Email == '') || ($Login == '') || ($Password == '') || ($Password2 == '')  || ($Name == '')) $Result = DATA_MISSING;
     
    172171
    173172            $Result = USER_REGISTRATED;
    174             $this->System->ModuleManager->Modules['Log']->NewRecord('User', 'NewRegistration', $Login);
     173            ModuleLog::Cast($this->System->GetModule('Log'))->NewRecord('User', 'NewRegistration', $Login);
    175174          }
    176175        }
     
    180179  }
    181180
    182   function RegisterConfirm($Id, $Hash)
     181  function RegisterConfirm(string $Id, string $Hash): string
    183182  {
    184183    $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
     
    191190        $this->Database->update('User', 'Id='.$Row['Id'], array('Locked' => 0));
    192191        $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='.
    194193          $Row['Login'].', Id='.$Row['Id']);
    195194      } else $Output = PASSWORDS_UNMATCHED;
     
    198197  }
    199198
    200   function Login($Login, $Password, $StayLogged = false)
     199  function Login(string $Login, string $Password, bool $StayLogged = false): string
    201200  {
    202201    if ($StayLogged) $StayLogged = 1; else $StayLogged = 0;
     
    228227        $Result = USER_LOGGED_IN;
    229228        $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()));
    231230      }
    232231    } else $Result = USER_NOT_REGISTRED;
     
    234233  }
    235234
    236   function Logout()
     235  function Logout(): string
    237236  {
    238237    $SID = session_id();
    239238    $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']);
    241240    $this->Check();
    242241    return USER_LOGGED_OUT;
     
    248247    $DbResult = $this->Database->select('UserRole', '*');
    249248    while ($DbRow = $DbResult->fetch_array())
     249    {
    250250      $this->Roles[] = $DbRow;
     251    }
    251252  }
    252253
     
    257258    if ($DbResult->num_rows > 0)
    258259    while ($DbRow = $DbResult->fetch_array())
     260    {
    259261      $this->User['Permission'][$DbRow['Operation']] = $DbRow;
    260   }
    261 
    262   function PermissionMatrix()
     262    }
     263  }
     264
     265  function PermissionMatrix(): array
    263266  {
    264267    $Result = array();
     
    274277  }
    275278
    276   function CheckGroupPermission($GroupId, $OperationId)
     279  function CheckGroupPermission(string $GroupId, string $OperationId): bool
    277280  {
    278281    $PermissionExists = false;
     
    322325  }
    323326
    324   function CheckPermission($Module, $Operation, $ItemType = '', $ItemIndex = 0)
     327  function CheckPermission(string $Module, string $Operation, string $ItemType = '', int $ItemIndex = 0): bool
    325328  {
    326329    // Get module id
     
    373376  }
    374377
    375   function PasswordRecoveryRequest($Login, $Email)
     378  function PasswordRecoveryRequest(string $Login, string $Email): string
    376379  {
    377380    $DbResult = $this->Database->select('User', 'Login, Name, Id, Email, Password', '`Login`="'.$Login.'" AND `Email`="'.$Email.'"');
     
    395398
    396399      $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);
    398401    } else $Output = USER_PASSWORD_RECOVERY_FAIL;
    399402    return $Output;
    400403  }
    401404
    402   function PasswordRecoveryConfirm($Id, $Hash, $NewPassword)
     405  function PasswordRecoveryConfirm(string $Id, string $Hash, string $NewPassword): string
    403406  {
    404407    $DbResult = $this->Database->select('User', 'Id, Login, Password', 'Id = '.$Id);
     
    414417          'Salt' => $Salt, 'Locked' => 0));
    415418        $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']);
    417420      } else $Output = PASSWORDS_UNMATCHED;
    418421    } else $Output = USER_NOT_FOUND;
     
    420423  }
    421424
    422   function CheckToken($Module, $Operation, $Token)
     425  function CheckToken(string $Module, string $Operation, string $Token): bool
    423426  {
    424427    $DbResult = $this->Database->select('APIToken', 'User', '`Token`="'.$Token.'"');
  • trunk/Modules/User/UserPage.php

    r874 r887  
    33class PageUser extends Page
    44{
    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
    1014  {
    1115    if (count($Menu) > 0)
     
    1519  }
    1620
    17   function ShowContacts()
     21  function ShowContacts(): string
    1822  {
    1923    $Query = 'SELECT `Contact`.`Value`, `Contact`.`Description`, (SELECT `Name` FROM `ContactCategory` WHERE `ContactCategory`.`Id` = `Contact`.`Category`) AS `Category` '.
    2024      'FROM `Contact` WHERE `User` = '.
    21       $this->System->User->User['Id'];
     25      ModuleUser::Cast($this->System->GetModule('User'))->User->User['Id'];
    2226    $DbResult = $this->Database->query('SELECT COUNT(*) FROM ('.$Query.') AS T');
    2327    $DbRow = $DbResult->fetch_row();
     
    5357  }
    5458
    55   function ShowUserPanel()
    56   {
     59  function ShowUserPanel(): string
     60  {
     61    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
    5762    $Output = '';
    58     if ($this->System->User->User['Id'] != null)
     63    if ($User->User['Id'] != null)
    5964    {
    6065      $Actions = '';
    61       foreach ($this->System->ModuleManager->Modules['User']->UserPanel as $Action)
     66      foreach (ModuleUser::Cast($this->System->GetModule('User'))->UserPanel as $Action)
    6267      {
    6368        if (is_string($Action[0]))
     
    7176      $Output .= $this->Panel('Nabídka uživatele', $Actions);
    7277      $Output .= '</td><td style="vertical-align:top;">';
    73       if ($this->System->User->User['Id'] != null)
     78      if ($User->User['Id'] != null)
    7479        {
    7580          $Form = new Form($this->System->FormManager);
    7681          $Form->SetClass('UserOptions');
    77           $Form->LoadValuesFromDatabase($this->System->User->User['Id']);
     82          $Form->LoadValuesFromDatabase($User->User['Id']);
    7883          $Form->OnSubmit = '?Action=UserOptionsSave';
    7984          $Output .= $Form->ShowViewForm();
     
    8893  }
    8994
    90   function Show()
    91   {
     95  function Show(): string
     96  {
     97    $User = &ModuleUser::Cast($this->System->GetModule('User'))->User;
    9298    $Output = '';
    9399    if (array_key_exists('Action', $_GET))
     
    112118          if (array_key_exists('StayLogged', $_POST) and ($_POST['StayLogged'] == 'on')) $StayLogged = true;
    113119            else $StayLogged = false;
    114           $Result = $this->System->User->Login($_POST['Username'], $_POST['Password'], $StayLogged);
     120          $Result = $User->Login($_POST['Username'], $_POST['Password'], $StayLogged);
    115121          $Output .= $this->SystemMessage('Přihlášení', $Result);
    116122          if ($Result <> USER_LOGGED_IN)
     
    130136      if ($Action == 'Logout')
    131137      {
    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());
    135141        } else $Output .= $this->SystemMessage('Nastavení uživatele', 'Nejste přihlášen');
    136142      } else
    137143      if ($Action == 'UserOptions')
    138144      {
    139         if ($this->System->User->User['Id'] != null)
     145        if ($User->User['Id'] != null)
    140146        {
    141147          $Form = new Form($this->System->FormManager);
    142148          $Form->SetClass('UserOptions');
    143           $Form->LoadValuesFromDatabase($this->System->User->User['Id']);
     149          $Form->LoadValuesFromDatabase($User->User['Id']);
    144150          $Form->OnSubmit = '?Action=UserOptionsSave';
    145151          $Output .= $Form->ShowEditForm();
     
    151157        $Form->SetClass('UserOptions');
    152158        $Form->LoadValuesFromForm();
    153         $Form->SaveValuesToDatabase($this->System->User->User['Id']);
     159        $Form->SaveValuesToDatabase($User->User['Id']);
    154160        $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']);
    157163        $Form->OnSubmit = '?Action=UserOptionsSave';
    158164        $Output .= $Form->ShowEditForm();
     
    169175      {
    170176        $Output .= $this->SystemMessage('Potvrzení registrace',
    171           $this->System->User->RegisterConfirm($_GET['User'], $_GET['H']));
     177          $User->RegisterConfirm($_GET['User'], $_GET['H']));
    172178      } else
    173179      if ($Action == 'PasswordRecovery')
     
    183189        $Form->SetClass('PasswordRecovery');
    184190        $Form->LoadValuesFromForm();
    185         $Result = $this->System->User->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']);
     191        $Result = $User->PasswordRecoveryRequest($Form->Values['Name'], $Form->Values['Email']);
    186192        $Output .= $this->SystemMessage('Obnova hesla', $Result);
    187193        if ($Result <> USER_PASSWORD_RECOVERY_SUCCESS)
     
    192198      if ($Action == 'PasswordRecoveryConfirm')
    193199      {
    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']));
    195201      } else
    196202      if ($Action == 'UserRegisterSave')
     
    199205        $Form->SetClass('UserRegister');
    200206        $Form->LoadValuesFromForm();
    201         $Result = $this->System->User->Register($Form->Values['Login'], $Form->Values['Password'],
     207        $Result = $User->Register($Form->Values['Login'], $Form->Values['Password'],
    202208          $Form->Values['Password2'], $Form->Values['Email'], $Form->Values['Name']);
    203209        $Output .= $this->SystemMessage('Registrace nového účtu', $Result);
     
    216222  }
    217223
    218   function ShowMain()
     224  function ShowMain(): string
    219225  {
    220226    $Output = 'Nebyla vybrána akce';
Note: See TracChangeset for help on using the changeset viewer.