Skip to content

Commit

Permalink
Add CreateUser page (#825)
Browse files Browse the repository at this point in the history
  • Loading branch information
Boy132 authored Dec 13, 2024
1 parent 7a4c4ce commit 993e2c4
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 58 deletions.
1 change: 1 addition & 0 deletions app/Filament/Admin/Resources/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
];
}
Expand Down
83 changes: 83 additions & 0 deletions app/Filament/Admin/Resources/UserResource/Pages/CreateUser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace App\Filament\Admin\Resources\UserResource\Pages;

use App\Filament\Admin\Resources\UserResource;
use App\Models\Role;
use App\Services\Users\UserCreationService;
use Filament\Forms\Components\CheckboxList;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Database\Eloquent\Model;

class CreateUser extends CreateRecord
{
protected static string $resource = UserResource::class;

protected static bool $canCreateAnother = false;

private UserCreationService $service;

public function boot(UserCreationService $service): void
{
$this->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;
}
}
17 changes: 12 additions & 5 deletions app/Filament/Admin/Resources/UserResource/Pages/EditUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,34 @@ 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)
->relationship('roles', 'name')
->label('Admin Roles')
->columnSpanFull()
->bulkToggleable(false),
])->columns(),
])
->columns(['default' => 1, 'lg' => 3]),
]);
}

Expand Down
55 changes: 2 additions & 53 deletions app/Filament/Admin/Resources/UserResource/Pages/ListUsers.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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'),
];
}
}

0 comments on commit 993e2c4

Please sign in to comment.