User resource adjustments

This commit is contained in:
Lance Pioch 2024-04-06 23:58:10 -04:00
parent a19c8e72b3
commit 732db9a5b4
2 changed files with 56 additions and 12 deletions

View File

@ -28,21 +28,52 @@ class UserResource extends Resource
->schema([
Forms\Components\TextInput::make('username')->required()->maxLength(191),
Forms\Components\TextInput::make('email')->email()->required()->maxLength(191),
Forms\Components\TextInput::make('name_first')->maxLength(191)->label('First Name'),
Forms\Components\TextInput::make('name_last')->maxLength(191)->label('Last Name'),
Forms\Components\TextInput::make('name_first')
->maxLength(191)
->hidden(fn (string $operation): bool => $operation === 'create')
->label('First Name'),
Forms\Components\TextInput::make('name_last')
->maxLength(191)
->hidden(fn (string $operation): bool => $operation === 'create')
->label('Last Name'),
Forms\Components\TextInput::make('password')
->password()
->dehydrateStateUsing(fn (string $state): string => Hash::make($state))
->dehydrated(fn (?string $state): bool => filled($state))
->required(fn (string $operation): bool => $operation === 'create')
->columnSpanFull(),
Forms\Components\Hidden::make('skipValidation')->default(true),
Forms\Components\Select::make('language')->required()->default('en')
->options(fn (User $user) => $user->getAvailableLanguages()),
Forms\Components\Toggle::make('root_admin')
->password(),
Forms\Components\ToggleButtons::make('root_admin')
->label('Administrator (Root)')
->options([
false => 'No',
true => 'Admin',
])
->colors([
false => 'primary',
true => 'danger',
])
->disableOptionWhen(function (string $operation, $value, User $user) {
if ($operation !== 'edit' || $value) {
return false;
}
return $user->isLastRootAdmin();
})
->hint(fn (User $user) => $user->isLastRootAdmin() ? 'This is the last root administrator!' : '')
->helperText(fn (User $user) => $user->isLastRootAdmin() ? 'You must have at least one root administrator in your system.' : '')
->hintColor('warning')
->inline()
->required()
->default(false),
// ->disabled(fn () => User::where('root_admin', true)->count() <= 1),
Forms\Components\Hidden::make('skipValidation')->default(true),
Forms\Components\Select::make('language')
->required()
->hidden()
->default('en')
->options(fn (User $user) => $user->getAvailableLanguages()),
]);
}
@ -65,8 +96,13 @@ class UserResource extends Resource
Tables\Columns\TextColumn::make('email')
->searchable()
->icon('tabler-mail'),
Tables\Columns\TextColumn::make('name')
->hidden()
Tables\Columns\TextColumn::make('name_first')
->toggleable(isToggledHiddenByDefault: true)
->label('First Name')
->searchable(),
Tables\Columns\TextColumn::make('name_last')
->toggleable(isToggledHiddenByDefault: true)
->label('Last Name')
->searchable(),
Tables\Columns\IconColumn::make('root_admin')
->label('Admin')
@ -82,10 +118,11 @@ class UserResource extends Resource
->icon('tabler-server')
->label('Servers'),
Tables\Columns\TextColumn::make('subusers_count')
->toggleable(isToggledHiddenByDefault: true)
->counts('subusers')
->icon('tabler-users')
// ->formatStateUsing(fn (string $state, $record): string => (string) ($record->servers_count + $record->subusers_count))
->label('Subusers'),
->label('Subuser Accounts'),
Tables\Columns\TextColumn::make('created_at')
->dateTime()
->sortable()

View File

@ -336,6 +336,13 @@ class User extends Model implements AuthenticatableContract, AuthorizableContrac
return $this->canned($abilities, $arguments);
}
public function isLastRootAdmin(): bool
{
$rootAdmins = User::query()->where('root_admin', true)->limit(2)->get();
return once(fn() => $rootAdmins->count() === 1 && $rootAdmins->first()->is($this));
}
public function canAccessPanel(Panel $panel): bool
{
return $this->root_admin;