feat: Add a reset password action to user records with a new password input and notification.

This commit is contained in:
Yoga Pangestu 2026-01-27 15:49:56 +07:00
parent bf59f1afc6
commit 337238b518

View File

@ -9,6 +9,7 @@
use App\Filament\Actions\Cheerful\ForceDeleteAction;
use App\Filament\Actions\Cheerful\RestoreAction;
use App\Filament\Actions\DefaultBulkActions;
use App\Filament\Support\CheerfulNotification;
use App\Filament\Columns\TimestampColumns;
use App\Filament\Resources\Master\Users\Pages\ManageUsers;
use App\Models\User;
@ -29,7 +30,9 @@
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use UnitEnum;
use Filament\Actions\Action;
class UserResource extends Resource
{
@ -89,7 +92,7 @@ public static function form(Schema $schema): Schema
->searchable(),
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);
}
@ -116,7 +119,7 @@ public static function table(Table $table): Table
ToggleColumn::make('is_active')
->label('Status')
->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 {
$record->is_active = $state ? IsActive::ACTIVE : IsActive::INACTIVE;
$record->save();
@ -133,35 +136,60 @@ public static function table(Table $table): Table
->success()
->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')
->label('Peran')
->searchable()
->sortable()
->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(),
])
->filters([
TrashedFilter::make()
->native(false)
->visible(fn () => auth()->user()?->hasRole('Developer')),
->visible(fn() => auth()->user()?->hasRole('Developer')),
])
->recordActions([
EditAction::make()
->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()
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
ForceDeleteAction::make()
->hidden(fn (User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
->hidden(fn(User $record): bool => $record->hasRole(RoleEnum::PERUSAHAAN->value)),
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([
BulkActionGroup::make([