mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 00:34:44 +02:00
Show different roles
CheckboxList for root admins and non root admins (#1219)
* show different roles checkbox list for root admins and non root admins * simplify saveRelationshipsUsing * remove disableOptionWhen * add migration to remove additional roles from root admins
This commit is contained in:
parent
fa8ae0aea5
commit
92fbd75772
@ -18,6 +18,7 @@ use Filament\Tables\Columns\IconColumn;
|
|||||||
use Filament\Tables\Columns\ImageColumn;
|
use Filament\Tables\Columns\ImageColumn;
|
||||||
use Filament\Tables\Columns\TextColumn;
|
use Filament\Tables\Columns\TextColumn;
|
||||||
use Filament\Tables\Table;
|
use Filament\Tables\Table;
|
||||||
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
|
|
||||||
class UserResource extends Resource
|
class UserResource extends Resource
|
||||||
{
|
{
|
||||||
@ -122,17 +123,26 @@ class UserResource extends Resource
|
|||||||
->hintIconTooltip(fn ($operation) => $operation === 'create' ? trans('admin/user.password_help') : null)
|
->hintIconTooltip(fn ($operation) => $operation === 'create' ? trans('admin/user.password_help') : null)
|
||||||
->password(),
|
->password(),
|
||||||
CheckboxList::make('roles')
|
CheckboxList::make('roles')
|
||||||
->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id)
|
->hidden(fn (User $user) => $user->isRootAdmin())
|
||||||
->relationship('roles', 'name')
|
->relationship('roles', 'name', fn (Builder $query) => $query->whereNot('id', Role::getRootAdmin()->id))
|
||||||
->saveRelationshipsUsing(function (User $user, array $state) {
|
->saveRelationshipsUsing(fn (User $user, array $state) => $user->syncRoles(collect($state)->map(fn ($role) => Role::findById($role))))
|
||||||
$roles = collect($state)->map(fn ($role) => Role::findById($role))->filter(fn ($role) => $role->id !== Role::getRootAdmin()->id);
|
|
||||||
|
|
||||||
$user->syncRoles($roles);
|
|
||||||
})
|
|
||||||
->dehydrated()
|
->dehydrated()
|
||||||
->label(trans('admin/user.admin_roles'))
|
->label(trans('admin/user.admin_roles'))
|
||||||
->columnSpanFull()
|
->columnSpanFull()
|
||||||
->bulkToggleable(false),
|
->bulkToggleable(false),
|
||||||
|
CheckboxList::make('root_admin_role')
|
||||||
|
->visible(fn (User $user) => $user->isRootAdmin())
|
||||||
|
->disabled()
|
||||||
|
->options([
|
||||||
|
'root_admin' => Role::ROOT_ADMIN,
|
||||||
|
])
|
||||||
|
->descriptions([
|
||||||
|
'root_admin' => trans('admin/role.root_admin', ['role' => Role::ROOT_ADMIN]),
|
||||||
|
])
|
||||||
|
->formatStateUsing(fn () => ['root_admin'])
|
||||||
|
->dehydrated(false)
|
||||||
|
->label(trans('admin/user.admin_roles'))
|
||||||
|
->columnSpanFull(),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,27 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
use App\Models\Role;
|
||||||
|
use App\Models\User;
|
||||||
|
use Illuminate\Database\Migrations\Migration;
|
||||||
|
|
||||||
|
return new class extends Migration
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the migrations.
|
||||||
|
*/
|
||||||
|
public function up(): void
|
||||||
|
{
|
||||||
|
$rootAdmins = User::all()->filter(fn ($user) => $user->isRootAdmin());
|
||||||
|
foreach ($rootAdmins as $rootAdmin) {
|
||||||
|
$rootAdmin->syncRoles(Role::getRootAdmin());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Reverse the migrations.
|
||||||
|
*/
|
||||||
|
public function down(): void
|
||||||
|
{
|
||||||
|
// No going back
|
||||||
|
}
|
||||||
|
};
|
Loading…
x
Reference in New Issue
Block a user