joaopaulolndev/filament-edit-profile

编辑个人资料的 Filament 包

v1.0.30 2024-09-19 11:30 UTC

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

The Filament library is a user-friendly tool that simplifies profile editing, offering an intuitive interface and robust features to easily customize and manage user information.

Screenshot of Application Feature

功能 & 截图

  • 编辑信息:管理您的信息,例如电子邮件和密码。
  • 更改密码:更改您的密码。
  • 个人照片:上传和管理您的个人照片。
  • 删除账户:管理您的账户,例如删除账户。
  • Sanctum 个人访问令牌:管理您的个人访问令牌。
  • 浏览器会话:管理并注销其他浏览器和设备上的活动会话。
  • 自定义字段:在表单中添加自定义字段。
  • 自定义组件:向页面添加自定义组件。
  • 支持: Laravel 11Filament 3.x

安装

您可以通过 composer 安装此包

composer require joaopaulolndev/filament-edit-profile

您可以使用以下命令发布和运行迁移

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

php artisan vendor:publish --tag="filament-edit-profile-views"

可选,您可以使用以下命令发布翻译

php artisan vendor:publish --tag="filament-edit-profile-translations"

您可以使用以下命令发布和运行所有迁移

php artisan vendor:publish --tag="filament-edit-profile-migrations"
php artisan migrate

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

php artisan vendor:publish --tag="filament-edit-profile-config"

用法

在 AdminPanelProvider.php 中添加

use Joaopaulolndev\FilamentEditProfile\FilamentEditProfilePlugin;

->plugins([
    FilamentEditProfilePlugin::make()
])

如果您想显示特定参数以排序、图标、标题、导航组、导航标签,并且可以访问,可以使用以下示例

use Joaopaulolndev\FilamentEditProfile\FilamentEditProfilePlugin;

 ->plugins([
     FilamentEditProfilePlugin::make()
        ->slug('my-profile')
        ->setTitle('My Profile')
        ->setNavigationLabel('My Profile')
        ->setNavigationGroup('Group Profile')
        ->setIcon('heroicon-o-user')
        ->setSort(10)
        ->canAccess(fn () => auth()->user()->id === 1)
        ->shouldRegisterNavigation(false)
        ->shouldShowDeleteAccountForm(false)
        ->shouldShowSanctumTokens()
        ->shouldShowBrowserSessionsForm()
        ->shouldShowAvatarForm()
        ->customProfileComponents([
            \App\Livewire\CustomProfileComponent::class,
        ])
 ])

可选,您可以将用户菜单项添加到导航栏的用户菜单中

use Filament\Navigation\MenuItem;
use Joaopaulolndev\FilamentEditProfile\Pages\EditProfilePage;

->userMenuItems([
    'profile' => MenuItem::make()
        ->label(fn() => auth()->user()->name)
        ->url(fn (): string => EditProfilePage::getUrl())
        ->icon('heroicon-m-user-circle')
        //If you are using tenancy need to check with the visible method where ->company() is the relation between the user and tenancy model as you called
        ->visible(function (): bool {
            return auth()->user()->company()->exists();
        }),
])

个人头像

头像功能截图 使用 shouldShowAvatarForm() 显示用户头像表单。此包遵循 Filament 用户头像 来管理头像。

要显示头像表单,您需要以下步骤

  1. 发布迁移文件以将 avatar_url 字段添加到用户表
php artisan vendor:publish --tag="filament-edit-profile-avatar-migration"
php artisan migrate
  1. 在您的用户模型中,在 fillable 数组中添加 avatar_url 字段
protected $fillable = [
    'name',
    'email',
    'password',
    'avatar_url',
];
  1. 在您的用户模型中设置 getFilamentAvatarUrlAttribute 方法
use Filament\Models\Contracts\HasAvatar;
use Illuminate\Support\Facades\Storage;

class User extends Authenticatable implements HasAvatar
{
    // ...
    public function getFilamentAvatarUrl(): ?string
    {
        return $this->avatar_url ? Storage::url("$this->avatar_url") : null;
    }
}
  1. 可选,您可以指定图像目录路径和文件上传规则。
->shouldShowAvatarForm(
    value: true,
    directory: 'avatars', // image will be stored in 'storage/app/public/avatars
    rules: 'mimes:jpeg,png|max:1024' //only accept jpeg and png files with a maximum size of 1MB
)
  1. 别忘了运行命令 php artisan storage:link

Sanctum 个人访问令牌

显示 Sanctum 令牌管理组件

请参阅 Laravel Sanctum 文档

您可以通过 install:api Artisan 命令安装 Laravel Sanctum

php artisan install:api

Sanctum 允许您颁发 API 令牌/个人访问令牌,这些令牌可用于对您的应用程序进行 API 请求的认证。在通过 API 令牌发出请求时,令牌应包含在授权头中,作为 Bearer 令牌。

use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
{
    use HasApiTokens, HasFactory, Notifiable;
}

Screenshot of Application Feature

如果您想控制访问,您可以使用 condition,传递 Closure 或布尔值

Sanctum 允许您将 "abilities" 分配给令牌。默认情况下,我们有 ['create', 'view', 'update', 'delete'] 使用 permissions 进行自定义

 ->plugins([
    FilamentEditProfilePlugin::make()
        ->shouldShowSanctumTokens(
            condition: fn() => auth()->user()->id === 1, //optional
            permissions: ['custom', 'abilities', 'permissions'] //optional
        )
 ])

浏览器会话

Screenshot of Application Feature

要利用浏览器会话,请确保您的会话配置的驱动程序(或 SESSION_DRIVER 环境变量)设置为 database

SESSION_DRIVER=database

如果您想控制访问或禁用浏览器会话,您可以传递一个闭包或布尔值。

 ->plugins([
    FilamentEditProfilePlugin::make()
        ->shouldShowBrowserSessionsForm(
            fn() => auth()->user()->id === 1, //optional
                //OR
            false //optional
        )
 ])

自定义字段

应用功能截图 可选地,您可以为表单添加自定义字段。要创建自定义字段,需要遵循以下步骤

  1. 发布迁移文件以将自定义字段添加到用户表
php artisan vendor:publish --tag="filament-edit-profile-custom-field-migration"
php artisan migrate
  1. 在您的用户模型中添加自定义字段到 fillable 数组
protected $fillable = [
    'name',
    'email',
    'password',
    'custom_fields',
];
  1. 在您的用户模型中添加自定义字段到 casts 数组
protected function casts(): array
{
    return [
        'email_verified_at' => 'datetime',
        'password' => 'hashed',
        'custom_fields' => 'array'
    ];
}
  1. 使用以下命令发布配置文件
php artisan vendor:publish --tag="filament-edit-profile-config"
  1. 编辑配置文件 config/filament-edit-profile.php 以将自定义字段添加到表单,如下例所示
<?php

return [
    'show_custom_fields' => true,
    'custom_fields' => [
        'custom_field_1' => [
            'type' => 'text',
            'label' => 'Custom Textfield 1',
            'placeholder' => 'Custom Field 1',
            'required' => true,
            'rules' => 'required|string|max:255',
        ],
        'custom_field_2' => [
            'type' => 'password',
            'label' => 'Custom Password field 2',
            'placeholder' => 'Custom Password Field 2',
            'required' => true,
            'rules' => 'required|string|max:255',
        ],
        'custom_field_3' => [
            'type' => 'select',
            'label' => 'Custom Select 3',
            'placeholder' => 'Select',
            'required' => true,
            'options' => [
                'option_1' => 'Option 1',
                'option_2' => 'Option 2',
                'option_3' => 'Option 3',
            ],
        ],
        'custom_field_4' => [
            'type' =>'textarea',
            'label' => 'Custom Textarea 4',
            'placeholder' => 'Textarea',
            'rows' => '3',
            'required' => true,
        ],
        'custom_field_5' => [
            'type' => 'datetime',
            'label' => 'Custom Datetime 5',
            'placeholder' => 'Datetime',
            'seconds' => false,
        ],
        'custom_field_6' => [
            'type' => 'boolean',
            'label' => 'Custom Boolean 6',
            'placeholder' => 'Boolean'
        ],
    ]
];

自定义组件

如果您需要更多控制您的个人资料编辑字段,您可以创建一个自定义组件。为了使此过程更简单,只需使用 artisan 命令。

注意

如果您对使用自定义组件没有信心,请查看 Filament 文档

php artisan make:edit-profile-form CustomProfileComponent

这将生成一个新的 app/Livewire/CustomProfileComponent.php 组件和一个新的 resources/views/livewire/custom-profile-component.blade.php 视图,您可以对其进行自定义。

现在,在您的 Panel Provider 中注册新组件。

->plugins([
    FilamentEditProfilePlugin::make()
        ->customProfileComponents([
            \App\Livewire\CustomProfileComponent::class,
        ]);
])

测试

composer test

变更日志

有关最近更改的更多信息,请参阅 变更日志

贡献

有关详细信息,请参阅 贡献指南

安全漏洞

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

致谢

许可证

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