Skip to content

Commit

Permalink
Impersonate from user menu dropdown EasyCorp#6524
Browse files Browse the repository at this point in the history
  • Loading branch information
elias-playfinder committed Nov 12, 2024
1 parent e7141eb commit bf8dd86
Show file tree
Hide file tree
Showing 15 changed files with 384 additions and 287 deletions.
9 changes: 9 additions & 0 deletions assets/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ class App {
this.#createModalWindowsForDeleteActions();
this.#createPopovers();
this.#createTooltips();
this.#createImpersonateModal();

document.addEventListener('ea.collection.item-added', () => this.#createAutoCompleteFields());
}
Expand Down Expand Up @@ -449,4 +450,12 @@ class App {
});
});
}

#createImpersonateModal(){
const impersonateButton = document.querySelector('#modal-impersonate-link');
const impersonateModal = document.querySelector(impersonateButton.getAttribute('data-bs-target'));
document.querySelector('#impersonate-modal-apply-button').addEventListener('click', () => {
impersonateModal.querySelector('form').submit();
});
}
}
28 changes: 28 additions & 0 deletions src/Config/Menu/AccessImpersonationMenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Config\Menu;

use EasyCorp\Bundle\EasyAdminBundle\Config\Option\EA;
use EasyCorp\Bundle\EasyAdminBundle\Contracts\Menu\MenuItemInterface;
use EasyCorp\Bundle\EasyAdminBundle\Dto\MenuItemDto;
use Symfony\Contracts\Translation\TranslatableInterface;

/**
* @see EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem::linkToExitImpersonation()
*
* @author Elías Fernández <[email protected]>
*/
final class AccessImpersonationMenuItem implements MenuItemInterface
{
use MenuItemTrait;

public function __construct(TranslatableInterface|string $label, ?string $icon)
{
$this->dto = new MenuItemDto();

$this->dto->setType(MenuItemDto::TYPE_ACCESS_IMPERSONATION);
$this->dto->setLabel($label);
$this->dto->setIcon($icon);
$this->dto->setLinkUrl('test');
}
}
10 changes: 10 additions & 0 deletions src/Config/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\CrudMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\DashboardMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\ExitImpersonationMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\AccessImpersonationMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\LogoutMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\RouteMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\SectionMenuItem;
Expand Down Expand Up @@ -45,6 +46,15 @@ public static function linkToExitImpersonation(TranslatableInterface|string $lab
return new ExitImpersonationMenuItem($label, $icon);
}

/**
* @param string|null $icon The full CSS classes of the FontAwesome icon to render (see https://fontawesome.com/v6/search?m=free)
*/
public static function linkToAccessImpersonation(TranslatableInterface|string $label, ?string $icon = null): AccessImpersonationMenuItem
{
return new AccessImpersonationMenuItem($label, $icon);
}


/**
* @param string|null $icon The full CSS classes of the FontAwesome icon to render (see https://fontawesome.com/v6/search?m=free)
*/
Expand Down
3 changes: 3 additions & 0 deletions src/Controller/AbstractDashboardController.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ public function configureUserMenu(UserInterface $user): UserMenu
if ($this->isGranted(Permission::EA_EXIT_IMPERSONATION)) {
$userMenuItems[] = MenuItem::linkToExitImpersonation(t('user.exit_impersonation', domain: 'EasyAdminBundle'), 'fa-user-lock');
}
if (!$this->isGranted(Permission::EA_EXIT_IMPERSONATION) && $this->isGranted(Permission::EA_CAN_SWITCH_USER)) {
$userMenuItems[] = MenuItem::linkToAccessImpersonation(t('user.impersonate_user', domain: 'EasyAdminBundle'), 'fa-user-lock');
}

$userName = method_exists($user, '__toString') ? (string) $user : $user->getUserIdentifier();

Expand Down
1 change: 1 addition & 0 deletions src/Dto/MenuItemDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ final class MenuItemDto
public const TYPE_URL = 'url';
public const TYPE_SECTION = 'section';
public const TYPE_EXIT_IMPERSONATION = 'exit_impersonation';
public const TYPE_ACCESS_IMPERSONATION = 'access_impersonation';
public const TYPE_DASHBOARD = 'dashboard';
public const TYPE_LOGOUT = 'logout';
public const TYPE_SUBMENU = 'submenu';
Expand Down
4 changes: 2 additions & 2 deletions src/Resources/public/app.d52c6a33.css

Large diffs are not rendered by default.

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/Resources/public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
"/app.d52c6a33.css"
],
"js": [
"/app.3048ce28.js"
"/app.fb2fb73c.js"
]
},
"form": {
Expand Down
Loading

0 comments on commit bf8dd86

Please sign in to comment.