Skip to content
This repository was archived by the owner on Oct 15, 2025. It is now read-only.

Commit af3dc41

Browse files
committed
feat: Allow to impersonate users
1 parent d2b5725 commit af3dc41

File tree

4 files changed

+20
-4
lines changed

4 files changed

+20
-4
lines changed

config/packages/security.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,12 @@ security:
2727
secret: "%kernel.secret%" # required
2828
lifetime: 604800 # 1 week in seconds
2929

30+
# https://symfony.com/doc/current/security/impersonating_user.html
31+
switch_user: true
32+
3033
# activate different ways to authenticate
3134
# https://symfony.com/doc/current/security.html#the-firewall
3235

33-
# https://symfony.com/doc/current/security/impersonating_user.html
34-
# switch_user: true
35-
3636
# Easy way to control access for large sections of your site
3737
# Note: Only the *first* access control that matches will be used
3838
access_control:

src/Controller/Admin/UserCrudController.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
namespace App\Controller\Admin;
66

77
use App\Entity\User;
8+
use EasyCorp\Bundle\EasyAdminBundle\Config\Action;
9+
use EasyCorp\Bundle\EasyAdminBundle\Config\Actions;
10+
use EasyCorp\Bundle\EasyAdminBundle\Config\Crud;
811
use EasyCorp\Bundle\EasyAdminBundle\Controller\AbstractCrudController;
912
use EasyCorp\Bundle\EasyAdminBundle\Field\ArrayField;
1013
use EasyCorp\Bundle\EasyAdminBundle\Field\AssociationField;
@@ -54,4 +57,13 @@ public function configureFields(string $pageName): iterable
5457
DateTimeField::new('updated_at', 'Updated at')->hideOnForm(),
5558
];
5659
}
60+
61+
public function configureActions(Actions $actions): Actions
62+
{
63+
return $actions->add(
64+
Crud::PAGE_INDEX,
65+
Action::new('impersonate', 'Impersonate')
66+
->linkToUrl(fn (User $user) => "/?_switch_user={$user->getUserIdentifier()}")
67+
);
68+
}
5769
}

templates/components/Navbar.html.twig

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,12 @@
5757
{{ app.user.name }}
5858
</button>
5959
<ul class="dropdown-menu dropdown-menu-end p-2 rounded-3">
60-
{% if 'ROLE_ADMIN' in app.user.roles %}
60+
{% if is_granted('ROLE_ADMIN') %}
6161
<li><a class="dropdown-item rounded-2 mb-2" href="{{ path('admin') }}">管理後台</a></li>
6262
{% endif %}
63+
{% if is_granted('IS_IMPERSONATOR') %}
64+
<li><a class="dropdown-item rounded-2 mb-2" href="{{ impersonation_exit_path(path('app_home')) }}">結束模擬</a></li>
65+
{% endif %}
6366
<li><a class="dropdown-item rounded-2 mb-2" href="{{ path('app_profile') }}">個人資料</a></li>
6467
<li><a class="dropdown-item rounded-2" href="{{ path('app_logout') }}">登出</a></li>
6568
</ul>

translations/messages.zh_TW.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ Mark Resolved: 標記為已解決
5454
Mark Closed: 標記為已關閉
5555
New & In Progress: 新問題 & 處理中
5656
Resolved & Closed: 已解決 & 已關閉
57+
Impersonate: 模擬使用者
5758

5859
challenge.error-type.user: 輸入錯誤
5960
challenge.error-type.server: 伺服器錯誤

0 commit comments

Comments
 (0)