chiiya/filament-access-control
Composer 安装命令:
composer require chiiya/filament-access-control
包简介
Admin user, role and permission management for Laravel Filament
README 文档
README
Filament Access Control
Opinionated setup for managing admin users, roles and permissions within Laravel Filament
Features
- Separate database table for filament admin users (separate model, separate guard, separate password broker)
- Uses spatie/laravel-permission for roles and permissions
- Fully localized
- CRUD resources for admin users, roles and permissions
- Admin users may belong to one role
- Admin users can have direct permissions or indirect permissions through their role
- When creating admin users through the admin interface, no password is specified. Instead, the user receives an email prompting them to set their password
- Optional account expiry for admin users. Expired accounts are no longer able to log in
- Optional email based two-factor authentication.
Installation
- Install the package via composer:
composer require chiiya/filament-access-control
- Update your Filament Panel ServiceProvider and register the plugin:
use Chiiya\FilamentAccessControl\FilamentAccessControlPlugin; return $panel ->default() ->id('admin') ->path('admin') ->plugin(FilamentAccessControlPlugin::make())
You may remove any calls to login() or other methods that configure the authentication process, since the plugin
takes care of that.
- Publish the migrations and config, then run the migrations. Make sure you also publish and run the spatie/laravel-permission migrations if you haven't done so yet.
php artisan vendor:publish --tag="filament-access-control-migrations" php artisan vendor:publish --tag="filament-access-control-config" php artisan vendor:publish --provider="Spatie\Permission\PermissionServiceProvider" php artisan migrate
-
To seed the necessary base data (role & permissions), run
php artisan filament-access-control:installor call theChiiya\FilamentAccessControl\Database\Seeders\FilamentAccessControlSeederseeder in your database seeder. -
Create an admin user using
php artisan filament-access-control:user. If you create users programmatically (e.g. in your database seeder), make sure to assign them thesuper-adminrole if you want them to be able to access the role and user management.
Optionally, you can publish the translations with:
php artisan vendor:publish --tag="filament-access-control-translations"
Optionally, you can publish the views with:
php artisan vendor:publish --tag="filament-access-control-views"
Usage
Authorizing Resources, Pages & Actions
Authorizing Resources
To authorize access to resources, use policies as described in the Filament documentation.
class ProductPolicy { public function viewAny(FilamentUser $user): bool { return $user->can('products.view'); } // ... }
Authorizing Pages
This package comes with a simple trait that you can use to authorize access to custom pages based on a permission.
use Chiiya\FilamentAccessControl\Traits\AuthorizesPageAccess; class MyPage extends Page { use AuthorizesPageAccess; public static string $permission = 'my-page.view'; public function mount(): void { static::authorizePageAccess(); } }
Authorizing Actions
One way to authorize actions is to use the visible() method:
ButtonAction::make('exports') ->visible(fn () => Filament::auth()->user()->can('exports.view'))
Localizing Role & Permission Names
Roles and permissions should have names that make them easy to use in code (e.g. admin-users.update).
For the admin you may however wish to localize them or make them more readable. You can do so by simply
adding a JSON translation entry for the given role or permission name (e.g. lang/en.json):
{
"admin-users.update": "Admin Users → Edit"
}
Feature: Account Expiry
With the optional account expiry feature, all accounts require an expiration date. When accounts are expired, they can no longer log in. To enable the account expiry feature, enable the feature flag in the config file:
'features' => [ \Chiiya\FilamentAccessControl\Enumerators\Feature::ACCOUNT_EXPIRY, ],
You will also need to add the EnsureAccountIsNotExpired middleware to your filament auth middleware config in your
panel service provider:
use Chiiya\FilamentAccessControl\Http\Middleware\EnsureAccountIsNotExpired; ... ->authMiddleware([ Authenticate::class, EnsureAccountIsNotExpired::class, ]);
Feature: Two-Factor Authentication
With the optional two-factor authentication feature, users must enter a verification code sent via email upon login. To enable the two-factor authentication feature, enable the feature flag in the config file:
'features' => [ \Chiiya\FilamentAccessControl\Enumerators\Feature::TWO_FACTOR, ],
Custom User Model
To use your own custom user model for the admin (instead of Chiiya\FilamentAccessControl\Models\FilamentUser),
point the value of user_model in the filament-access-control config file to your own model.
'user_model' => CustomFilamentUser::class,
Please make sure that your model either extends the FilamentUser base case or implements the
Chiiya\FilamentAccessControl\Contracts\AccessControlUser interface.
use Chiiya\FilamentAccessControl\Models\FilamentUser; use Chiiya\FilamentAccessControl\Contracts\AccessControlUser; use Filament\Models\Contracts\FilamentUser as FilamentUserInterface; use Filament\Models\Contracts\HasName; use Illuminate\Foundation\Auth\User as Authenticatable; class CustomFilamentUser extends FilamentUser { // ... } // Or alternatively class CustomFilamentUser extends Authenticatable implements AccessControlUser, FilamentUserInterface, HasName { // ... }
Extending Resources
To extend the resources used for managing admin users, roles and permissions, you can adjust the resources config
value:
/* |-------------------------------------------------------------------------- | Resources |-------------------------------------------------------------------------- | Resources used for managing users, roles and permissions. */ 'resources' => [ 'user' => FilamentUserResource::class, 'role' => RoleResource::class, 'permission' => PermissionResource::class, ]
The easiest way to extend the resources is to create your own resource classes that extend the default ones, and overwrite the following methods:
public static function insertBeforeFormSchema(): array
{
return [];
}
public static function insertAfterFormSchema(): array
{
return [];
}
public static function insertBeforeTableSchema(): array
{
return [];
}
public static function insertAfterTableSchema(): array
{
return [];
}
Screenshots
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
License
The MIT License (MIT). Please see License File for more information.
chiiya/filament-access-control 适用场景与选型建议
chiiya/filament-access-control 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 51.87k 次下载、GitHub Stars 达 218, 最近一次更新时间为 2022 年 02 月 23 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「roles」 「permissions」 「filament」 「chiiya」 「filament-access-control」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 chiiya/filament-access-control 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 chiiya/filament-access-control 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 chiiya/filament-access-control 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Laravel Multiauth package
User permissions for MoonShine
This package provides a flexible way to add Role-based Permissions to Laravel 6.x
This package provides a flexible way to add Role-based Permissions to Laravel
Alfabank REST API integration
Library for advanced filesystem functions
统计信息
- 总下载量: 51.87k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 218
- 点击次数: 28
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-02-23




