|
|
@@ -8,6 +8,7 @@ use Illuminate\Support\Facades\DB;
|
|
|
use Illuminate\Support\Facades\Log;
|
|
|
use Livewire\Component;
|
|
|
use App\Http\Middleware\TenantMiddleware;
|
|
|
+use Illuminate\Support\Facades\Mail;
|
|
|
|
|
|
class Profile extends Component
|
|
|
{
|
|
|
@@ -53,12 +54,13 @@ class Profile extends Component
|
|
|
$currentUser = Auth::user();
|
|
|
|
|
|
try {
|
|
|
- DB::beginTransaction();
|
|
|
-
|
|
|
+ // Update user in tenant database
|
|
|
$user = \App\Models\User::findOrFail($currentUser->id);
|
|
|
$oldEmail = $user->email;
|
|
|
+ $oldName = $user->name;
|
|
|
$passwordChanged = !empty($this->password);
|
|
|
$emailChanged = $oldEmail !== $this->email;
|
|
|
+ $nameChanged = $oldName !== $this->name;
|
|
|
|
|
|
$user->name = $this->name;
|
|
|
$user->cognome = $this->cognome;
|
|
|
@@ -76,22 +78,32 @@ class Profile extends Component
|
|
|
'user_id' => $user->id,
|
|
|
'tenant_database' => DB::connection()->getDatabaseName(),
|
|
|
'email_changed' => $emailChanged,
|
|
|
+ 'name_changed' => $nameChanged,
|
|
|
'password_changed' => $passwordChanged
|
|
|
]);
|
|
|
|
|
|
- if ($emailChanged || $passwordChanged || $currentUser->name !== $this->name) {
|
|
|
- $this->updateMasterDatabase($currentUser, $oldEmail, $passwordChanged);
|
|
|
+ // Update master database if needed
|
|
|
+ if ($emailChanged || $passwordChanged || $nameChanged) {
|
|
|
+ $masterUpdated = $this->updateMasterDatabase($currentUser, $oldEmail, $passwordChanged);
|
|
|
}
|
|
|
|
|
|
- DB::commit();
|
|
|
+ // Send password change notification if password was changed
|
|
|
+ if ($passwordChanged) {
|
|
|
+ $notificationSent = $this->sendPasswordChangeNotification($this->email, $this->name);
|
|
|
+
|
|
|
+ if ($notificationSent) {
|
|
|
+ session()->flash('message', 'Profilo aggiornato con successo! Ti abbiamo inviato una email di conferma per la modifica della password.');
|
|
|
+ } else {
|
|
|
+ session()->flash('message', 'Profilo aggiornato con successo! (Errore nell\'invio dell\'email di notifica)');
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ session()->flash('message', 'Profilo aggiornato con successo!');
|
|
|
+ }
|
|
|
|
|
|
- session()->flash('message', 'Profilo aggiornato con successo!');
|
|
|
$this->editMode = false;
|
|
|
- $this->password = '';
|
|
|
+ $this->password = ''; // Clear password field
|
|
|
|
|
|
} catch (\Exception $e) {
|
|
|
- DB::rollBack();
|
|
|
-
|
|
|
Log::error('Profile update failed', [
|
|
|
'user_id' => $currentUser->id,
|
|
|
'error' => $e->getMessage(),
|
|
|
@@ -101,7 +113,6 @@ class Profile extends Component
|
|
|
session()->flash('error', 'Errore durante l\'aggiornamento: ' . $e->getMessage());
|
|
|
}
|
|
|
}
|
|
|
-
|
|
|
/**
|
|
|
* Update user information in master database
|
|
|
*/
|
|
|
@@ -164,7 +175,6 @@ class Profile extends Component
|
|
|
|
|
|
config(['database.default' => $currentConnection]);
|
|
|
DB::purge('master_temp');
|
|
|
-
|
|
|
} catch (\Exception $e) {
|
|
|
Log::error('Failed to update master database', [
|
|
|
'error' => $e->getMessage(),
|
|
|
@@ -197,4 +207,35 @@ class Profile extends Component
|
|
|
{
|
|
|
return view('livewire.profile');
|
|
|
}
|
|
|
+
|
|
|
+ private function sendPasswordChangeNotification($email, $name)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $emailData = [
|
|
|
+ 'name' => $name,
|
|
|
+ 'email' => $email,
|
|
|
+ 'change_time' => now()->format('d/m/Y H:i'),
|
|
|
+ 'ip_address' => request()->ip()
|
|
|
+ ];
|
|
|
+
|
|
|
+ Mail::send('emails.password-changed', $emailData, function ($message) use ($email, $name) {
|
|
|
+ $message->to($email, $name)
|
|
|
+ ->subject('La tua password è stata modificata')
|
|
|
+ ->from(config('mail.from.address'), config('mail.from.name'));
|
|
|
+ });
|
|
|
+
|
|
|
+ Log::info('Password change notification sent from profile', [
|
|
|
+ 'email' => $email,
|
|
|
+ 'name' => $name
|
|
|
+ ]);
|
|
|
+
|
|
|
+ return true;
|
|
|
+ } catch (\Exception $e) {
|
|
|
+ Log::error('Failed to send password change notification from profile', [
|
|
|
+ 'email' => $email,
|
|
|
+ 'error' => $e->getMessage()
|
|
|
+ ]);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
}
|