From e560c190f3fe176fbf15424989bdf922e64158f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Fri, 18 Dec 2020 18:32:41 +0100 Subject: [PATCH] AdminIdentity: Save admin identity to AdminIdentity entity and rewrite basic values to AdminBar. --- src/Admin.php | 2 +- src/User/AdminBarUser.php | 36 ++++++++++++++++++++++++++++++++++-- src/User/UserManager.php | 12 +++++++++--- 3 files changed, 44 insertions(+), 6 deletions(-) diff --git a/src/Admin.php b/src/Admin.php index bb95f76..48af16c 100644 --- a/src/Admin.php +++ b/src/Admin.php @@ -401,7 +401,7 @@ private function processAdminBar(): void { AdminBar::enable(true); AdminBar::addPanel($this->context->getBasicInformation()); - AdminBar::setUser(new AdminBarUser); + AdminBar::setUser(new AdminBarUser($this->context->getUser()->getIdentity())); if ($this->context->getUser()->isAllowed('user', 'detail') === true) { // Show link only in case of user can edit profile AdminBar::addLink('My Profile', $this->linkGenerator->link('User:detail', ['id' => $this->context->getUser()->getId()])); diff --git a/src/User/AdminBarUser.php b/src/User/AdminBarUser.php index f226a1d..d3f1eb5 100644 --- a/src/User/AdminBarUser.php +++ b/src/User/AdminBarUser.php @@ -5,13 +5,38 @@ namespace Baraja\Cms\User; +use Baraja\AdminBar\AdminIdentity; +use Baraja\AdminBar\Shorts; use Baraja\AdminBar\User; +use Nette\Security\IIdentity; final class AdminBarUser implements User { + private ?IIdentity $identity; + + + public function __construct(?IIdentity $identity) + { + $this->identity = $identity; + } + + public function getName(): ?string { - return 'Baraja'; + if ($this->identity === null) { + return null; + } + $name = null; + if ($this->identity instanceof AdminIdentity) { + $name = $this->identity->getName(); + if ($name === null) { + $name = 'Admin'; + } + } elseif (method_exists($this->identity, 'getName')) { + $name = ((string) $this->identity->getName()) ?: null; + } + + return $name ? Shorts::process($name, 16) : null; } @@ -23,12 +48,19 @@ public function isAdmin(): bool public function getAvatarUrl(): ?string { + if ($this->identity === null) { + return null; + } + if ($this->identity instanceof AdminIdentity) { + return $this->identity->getAvatarUrl(); + } + return null; } public function isLoggedIn(): bool { - return true; + return $this->identity !== null; } } diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 93479e4..d069a30 100644 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -5,6 +5,7 @@ namespace Baraja\Cms\User; +use Baraja\AdminBar\AdminIdentity; use Baraja\BarajaCloud\CloudManager; use Baraja\Cms\Helpers; use Baraja\Cms\User\Entity\User; @@ -16,7 +17,6 @@ use Doctrine\ORM\NoResultException; use Nette\Security\AuthenticationException; use Nette\Security\IAuthenticator; -use Nette\Security\Identity; use Nette\Security\IIdentity; use Nette\Security\IUserStorage; use Nette\Security\Passwords; @@ -57,8 +57,15 @@ public function getIdentity(): ?IIdentity public function createIdentity(IIdentity $user, string $expiration = '2 hours'): IIdentity { + $name = null; + $avatarUrl = null; + if ($user instanceof User) { + $name = $user->getName(); + $avatarUrl = $user->getAvatarUrl(); + } + $this->userStorage - ->setIdentity($identity = new Identity($user->getId(), $user->getRoles())) + ->setIdentity($identity = new AdminIdentity($user->getId(), $user->getRoles(), [], $name, $avatarUrl)) ->setAuthenticated(true) ->setExpiration($expiration); @@ -74,7 +81,6 @@ public function getUserStorage(): IUserStorage /** * @param mixed[] $credentials - * @return IIdentity|User * @throws AuthenticationException */ public function authenticate(array $credentials): IIdentity