bioman1464/laravel-impersonate

Laravel Impersonate 是一个插件,允许您以您的用户身份进行身份验证。

1.7.7 2024-04-22 15:35 UTC

This package is auto-updated.

Last update: 2024-09-22 16:36:52 UTC


README

Build Status Scrutinizer Code Quality

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 添加到您的 用户 模型中。

简单用法

模仿用户

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