Skip to content

Commit

Permalink
Reset password action
Browse files Browse the repository at this point in the history
  • Loading branch information
alexPopaCode4 committed Dec 3, 2024
1 parent 31af2c5 commit 51f9c2e
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 63 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace App\Filament\Organizations\Resources\UserResource\Actions;

use App\Models\User;
use Exception;
use Filament\Actions\Action;
use Filament\Facades\Filament;
use Filament\Notifications\Auth\ResetPassword as ResetPasswordNotification;
use Filament\Notifications\Notification;
use Illuminate\Contracts\Auth\CanResetPassword;
use Illuminate\Support\Facades\Password;

class ResetPasswordAction extends Action
{
public static function getDefaultName(): ?string
{
return 'reset-password';
}

protected function setUp(): void
{
parent::setUp();

$this->visible(fn (User $record) => $record->userStatus->isActive() && ! Filament::auth()->user()->is($record));

$this->label(__('user.actions.reset_password'));

$this->icon('heroicon-o-lock-open');

$this->outlined();

$this->modalHeading(__('user.action_reset_password_confirm.title'));

$this->modalSubmitActionLabel(__('user.action_reset_password_confirm.title'));

$this->modalWidth('md');

$this->action(function (User $record) {
$status = Password::broker(Filament::getAuthPasswordBroker())
->sendResetLink(
['email' => $record->email],
function (CanResetPassword $user, string $token): void {
if (! method_exists($user, 'notify')) {
$userClass = $user::class;

throw new Exception("Model [{$userClass}] does not have a [notify()] method.");
}

$notification = new ResetPasswordNotification($token);
$notification->url = Filament::getResetPasswordUrl($token, $user);

$user->notify($notification);
}
);

if ($status !== Password::RESET_LINK_SENT) {
$this->failureNotificationTitle(__($status));
$this->failure();

return;
}

$this->successNotificationTitle(__($status));
$this->success();
});

$this->successNotificationTitle(__('user.action_reset_password_confirm.success'));

$this->failureNotification(
fn (Notification $notification) => $notification
->danger()
->title(__('user.action_reset_password_confirm.failure_title'))
->body(__('user.action_reset_password_confirm.failure_body'))
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
use App\Models\User;
use Filament\Facades\Filament;
use Filament\Resources\Pages\CreateRecord;
use Illuminate\Contracts\Support\Htmlable;
use Illuminate\Database\Eloquent\Model;

class CreateUser extends CreateRecord
Expand All @@ -19,6 +20,11 @@ class CreateUser extends CreateRecord

protected static bool $canCreateAnother = false;

public function getTitle(): string|Htmlable
{
return __('user.titles.create_specialist');
}

protected function afterSave(): void
{
/** @var User $user */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use App\Filament\Organizations\Resources\UserResource\Actions\DeactivateUserAction;
use App\Filament\Organizations\Resources\UserResource\Actions\ReactivateUserAction;
use App\Filament\Organizations\Resources\UserResource\Actions\ResendInvitationAction;
use App\Filament\Organizations\Resources\UserResource\Actions\ResetPasswordAction;
use App\Infolists\Components\SectionHeader;
use App\Models\User;
use Filament\Infolists\Components\Actions\Action;
Expand Down Expand Up @@ -112,7 +113,7 @@ protected function getHeaderActions(): array
return [
DeactivateUserAction::make(),

// UserResource\Actions\ResetPassword::make('reset-password'),
ResetPasswordAction::make(),

ResendInvitationAction::make(),

Expand Down
5 changes: 0 additions & 5 deletions app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -227,11 +227,6 @@ public function scopeWithLastLoginAt(Builder $query): Builder
->withCasts(['last_login_at' => 'datetime']);
}

// TODO create notifications
public function resetPassword(): void
{
}

public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
Expand Down
9 changes: 9 additions & 0 deletions lang/ro/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
'plural' => 'Specialiști',
],

'titles' => [
'create_specialist' => 'Adaugă specialist',
],

'labels' => [
'first_name' => 'Nume',
'last_name' => 'Prenume',
Expand Down Expand Up @@ -77,6 +81,11 @@
'success' => 'Cont reactivat cu succes',
],

'action_reset_password_confirm' => [
'title' => 'Resetează parola',
'success' => 'Email-ul a fost trimis cu succes',
],

'status' => [
'active' => 'Activ',
'inactive' => 'Inactiv',
Expand Down

0 comments on commit 51f9c2e

Please sign in to comment.