macromindonline / laravel-impersonate
Laravel Impersonate 是一个插件,允许您以用户的身份进行身份验证。
1.2.1
2017-09-03 19:24 UTC
Requires
- php: >=7.0
- laravel/framework: >=5.5
Requires (Dev)
- orchestra/database: ~3.4.0
- orchestra/testbench: ~3.4.0
- phpunit/phpunit: ^5.7
README
Laravel Impersonate 使您能够轻松地 以用户的身份进行身份验证。只需将一个简单的 特质 添加到您的 用户模型 中,即可一键模拟用户。
要求
- Laravel >= 5.5
- PHP >= 7
有关 Laravel <= 5.4 的支持,请参阅版本 v1.1。
安装
- 使用 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.
使用内置控制器
在您的路由文件中,您必须调用 impersonate
路由宏。
Route::impersonate();
// Where $id is the ID of the user you want impersonate route('impersonate', $id) // 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_impersonate == 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 <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a> @endCanImpersonate
当用户可以被模拟时
当您有一个用户列表并想在所有用户旁边显示“模拟”按钮时,这非常有用。但您不希望在当前已认证的用户或根据您对 canBeImpersonated()
的实现不应被模拟的用户旁边显示此按钮。
@canBeImpersonated($user) <a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a> @endCanBeImpersonated
当用户被模拟时
@impersonating <a href="{{ route('impersonate.leave') }}">Leave impersonation</a> @endImpersonating
测试
vendor/bin/phpunit
贡献者
许可证
MIT