diff --git a/app/Filament/Admin/Resources/UserResource.php b/app/Filament/Admin/Resources/UserResource.php index 4b6c9b788b..26d7fc5ef2 100644 --- a/app/Filament/Admin/Resources/UserResource.php +++ b/app/Filament/Admin/Resources/UserResource.php @@ -31,6 +31,7 @@ public static function getPages(): array { return [ 'index' => Pages\ListUsers::route('/'), + 'create' => Pages\CreateUser::route('/create'), 'edit' => Pages\EditUser::route('/{record}/edit'), ]; } diff --git a/app/Filament/Admin/Resources/UserResource/Pages/CreateUser.php b/app/Filament/Admin/Resources/UserResource/Pages/CreateUser.php new file mode 100644 index 0000000000..8d4d8d1b73 --- /dev/null +++ b/app/Filament/Admin/Resources/UserResource/Pages/CreateUser.php @@ -0,0 +1,83 @@ +service = $service; + } + + public function form(Form $form): Form + { + return $form + ->columns(['default' => 1, 'lg' => 3]) + ->schema([ + TextInput::make('username') + ->alphaNum() + ->required() + ->unique() + ->minLength(3) + ->maxLength(255), + TextInput::make('email') + ->email() + ->required() + ->unique() + ->maxLength(255), + TextInput::make('password') + ->hintIcon('tabler-question-mark') + ->hintIconTooltip('Providing a user password is optional. New user email will prompt users to create a password the first time they login.') + ->password(), + CheckboxList::make('roles') + ->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id) + ->relationship('roles', 'name') + ->dehydrated() + ->label('Admin Roles') + ->columnSpanFull() + ->bulkToggleable(false), + ]); + } + + protected function getHeaderActions(): array + { + return [ + $this->getCreateFormAction()->formId('form'), + ]; + } + + protected function getFormActions(): array + { + return []; + } + + protected function handleRecordCreation(array $data): Model + { + $data['root_admin'] = false; + + $roles = $data['roles']; + $roles = collect($roles)->map(fn ($role) => Role::findById($role)); + unset($data['roles']); + + $user = $this->service->handle($data); + + $user->syncRoles($roles); + + return $user; + } +} diff --git a/app/Filament/Admin/Resources/UserResource/Pages/EditUser.php b/app/Filament/Admin/Resources/UserResource/Pages/EditUser.php index 412a0e08f8..40c0f2957e 100644 --- a/app/Filament/Admin/Resources/UserResource/Pages/EditUser.php +++ b/app/Filament/Admin/Resources/UserResource/Pages/EditUser.php @@ -24,19 +24,25 @@ public function form(Form $form): Form return $form ->schema([ Section::make()->schema([ - TextInput::make('username')->required()->minLength(3)->maxLength(255), - TextInput::make('email')->email()->required()->maxLength(255), + TextInput::make('username') + ->required() + ->minLength(3) + ->maxLength(255), + TextInput::make('email') + ->email() + ->required() + ->maxLength(255), TextInput::make('password') ->dehydrateStateUsing(fn (string $state): string => Hash::make($state)) ->dehydrated(fn (?string $state): bool => filled($state)) - ->required(fn (string $operation): bool => $operation === 'create') ->password(), Select::make('language') ->required() ->hidden() ->default('en') ->options(fn (User $user) => $user->getAvailableLanguages()), - Hidden::make('skipValidation')->default(true), + Hidden::make('skipValidation') + ->default(true), CheckboxList::make('roles') ->disabled(fn (User $user) => $user->id === auth()->user()->id) ->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id) @@ -44,7 +50,8 @@ public function form(Form $form): Form ->label('Admin Roles') ->columnSpanFull() ->bulkToggleable(false), - ])->columns(), + ]) + ->columns(['default' => 1, 'lg' => 3]), ]); } diff --git a/app/Filament/Admin/Resources/UserResource/Pages/ListUsers.php b/app/Filament/Admin/Resources/UserResource/Pages/ListUsers.php index af71733314..4e56927ce0 100644 --- a/app/Filament/Admin/Resources/UserResource/Pages/ListUsers.php +++ b/app/Filament/Admin/Resources/UserResource/Pages/ListUsers.php @@ -3,14 +3,8 @@ namespace App\Filament\Admin\Resources\UserResource\Pages; use App\Filament\Admin\Resources\UserResource; -use App\Models\Role; use App\Models\User; -use App\Services\Users\UserCreationService; use Filament\Actions\CreateAction; -use Filament\Forms\Components\CheckboxList; -use Filament\Forms\Components\Grid; -use Filament\Forms\Components\TextInput; -use Filament\Notifications\Notification; use Filament\Resources\Pages\ListRecords; use Filament\Tables\Actions\BulkActionGroup; use Filament\Tables\Actions\DeleteBulkAction; @@ -82,53 +76,8 @@ public function table(Table $table): Table protected function getHeaderActions(): array { return [ - CreateAction::make('create') - ->label('Create User') - ->createAnother(false) - ->form([ - Grid::make() - ->schema([ - TextInput::make('username') - ->alphaNum() - ->required() - ->unique() - ->minLength(3) - ->maxLength(255), - TextInput::make('email') - ->email() - ->required() - ->unique() - ->maxLength(255), - TextInput::make('password') - ->hintIcon('tabler-question-mark') - ->hintIconTooltip('Providing a user password is optional. New user email will prompt users to create a password the first time they login.') - ->password(), - CheckboxList::make('roles') - ->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id) - ->relationship('roles', 'name') - ->dehydrated() - ->label('Admin Roles') - ->columnSpanFull() - ->bulkToggleable(false), - ]), - ]) - ->successRedirectUrl(route('filament.admin.resources.users.index')) - ->action(function (array $data, UserCreationService $creationService) { - $roles = $data['roles']; - $roles = collect($roles)->map(fn ($role) => Role::findById($role)); - unset($data['roles']); - - $user = $creationService->handle($data); - - $user->syncRoles($roles); - - Notification::make() - ->title('User Created!') - ->success() - ->send(); - - return redirect()->route('filament.admin.resources.users.index'); - }), + CreateAction::make() + ->label('Create User'), ]; } }