maurohmartinez / impersonate-users-backpack-laravel
一个简单的包,为管理员添加模拟用户操作
1.1.1
2024-08-14 12:20 UTC
Requires
- php: ^8.0
- backpack/crud: ^5.0|^6.0
- laravel/framework: ^8.0.0|^9.0.0|^10.0.0|^11.0.0
README
这是一个允许管理员模拟用户的简单包。
安装
在您的终端中
# install the package
composer require maurohmartinez/impersonate-users-backpack-laravel
如果您想/需要发布文件以进一步自定义此包
php artisan vendor:publish --provider="MHMartinez\ImpersonateUser\app\Providers\ImpersonateUserServiceProvider"
用法
1- 在您的 UserCrudController
中添加。
use \MHMartinez\ImpersonateUser\app\Http\Controllers\Operations\ImpersonateUserOperation;
这将为列表和显示操作添加模拟用户的按钮。
2- 下一步是处理逻辑,以指示哪些管理员有权限模拟其他用户,或可以被模拟。您只需修改一下您的 User Model
以实现接口 ImpersonateInterface
如下所示
class User extends Authenticatable implements ImpersonateInterface
然后在您的 User Model
中添加以下两个方法
/** * If you use Laravel-Backpack/PermissionManager you can do like this. * But you can also add any logic you need. */ public function canImpersonateOthers(): bool { return $this->can('permission_to_impersonate'); // or replace "permission_to_impersonate" with the right permission } /** * Following the same example, you can deny admins from impersonating super admins. */ public function canBeImpersonated(): bool { return !$this->hasRole('superadmin'); // or replace "superadmin" with the right permission }
3- 现在,您只需添加退出模拟的按钮(无需担心,它只会在需要时显示)。例如,您可以在 topbar_right_content.blade.php
中添加按钮如下
@include('impersonate_user::exit_impersonated')
4- 重要 — 如果您想模拟非管理员用户,则需要跳过确定用户是否为管理员的 backpack 中间件。这是因为此操作需要允许您的模拟非管理员用户使用 backpack 路由来退出。如何做?
- 如果您还没有这样做,请发布配置文件(如上一步所述)。
php artisan vendor:publish --provider="MHMartinez\ImpersonateUser\app\Providers\ImpersonateUserServiceProvider" --tag=config
- 添加中间件类名,如下所示,然后您就可以开始了
return [ 'session_key' => 'impersonating_user', 'base_guard' => 'backpack', 'admin_middleware' => Path\To\Middleware\IsAdmin::class, ];
这将允许此操作在退出模拟的非管理员用户时跳过该中间件。