开源软件名称(OpenSource Name):404labfr/laravel-impersonate开源软件地址(OpenSource Url):https://github.com/404labfr/laravel-impersonate开源编程语言(OpenSource Language):PHP 99.2%开源软件介绍(OpenSource Introduction):Laravel ImpersonateLaravel Impersonate makes it easy to authenticate as your users. Add a simple trait to your user model and impersonate as one of your users in one click.
Requirements
Laravel support
Installation
composer require lab404/laravel-impersonate
'providers' => [
// ...
Lab404\Impersonate\ImpersonateServiceProvider::class,
],
Simple usageImpersonate 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 controllerIn your routes file, under web middleware, you must call the Route::impersonate(); Alternatively, you can execute this macro with your 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') Advanced UsageDefining impersonation authorizationBy default all users can impersonate an user. /**
* @return bool
*/
public function canImpersonate()
{
// For example
return $this->is_admin == 1;
} By default all users can be impersonated. /**
* @return bool
*/
public function canBeImpersonated()
{
// For example
return $this->can_be_impersonated == 1;
} Using your own strategy
// 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(); MiddlewareProtect From Impersonation You can use the middleware Router::get('/my-credit-card', function() {
echo "Can't be accessed by an impersonator";
})->middleware('impersonate.protect'); EventsThere are two events available that can be used to improve your workflow:
Each events returns two properties ConfigurationThe package comes with a configuration file. Publish it with the following command: php artisan vendor:publish --tag=impersonate Available options: // 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' => '/' BladeThere are three Blade directives available. When the user can impersonate@canImpersonate($guard = null)
<a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanImpersonate When the user can be impersonatedThis comes in handy when you have a user list and want to show an "Impersonate" button next to all the users.
But you don't want that button next to the current authenticated user neither to that users which should not be able to impersonated according your implementation of @canBeImpersonated($user, $guard = null)
<a href="{{ route('impersonate', $user->id) }}">Impersonate this user</a>
@endCanBeImpersonated When the user is impersonated@impersonating($guard = null)
<a href="{{ route('impersonate.leave') }}">Leave impersonation</a>
@endImpersonating Testsvendor/bin/phpunit Contributors
Rationale
Why not just use |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论