mirror of
https://github.com/pelican-dev/panel.git
synced 2025-05-20 13:24:46 +02:00
Add CreateUser page (#825)
This commit is contained in:
parent
7a4c4ce02a
commit
993e2c4244
@ -31,6 +31,7 @@ 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'),
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -24,19 +24,25 @@ class EditUser extends EditRecord
|
|||||||
return $form
|
return $form
|
||||||
->schema([
|
->schema([
|
||||||
Section::make()->schema([
|
Section::make()->schema([
|
||||||
TextInput::make('username')->required()->minLength(3)->maxLength(255),
|
TextInput::make('username')
|
||||||
TextInput::make('email')->email()->required()->maxLength(255),
|
->required()
|
||||||
|
->minLength(3)
|
||||||
|
->maxLength(255),
|
||||||
|
TextInput::make('email')
|
||||||
|
->email()
|
||||||
|
->required()
|
||||||
|
->maxLength(255),
|
||||||
TextInput::make('password')
|
TextInput::make('password')
|
||||||
->dehydrateStateUsing(fn (string $state): string => Hash::make($state))
|
->dehydrateStateUsing(fn (string $state): string => Hash::make($state))
|
||||||
->dehydrated(fn (?string $state): bool => filled($state))
|
->dehydrated(fn (?string $state): bool => filled($state))
|
||||||
->required(fn (string $operation): bool => $operation === 'create')
|
|
||||||
->password(),
|
->password(),
|
||||||
Select::make('language')
|
Select::make('language')
|
||||||
->required()
|
->required()
|
||||||
->hidden()
|
->hidden()
|
||||||
->default('en')
|
->default('en')
|
||||||
->options(fn (User $user) => $user->getAvailableLanguages()),
|
->options(fn (User $user) => $user->getAvailableLanguages()),
|
||||||
Hidden::make('skipValidation')->default(true),
|
Hidden::make('skipValidation')
|
||||||
|
->default(true),
|
||||||
CheckboxList::make('roles')
|
CheckboxList::make('roles')
|
||||||
->disabled(fn (User $user) => $user->id === auth()->user()->id)
|
->disabled(fn (User $user) => $user->id === auth()->user()->id)
|
||||||
->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id)
|
->disableOptionWhen(fn (string $value): bool => $value == Role::getRootAdmin()->id)
|
||||||
@ -44,7 +50,8 @@ class EditUser extends EditRecord
|
|||||||
->label('Admin Roles')
|
->label('Admin Roles')
|
||||||
->columnSpanFull()
|
->columnSpanFull()
|
||||||
->bulkToggleable(false),
|
->bulkToggleable(false),
|
||||||
])->columns(),
|
])
|
||||||
|
->columns(['default' => 1, 'lg' => 3]),
|
||||||
]);
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -3,14 +3,8 @@
|
|||||||
namespace App\Filament\Admin\Resources\UserResource\Pages;
|
namespace App\Filament\Admin\Resources\UserResource\Pages;
|
||||||
|
|
||||||
use App\Filament\Admin\Resources\UserResource;
|
use App\Filament\Admin\Resources\UserResource;
|
||||||
use App\Models\Role;
|
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
use App\Services\Users\UserCreationService;
|
|
||||||
use Filament\Actions\CreateAction;
|
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\Resources\Pages\ListRecords;
|
||||||
use Filament\Tables\Actions\BulkActionGroup;
|
use Filament\Tables\Actions\BulkActionGroup;
|
||||||
use Filament\Tables\Actions\DeleteBulkAction;
|
use Filament\Tables\Actions\DeleteBulkAction;
|
||||||
@ -82,53 +76,8 @@ class ListUsers extends ListRecords
|
|||||||
protected function getHeaderActions(): array
|
protected function getHeaderActions(): array
|
||||||
{
|
{
|
||||||
return [
|
return [
|
||||||
CreateAction::make('create')
|
CreateAction::make()
|
||||||
->label('Create User')
|
->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');
|
|
||||||
}),
|
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user