From 80c2a69d39d9c36a3250f082a230cbffa03ad11e Mon Sep 17 00:00:00 2001 From: Yuri Kuznetsov Date: Sat, 5 Mar 2022 17:08:24 +0200 Subject: [PATCH] type fixes --- .../Core/Authentication/AuthToken/Data.php | 3 ++ .../Core/Authentication/Authentication.php | 25 ++++----- .../Espo/Core/Authentication/Hook/Manager.php | 6 +-- .../Authentication/LDAP/ClientFactory.php | 3 ++ .../Espo/Core/Authentication/LDAP/Utils.php | 54 +++++++++++-------- .../Core/Authentication/Login/DataBuilder.php | 6 +-- .../Espo/Core/Authentication/LoginFactory.php | 8 +-- .../Espo/Core/Authentication/Logins/LDAP.php | 54 ++++++++++++------- .../Espo/Core/Authentication/Result.php | 16 +++--- .../Espo/Core/Authentication/Result/Data.php | 15 +++--- .../Authentication/TwoFactor/LoginFactory.php | 5 +- .../TwoFactor/UserSetupFactory.php | 5 +- 12 files changed, 118 insertions(+), 82 deletions(-) diff --git a/application/Espo/Core/Authentication/AuthToken/Data.php b/application/Espo/Core/Authentication/AuthToken/Data.php index 03648fc201..88c2a6ede2 100644 --- a/application/Espo/Core/Authentication/AuthToken/Data.php +++ b/application/Espo/Core/Authentication/AuthToken/Data.php @@ -75,6 +75,9 @@ class Data return $this->createSecret; } + /** + * @param array $data + */ public static function create(array $data): self { $object = new self(); diff --git a/application/Espo/Core/Authentication/Authentication.php b/application/Espo/Core/Authentication/Authentication.php index 54338467d2..af6c1bef83 100644 --- a/application/Espo/Core/Authentication/Authentication.php +++ b/application/Espo/Core/Authentication/Authentication.php @@ -70,30 +70,27 @@ class Authentication { private const LOGOUT_USERNAME = '**logout'; - private $allowAnyAccess; + private bool $allowAnyAccess; - private $portal; + private ?Portal $portal = null; - private $applicationUser; + private ApplicationUser $applicationUser; - private $applicationState; + private ApplicationState $applicationState; - private $configDataProvider; + private ConfigDataProvider $configDataProvider; - /** - * @var EntityManagerProxy - */ - private $entityManager; + private EntityManagerProxy $entityManager; - private $loginFactory; + private LoginFactory $loginFactory; - private $twoFactorLoginFactory; + private TwoFactorLoginFactory $twoFactorLoginFactory; - private $authTokenManager; + private AuthTokenManager $authTokenManager; - private $hookManager; + private HookManager $hookManager; - private $log; + private Log $log; public function __construct( ApplicationUser $applicationUser, diff --git a/application/Espo/Core/Authentication/Hook/Manager.php b/application/Espo/Core/Authentication/Hook/Manager.php index 0d9cb6d54c..2b29b7fdfc 100644 --- a/application/Espo/Core/Authentication/Hook/Manager.php +++ b/application/Espo/Core/Authentication/Hook/Manager.php @@ -38,9 +38,9 @@ use Espo\Core\Authentication\Result; class Manager { - private $metadata; + private Metadata $metadata; - private $injectableFactory; + private InjectableFactory $injectableFactory; public function __construct(Metadata $metadata, InjectableFactory $injectableFactory) { @@ -84,7 +84,7 @@ class Manager } /** - * @return string[] + * @return class-string[] */ private function getHookClassNameList(string $type): array { diff --git a/application/Espo/Core/Authentication/LDAP/ClientFactory.php b/application/Espo/Core/Authentication/LDAP/ClientFactory.php index bf11872090..9a1cea721d 100644 --- a/application/Espo/Core/Authentication/LDAP/ClientFactory.php +++ b/application/Espo/Core/Authentication/LDAP/ClientFactory.php @@ -31,6 +31,9 @@ namespace Espo\Core\Authentication\LDAP; class ClientFactory { + /** + * @param array $options + */ public function create(array $options): Client { return new Client($options); diff --git a/application/Espo/Core/Authentication/LDAP/Utils.php b/application/Espo/Core/Authentication/LDAP/Utils.php index 96d0ce7ac9..edae502852 100644 --- a/application/Espo/Core/Authentication/LDAP/Utils.php +++ b/application/Espo/Core/Authentication/LDAP/Utils.php @@ -33,11 +33,17 @@ use Espo\Core\Utils\Config; class Utils { - private $config; + private Config $config; - protected $options = null; + /** + * @var ?array + */ + private ?array $options = null; - protected $fieldMap = [ + /** + * @var array + */ + private $fieldMap = [ 'host' => 'ldapHost', 'port' => 'ldapPort', 'useSsl' => 'ldapSecurity', @@ -69,7 +75,10 @@ class Utils 'portalUserRolesIds' => 'ldapPortalUserRolesIds', ]; - protected $permittedEspoOptions = [ + /** + * @var array + */ + private $permittedEspoOptions = [ 'createEspoUser', 'userNameAttribute', 'userObjectClass', @@ -87,9 +96,11 @@ class Utils ]; /** - * accountCanonicalForm Map between Espo and Laminas value. + * AccountCanonicalForm Map between Espo and Laminas value. + * + * @var array */ - protected $accountCanonicalFormMap = [ + private $accountCanonicalFormMap = [ 'Dn' => 1, 'Username' => 2, 'Backslash' => 3, @@ -103,25 +114,21 @@ class Utils } } - protected function getConfig() - { - return $this->config; - } - /** * Get Options from espo config according to $this->fieldMap. * - * @return array + * @return array */ - public function getOptions() + public function getOptions(): array { if (isset($this->options)) { return $this->options; } $options = []; + foreach ($this->fieldMap as $ldapName => $espoName) { - $option = $this->getConfig()->get($espoName); + $option = $this->config->get($espoName); if (isset($option)) { $options[$ldapName] = $option; @@ -136,24 +143,24 @@ class Utils /** * Normalize options to LDAP client format * - * @param array $options + * @param array $options * - * @return array + * @return array */ - public function normalizeOptions(array $options) + public function normalizeOptions(array $options): array { $options['useSsl'] = (bool) ($options['useSsl'] == 'SSL'); $options['useStartTls'] = (bool) ($options['useStartTls'] == 'TLS'); - $options['accountCanonicalForm'] = $this->accountCanonicalFormMap[ $options['accountCanonicalForm'] ]; + $options['accountCanonicalForm'] = $this->accountCanonicalFormMap[$options['accountCanonicalForm']]; return $options; } /** - * Get an ldap option. + * Get an LDAP option. * - * @param string $name - * @param mixed $returns Return value + * @param string $name + * @param mixed $returns A default value. * @return mixed */ public function getOption($name, $returns = null) @@ -172,11 +179,12 @@ class Utils /** * Get Laminas options for using Laminas\Ldap. * - * @return array + * @return array */ - public function getLdapClientOptions() + public function getLdapClientOptions(): array { $options = $this->getOptions(); + $zendOptions = array_diff_key($options, array_flip($this->permittedEspoOptions)); return $zendOptions; diff --git a/application/Espo/Core/Authentication/Login/DataBuilder.php b/application/Espo/Core/Authentication/Login/DataBuilder.php index cb9fa6b135..c2e55bfb0d 100644 --- a/application/Espo/Core/Authentication/Login/DataBuilder.php +++ b/application/Espo/Core/Authentication/Login/DataBuilder.php @@ -33,11 +33,11 @@ use Espo\Core\Authentication\AuthToken\AuthToken; class DataBuilder { - private $username = null; + private ?string $username = null; - private $password = null; + private ?string $password = null; - private $authToken = null; + private ?AuthToken $authToken = null; public function setUsername(?string $username): self { diff --git a/application/Espo/Core/Authentication/LoginFactory.php b/application/Espo/Core/Authentication/LoginFactory.php index 5c32537dfd..c30a4cf50b 100644 --- a/application/Espo/Core/Authentication/LoginFactory.php +++ b/application/Espo/Core/Authentication/LoginFactory.php @@ -37,11 +37,11 @@ class LoginFactory { private const DEFAULT_METHOD = 'Espo'; - private $injectableFactory; + private InjectableFactory $injectableFactory; - private $metadata; + private Metadata $metadata; - private $config; + private Config $config; public function __construct(InjectableFactory $injectableFactory, Metadata $metadata, Config $config) { @@ -52,12 +52,14 @@ class LoginFactory public function create(string $method, bool $isPortal = false): Login { + /** @var class-string */ $className = $this->metadata->get(['authenticationMethods', $method, 'implementationClassName']); if (!$className) { $sanitizedName = preg_replace('/[^a-zA-Z0-9]+/', '', $method); if (!class_exists($className)) { + /** @var class-string */ $className = "Espo\\Core\\Authentication\\Logins\\" . $sanitizedName; } } diff --git a/application/Espo/Core/Authentication/Logins/LDAP.php b/application/Espo/Core/Authentication/Logins/LDAP.php index 56a52672df..105a46f959 100644 --- a/application/Espo/Core/Authentication/Logins/LDAP.php +++ b/application/Espo/Core/Authentication/Logins/LDAP.php @@ -60,29 +60,32 @@ class LDAP implements Login { private $utils; - private $client; + /** + * @var ?Client + */ + private $client = null; - private $isPortal; + private bool $isPortal; - private $config; + private Config $config; - private $entityManager; + private EntityManager $entityManager; - private $passwordHash; + private PasswordHash $passwordHash; - private $language; + private Language $language; - private $log; + private Log $log; - private $baseLogin; + private Espo $baseLogin; - private $clientFactory; + private ClientFactory $clientFactory; - private $linkMultipleSaver; + private LinkMultipleSaver $linkMultipleSaver; - private $emailAddressSaver; + private EmailAddressSaver $emailAddressSaver; - private $phoneNumberSaver; + private PhoneNumberSaver $phoneNumberSaver; public function __construct( Config $config, @@ -113,6 +116,9 @@ class LDAP implements Login $this->utils = new LDAPUtils($config); } + /** + * @var array + */ private $ldapFieldMap = [ 'userName' => 'userNameAttribute', 'firstName' => 'userFirstNameAttribute', @@ -122,11 +128,17 @@ class LDAP implements Login 'phoneNumber' => 'userPhoneNumberAttribute', ]; + /** + * @var array + */ private $userFieldMap = [ 'teamsIds' => 'userTeamsIds', 'defaultTeamId' => 'userDefaultTeamId', ]; + /** + * @var array + */ private $portalUserFieldMap = [ 'portalsIds' => 'portalUserPortalsIds', 'portalRolesIds' => 'portalUserRolesIds', @@ -273,9 +285,9 @@ class LDAP implements Login /** * Login by authorization token. */ - private function loginByToken($username, AuthToken $authToken = null): ?User + private function loginByToken(?string $username, AuthToken $authToken = null): ?User { - if (!isset($authToken)) { + if (!isset($authToken) || $username === null) { return null; } @@ -308,7 +320,7 @@ class LDAP implements Login ->findOne(); } - private function adminLogin($username, $password) + private function adminLogin(string $username, string $password): ?User { $hash = $this->passwordHash->hash($password); @@ -324,8 +336,10 @@ class LDAP implements Login /** * Create Espo user with data gets from LDAP server. + * + * @param array $userData */ - private function createUser(array $userData, $isPortal = false) + private function createUser(array $userData, bool $isPortal = false): ?User { $this->log->info('Creating new user...'); @@ -387,7 +401,7 @@ class LDAP implements Login /** * Find LDAP user DN by his username. */ - private function findLdapUserDnByUsername($username) + private function findLdapUserDnByUsername(string $username): ?string { $ldapClient = $this->getLdapClient(); @@ -411,12 +425,14 @@ class LDAP implements Login foreach ($result as $item) { return $item["dn"]; } + + return null; } /** * Check and convert filter item into LDAP format. */ - private function convertToFilterFormat($filter) + private function convertToFilterFormat(string $filter): string { $filter = trim($filter); @@ -433,6 +449,8 @@ class LDAP implements Login /** * Load fields for a user. + * + * @return array */ private function loadFields(string $type): array { diff --git a/application/Espo/Core/Authentication/Result.php b/application/Espo/Core/Authentication/Result.php index eb8b4b8545..c22615626f 100644 --- a/application/Espo/Core/Authentication/Result.php +++ b/application/Espo/Core/Authentication/Result.php @@ -46,21 +46,21 @@ class Result public const STATUS_FAIL = 'fail'; - private $user; + private ?User $user = null; - private $status; + private string $status; - private $message = null; + private ?string $message = null; - private $token = null; + private ?string $token = null; - private $view = null; + private ?string $view = null; - private $loggedUser = null; + private ?User $loggedUser = null; - private $failReason = null; + private ?string $failReason = null; - private $data = null; + private ?Data $data = null; private function __construct(string $status, ?User $user = null, ?Data $data = null) { diff --git a/application/Espo/Core/Authentication/Result/Data.php b/application/Espo/Core/Authentication/Result/Data.php index 09b5cb3649..4ac2ff67ed 100644 --- a/application/Espo/Core/Authentication/Result/Data.php +++ b/application/Espo/Core/Authentication/Result/Data.php @@ -35,17 +35,20 @@ use stdClass; class Data { - private $message = null; + private ?string $message = null; - private $token = null; + private ?string$token = null; - private $view = null; + private ?string $view = null; - private $loggedUser = null; + private ?User $loggedUser = null; - private $failReason = null; + private ?string $failReason = null; - private $data = []; + /** + * @var array + */ + private array $data = []; private function __construct( ?string $message = null, diff --git a/application/Espo/Core/Authentication/TwoFactor/LoginFactory.php b/application/Espo/Core/Authentication/TwoFactor/LoginFactory.php index 6cfdf6b732..5e8de50392 100644 --- a/application/Espo/Core/Authentication/TwoFactor/LoginFactory.php +++ b/application/Espo/Core/Authentication/TwoFactor/LoginFactory.php @@ -35,9 +35,9 @@ use Espo\Core\Exceptions\Error; class LoginFactory { - private $injectableFactory; + private InjectableFactory $injectableFactory; - private $metadata; + private Metadata $metadata; public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) { @@ -47,6 +47,7 @@ class LoginFactory public function create(string $method): Login { + /** @var ?class-string */ $className = $this->metadata->get(['app', 'authentication2FAMethods', $method, 'loginClassName']); if (!$className) { diff --git a/application/Espo/Core/Authentication/TwoFactor/UserSetupFactory.php b/application/Espo/Core/Authentication/TwoFactor/UserSetupFactory.php index 39ec7fbb1f..782ebd6b30 100644 --- a/application/Espo/Core/Authentication/TwoFactor/UserSetupFactory.php +++ b/application/Espo/Core/Authentication/TwoFactor/UserSetupFactory.php @@ -36,9 +36,9 @@ use Espo\Core\Exceptions\Error; class UserSetupFactory { - private $injectableFactory; + private InjectableFactory $injectableFactory; - private $metadata; + private Metadata $metadata; public function __construct(InjectableFactory $injectableFactory, Metadata $metadata) { @@ -48,6 +48,7 @@ class UserSetupFactory public function create(string $method): UserSetup { + /** @var ?class-string */ $className = $this->metadata->get(['app', 'authentication2FAMethods', $method, 'userSetupClassName']); if (!$className) {