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 e80ccc5
Show file tree
Hide file tree
Showing 20 changed files with 106 additions and 11 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();
});
}
}
2 changes: 1 addition & 1 deletion public/app.d52c6a33.css

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions public/app.3048ce28.js → public/app.fb2fb73c.js

Large diffs are not rendered by default.

File renamed without changes.
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"app.css": "app.d52c6a33.css",
"app.js": "app.3048ce28.js",
"app.js": "app.fb2fb73c.js",
"form.js": "form.ec4ce275.js",
"page-layout.js": "page-layout.3347892e.js",
"page-color-scheme.js": "page-color-scheme.30cb23c2.js",
Expand Down
27 changes: 27 additions & 0 deletions src/Config/Menu/AccessImpersonationMenuItem.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace EasyCorp\Bundle\EasyAdminBundle\Config\Menu;

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

/**
* @see EasyCorp\Bundle\EasyAdminBundle\Config\MenuItem::linkToAccessImpersonation()
*
* @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');
}
}
9 changes: 9 additions & 0 deletions src/Config/MenuItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace EasyCorp\Bundle\EasyAdminBundle\Config;

use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\AccessImpersonationMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\CrudMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\DashboardMenuItem;
use EasyCorp\Bundle\EasyAdminBundle\Config\Menu\ExitImpersonationMenuItem;
Expand Down Expand Up @@ -45,6 +46,14 @@ 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
2 changes: 1 addition & 1 deletion 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
2 changes: 1 addition & 1 deletion src/Resources/public/manifest.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"app.css": "app.d52c6a33.css",
"app.js": "app.3048ce28.js",
"app.js": "app.fb2fb73c.js",
"form.js": "form.ec4ce275.js",
"page-layout.js": "page-layout.3347892e.js",
"page-color-scheme.js": "page-color-scheme.30cb23c2.js",
Expand Down
1 change: 1 addition & 0 deletions src/Security/Permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ final class Permission
public const EA_VIEW_MENU_ITEM = 'EA_VIEW_MENU_ITEM';
public const EA_VIEW_FIELD = 'EA_VIEW_FIELD';
public const EA_EXIT_IMPERSONATION = 'EA_EXIT_IMPERSONATION';
public const EA_CAN_SWITCH_USER = 'EA_CAN_SWITCH_USER';

public static function exists(?string $permissionName): bool
{
Expand Down
29 changes: 29 additions & 0 deletions templates/includes/_impersonate_modal.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<div id="modal-impersonate" class="modal fade" tabindex="-1">
<div class="modal-dialog">
<div class="modal-content">
<div class="modal-body p-4">
<form method="get">
<div class="row">
<div class="col-12">
<div class="field-text form-group">
<label for="_switch_user" class="form-control-label required">{{ 'impersonate.username'|trans(domain = 'EasyAdminBundle') }}</label>
<div class="form-widget">
<input type="text" name="_switch_user" id="impersonate_user_name" required="required" class="form-control" value="">
</div>
</div>
</div>
</div>
</form>
</div>
<div class="modal-footer">
<button type="button" data-bs-dismiss="modal" class="btn btn-sm btn-secondary" id="impersonate-modal-clear-button" formtarget="_self">
<i class="fa fa-close"></i> {{ 'action.close'|trans(domain = 'EasyAdminBundle') }}
</button>

<button type="button" data-bs-dismiss="modal" class="btn btn-sm btn-primary" id="impersonate-modal-apply-button" formtarget="_self">
<i class="fa fa-check"></i> {{ 'user.impersonate_user'|trans(domain = 'EasyAdminBundle') }}
</button>
</div>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions templates/layout.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,9 @@
{% elseif not item.isMenuSection %}
<a href="{{ item.linkUrl }}" class="dropdown-item user-action {{ item.cssClass }}"
target="{{ item.linkTarget }}" rel="{{ item.linkRel }}"
{% if item.type == "access_impersonation" %}
data-bs-toggle="modal" data-bs-target="#modal-impersonate" id="modal-impersonate-link"
{% endif %}
referrerpolicy="origin-when-cross-origin">
{% if item.icon is not empty %}<i class="fa fa-fw {{ item.icon }}"></i>{% endif %}
{{ item.label|trans }}
Expand Down Expand Up @@ -361,6 +364,9 @@

<section id="main" class="content-body">
{% block main %}{% endblock %}
{% if impersonator_permission == 'IS_IMPERSONATOR' %}
{{ include('@EasyAdmin/includes/_impersonate_modal.html.twig') }}
{% endif %}
</section>

{% block content_footer_wrapper %}
Expand Down
5 changes: 5 additions & 0 deletions translations/EasyAdminBundle.en.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@
'anonymous' => 'Anonymous User',
'sign_out' => 'Sign out',
'exit_impersonation' => 'Exit impersonation',
'impersonate_user' => 'Impersonate user',
],

'settings' => [
Expand Down Expand Up @@ -141,4 +142,8 @@
'no-more-results' => 'No more results',
'loading-more-results' => 'Loading more results…',
],

'impersonate' => [
'username' => 'Username to impersonate',
],
];
7 changes: 6 additions & 1 deletion translations/EasyAdminBundle.es.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,8 @@
'unnamed' => 'Usuario sin nombre',
'anonymous' => 'Usuario anónimo',
'sign_out' => 'Cerrar sesión',
'exit_impersonation' => 'Terminar impersonación',
'exit_impersonation' => 'Terminar suplantación',
'impersonate_user' => 'Suplantar usuario',
],

'settings' => [
Expand Down Expand Up @@ -141,4 +142,8 @@
'no-more-results' => 'No hay más resultados',
'loading-more-results' => 'Cargando más resultados…',
],

'impersonate' => [
'username' => 'Nombre de usuario a suplantar',
],
];

0 comments on commit e80ccc5

Please sign in to comment.