alfaandfriends / user-impersonate
Laravel Impersonate 是一个插件,允许您以用户身份进行认证。
Requires
- php: ^7.2 | ^8.0
- laravel/framework: ^6.0 | ^7.0 | ^8.0 | ^9.0 | ^10.0 | ^11.0
Requires (Dev)
- mockery/mockery: ^1.3.3
- orchestra/testbench: ^4.0 | ^5.0 | ^6.0 | ^7.0 | ^8.0 | ^9.0
- phpunit/phpunit: ^7.5 | ^8.0 | ^9.0 | ^10.0
This package is auto-updated.
Last update: 2024-09-18 09:19:40 UTC
README
Laravel Impersonate 使您能够轻松地 以用户身份进行认证。只需将一个简单的 特质 添加到您的 用户模型 中,即可一键模拟用户。
要求
- Laravel 6.x 到 11.x
- PHP >= 7.2 或 >= 8.0
Laravel 支持
安装
- 使用 Composer 安装它
composer require lab404/laravel-impersonate
- 在您的
config/app.php
文件的末尾添加服务提供程序
'providers' => [ // ... Lab404\Impersonate\ImpersonateServiceProvider::class, ],
- 将特质
Lab404\Impersonate\Models\Impersonate
添加到您的 User 模型。
简单用法
模拟用户
Auth::user()->impersonate($other_user); // You're now logged as the $other_user
退出模拟
Auth::user()->leaveImpersonation(); // You're now logged as your original user.
使用内置控制器
在您的路由文件中,在 web 中间件下,您必须调用 impersonate
路由宏。
Route::impersonate();
或者,您可以使用 RouteServiceProvider
执行此宏。
namespace App\Providers; class RouteServiceProvider extends ServiceProvider { public function map() { Route::middleware('web')->group(function (Router $router) { $router->impersonate(); }); } }
// Where $id is the ID of the user you want impersonate route('impersonate', $id) // Or in case of multi guards, you should also add `guardName` (defaults to `web`) route('impersonate', ['id' => $id, 'guardName' => 'admin']) // Generate an URL to leave current impersonation route('impersonate.leave')
高级用法
定义模拟授权
默认情况下,所有用户都可以 模拟 用户。
您需要在用户模型中添加方法 canImpersonate()
/** * @return bool */ public function canImpersonate() { // For example return $this->is_admin == 1; }
默认情况下,所有用户都可以 被模拟。
您需要将方法 canBeImpersonated()
添加到用户模型以扩展此行为
/** * @return bool */ public function canBeImpersonated() { // For example return $this->can_be_impersonated == 1; }
使用自己的策略
- 获取管理器
// With the app helper app('impersonate') // Dependency Injection public function impersonate(ImpersonateManager $manager, $user_id) { /* ... */ }
- 与管理器一起工作
$manager = app('impersonate'); // Find an user by its ID $manager->findUserById($id); // TRUE if your are impersonating an user. $manager->isImpersonating(); // Impersonate an user. Pass the original user and the user you want to impersonate $manager->take($from, $to); // Leave current impersonation $manager->leave(); // Get the impersonator ID $manager->getImpersonatorId();
中间件
防止模拟
您可以使用中间件 impersonate.protect
来防止您的路由受到用户模拟的影响。
当您想保护特定页面,如用户订阅、用户信用卡等时,此中间件非常有用。
Router::get('/my-credit-card', function() { echo "Can't be accessed by an impersonator"; })->middleware('impersonate.protect');
事件
有两个事件可用于改进您的流程
TakeImpersonation
在进行模拟时触发。LeaveImpersonation
在退出模拟时触发。
每个事件都返回两个属性 $event->impersonator
和 $event->impersonated
,包含用户模型实例。
配置
此软件包包含一个配置文件。
使用以下命令发布它
php artisan vendor:publish --tag=impersonate
可用选项
// The session key used to store the original user id. 'session_key' => 'impersonated_by', // Where to redirect after taking an impersonation. // Only used in the built-in controller. // You can use: an URI, the keyword back (to redirect back) or a route name 'take_redirect_to' => '/', // Where to redirect after leaving an impersonation. // Only used in the built-in controller. // You can use: an URI, the keyword back (to redirect back) or a route name 'leave_redirect_to' => '/'
Blade
有三种 Blade 指令可用。
当用户可以模拟时
@canImpersonate($guard = null) <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a> @endCanImpersonate
当用户可以被模拟时
这在您有一个用户列表并希望在每个用户旁边显示一个“模拟”按钮时很有用。但您不希望在当前认证的用户或根据您对 canBeImpersonated()
的实现不应被模拟的用户旁边显示该按钮。
@canBeImpersonated($user, $guard = null) <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a> @endCanBeImpersonated
当用户被模拟时
@impersonating($guard = null) <a href="{{ route('impersonate.leave') }}">Leave impersonation</a> @endImpersonating
测试
vendor/bin/phpunit
贡献者
原理
为什么不使用 loginAsId()
呢?
此软件包添加了更广泛的功能,包括 Blade 指令,允许您在模拟时覆盖分析和其他跟踪事件,根据模拟状态触发事件,等等。有关简短讨论,请参阅 issues/5
许可证
MIT