Profile.php.bak 7.7 KB

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