xlite-dev/filament-impersonate

这是我的包 filament-impersonate

2.0.1 2024-06-27 05:21 UTC

This package is auto-updated.

Last update: 2024-09-27 05:49:44 UTC


README

Latest Version on Packagist Total Downloads

安装

您可以通过composer安装此包

composer require xlite-dev/filament-impersonate

用法

1. 添加 Table 动作

打开您希望出现模拟动作的资源。这通常是您的 UserResource 类。

滚动到 table 方法。在 actionsprependActions 中添加 ImpersonateAction::make 作为新动作。您的类应如下所示

namespace App\Filament\Resources;

use Filament\Resources\Resource;
use XliteDev\FilamentImpersonate\Tables\Actions\ImpersonateAction; // <---

class UserResource extends Resource {
    // ...

    public static function table(Table $table)
    {
        return $table
            ->columns([
                // ...
            ])
            ->actions([
                ImpersonateAction::make(), // <--- 
                // ...
            ]);
    }

现在您应该在 Filament UserResource 列表中的每个用户旁边看到动作图标

image

当您点击模拟图标时,您将登录为该用户,并被重定向到主应用。您将在页面的顶部看到模拟横幅,其中有一个按钮可以离开并返回 Filament

image

2. 添加 Page 动作

打开 EditUserViewUser 类,您希望出现模拟动作的地方。

滚动到 getActions 方法并添加 ImpersonateAction::make 作为新动作。您的类应如下所示

namespace App\Filament\Resources;

use Filament\Resources\Resource;
use XliteDev\FilamentImpersonate\Pages\Actions\ImpersonateAction; // <---

class EditUser extends ViewRecord
{
    // ...

    protected function getActions(): array
    {
        return [
            ImpersonateAction::make()->record($this->getRecord()), // <---
            // ...
        ];
    }

现在您应该在 EditUserViewUser 页面上看到动作图标

image

配置

您可以使用以下命令发布配置文件

php artisan vendor:publish --tag="filament-impersonate-config"

这是已发布配置文件的内容

return [

    // This is the guard used when logging in as the impersonated user.
    'guard' => env('FILAMENT_IMPERSONATE_GUARD', 'web'),

    // After impersonating this is where we'll redirect you to.
    'redirect_to' => env('FILAMENT_IMPERSONATE_REDIRECT', '/'),

    // We wire up a route for the "leave" button. You can change the middleware stack here if needed.
    'leave_middlewares' => [
        env('FILAMENT_IMPERSONATE_LEAVE_MIDDLEWARE', 'web'),
    ],

    'banner' => [
        // Currently supports 'dark' and 'light'.
        'style' => env('FILAMENT_IMPERSONATE_BANNER_STYLE', 'dark'),

        // Turn this off if you want `absolute` positioning, so the banner scrolls out of view
        'fixed' => env('FILAMENT_IMPERSONATE_BANNER_FIXED', true),

        // Currently supports 'top' and 'bottom'.
        'position' => env('FILAMENT_IMPERSONATE_BANNER_POSITION', 'top'),
    ],
];

授权

默认情况下,只有 Filament 管理员可以模拟其他用户。您可以通过向您的 FilamentUser 类添加 canImpersonate 方法来控制此操作

class User implements FilamentUser {
    
    public function canImpersonate()
    {
        return true;
    }
    
}

您还可以控制哪些目标可以被模拟。只需在用户类中添加 canBeImpersonated 方法,并添加您需要的任何逻辑即可

class User {

    public function canBeImpersonated()
    {
        // Let's prevent impersonating other users at our own company
        return !Str::endsWith($this->email, '@mycorp.com');
    }
    
}

自定义横幅

您可以使用以下命令发布视图

php artisan vendor:publish --tag="filament-impersonate-views"

Blade 组件有几个可定制的选项。

样式

横幅默认为深色,您可以将其设置为浅色

<x-filament-impersonate::banner style='light'/>

显示名称

横幅将显示被模拟用户的名称,前提是有 name 属性。如果需要,您可以自定义此设置

<x-filament-impersonate::banner :display='auth()->user()->email'/>

测试

composer test

更改日志

有关最近更改的更多信息,请参阅CHANGELOG

贡献

有关详细信息,请参阅CONTRIBUTING

安全漏洞

有关如何报告安全漏洞的详细信息,请参阅我们的安全策略

致谢

许可协议

MIT 许可协议(MIT)。有关更多信息,请参阅许可文件