Merge pull request #244 from pelican-dev/charles/fix-newuser

fix: Creating User
This commit is contained in:
Charles 2024-05-17 20:20:10 -04:00 committed by GitHub
commit 89d555f308
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 47 additions and 73 deletions

View File

@ -31,7 +31,6 @@ class UserResource extends Resource
{ {
return [ return [
'index' => Pages\ListUsers::route('/'), 'index' => Pages\ListUsers::route('/'),
'create' => Pages\CreateUser::route('/create'),
'edit' => Pages\EditUser::route('/{record}/edit'), 'edit' => Pages\EditUser::route('/{record}/edit'),
]; ];
} }

View File

@ -1,69 +0,0 @@
<?php
namespace App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource;
use Filament\Resources\Pages\CreateRecord;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Components\Section;
use Filament\Forms\Form;
use Illuminate\Support\Facades\Hash;
class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;
protected static bool $canCreateAnother = false;
public function form(Form $form): Form
{
return $form
->schema([
Section::make()->schema([
Forms\Components\TextInput::make('username')
->alphaNum()
->required()
->maxLength(191),
Forms\Components\TextInput::make('email')->email()->required()->maxLength(191),
Forms\Components\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(),
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),
Forms\Components\Hidden::make('skipValidation')->default(true),
Forms\Components\Select::make('language')
->required()
->hidden()
->default('en')
->options(fn (User $user) => $user->getAvailableLanguages()),
])->columns(2),
]);
}
}

View File

@ -4,10 +4,13 @@ namespace App\Filament\Resources\UserResource\Pages;
use App\Filament\Resources\UserResource; use App\Filament\Resources\UserResource;
use App\Models\User; use App\Models\User;
use App\Services\Users\UserCreationService;
use Filament\Actions; use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\ListRecords; use Filament\Resources\Pages\ListRecords;
use Filament\Tables\Table; use Filament\Tables\Table;
use Filament\Tables; use Filament\Tables;
use Filament\Forms;
class ListUsers extends ListRecords class ListUsers extends ListRecords
{ {
@ -73,8 +76,49 @@ class ListUsers extends ListRecords
protected function getHeaderActions(): array protected function getHeaderActions(): array
{ {
return [ return [
Actions\CreateAction::make() Actions\CreateAction::make('create')
->label('Create User'), ->label('Create User')
->createAnother(false)
->form([
Forms\Components\Grid::make()
->schema([
Forms\Components\TextInput::make('username')
->alphaNum()
->required()
->maxLength(191),
Forms\Components\TextInput::make('email')
->email()
->required()
->unique()
->maxLength(191),
Forms\Components\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(),
Forms\Components\ToggleButtons::make('root_admin')
->label('Administrator (Root)')
->options([
false => 'No',
true => 'Admin',
])
->colors([
false => 'primary',
true => 'danger',
])
->inline()
->required()
->default(false),
]),
])
->successRedirectUrl(route('filament.admin.resources.users.index'))
->action(function (array $data) {
resolve(UserCreationService::class)->handle($data);
Notification::make()->title('User Created!')->success()->send();
return redirect()->route('filament.admin.resources.users.index');
}),
]; ];
} }
} }

File diff suppressed because one or more lines are too long