From 73fb4cc7b047ca0b3a23cf2fbf17bfe0e552e5bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Thu, 7 Jan 2021 21:44:57 +0100 Subject: [PATCH] Fix PhpStan errors --- src/Api/CmsEndpoint.php | 7 +++++++ src/Api/UserEndpoint.php | 8 ++------ src/Menu/MenuAuthorizator.php | 4 ++-- src/User/Entity/CmsUser.php | 18 +++++++++--------- src/User/UserManager.php | 1 + 5 files changed, 21 insertions(+), 17 deletions(-) diff --git a/src/Api/CmsEndpoint.php b/src/Api/CmsEndpoint.php index 2f8ed86..4cec42b 100644 --- a/src/Api/CmsEndpoint.php +++ b/src/Api/CmsEndpoint.php @@ -9,6 +9,7 @@ use Baraja\Cms\Helpers; use Baraja\Cms\Settings; use Baraja\Cms\User\Entity\CmsUser; +use Baraja\Cms\User\Entity\User; use Baraja\Cms\User\Entity\UserResetPasswordRequest; use Baraja\Cms\User\UserManager; use Baraja\Doctrine\EntityManager; @@ -118,6 +119,12 @@ public function postForgotPassword(string $locale, string $username): void ->getQuery() ->getSingleResult(); + if (!$user instanceof User) { + $this->sendError('Reset password is available only for system CMS Users. Please contact your administrator'); + + return; + } + $this->entityManager->persist($request = new UserResetPasswordRequest($user, '3 hours')); $this->entityManager->flush(); diff --git a/src/Api/UserEndpoint.php b/src/Api/UserEndpoint.php index 5b1a26f..1a8d43f 100644 --- a/src/Api/UserEndpoint.php +++ b/src/Api/UserEndpoint.php @@ -138,14 +138,10 @@ public function createDefault(string $fullName, string $email, string $role, ?st } try { try { + /* @phpstan-ignore-next-line */ $ref = new \ReflectionClass($this->userManager->getDefaultEntity()); /** @var CmsUser $user */ - $user = $ref->newInstanceArgs([ - 'username' => $email, - 'password' => $password, - 'email' => $email, - 'role' => CmsUser::ROLE_USER, - ]); + $user = $ref->newInstanceArgs([$email, $password, $email, CmsUser::ROLE_USER]); } catch (\Throwable $e) { if (class_exists(Debugger::class)) { Debugger::log($e, ILogger::CRITICAL); diff --git a/src/Menu/MenuAuthorizator.php b/src/Menu/MenuAuthorizator.php index 21ff02d..b7bf22b 100644 --- a/src/Menu/MenuAuthorizator.php +++ b/src/Menu/MenuAuthorizator.php @@ -63,7 +63,7 @@ public function getId(): ?string */ public function getRoles(): array { - return $this->roles; + return array_keys($this->roles); } @@ -72,7 +72,7 @@ public function getRoles(): array */ public function getPrivileges(): array { - return $this->privileges; + return array_keys($this->privileges); } diff --git a/src/User/Entity/CmsUser.php b/src/User/Entity/CmsUser.php index 0d5420c..1b20e38 100644 --- a/src/User/Entity/CmsUser.php +++ b/src/User/Entity/CmsUser.php @@ -87,21 +87,21 @@ public function setCreateDate(\DateTime $createDate): void; */ public function getMetaData(): array; - public function getOtpCode(); + public function getOtpCode(): ?string; - public function setOtpCode(?string $otpCode); + public function setOtpCode(?string $otpCode): void; - public function isActive(); + public function isActive(): bool; - public function setActive(bool $active); + public function setActive(bool $active): void; - public function getAvatarUrl(); + public function getAvatarUrl(): ?string; - public function setAvatarUrl(?string $avatarUrl); + public function setAvatarUrl(?string $avatarUrl): void; - public function getName(bool $reverse = false); + public function getName(bool $reverse = false): string; - public function getPhone(); + public function getPhone(): ?string; - public function setPhone(?string $phone, int $region = 420); + public function setPhone(?string $phone, int $region = 420): void; } diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 5ff6ecb..a9b3cfe 100644 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -34,6 +34,7 @@ final class UserManager implements Authenticator public function __construct(EntityManager $entityManager, UserStorage $userStorage, ?string $userEntity = null) { + /* @phpstan-ignore-next-line */ if ((class_implements($userEntity = $userEntity ?? User::class)[CmsUser::class] ?? false) === false) { throw new \InvalidArgumentException('User entity "' . $userEntity . '" must implements "' . CmsUser::class . '" interface.'); }