README.md
以下内容由 AI 翻译,如有问题请点此提交 issue 反馈
Laravel Impersonate
Laravel Impersonate 让以用户身份进行身份验证变得轻而易举。只需在你的用户模型中添加一个简单的trait,即可一键模拟为其中一位用户。
- Requirements
- Installation
- Simple usage
- Advanced Usage
- Configuration
- Blade
- Tests
- Contributors
- Why Not Just Use loginAsId()?
Requirements
- Laravel 6.x to 13.x
- PHP >= 7.2 or >= 8.0
Laravel support
| Version | Release |
|---|---|
| 6.x to 13.x | 1.7 |
| 6.x, 7.x | 1.6 |
| 5.8 | 1.5 |
| 5.7, 5.6 | 1.2 |
| 5.5, 5.4 | 1.1 |
Installation
- Require it with Composer:
composer require lab404/laravel-impersonate
- Add the service provider at the end of your
config/app.php:
'providers' => [
// ...
Lab404\Impersonate\ImpersonateServiceProvider::class,
],
- Add the trait
Lab404\Impersonate\Models\Impersonateto your User model.
Simple usage
Impersonate a user:
Auth::user()->impersonate($other_user);
// You're now logged as the $other_user
Leave impersonation:
Auth::user()->leaveImpersonation();
// You're now logged as your original user.
Using the built-in controller
In your routes file, under web middleware, you must call the impersonate route macro.
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,其中包含 User 模型实例。
配置
该包附带一个配置文件。
使用以下命令发布它:
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
贡献者
- 本包由 MarceauKa 和 tghpow 创建。感谢所有 contributors。
设计理由
为什么不直接使用 loginAsId()?
本包增加了更广泛的功能,包括允许在模拟身份时覆盖分析和其他跟踪事件的 Blade 指令,基于模拟状态触发事件等。简要讨论见 issues/5
许可证
MIT
