Skip to content

Commit

Permalink
AdminIdentity: Save admin identity to AdminIdentity entity and rewrit…
Browse files Browse the repository at this point in the history
…e basic values to AdminBar.
  • Loading branch information
janbarasek committed Dec 18, 2020
1 parent 168af3c commit e560c19
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/Admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()]));
Expand Down
36 changes: 34 additions & 2 deletions src/User/AdminBarUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}


Expand All @@ -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;
}
}
12 changes: 9 additions & 3 deletions src/User/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -74,7 +81,6 @@ public function getUserStorage(): IUserStorage

/**
* @param mixed[] $credentials
* @return IIdentity|User
* @throws AuthenticationException
*/
public function authenticate(array $credentials): IIdentity
Expand Down

0 comments on commit e560c19

Please sign in to comment.