feat: Add a reset password action to user records with a new password input and notification.
This commit is contained in:
parent
bf59f1afc6
commit
337238b518
@ -9,6 +9,7 @@
|
|||||||
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
use App\Filament\Actions\Cheerful\ForceDeleteAction;
|
||||||
use App\Filament\Actions\Cheerful\RestoreAction;
|
use App\Filament\Actions\Cheerful\RestoreAction;
|
||||||
use App\Filament\Actions\DefaultBulkActions;
|
use App\Filament\Actions\DefaultBulkActions;
|
||||||
|
use App\Filament\Support\CheerfulNotification;
|
||||||
use App\Filament\Columns\TimestampColumns;
|
use App\Filament\Columns\TimestampColumns;
|
||||||
use App\Filament\Resources\Master\Users\Pages\ManageUsers;
|
use App\Filament\Resources\Master\Users\Pages\ManageUsers;
|
||||||
use App\Models\User;
|
use App\Models\User;
|
||||||
@ -29,7 +30,9 @@
|
|||||||
use Illuminate\Database\Eloquent\Builder;
|
use Illuminate\Database\Eloquent\Builder;
|
||||||
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
use Illuminate\Database\Eloquent\SoftDeletingScope;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Hash;
|
||||||
use UnitEnum;
|
use UnitEnum;
|
||||||
|
use Filament\Actions\Action;
|
||||||
|
|
||||||
class UserResource extends Resource
|
class UserResource extends Resource
|
||||||
{
|
{
|
||||||
@ -89,7 +92,7 @@ public static function form(Schema $schema): Schema
|
|||||||
->searchable(),
|
->searchable(),
|
||||||
|
|
||||||
Hidden::make('password')
|
Hidden::make('password')
|
||||||
->default(fn (?User $record): ?string => $record ? null : config('auth.password_default')),
|
->default(fn(?User $record): ?string => $record ? null : config('auth.password_default')),
|
||||||
])
|
])
|
||||||
->columns(1);
|
->columns(1);
|
||||||
}
|
}
|
||||||
@ -116,7 +119,7 @@ public static function table(Table $table): Table
|
|||||||
ToggleColumn::make('is_active')
|
ToggleColumn::make('is_active')
|
||||||
->label('Status')
|
->label('Status')
|
||||||
->sortable()
|
->sortable()
|
||||||
->getStateUsing(fn (User $record): bool => $record->is_active === IsActive::ACTIVE)
|
->getStateUsing(fn(User $record): bool => $record->is_active === IsActive::ACTIVE)
|
||||||
->updateStateUsing(function (User $record, bool $state): void {
|
->updateStateUsing(function (User $record, bool $state): void {
|
||||||
$record->is_active = $state ? IsActive::ACTIVE : IsActive::INACTIVE;
|
$record->is_active = $state ? IsActive::ACTIVE : IsActive::INACTIVE;
|
||||||
$record->save();
|
$record->save();
|
||||||
@ -133,35 +136,60 @@ public static function table(Table $table): Table
|
|||||||
->success()
|
->success()
|
||||||
->send();
|
->send();
|
||||||
})
|
})
|
||||||
->disabled(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
->disabled(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
|
|
||||||
TextColumn::make('roles.name')
|
TextColumn::make('roles.name')
|
||||||
->label('Peran')
|
->label('Peran')
|
||||||
->searchable()
|
->searchable()
|
||||||
->sortable()
|
->sortable()
|
||||||
->badge()
|
->badge()
|
||||||
->getStateUsing(fn (User $record): array => $record->roles->pluck('name', 'id')->toArray()),
|
->getStateUsing(fn(User $record): array => $record->roles->pluck('name', 'id')->toArray()),
|
||||||
|
|
||||||
...TimestampColumns::make(),
|
...TimestampColumns::make(),
|
||||||
])
|
])
|
||||||
->filters([
|
->filters([
|
||||||
TrashedFilter::make()
|
TrashedFilter::make()
|
||||||
->native(false)
|
->native(false)
|
||||||
->visible(fn () => auth()->user()?->hasRole('Developer')),
|
->visible(fn() => auth()->user()?->hasRole('Developer')),
|
||||||
])
|
])
|
||||||
->recordActions([
|
->recordActions([
|
||||||
EditAction::make()
|
EditAction::make()
|
||||||
->modalWidth(Width::Large)
|
->modalWidth(Width::Large)
|
||||||
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
|
|
||||||
DeleteAction::make()
|
DeleteAction::make()
|
||||||
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
|
|
||||||
ForceDeleteAction::make()
|
ForceDeleteAction::make()
|
||||||
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
|
|
||||||
RestoreAction::make()
|
RestoreAction::make()
|
||||||
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
|
|
||||||
|
Action::make('resetPassword')
|
||||||
|
->label('Ganti Kata Sandi')
|
||||||
|
->icon(Heroicon::Key)
|
||||||
|
->color('warning')
|
||||||
|
->modalWidth(Width::Medium)
|
||||||
|
->schema([
|
||||||
|
TextInput::make('password')
|
||||||
|
->label('Kata Sandi Baru')
|
||||||
|
->password()
|
||||||
|
->required()
|
||||||
|
->minLength(8)
|
||||||
|
->default(config('auth.password_default')),
|
||||||
|
])
|
||||||
|
->action(function (User $record, array $data): void {
|
||||||
|
$record->update([
|
||||||
|
'password' => Hash::make($data['password']),
|
||||||
|
]);
|
||||||
|
|
||||||
|
CheerfulNotification::success(
|
||||||
|
'Kata sandi berhasil diubah! 🔑',
|
||||||
|
"Kata sandi untuk pengguna {$record->name} telah diperbarui."
|
||||||
|
)->send();
|
||||||
|
})
|
||||||
|
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
|
||||||
])
|
])
|
||||||
->toolbarActions([
|
->toolbarActions([
|
||||||
BulkActionGroup::make([
|
BulkActionGroup::make([
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user