Skip to content

Commit

Permalink
AdminBar: Add native connection to Bar by Bridge service which is com…
Browse files Browse the repository at this point in the history
…piled to DIC
  • Loading branch information
janbarasek committed Mar 21, 2021
1 parent d33c168 commit c13576b
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 40 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"baraja-core/dynamic-configuration": "^2.0",
"baraja-core/doctrine": "^3.0",
"baraja-core/structured-api": "^3.0",
"baraja-core/admin-bar": "^2.0",
"baraja-core/admin-bar": "^2.1",
"baraja-core/plugin-system": "^2.0",
"baraja-core/localization": "^2.0",
"baraja-core/baraja-cloud": "^2.0",
Expand All @@ -36,7 +36,8 @@
},
"conflict": {
"nette/security": "<3.1.2",
"nette/http": "<3.1"
"nette/http": "<3.1",
"baraja-core/admin-bar": "<2.1"
},
"autoload": {
"classmap": [
Expand Down
41 changes: 41 additions & 0 deletions src/Bridge/AdminBarBridge.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Baraja\Cms\MiddleWare\Bridge;


use Baraja\AdminBar\AdminBar;
use Baraja\Cms\LinkGenerator;
use Baraja\Cms\MenuAuthorizatorAccessor;
use Nette\Security\User;

final class AdminBarBridge
{
public function __construct(
private LinkGenerator $linkGenerator,
private User $user,
private MenuAuthorizatorAccessor $menuAuthorizator,
) {
}


public function setup(): void
{
$menu = AdminBar::getBar()->getMenu();

// Show link only in case of user can edit profile
if ($this->menuAuthorizator->get()->isAllowedComponent('user', 'user-overview') === true) {
$menu->addLink(
'My Profile',
$this->linkGenerator->link('User:detail', [
'id' => $this->user->getId(),
]),
'user',
);
}

$menu->addLink('Settings', $this->linkGenerator->link('Settings:default'), 'ui');
$menu->addLink('Sign out', $this->linkGenerator->link('Cms:signOut'), 'ui');
}
}
19 changes: 18 additions & 1 deletion src/CmsExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
namespace Baraja\Cms;


use Baraja\AdminBar\AdminBar;
use Baraja\Cms\Component\ErrorComponent;
use Baraja\Cms\MiddleWare\Bridge\AdminBarBridge;
use Baraja\Cms\Plugin\CommonSettingsPlugin;
use Baraja\Cms\Plugin\ErrorPlugin;
use Baraja\Cms\Plugin\HomepagePlugin;
Expand All @@ -14,6 +16,7 @@
use Baraja\Cms\Proxy\GlobalAsset\CustomGlobalAssetManagerAccessor;
use Baraja\Cms\Support\Support;
use Baraja\Cms\Translator\TranslatorFilter;
use Baraja\Cms\User\AdminBarUser;
use Baraja\Cms\User\UserManager;
use Baraja\Cms\User\UserManagerAccessor;
use Baraja\Doctrine\ORM\DI\OrmAnnotationsExtension;
Expand All @@ -30,7 +33,7 @@
use Nette\PhpGenerator\ClassType;
use Nette\Schema\Expect;
use Nette\Schema\Schema;
use Nette\Utils\FileSystem;
use Nette\Security\User;
use Tracy\Debugger;
use Tracy\ILogger;

Expand Down Expand Up @@ -94,6 +97,10 @@ public function beforeCompile(): void
$builder->addDefinition($this->prefix('support'))
->setFactory(Support::class);

// bridge
$builder->addDefinition($this->prefix('adminBarBridge'))
->setFactory(AdminBarBridge::class);

// translator
$builder->addDefinition($this->prefix('translatorFilter'))
->setFactory(TranslatorFilter::class);
Expand Down Expand Up @@ -234,6 +241,12 @@ public function afterCompile(ClassType $class): void
/** @var ServiceDefinition $admin */
$admin = $builder->getDefinitionByType(Admin::class);

/** @var ServiceDefinition $netteUser */
$netteUser = $builder->getDefinitionByType(User::class);

/** @var ServiceDefinition $adminBarBridge */
$adminBarBridge = $builder->getDefinitionByType(AdminBarBridge::class);

/** @var FactoryDefinition $latte */
$latte = $builder->getDefinitionByType(ILatteFactory::class);
$latte->getResultDefinition()->addSetup('addFilter(?, ?)', [
Expand All @@ -255,11 +268,15 @@ public function afterCompile(ClassType $class): void
. "\t\t\t" . '}' . "\n"
. "\t\t" . '};' . "\n"
. "\t" . '}' . "\n"
. "\t" . AdminBar::class . '::getBar()->setUser(new ' . AdminBarUser::class . '($this->getService(?)));' . "\n"
. "\t" . '$this->getService(?)->setup();' . "\n"
. '})();',
[
'/^admin(?:\/+(?<locale>' . implode('|', Admin::SUPPORTED_LOCALES) . '))?(?<path>\/.*|\?.*|)$/',
$application->getName(),
$admin->getName(),
$netteUser->getName(),
$adminBarBridge->getName(),
],
);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Baraja\Cms;


use Baraja\AdminBar\BasicPanel;
use Baraja\AdminBar\Panel\BasicPanel;
use Baraja\Cms\Proxy\GlobalAsset\CustomGlobalAssetManagerAccessor;
use Baraja\Cms\Translator\TranslatorFilter;
use Baraja\Cms\User\UserManagerAccessor;
Expand Down
24 changes: 1 addition & 23 deletions src/MiddleWare/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public function run(string $plugin, string $view, string $locale, string $path):

$this->trySystemWorkflow($plugin, $path, $locale);
$this->processLoginPage($path, $locale);
$this->setupAdminBar();
AdminBar::enable(AdminBar::MODE_ENABLED);

try {
$pluginService = $this->context->getPluginByName($plugin);
Expand Down Expand Up @@ -166,26 +166,4 @@ private function redirect(string $url, int $httpCode = IResponse::S302_FOUND): v
$this->context->getResponse()->redirect($url, $httpCode);
$this->terminate();
}


private function setupAdminBar(): void
{
AdminBar::enable(true);
AdminBar::addPanel($this->context->getBasicInformation());
AdminBar::setUser(new AdminBarUser($this->context->getUser()->getIdentity()));

// Show link only in case of user can edit profile
if ($this->context->checkPermission('user', 'user-overview') === true) {
AdminBar::addLink(
'My Profile',
$this->linkGenerator->link('User:detail', [
'id' => $this->context->getUser()->getId(),
]),
);
AdminBar::addSeparator();
}

AdminBar::addLink('Settings', $this->linkGenerator->link('Settings:default'));
AdminBar::addLink('Sign out', $this->linkGenerator->link('Cms:signOut'));
}
}
30 changes: 18 additions & 12 deletions src/User/AdminBarUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,32 @@
namespace Baraja\Cms\User;


use Baraja\AdminBar\AdminIdentity;
use Baraja\AdminBar\Shorts;
use Baraja\AdminBar\User;
use Baraja\AdminBar\User\AdminIdentity;
use Baraja\AdminBar\User\User;
use Nette\Security\IIdentity;

final class AdminBarUser implements User
{
public function __construct(
private ?IIdentity $identity,
private \Nette\Security\User $user,
) {
}


public function getName(): ?string
{
if ($this->identity === null) {
if ($this->getIdentity() === null) {
return null;
}
$name = null;
if ($this->identity instanceof AdminIdentity) {
$name = $this->identity->getName();
if ($this->getIdentity() instanceof AdminIdentity) {
$name = $this->getIdentity()->getName();
if ($name === null) {
$name = 'Admin';
}
} elseif (method_exists($this->identity, 'getName')) {
$name = (string) $this->identity->getName() ?: null;
} elseif (method_exists($this->getIdentity(), 'getName')) {
$name = (string) $this->getIdentity()->getName() ?: null;
}

return $name ? Shorts::process($name, 16) : null;
Expand All @@ -45,11 +45,11 @@ public function isAdmin(): bool

public function getAvatarUrl(): ?string
{
if ($this->identity === null) {
if ($this->getIdentity() === null) {
return null;
}
if ($this->identity instanceof AdminIdentity) {
return $this->identity->getAvatarUrl();
if ($this->getIdentity() instanceof AdminIdentity) {
return $this->getIdentity()->getAvatarUrl();
}

return null;
Expand All @@ -58,6 +58,12 @@ public function getAvatarUrl(): ?string

public function isLoggedIn(): bool
{
return $this->identity !== null;
return $this->user->isLoggedIn();
}


private function getIdentity(): ?IIdentity
{
return $this->user->getIdentity();
}
}
2 changes: 1 addition & 1 deletion src/User/UserManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace Baraja\Cms\User;


use Baraja\AdminBar\AdminIdentity;
use Baraja\AdminBar\User\AdminIdentity;
use Baraja\Cms\Helpers;
use Baraja\Cms\User\Entity\CmsUser;
use Baraja\Cms\User\Entity\User;
Expand Down

0 comments on commit c13576b

Please sign in to comment.