From c13576bcd5adcc12d010c77becf94ead8a44dfc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20Bar=C3=A1=C5=A1ek?= Date: Sun, 21 Mar 2021 17:58:07 +0100 Subject: [PATCH] AdminBar: Add native connection to Bar by Bridge service which is compiled to DIC --- composer.json | 5 +++-- src/Bridge/AdminBarBridge.php | 41 ++++++++++++++++++++++++++++++++++ src/CmsExtension.php | 19 +++++++++++++++- src/Context.php | 2 +- src/MiddleWare/Application.php | 24 +------------------- src/User/AdminBarUser.php | 30 +++++++++++++++---------- src/User/UserManager.php | 2 +- 7 files changed, 83 insertions(+), 40 deletions(-) create mode 100644 src/Bridge/AdminBarBridge.php diff --git a/composer.json b/composer.json index 22442e8..2a42074 100644 --- a/composer.json +++ b/composer.json @@ -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", @@ -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": [ diff --git a/src/Bridge/AdminBarBridge.php b/src/Bridge/AdminBarBridge.php new file mode 100644 index 0000000..1b1a42d --- /dev/null +++ b/src/Bridge/AdminBarBridge.php @@ -0,0 +1,41 @@ +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'); + } +} diff --git a/src/CmsExtension.php b/src/CmsExtension.php index 70a5662..642a1c7 100644 --- a/src/CmsExtension.php +++ b/src/CmsExtension.php @@ -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; @@ -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; @@ -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; @@ -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); @@ -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(?, ?)', [ @@ -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(?:\/+(?' . implode('|', Admin::SUPPORTED_LOCALES) . '))?(?\/.*|\?.*|)$/', $application->getName(), $admin->getName(), + $netteUser->getName(), + $adminBarBridge->getName(), ], ); } diff --git a/src/Context.php b/src/Context.php index c95cab2..4f3035a 100644 --- a/src/Context.php +++ b/src/Context.php @@ -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; diff --git a/src/MiddleWare/Application.php b/src/MiddleWare/Application.php index 12d329f..17c4dae 100644 --- a/src/MiddleWare/Application.php +++ b/src/MiddleWare/Application.php @@ -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); @@ -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')); - } } diff --git a/src/User/AdminBarUser.php b/src/User/AdminBarUser.php index 6934b17..32793c8 100644 --- a/src/User/AdminBarUser.php +++ b/src/User/AdminBarUser.php @@ -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; @@ -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; @@ -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(); } } diff --git a/src/User/UserManager.php b/src/User/UserManager.php index 0a97770..a132d19 100644 --- a/src/User/UserManager.php +++ b/src/User/UserManager.php @@ -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;