Profile.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. class Profile extends Component
  10. {
  11. public $editMode = false;
  12. public $name;
  13. public $cognome;
  14. public $email;
  15. public $telefono;
  16. public $cellulare;
  17. public $password;
  18. public function boot()
  19. {
  20. app(TenantMiddleware::class)->setupTenantConnection();
  21. }
  22. public function mount()
  23. {
  24. $currentUser = Auth::user();
  25. $user = \App\Models\User::findOrFail($currentUser->id);
  26. $this->name = $user->name;
  27. $this->cognome = $user->cognome;
  28. $this->email = $user->email;
  29. $this->telefono = $user->telefono;
  30. $this->cellulare = $user->cellulare;
  31. }
  32. public function enableEditMode()
  33. {
  34. $this->editMode = true;
  35. }
  36. public function save()
  37. {
  38. $this->validate([
  39. 'name' => 'required',
  40. 'cognome' => 'required',
  41. 'email' => 'required|email',
  42. 'password' => 'nullable|min:6',
  43. ]);
  44. $currentUser = Auth::user();
  45. try {
  46. DB::beginTransaction();
  47. $user = \App\Models\User::findOrFail($currentUser->id);
  48. $oldEmail = $user->email;
  49. $passwordChanged = !empty($this->password);
  50. $emailChanged = $oldEmail !== $this->email;
  51. $user->name = $this->name;
  52. $user->cognome = $this->cognome;
  53. $user->email = $this->email;
  54. $user->telefono = $this->telefono;
  55. $user->cellulare = $this->cellulare;
  56. if ($passwordChanged) {
  57. $user->password = Hash::make($this->password);
  58. }
  59. $user->save();
  60. Log::info('Updated user in tenant database', [
  61. 'user_id' => $user->id,
  62. 'tenant_database' => DB::connection()->getDatabaseName(),
  63. 'email_changed' => $emailChanged,
  64. 'password_changed' => $passwordChanged
  65. ]);
  66. if ($emailChanged || $passwordChanged || $currentUser->name !== $this->name) {
  67. $this->updateMasterDatabase($currentUser, $oldEmail, $passwordChanged);
  68. }
  69. DB::commit();
  70. session()->flash('message', 'Profilo aggiornato con successo!');
  71. $this->editMode = false;
  72. $this->password = '';
  73. } catch (\Exception $e) {
  74. DB::rollBack();
  75. Log::error('Profile update failed', [
  76. 'user_id' => $currentUser->id,
  77. 'error' => $e->getMessage(),
  78. 'trace' => $e->getTraceAsString()
  79. ]);
  80. session()->flash('error', 'Errore durante l\'aggiornamento: ' . $e->getMessage());
  81. }
  82. }
  83. /**
  84. * Update user information in master database
  85. */
  86. private function updateMasterDatabase($currentUser, $oldEmail, $passwordChanged)
  87. {
  88. try {
  89. // Store current tenant connection info
  90. $currentConnection = DB::getDefaultConnection();
  91. $currentDatabase = DB::connection()->getDatabaseName();
  92. Log::info('Updating master database', [
  93. 'current_connection' => $currentConnection,
  94. 'current_database' => $currentDatabase,
  95. 'user_id' => $currentUser->id,
  96. 'old_email' => $oldEmail,
  97. 'new_email' => $this->email
  98. ]);
  99. $masterConfig = [
  100. 'driver' => 'mysql',
  101. 'host' => env('DB_HOST', '127.0.0.1'),
  102. 'port' => env('DB_PORT', '3306'),
  103. 'database' => env('DB_DATABASE'),
  104. 'username' => env('DB_USERNAME'),
  105. 'password' => env('DB_PASSWORD'),
  106. 'charset' => 'utf8mb4',
  107. 'collation' => 'utf8mb4_unicode_ci',
  108. 'prefix' => '',
  109. 'strict' => true,
  110. 'engine' => null,
  111. ];
  112. config(['database.connections.master_temp' => $masterConfig]);
  113. $updateData = [
  114. 'name' => $this->name,
  115. 'email' => $this->email
  116. ];
  117. if ($passwordChanged) {
  118. $updateData['password'] = Hash::make($this->password);
  119. }
  120. $updated = DB::connection('master_temp')
  121. ->table('users')
  122. ->where('email', $oldEmail)
  123. ->update($updateData);
  124. if ($updated) {
  125. Log::info('Successfully updated user in master database', [
  126. 'old_email' => $oldEmail,
  127. 'new_email' => $this->email,
  128. 'password_changed' => $passwordChanged
  129. ]);
  130. } else {
  131. Log::warning('No user found in master database to update', [
  132. 'email' => $oldEmail
  133. ]);
  134. }
  135. config(['database.default' => $currentConnection]);
  136. DB::purge('master_temp');
  137. } catch (\Exception $e) {
  138. Log::error('Failed to update master database', [
  139. 'error' => $e->getMessage(),
  140. 'user_id' => $currentUser->id,
  141. 'old_email' => $oldEmail,
  142. 'new_email' => $this->email
  143. ]);
  144. }
  145. }
  146. public function cancel()
  147. {
  148. $this->editMode = false;
  149. $this->password = '';
  150. $this->mount();
  151. }
  152. private function resetInputFields()
  153. {
  154. $this->name = '';
  155. $this->cognome = '';
  156. $this->email = '';
  157. $this->telefono = '';
  158. $this->cellulare = '';
  159. $this->password = '';
  160. }
  161. public function render()
  162. {
  163. return view('livewire.profile');
  164. }
  165. }