feat: RoleTemplateResource + UserResource UI for Permission System
This commit is contained in:
11
app/Filament/Resources/Users/Pages/CreateUser.php
Normal file
11
app/Filament/Resources/Users/Pages/CreateUser.php
Normal file
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Pages;
|
||||
|
||||
use App\Filament\Resources\Users\UserResource;
|
||||
use Filament\Resources\Pages\CreateRecord;
|
||||
|
||||
class CreateUser extends CreateRecord
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
}
|
||||
19
app/Filament/Resources/Users/Pages/EditUser.php
Normal file
19
app/Filament/Resources/Users/Pages/EditUser.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Pages;
|
||||
|
||||
use App\Filament\Resources\Users\UserResource;
|
||||
use Filament\Actions\DeleteAction;
|
||||
use Filament\Resources\Pages\EditRecord;
|
||||
|
||||
class EditUser extends EditRecord
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
DeleteAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
19
app/Filament/Resources/Users/Pages/ListUsers.php
Normal file
19
app/Filament/Resources/Users/Pages/ListUsers.php
Normal file
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Pages;
|
||||
|
||||
use App\Filament\Resources\Users\UserResource;
|
||||
use Filament\Actions\CreateAction;
|
||||
use Filament\Resources\Pages\ListRecords;
|
||||
|
||||
class ListUsers extends ListRecords
|
||||
{
|
||||
protected static string $resource = UserResource::class;
|
||||
|
||||
protected function getHeaderActions(): array
|
||||
{
|
||||
return [
|
||||
CreateAction::make(),
|
||||
];
|
||||
}
|
||||
}
|
||||
61
app/Filament/Resources/Users/Schemas/UserForm.php
Normal file
61
app/Filament/Resources/Users/Schemas/UserForm.php
Normal file
@@ -0,0 +1,61 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Schemas;
|
||||
|
||||
use App\Models\RoleTemplate;
|
||||
use Filament\Forms\Components\Select;
|
||||
use Filament\Forms\Components\TagsInput;
|
||||
use Filament\Forms\Components\TextInput;
|
||||
use Filament\Schemas\Components\Section;
|
||||
use Filament\Schemas\Components\Grid;
|
||||
use Filament\Schemas\Schema;
|
||||
|
||||
class UserForm
|
||||
{
|
||||
public static function configure(Schema $schema): Schema
|
||||
{
|
||||
return $schema
|
||||
->components([
|
||||
Grid::make(2)
|
||||
->schema([
|
||||
Section::make('Thông tin tài khoản')
|
||||
->columnSpan(1)
|
||||
->schema([
|
||||
TextInput::make('name')
|
||||
->label('Họ tên')
|
||||
->required(),
|
||||
TextInput::make('email')
|
||||
->label('Email')
|
||||
->email()
|
||||
->required()
|
||||
->unique(ignoreRecord: true),
|
||||
]),
|
||||
|
||||
Section::make('Phân quyền')
|
||||
->columnSpan(1)
|
||||
->schema([
|
||||
Select::make('role_template_id')
|
||||
->label('Nhóm quyền (Mẫu)')
|
||||
->relationship('roleTemplate', 'name')
|
||||
->searchable()
|
||||
->preload()
|
||||
->placeholder('Không theo mẫu nào'),
|
||||
|
||||
TagsInput::make('extra_permissions')
|
||||
->label('Thêm quyền (vượt cấp)')
|
||||
->placeholder('ví dụ: contracts.export, payments.delete')
|
||||
->helperText('Nhập quyền muốn thêm cho user này, bất chấp mẫu nhóm')
|
||||
->separator(',')
|
||||
->splitKeys([',', 'Enter']),
|
||||
|
||||
TagsInput::make('excluded_permissions')
|
||||
->label('Bớt quyền (hạn chế)')
|
||||
->placeholder('ví dụ: contracts.delete')
|
||||
->helperText('Nhập quyền muốn tắt cho user này, bất chấp mẫu nhóm')
|
||||
->separator(',')
|
||||
->splitKeys([',', 'Enter']),
|
||||
]),
|
||||
]),
|
||||
]);
|
||||
}
|
||||
}
|
||||
43
app/Filament/Resources/Users/Tables/UsersTable.php
Normal file
43
app/Filament/Resources/Users/Tables/UsersTable.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users\Tables;
|
||||
|
||||
use Filament\Tables;
|
||||
use Filament\Tables\Columns\TextColumn;
|
||||
use Filament\Tables\Table;
|
||||
|
||||
class UsersTable
|
||||
{
|
||||
public static function configure(Table $table): Table
|
||||
{
|
||||
return $table
|
||||
->columns([
|
||||
TextColumn::make('name')
|
||||
->label('Họ tên')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('email')
|
||||
->label('Email')
|
||||
->searchable()
|
||||
->sortable(),
|
||||
|
||||
TextColumn::make('roleTemplate.name')
|
||||
->label('Nhóm quyền')
|
||||
->placeholder('Không có')
|
||||
->badge()
|
||||
->color('primary'),
|
||||
|
||||
TextColumn::make('created_at')
|
||||
->label('Ngày tạo')
|
||||
->dateTime('d/m/Y')
|
||||
->toggleable(isToggledHiddenByDefault: true),
|
||||
])
|
||||
->filters([
|
||||
Tables\Filters\SelectFilter::make('role_template_id')
|
||||
->label('Nhóm quyền')
|
||||
->relationship('roleTemplate', 'name'),
|
||||
])
|
||||
->defaultSort('created_at', 'desc');
|
||||
}
|
||||
}
|
||||
42
app/Filament/Resources/Users/UserResource.php
Normal file
42
app/Filament/Resources/Users/UserResource.php
Normal file
@@ -0,0 +1,42 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Resources\Users;
|
||||
|
||||
use App\Filament\Resources\Users\Pages;
|
||||
use App\Models\User;
|
||||
use App\Enums\NavigationGroup;
|
||||
use Filament\Schemas\Schema;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables\Table;
|
||||
use App\Filament\Resources\Users\Schemas\UserForm;
|
||||
use App\Filament\Resources\Users\Tables\UsersTable;
|
||||
|
||||
class UserResource extends Resource
|
||||
{
|
||||
protected static ?string $model = User::class;
|
||||
protected static string | \BackedEnum | null $navigationIcon = 'heroicon-o-user-group';
|
||||
protected static string | \UnitEnum | null $navigationGroup = NavigationGroup::SETTING->value;
|
||||
protected static ?int $navigationSort = 100;
|
||||
|
||||
protected static ?string $modelLabel = 'Ngườ dùng';
|
||||
protected static ?string $pluralModelLabel = 'Ngườ dùng';
|
||||
|
||||
public static function form(Schema $schema): Schema
|
||||
{
|
||||
return UserForm::configure($schema);
|
||||
}
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return UsersTable::configure($table);
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => Pages\ListUsers::route('/'),
|
||||
'create' => Pages\CreateUser::route('/create'),
|
||||
'edit' => Pages\EditUser::route('/{record}/edit'),
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user