complete story 2.4 with qa test and added a feature disallowing admin from deactivating his own account

This commit is contained in:
Naser Mansour
2025-12-26 16:10:52 +02:00
parent b207be196e
commit f508f2b7bf
15 changed files with 1656 additions and 69 deletions
+44
View File
@@ -117,6 +117,30 @@ class User extends Authenticatable
return $this->status === UserStatus::Active;
}
/**
* Check if user is deactivated.
*/
public function isDeactivated(): bool
{
return $this->status === UserStatus::Deactivated;
}
/**
* Deactivate the user account.
*/
public function deactivate(): void
{
$this->update(['status' => UserStatus::Deactivated]);
}
/**
* Reactivate the user account.
*/
public function reactivate(): void
{
$this->update(['status' => UserStatus::Active]);
}
/**
* Scope to filter admin users.
*/
@@ -196,4 +220,24 @@ class User extends Authenticatable
{
return $this->hasMany(TimelineUpdate::class, 'admin_id');
}
/**
* Boot the model and register cascade delete event.
*/
protected static function booted(): void
{
static::deleting(function (User $user) {
// Cascade delete consultations
$user->consultations()->delete();
// Cascade delete timelines and their updates
$user->timelines->each(function ($timeline) {
$timeline->updates()->delete();
$timeline->delete();
});
// Cascade delete custom notifications
$user->customNotifications()->delete();
});
}
}
@@ -0,0 +1,50 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class AccountReactivatedNotification extends Notification implements ShouldQueue
{
use Queueable;
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$locale = $notifiable->preferred_language ?? 'ar';
return (new MailMessage)
->subject(__('emails.account_reactivated_subject', [], $locale))
->view('emails.account-reactivated', [
'user' => $notifiable,
'locale' => $locale,
]);
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'account_reactivated',
];
}
}
@@ -0,0 +1,55 @@
<?php
namespace App\Notifications;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Notifications\Messages\MailMessage;
use Illuminate\Notifications\Notification;
class PasswordResetByAdminNotification extends Notification implements ShouldQueue
{
use Queueable;
public function __construct(
public string $newPassword
) {}
/**
* Get the notification's delivery channels.
*
* @return array<int, string>
*/
public function via(object $notifiable): array
{
return ['mail'];
}
/**
* Get the mail representation of the notification.
*/
public function toMail(object $notifiable): MailMessage
{
$locale = $notifiable->preferred_language ?? 'ar';
return (new MailMessage)
->subject(__('emails.password_reset_by_admin_subject', [], $locale))
->view('emails.password-reset-by-admin', [
'user' => $notifiable,
'newPassword' => $this->newPassword,
'locale' => $locale,
]);
}
/**
* Get the array representation of the notification.
*
* @return array<string, mixed>
*/
public function toArray(object $notifiable): array
{
return [
'type' => 'password_reset_by_admin',
];
}
}