rickycezar / laravel-jwt-impersonate
Laravel Impersonate 是一个插件,允许您以用户身份进行身份验证。
1.3.1
2021-05-19 11:09 UTC
Requires
- php: ^7.1.3|^8.0
- laravel/framework: >=5.8
- tymon/jwt-auth: *
Requires (Dev)
- orchestra/database: ~3.8.0
- orchestra/testbench: ~3.8.0
- phpunit/phpunit: ^7.5 || ^8.0
README
免责声明:这是一个 lab404/laravel-impersonate 的分支,修复后可配合 JWTAuth 在 REST API 应用程序中使用。我始终推荐您使用原始组件。
Laravel JWT Impersonate 使您能够轻松地 以用户身份进行身份验证。只需将一个简单的 特质 添加到您的 用户模型 中,即可一键模拟您的用户。
需求
- Laravel >= 5.8
- PHP >= 7.1
- JWT-Auth >= dev-develop
安装
- 使用 Composer 安装它
composer require rickycezar/laravel-jwt-impersonate
- 将服务提供者在您的
config/app.php文件末尾添加
'providers' => [ // ... Rickycezar\Impersonate\ImpersonateServiceProvider::class, ],
- 将特质
Rickycezar\Impersonate\Models\Impersonate添加到您的 User 模型。
简单用法
模拟用户
$token = Auth::user()->impersonate($other_user); // You're now logged as the $other_user and the authentication token is stored in $token.
离开模拟
$token = Auth::user()->leaveImpersonation(); // You're now logged as your original user and the authentication token is stored in $token.
使用内置控制器
在您的路由文件中,如果您想使用内置控制器,可以调用 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) //the url path is "impersonate/take/{id}".
// Generate an URL to leave current impersonation route('impersonate.leave') //the url path is "impersonate/leave".
// Check the current user impersonation status route('impersonate.info') //the url path is "impersonate/info".
高级用法
定义模拟授权
默认情况下,所有用户都可以 模拟 其他用户。
您需要将方法 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; }
使用自定义策略
您可以实现自己的控制器来处理模拟
use Rickycezar\Impersonate\Services\ImpersonateManager; class ImpersonateController extends Controller { protected $manager; // Dependency Injection public function __construct(ImpersonateManager $manager) { $this->manager = $manager; } public function impersonate(){ /*....*/ } public function leave(){ /*....*/ } }
class ImpersonateController extends Controller { protected $manager; //Direct app call public function __construct() { $this->manager = app('impersonate'); } public function impersonate(){ /*....*/ } public function leave(){ /*....*/ } }
- 与管理者协同工作
$manager = app('impersonate'); // Find a user by its ID $manager->findUserById($id); // TRUE if you are impersonating an user. $manager->isImpersonating(); // Impersonate a user. Pass the original user and the user you want to impersonate. Returns authentication token $token = $manager->take($from, $to); // Leave current impersonation. Returns authentication token $token = $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');
异常
服务可以抛出六个可能的异常
AlreadyImpersonatingException在模拟者尝试在没有离开第一个角色的前提下接受另一个角色时抛出。CantBeImpersonatedException在canBeImpersonated()方法失败时抛出。CantImpersonateException在canImpersonate()方法失败时抛出。CantImpersonateSelfException在用户尝试模拟自己时抛出。NotImpersonatingException在用户尝试在没有模拟的情况下离开模拟时抛出。ProtectedFromImpersonationException在模拟者尝试访问由中间件保护的路由时抛出。
每个异常都有通过相应方法 getErrorMessage() 和 getErrorCode() 可用的消息和状态码。
事件
有两个事件可用于改进您的流程
TakeImpersonation在接受模拟时触发。LeaveImpersonation在离开模拟时触发。
每个事件返回两个属性 $event->impersonator 和 $event->impersonated,它们包含一个 User 模型实例。
配置
该包包含一个配置文件。
使用以下命令发布它
php artisan vendor:publish --tag=impersonate
可用选项
// The custom claim key used to store the original user id in the JWT token. 'session_key' => 'impersonated_by',
// The alias for the authentication middleware to be used in the routes. 'auth_alias' => 'auth',
许可协议
MIT