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 0e0bd9f
Show file tree
Hide file tree
Showing 15 changed files with 399 additions and 302 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

Large diffs are not rendered by default.

Large diffs are not rendered by default.

32 changes: 16 additions & 16 deletions src/Resources/public/entrypoints.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,76 @@
"entrypoints": {
"app": {
"css": [
"/app.d52c6a33.css"
"/bundles/easyadmin/app.f54c2204.css"
],
"js": [
"/app.3048ce28.js"
"/bundles/easyadmin/app.fb2fb73c.js"
]
},
"form": {
"js": [
"/form.ec4ce275.js"
"/bundles/easyadmin/form.ec4ce275.js"
]
},
"page-layout": {
"js": [
"/page-layout.3347892e.js"
"/bundles/easyadmin/page-layout.3347892e.js"
]
},
"page-color-scheme": {
"js": [
"/page-color-scheme.30cb23c2.js"
"/bundles/easyadmin/page-color-scheme.30cb23c2.js"
]
},
"field-boolean": {
"js": [
"/field-boolean.ae2abe46.js"
"/bundles/easyadmin/field-boolean.ae2abe46.js"
]
},
"field-code-editor": {
"css": [
"/field-code-editor.b7d8b94d.css"
"/bundles/easyadmin/field-code-editor.b7d8b94d.css"
],
"js": [
"/field-code-editor.d0df5dd8.js"
"/bundles/easyadmin/field-code-editor.d0df5dd8.js"
]
},
"field-collection": {
"js": [
"/field-collection.f685c209.js"
"/bundles/easyadmin/field-collection.f685c209.js"
]
},
"field-file-upload": {
"js": [
"/field-file-upload.7f9cb952.js"
"/bundles/easyadmin/field-file-upload.7f9cb952.js"
]
},
"field-image": {
"js": [
"/field-image.672243bd.js"
"/bundles/easyadmin/field-image.672243bd.js"
]
},
"field-slug": {
"js": [
"/field-slug.978b0c9c.js"
"/bundles/easyadmin/field-slug.978b0c9c.js"
]
},
"field-textarea": {
"js": [
"/field-textarea.74319bb1.js"
"/bundles/easyadmin/field-textarea.74319bb1.js"
]
},
"field-text-editor": {
"css": [
"/field-text-editor.750725ec.css"
"/bundles/easyadmin/field-text-editor.750725ec.css"
],
"js": [
"/field-text-editor.fb2dd889.js"
"/bundles/easyadmin/field-text-editor.fb2dd889.js"
]
},
"login": {
"js": [
"/login.7259f5de.js"
"/bundles/easyadmin/login.7259f5de.js"
]
}
}
Expand Down
Loading

0 comments on commit 0e0bd9f

Please sign in to comment.