[Admin] Change the name of the service to provide the logged in admin user

This commit is contained in:
Grzegorz Sadowski 2024-07-08 13:48:45 +02:00
parent a724a8878a
commit f45e02a51a
10 changed files with 43 additions and 43 deletions

View file

@ -14,7 +14,6 @@ declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Provider;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\HttpFoundation\Exception\SessionNotFoundException;
@ -22,7 +21,7 @@ use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
final readonly class LoggedInAdminProvider implements LoggedInUserProviderInterface
final readonly class LoggedInAdminUserProvider implements LoggedInAdminUserProviderInterface
{
private const SECURITY_SESSION_KEY = '_security_admin';
@ -35,7 +34,7 @@ final readonly class LoggedInAdminProvider implements LoggedInUserProviderInterf
) {
}
public function getUser(): ?UserInterface
public function getUser(): ?AdminUserInterface
{
$user = $this->security->getUser();
if ($user instanceof AdminUserInterface) {

View file

@ -0,0 +1,14 @@
<?php
declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Provider;
use Sylius\Component\Core\Model\AdminUserInterface;
interface LoggedInAdminUserProviderInterface
{
public function hasUser(): bool;
public function getUser(): ?AdminUserInterface;
}

View file

@ -1,14 +0,0 @@
<?php
declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Provider;
use Sylius\Component\User\Model\UserInterface;
interface LoggedInUserProviderInterface
{
public function hasUser(): bool;
public function getUser(): ?UserInterface;
}

View file

@ -122,11 +122,12 @@
</service>
<service id="Sylius\Bundle\AdminBundle\Generator\TaxonSlugGeneratorInterface" alias="sylius_admin.generator.taxon_slug" />
<service id="sylius_admin.logged_in_user_provider" class="Sylius\Bundle\AdminBundle\Provider\LoggedInAdminProvider">
<service id="sylius_admin.logged_in_admin_user_provider" class="Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProvider">
<argument type="service" id="security.helper" />
<argument type="service" id="security.token_storage" />
<argument type="service" id="request_stack" />
<argument type="service" id="sylius.repository.admin_user" />
</service>
<service id="Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProviderInterface" alias="sylius_admin.logged_in_admin_user_provider" />
</services>
</container>

View file

@ -309,7 +309,7 @@
>
<argument type="service" id="router" />
<argument type="service" id="translator" />
<argument type="service" id="sylius_admin.logged_in_user_provider" />
<argument type="service" id="sylius_admin.logged_in_admin_user_provider" />
<tag
name="twig.component"

View file

@ -15,7 +15,7 @@
<services>
<service id="sylius_admin.twig.error_template_finder" class="Sylius\Bundle\AdminBundle\Twig\ErrorTemplateFinder\ErrorTemplateFinder">
<argument type="service" id="Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface" />
<argument type="service" id="sylius_admin.logged_in_user_provider" />
<argument type="service" id="sylius_admin.logged_in_admin_user_provider" />
<argument type="service" id="twig" />
<tag name="sylius.twig.error_template_finder" />
</service>

View file

@ -13,7 +13,7 @@ declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Twig\Component\Shared\Navbar;
use Sylius\Bundle\AdminBundle\Provider\LoggedInUserProviderInterface;
use Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProviderInterface;
use Sylius\Component\Core\Model\AdminUserInterface;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Contracts\Translation\TranslatorInterface;
@ -24,14 +24,14 @@ class UserDropdownComponent
public function __construct(
private UrlGeneratorInterface $urlGenerator,
private TranslatorInterface $translator,
private LoggedInUserProviderInterface $loggedInUserProvider,
private LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
) {
}
#[ExposeInTemplate(name: 'user')]
public function getUser(): AdminUserInterface
{
$user = $this->loggedInUserProvider->getUser();
$user = $this->loggedInAdminUserProvider->getUser();
if (!$user instanceof AdminUserInterface) {
throw new \RuntimeException('User must be an instance of ' . AdminUserInterface::class . '.');
}

View file

@ -13,7 +13,7 @@ declare(strict_types=1);
namespace Sylius\Bundle\AdminBundle\Twig\ErrorTemplateFinder;
use Sylius\Bundle\AdminBundle\Provider\LoggedInUserProviderInterface;
use Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProviderInterface;
use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
use Sylius\Bundle\UiBundle\Twig\ErrorTemplateFinder\ErrorTemplateFinderInterface;
@ -23,7 +23,7 @@ final readonly class ErrorTemplateFinder implements ErrorTemplateFinderInterface
{
public function __construct(
private SectionProviderInterface $sectionProvider,
private LoggedInUserProviderInterface $loggedInUserProvider,
private LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
private Environment $twig,
) {
}
@ -32,7 +32,7 @@ final readonly class ErrorTemplateFinder implements ErrorTemplateFinderInterface
{
$section = $this->sectionProvider->getSection();
if ($section instanceof AdminSection && $this->loggedInUserProvider->hasUser()) {
if ($section instanceof AdminSection && $this->loggedInAdminUserProvider->hasUser()) {
$template = sprintf('@SyliusAdmin/errors/error%s.html.twig', $statusCode);
if ($this->twig->getLoader()->exists($template)) {
return $template;

View file

@ -15,7 +15,7 @@ namespace spec\Sylius\Bundle\AdminBundle\Provider;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Sylius\Bundle\AdminBundle\Provider\LoggedInUserProviderInterface;
use Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProviderInterface;
use Sylius\Component\Core\Model\AdminUserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Bundle\SecurityBundle\Security;
@ -26,7 +26,7 @@ use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;
use Symfony\Component\Security\Core\Authentication\Token\TokenInterface;
final class LoggedInAdminProviderSpec extends ObjectBehavior
final class LoggedInAdminUserProviderSpec extends ObjectBehavior
{
private const SECURITY_SESSION_KEY = '_security_admin';
private const SERIALIZED_TOKEN = 'O:74:"Symfony\Component\Security\Core\Authentication\Token\UsernamePasswordToken":3:{i:0;N;i:1;s:5:"admin";i:2;a:5:{i:0;O:37:"Sylius\Component\Core\Model\AdminUser":8:{i:0;s:38:"sylius{x33enl1y0askgkgw8k0skocc4ko0kg}";i:1;s:30:"x33enl1y0askgkgw8k0skocc4ko0kg";i:2;s:6:"sylius";i:3;s:6:"sylius";i:4;b:0;i:5;b:1;i:6;i:404;i:7;s:9:"plaintext";}i:1;b:1;i:2;N;i:3;a:0:{}i:4;a:1:{i:0;s:26:"ROLE_ADMINISTRATION_ACCESS";}}}';
@ -40,9 +40,9 @@ final class LoggedInAdminProviderSpec extends ObjectBehavior
$this->beConstructedWith($security, $tokenStorage, $requestStack, $adminUserRepository);
}
function it_implements_logged_in_user_provider(): void
function it_implements_logged_in_admin_user_provider(): void
{
$this->shouldImplement(LoggedInUserProviderInterface::class);
$this->shouldImplement(LoggedInAdminUserProviderInterface::class);
}
function it_returns_true_when_user_is_in_security(

View file

@ -14,7 +14,7 @@ declare(strict_types=1);
namespace spec\Sylius\Bundle\AdminBundle\Twig\ErrorTemplateFinder;
use PhpSpec\ObjectBehavior;
use Sylius\Bundle\AdminBundle\Provider\LoggedInUserProviderInterface;
use Sylius\Bundle\AdminBundle\Provider\LoggedInAdminUserProviderInterface;
use Sylius\Bundle\AdminBundle\SectionResolver\AdminSection;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionInterface;
use Sylius\Bundle\CoreBundle\SectionResolver\SectionProviderInterface;
@ -28,10 +28,10 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function let(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
): void {
$this->beConstructedWith($sectionProvider, $loggedInUserProvider, $twig);
$this->beConstructedWith($sectionProvider, $loggedInAdminUserProvider, $twig);
}
function it_implements_error_template_finder_interface(): void
@ -41,13 +41,13 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function it_does_not_find_template_for_other_sections_than_admin(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
SectionInterface $section,
): void {
$sectionProvider->getSection()->willReturn($section);
$loggedInUserProvider->hasUser()->shouldNotBeCalled();
$loggedInAdminUserProvider->hasUser()->shouldNotBeCalled();
$twig->getLoader()->shouldNotBeCalled();
$this->findTemplate(404)->shouldReturn(null);
@ -55,12 +55,12 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function it_does_not_find_template_when_there_is_no_admin_user(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
): void {
$sectionProvider->getSection()->willReturn(new AdminSection());
$loggedInUserProvider->hasUser()->willReturn(false);
$loggedInAdminUserProvider->hasUser()->willReturn(false);
$twig->getLoader()->shouldNotBeCalled();
@ -69,14 +69,14 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function it_finds_template_for_admin(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
LoaderInterface $loader,
): void {
$templateName = self::TEMPLATE_PREFIX . '/error404.html.twig';
$sectionProvider->getSection()->willReturn(new AdminSection());
$loggedInUserProvider->hasUser()->willReturn(true);
$loggedInAdminUserProvider->hasUser()->willReturn(true);
$twig->getLoader()->willReturn($loader);
$loader->exists($templateName)->shouldBeCalled()->willReturn(true);
@ -86,7 +86,7 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function it_returns_null_if_neither_template_can_be_found(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
LoaderInterface $loader,
): void {
@ -94,7 +94,7 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
$fallbackTemplateName = self::TEMPLATE_PREFIX . '/error.html.twig';
$sectionProvider->getSection()->willReturn(new AdminSection());
$loggedInUserProvider->hasUser()->willReturn(true);
$loggedInAdminUserProvider->hasUser()->willReturn(true);
$twig->getLoader()->willReturn($loader);
$loader->exists($templateName)->shouldBeCalled()->willReturn(false);
@ -105,7 +105,7 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
function it_finds_fallback_template_for_admin(
SectionProviderInterface $sectionProvider,
LoggedInUserProviderInterface $loggedInUserProvider,
LoggedInAdminUserProviderInterface $loggedInAdminUserProvider,
Environment $twig,
LoaderInterface $loader,
): void {
@ -113,7 +113,7 @@ final class ErrorTemplateFinderSpec extends ObjectBehavior
$fallbackTemplateName = self::TEMPLATE_PREFIX . '/error.html.twig';
$sectionProvider->getSection()->willReturn(new AdminSection());
$loggedInUserProvider->hasUser()->willReturn(true);
$loggedInAdminUserProvider->hasUser()->willReturn(true);
$twig->getLoader()->willReturn($loader);
$loader->exists($templateName)->shouldBeCalled()->willReturn(false);