From 5d085d48ccd99132f61ea8f6d03827a91ec337a6 Mon Sep 17 00:00:00 2001 From: UnAfraid Date: Mon, 9 Jan 2023 21:51:15 +0200 Subject: [PATCH 1/2] Added user management --- app/Filament/Resources/UserResource.php | 111 ++++++++++++++++++ .../UserResource/Pages/CreateUser.php | 11 ++ .../Resources/UserResource/Pages/EditUser.php | 29 +++++ .../UserResource/Pages/ListUsers.php | 19 +++ 4 files changed, 170 insertions(+) create mode 100644 app/Filament/Resources/UserResource.php create mode 100644 app/Filament/Resources/UserResource/Pages/CreateUser.php create mode 100644 app/Filament/Resources/UserResource/Pages/EditUser.php create mode 100644 app/Filament/Resources/UserResource/Pages/ListUsers.php diff --git a/app/Filament/Resources/UserResource.php b/app/Filament/Resources/UserResource.php new file mode 100644 index 000000000..391f0e7b1 --- /dev/null +++ b/app/Filament/Resources/UserResource.php @@ -0,0 +1,111 @@ +schema([ + 'left' => Card::make([ + 'name' => TextInput::make('name') + ->required(), + 'email' => TextInput::make('email') + ->required() + ->unique(ignoreRecord: true), + 'password' => TextInput::make('password') + ->required() + ->password() + ->dehydrateStateUsing(fn($state) => Hash::make($state)) + ->visible(fn($livewire) => $livewire instanceof CreateUser) + ->rule(Password::default()), + 'new_password_group' => Group::make([ + 'new_password' => TextInput::make('new_password') + ->password() + ->label('New Password') + ->nullable() + ->rule(Password::default()) + ->visible(fn($livewire) => $livewire instanceof EditUser) + ->dehydrated(false), + 'new_password_confirmation' => TextInput::make('new_password_confirmation') + ->password() + ->label('Confirm New Password') + ->rule('required', fn($get) => !!$get('new_password')) + ->same('new_password') + ->visible(fn($livewire) => $livewire instanceof EditUser) + ->dehydrated(false), + ]) + ])->columnSpan(8), + 'right' => Card::make([ + 'created_at' => Placeholder::make('created_at') + ->content(fn($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('—')) + ])->columnSpan(4), + ]) + ->columns(12); + } + + public static function table(Table $table): Table + { + return $table + ->columns([ + TextColumn::make('name'), + TextColumn::make('email'), + TextColumn::make('email_verified_at') + ->dateTime(), + TextColumn::make('created_at') + ->dateTime(), + TextColumn::make('updated_at') + ->dateTime(), + ]) + ->filters([ + // + ]) + ->actions([ + EditAction::make(), + ]) + ->bulkActions([ + DeleteBulkAction::make(), + ]); + } + + public static function getRelations(): array + { + return [ + // + ]; + } + + 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/Resources/UserResource/Pages/CreateUser.php b/app/Filament/Resources/UserResource/Pages/CreateUser.php new file mode 100644 index 000000000..78a389490 --- /dev/null +++ b/app/Filament/Resources/UserResource/Pages/CreateUser.php @@ -0,0 +1,11 @@ +data) || !filled($this->data['new_password'])) { + return; + } + + $this->record->password = Hash::make($this->data['new_password']); + } +} diff --git a/app/Filament/Resources/UserResource/Pages/ListUsers.php b/app/Filament/Resources/UserResource/Pages/ListUsers.php new file mode 100644 index 000000000..54c3be834 --- /dev/null +++ b/app/Filament/Resources/UserResource/Pages/ListUsers.php @@ -0,0 +1,19 @@ + Date: Wed, 11 Jan 2023 12:37:00 +0200 Subject: [PATCH 2/2] Addressing pint code style issues --- app/Filament/Resources/UserResource.php | 14 +++++++------- .../Resources/UserResource/Pages/EditUser.php | 2 +- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/app/Filament/Resources/UserResource.php b/app/Filament/Resources/UserResource.php index 391f0e7b1..c13d3309f 100644 --- a/app/Filament/Resources/UserResource.php +++ b/app/Filament/Resources/UserResource.php @@ -41,8 +41,8 @@ public static function form(Form $form): Form 'password' => TextInput::make('password') ->required() ->password() - ->dehydrateStateUsing(fn($state) => Hash::make($state)) - ->visible(fn($livewire) => $livewire instanceof CreateUser) + ->dehydrateStateUsing(fn ($state) => Hash::make($state)) + ->visible(fn ($livewire) => $livewire instanceof CreateUser) ->rule(Password::default()), 'new_password_group' => Group::make([ 'new_password' => TextInput::make('new_password') @@ -50,20 +50,20 @@ public static function form(Form $form): Form ->label('New Password') ->nullable() ->rule(Password::default()) - ->visible(fn($livewire) => $livewire instanceof EditUser) + ->visible(fn ($livewire) => $livewire instanceof EditUser) ->dehydrated(false), 'new_password_confirmation' => TextInput::make('new_password_confirmation') ->password() ->label('Confirm New Password') - ->rule('required', fn($get) => !!$get('new_password')) + ->rule('required', fn ($get) => (bool) $get('new_password')) ->same('new_password') - ->visible(fn($livewire) => $livewire instanceof EditUser) + ->visible(fn ($livewire) => $livewire instanceof EditUser) ->dehydrated(false), - ]) + ]), ])->columnSpan(8), 'right' => Card::make([ 'created_at' => Placeholder::make('created_at') - ->content(fn($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('—')) + ->content(fn ($record) => $record?->created_at?->diffForHumans() ?? new HtmlString('—')), ])->columnSpan(4), ]) ->columns(12); diff --git a/app/Filament/Resources/UserResource/Pages/EditUser.php b/app/Filament/Resources/UserResource/Pages/EditUser.php index e0c5af683..7fc2e871c 100644 --- a/app/Filament/Resources/UserResource/Pages/EditUser.php +++ b/app/Filament/Resources/UserResource/Pages/EditUser.php @@ -20,7 +20,7 @@ protected function getActions(): array public function beforeSave() { - if (!array_key_exists('new_password', $this->data) || !filled($this->data['new_password'])) { + if (! array_key_exists('new_password', $this->data) || ! filled($this->data['new_password'])) { return; }