Profile.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Facades\Auth;
  4. use Illuminate\Support\Facades\Hash;
  5. use Illuminate\Support\Facades\DB;
  6. use Illuminate\Support\Facades\Log;
  7. use Livewire\Component;
  8. use App\Http\Middleware\TenantMiddleware;
  9. use Illuminate\Support\Facades\Mail;
  10. class Profile extends Component
  11. {
  12. public $editMode = false;
  13. public $name;
  14. public $cognome;
  15. public $email;
  16. public $telefono;
  17. public $cellulare;
  18. public $password;
  19. public $password_confirmation;
  20. public function boot()
  21. {
  22. app(TenantMiddleware::class)->setupTenantConnection();
  23. }
  24. public function mount()
  25. {
  26. $currentUser = Auth::user();
  27. $user = \App\Models\User::findOrFail($currentUser->id);
  28. $this->name = $user->name;
  29. $this->cognome = $user->cognome;
  30. $this->email = $user->email;
  31. $this->telefono = $user->telefono;
  32. $this->cellulare = $user->cellulare;
  33. }
  34. public function enableEditMode()
  35. {
  36. $this->editMode = true;
  37. }
  38. public function save()
  39. {
  40. $rules = [
  41. 'name' => 'required',
  42. 'cognome' => 'required',
  43. 'email' => 'required|email',
  44. 'password' => 'nullable|min:6',
  45. 'password_confirmation' => 'nullable|same:password'
  46. ];
  47. $messages = [
  48. 'name.required' => 'Il nome è obbligatorio',
  49. 'cognome.required' => 'Il cognome è obbligatorio',
  50. 'email.required' => 'La mail è obbligatoria',
  51. 'email.email' => 'La mail deve essere un indirizzo valido',
  52. 'email.unique' => 'Questa mail è già stata utilizzata',
  53. 'password.required' => 'La password è obbligatoria',
  54. 'password.min' => 'La password deve essere di almeno 6 caratteri',
  55. 'password_confirmation.required' => 'Ripeti la password inserita',
  56. 'password_confirmation.same' => 'Le password non coincidono',
  57. ];
  58. $this->validate($rules, $messages);
  59. $currentUser = Auth::user();
  60. try {
  61. // Update user in tenant database
  62. $user = \App\Models\User::findOrFail($currentUser->id);
  63. $oldEmail = $user->email;
  64. $oldName = $user->name;
  65. $passwordChanged = !empty($this->password);
  66. $emailChanged = $oldEmail !== $this->email;
  67. $nameChanged = $oldName !== $this->name;
  68. $user->name = $this->name;
  69. $user->cognome = $this->cognome;
  70. $user->email = $this->email;
  71. $user->telefono = $this->telefono;
  72. $user->cellulare = $this->cellulare;
  73. if ($passwordChanged) {
  74. $user->password = Hash::make($this->password);
  75. }
  76. $user->save();
  77. Log::info('Updated user in tenant database', [
  78. 'user_id' => $user->id,
  79. 'tenant_database' => DB::connection()->getDatabaseName(),
  80. 'email_changed' => $emailChanged,
  81. 'name_changed' => $nameChanged,
  82. 'password_changed' => $passwordChanged
  83. ]);
  84. // Update master database if needed
  85. if ($emailChanged || $passwordChanged || $nameChanged) {
  86. $masterUpdated = $this->updateMasterDatabase($currentUser, $oldEmail, $passwordChanged);
  87. }
  88. // Send password change notification if password was changed
  89. if ($passwordChanged) {
  90. $notificationSent = $this->sendPasswordChangeNotification($this->email, $this->name);
  91. if ($notificationSent) {
  92. session()->flash('message', 'Profilo aggiornato con successo! Ti abbiamo inviato una email di conferma per la modifica della password.');
  93. } else {
  94. session()->flash('message', 'Profilo aggiornato con successo! (Errore nell\'invio dell\'email di notifica)');
  95. }
  96. } else {
  97. session()->flash('message', 'Profilo aggiornato con successo!');
  98. }
  99. $this->editMode = false;
  100. $this->password = ''; // Clear password field
  101. $this->password_confirmation = ''; // Clear password_confirmation field
  102. } catch (\Exception $e) {
  103. Log::error('Profile update failed', [
  104. 'user_id' => $currentUser->id,
  105. 'error' => $e->getMessage(),
  106. 'trace' => $e->getTraceAsString()
  107. ]);
  108. session()->flash('error', 'Errore durante l\'aggiornamento: ' . $e->getMessage());
  109. }
  110. }
  111. /**
  112. * Update user information in master database
  113. */
  114. private function updateMasterDatabase($currentUser, $oldEmail, $passwordChanged)
  115. {
  116. try {
  117. // Store current tenant connection info
  118. $currentConnection = DB::getDefaultConnection();
  119. $currentDatabase = DB::connection()->getDatabaseName();
  120. Log::info('Updating master database', [
  121. 'current_connection' => $currentConnection,
  122. 'current_database' => $currentDatabase,
  123. 'user_id' => $currentUser->id,
  124. 'old_email' => $oldEmail,
  125. 'new_email' => $this->email
  126. ]);
  127. $masterConfig = [
  128. 'driver' => 'mysql',
  129. 'host' => env('DB_HOST', '127.0.0.1'),
  130. 'port' => env('DB_PORT', '3306'),
  131. 'database' => env('DB_DATABASE'),
  132. 'username' => env('DB_USERNAME'),
  133. 'password' => env('DB_PASSWORD'),
  134. 'charset' => 'utf8mb4',
  135. 'collation' => 'utf8mb4_unicode_ci',
  136. 'prefix' => '',
  137. 'strict' => true,
  138. 'engine' => null,
  139. ];
  140. config(['database.connections.master_temp' => $masterConfig]);
  141. $updateData = [
  142. 'name' => $this->name,
  143. 'email' => $this->email
  144. ];
  145. if ($passwordChanged) {
  146. $updateData['password'] = Hash::make($this->password);
  147. }
  148. $updated = DB::connection('master_temp')
  149. ->table('users')
  150. ->where('email', $oldEmail)
  151. ->update($updateData);
  152. if ($updated) {
  153. Log::info('Successfully updated user in master database', [
  154. 'old_email' => $oldEmail,
  155. 'new_email' => $this->email,
  156. 'password_changed' => $passwordChanged
  157. ]);
  158. } else {
  159. Log::warning('No user found in master database to update', [
  160. 'email' => $oldEmail
  161. ]);
  162. }
  163. config(['database.default' => $currentConnection]);
  164. DB::purge('master_temp');
  165. } catch (\Exception $e) {
  166. Log::error('Failed to update master database', [
  167. 'error' => $e->getMessage(),
  168. 'user_id' => $currentUser->id,
  169. 'old_email' => $oldEmail,
  170. 'new_email' => $this->email
  171. ]);
  172. }
  173. }
  174. public function cancel()
  175. {
  176. return redirect()->to('/dashboard');
  177. // $this->editMode = false;
  178. // $this->password = '';
  179. // $this->mount();
  180. }
  181. private function resetInputFields()
  182. {
  183. $this->name = '';
  184. $this->cognome = '';
  185. $this->email = '';
  186. $this->telefono = '';
  187. $this->cellulare = '';
  188. $this->password = '';
  189. $this->password_confirmation = '';
  190. }
  191. public function render()
  192. {
  193. return view('livewire.profile');
  194. }
  195. private function sendPasswordChangeNotification($email, $name)
  196. {
  197. try {
  198. $emailData = [
  199. 'name' => $name,
  200. 'email' => $email,
  201. 'change_time' => now()->format('d/m/Y H:i'),
  202. 'ip_address' => request()->ip()
  203. ];
  204. Mail::send('emails.password-changed', $emailData, function ($message) use ($email, $name) {
  205. $message->to($email, $name)
  206. ->subject('La tua password è stata modificata')
  207. ->from(config('mail.from.address'), config('mail.from.name'));
  208. });
  209. Log::info('Password change notification sent from profile', [
  210. 'email' => $email,
  211. 'name' => $name
  212. ]);
  213. return true;
  214. } catch (\Exception $e) {
  215. Log::error('Failed to send password change notification from profile', [
  216. 'email' => $email,
  217. 'error' => $e->getMessage()
  218. ]);
  219. return false;
  220. }
  221. }
  222. }