Skip to content

Commit

Permalink
feat(roles): 数据模型增加 description 和通用字段
Browse files Browse the repository at this point in the history
  • Loading branch information
zhaiyuxin103 committed Dec 12, 2024
1 parent ae2f9ce commit b755f0a
Show file tree
Hide file tree
Showing 8 changed files with 231 additions and 20 deletions.
42 changes: 41 additions & 1 deletion _ide_helper_models.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ class IdeHelperAbout {}
* @property-read int|null $owned_teams_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Spatie\Permission\Models\Permission> $permissions
* @property-read int|null $permissions_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Spatie\Permission\Models\Role> $roles
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Role> $roles
* @property-read int|null $roles_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\Team> $teams
* @property-read int|null $teams_count
Expand Down Expand Up @@ -292,6 +292,46 @@ class IdeHelperMembership {}
class IdeHelperModel {}
}

namespace App\Models{
/**
*
*
* @property int $id
* @property string $name
* @property string $guard_name
* @property \Illuminate\Support\Carbon|null $created_at
* @property \Illuminate\Support\Carbon|null $updated_at
* @property string|null $description
* @property bool $state 状态
* @property int $order 排序
* @property \Illuminate\Support\Carbon|null $deleted_at
* @property-read \Illuminate\Database\Eloquent\Collection<int, \Spatie\Permission\Models\Permission> $permissions
* @property-read int|null $permissions_count
* @property-read \Illuminate\Database\Eloquent\Collection<int, \App\Models\User> $users
* @property-read int|null $users_count
* @method static \Illuminate\Database\Eloquent\Builder|Role newModelQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Role newQuery()
* @method static \Illuminate\Database\Eloquent\Builder|Role onlyTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Role permission($permissions, $without = false)
* @method static \Illuminate\Database\Eloquent\Builder|Role query()
* @method static \Illuminate\Database\Eloquent\Builder|Role whereCreatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereDeletedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereDescription($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereGuardName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereId($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereName($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereOrder($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereState($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role whereUpdatedAt($value)
* @method static \Illuminate\Database\Eloquent\Builder|Role withTrashed()
* @method static \Illuminate\Database\Eloquent\Builder|Role withoutPermission($permissions)
* @method static \Illuminate\Database\Eloquent\Builder|Role withoutTrashed()
* @mixin \Eloquent
*/
#[\AllowDynamicProperties]
class IdeHelperRole {}
}

namespace App\Models{
/**
*
Expand Down
80 changes: 66 additions & 14 deletions app/Filament/Resources/Shield/RoleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Filament\Resources\Shield;

use App\Filament\Resources\Shield\RoleResource\Pages;
use App\Models\Role;
use BezhanSalleh\FilamentShield\Contracts\HasShieldPermissions;
use BezhanSalleh\FilamentShield\Facades\FilamentShield;
use BezhanSalleh\FilamentShield\FilamentShieldPlugin;
Expand Down Expand Up @@ -44,30 +45,27 @@ public static function form(Form $form): Form
->schema([
Forms\Components\Section::make()
->schema([
Forms\Components\TextInput::make('description')
->label(trans('fields.name'))
->maxLength(255),
Forms\Components\TextInput::make('name')
->label(__('filament-shield::filament-shield.field.name'))
->label(trans('fields.slug'))
->unique(ignoreRecord: true)
->required()
->maxLength(255),

Forms\Components\TextInput::make('guard_name')
->label(__('filament-shield::filament-shield.field.guard_name'))
->default(Utils::getFilamentAuthGuard())
->nullable()
->maxLength(255),

ShieldSelectAllToggle::make('select_all')
->onIcon('heroicon-s-shield-check')
->offIcon('heroicon-s-shield-exclamation')
->label(__('filament-shield::filament-shield.field.select_all.name'))
->helperText(fn (): HtmlString => new HtmlString(__('filament-shield::filament-shield.field.select_all.message')))
->dehydrated(fn ($state): bool => $state),

])
->columns([
'sm' => 2,
'lg' => 3,
]),
->columns(),
]),
Forms\Components\Tabs::make('Permissions')
->contained()
Expand All @@ -78,16 +76,47 @@ public static function form(Form $form): Form
static::getTabFormComponentForCustomPermissions(),
])
->columnSpan('full'),
Forms\Components\Section::make()
->columns()
->schema([
Forms\Components\Toggle::make('state')
->required()
->default(true)
->label(trans('fields.state')),
Forms\Components\TextInput::make('order')
->required()
->numeric()
->default(0)
->label(trans('fields.sort')),
]),
Forms\Components\Placeholder::make('created_at')
->label('Created Date')
->content(fn (?Role $record): string => $record?->created_at?->diffForHumans() ?? '-')
->label(trans('fields.created_at')),
Forms\Components\Placeholder::make('updated_at')
->label('Last Modified Date')
->content(fn (?Role $record): string => $record?->updated_at?->diffForHumans() ?? '-')
->label(trans('fields.updated_at')),
]);
}

public static function table(Table $table): Table
{
return $table
->columns([
Tables\Columns\TextColumn::make('index')
->rowIndex()
->toggleable(isToggledHiddenByDefault: true)
->label(trans('fields.index')),
Tables\Columns\TextColumn::make('id')
->sortable()
->searchable()
->label(trans('fields.id')),
Tables\Columns\TextColumn::make('description')
->label(trans('fields.name')),
Tables\Columns\TextColumn::make('name')
->badge()
->label(__('filament-shield::filament-shield.column.name'))
->label(trans('fields.slug'))
->formatStateUsing(fn ($state): string => Str::headline($state))
->colors(['primary'])
->searchable(),
Expand All @@ -99,12 +128,35 @@ public static function table(Table $table): Table
->label(__('filament-shield::filament-shield.column.permissions'))
->counts('permissions')
->colors(['success']),
Tables\Columns\IconColumn::make('state')
->boolean()
->label(trans('fields.state')),
Tables\Columns\TextColumn::make('order')
->numeric()
->sortable()
->label(trans('fields.order')),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->label(trans('fields.created_at')),
Tables\Columns\TextColumn::make('updated_at')
->label(__('filament-shield::filament-shield.column.updated_at'))
->dateTime(),
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->label(trans('fields.updated_at')),
Tables\Columns\TextColumn::make('deleted_at')
->dateTime()
->sortable()
->toggleable(isToggledHiddenByDefault: true)
->label(trans('fields.deleted_at')),
])
->filters([
//
Tables\Filters\TrashedFilter::make(),
Tables\Filters\SelectFilter::make('id')
->options(Role::pluck('id', 'id'))
->searchable()
->label(trans('fields.id')),
])
->actions([
Tables\Actions\EditAction::make(),
Expand Down Expand Up @@ -208,8 +260,8 @@ public static function getResourceEntitiesSchema(): ?array
->map(function ($entity) {
$sectionLabel = strval(
static::shield()->hasLocalizedPermissionLabels()
? FilamentShield::getLocalizedResourceLabel($entity['fqcn'])
: $entity['model']
? FilamentShield::getLocalizedResourceLabel($entity['fqcn'])
: $entity['model']
);

return Forms\Components\Section::make($sectionLabel)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
namespace App\Filament\Resources\Shield\RoleResource\Pages;

use App\Filament\Resources\Shield\RoleResource;
use App\Models\Role;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Permission\Models\Role;

class CreateRole extends CreateRecord
{
Expand All @@ -32,7 +32,7 @@ protected function mutateFormDataBeforeCreate(array $data): array
->flatten()
->unique();

return Arr::only($data, ['name', 'guard_name']);
return Arr::only($data, ['name', 'guard_name', 'description', 'state', 'order']);
}

protected function afterCreate(): void
Expand Down
26 changes: 24 additions & 2 deletions app/Filament/Resources/Shield/RoleResource/Pages/EditRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,41 @@
namespace App\Filament\Resources\Shield\RoleResource\Pages;

use App\Filament\Resources\Shield\RoleResource;
use App\Models\Role;
use BezhanSalleh\FilamentShield\Support\Utils;
use Filament\Actions;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Spatie\Permission\Models\Role;

class EditRole extends EditRecord
{
public Collection $permissions;

protected static string $resource = RoleResource::class;

public function getHeading(): string
{
/** @var Role $record */
$record = $this->record;

return $record->description ?? $record->name;
}

public function getBreadcrumbs(): array
{
/** @var Role $record */
$record = $this->record;

return [
route('filament.admin.resources.shield.roles.index') => __('filament-shield::filament-shield.nav.role.label'),
route('filament.admin.resources.shield.roles.view', [
'record' => $record,
]) => $record->description ?? $record->name,
'#' => __('filament-panels::resources/pages/edit-record.breadcrumb'),
];
}

protected function getRedirectUrl(): string
{
return $this->getResource()::getUrl('view', ['record' => $this->getRecord()]);
Expand All @@ -40,7 +62,7 @@ protected function mutateFormDataBeforeSave(array $data): array
->flatten()
->unique();

return Arr::only($data, ['name', 'guard_name']);
return Arr::only($data, ['name', 'guard_name', 'description', 'state', 'order']);
}

protected function afterSave(): void
Expand Down
23 changes: 23 additions & 0 deletions app/Filament/Resources/Shield/RoleResource/Pages/ViewRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,36 @@
namespace App\Filament\Resources\Shield\RoleResource\Pages;

use App\Filament\Resources\Shield\RoleResource;
use App\Models\Role;
use Filament\Actions;
use Filament\Resources\Pages\ViewRecord;

class ViewRole extends ViewRecord
{
protected static string $resource = RoleResource::class;

public function getHeading(): string
{
/** @var Role $record */
$record = $this->record;

return $record->description;
}

public function getBreadcrumbs(): array
{
/** @var Role $record */
$record = $this->record;

return [
route('filament.admin.resources.shield.roles.index') => __('filament-shield::filament-shield.nav.role.label'),
route('filament.admin.resources.shield.roles.edit', [
'record' => $record,
]) => $record->description,
'#' => __('filament-panels::resources/pages/edit-record.breadcrumb'),
];
}

protected function getActions(): array
{
return [
Expand Down
41 changes: 41 additions & 0 deletions app/Models/Role.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace App\Models;

use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Permission\Models\Role as SpatieRole;

/**
* @mixin IdeHelperRole
*/
class Role extends SpatieRole
{
use SoftDeletes;

/**
* The attributes that are mass assignable.
*
* @var list<string>
*/
protected $fillable = [
'name',
'guard_name',
'description',
'state',
'order',
];

/**
* Get the attributes that should be cast.
*
* @return array<string,string>
*/
protected function casts(): array
{
return [
'state' => 'boolean',
];
}
}
2 changes: 1 addition & 1 deletion config/permission.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
* `Spatie\Permission\Contracts\Role` contract.
*/

'role' => Spatie\Permission\Models\Role::class,
'role' => App\Models\Role::class,

],

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

declare(strict_types=1);

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('roles', function (Blueprint $table) {
$table->string('description')->nullable();
$table->boolean('state')->default(true)->comment('状态');
$table->unsignedInteger('order')->default(0)->comment('排序');
$table->softDeletesTz();
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('roles', function (Blueprint $table) {
$table->dropColumn(['description', 'state', 'order', 'deleted_at']);
});
}
};

0 comments on commit b755f0a

Please sign in to comment.