| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200 |
- <?php
- namespace App\Http\Livewire;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Support\Facades\Hash;
- use Illuminate\Support\Facades\DB;
- use Illuminate\Support\Facades\Log;
- use Livewire\Component;
- use App\Http\Middleware\TenantMiddleware;
- class Profile extends Component
- {
- public $editMode = false;
- public $name;
- public $cognome;
- public $email;
- public $telefono;
- public $cellulare;
- public $password;
- public function boot()
- {
- app(TenantMiddleware::class)->setupTenantConnection();
- }
- public function mount()
- {
- $currentUser = Auth::user();
- $user = \App\Models\User::findOrFail($currentUser->id);
- $this->name = $user->name;
- $this->cognome = $user->cognome;
- $this->email = $user->email;
- $this->telefono = $user->telefono;
- $this->cellulare = $user->cellulare;
- }
- public function enableEditMode()
- {
- $this->editMode = true;
- }
- public function save()
- {
- $this->validate([
- 'name' => 'required',
- 'cognome' => 'required',
- 'email' => 'required|email',
- 'password' => 'nullable|min:6',
- ]);
- $currentUser = Auth::user();
- try {
- DB::beginTransaction();
- $user = \App\Models\User::findOrFail($currentUser->id);
- $oldEmail = $user->email;
- $passwordChanged = !empty($this->password);
- $emailChanged = $oldEmail !== $this->email;
- $user->name = $this->name;
- $user->cognome = $this->cognome;
- $user->email = $this->email;
- $user->telefono = $this->telefono;
- $user->cellulare = $this->cellulare;
- if ($passwordChanged) {
- $user->password = Hash::make($this->password);
- }
- $user->save();
- Log::info('Updated user in tenant database', [
- 'user_id' => $user->id,
- 'tenant_database' => DB::connection()->getDatabaseName(),
- 'email_changed' => $emailChanged,
- 'password_changed' => $passwordChanged
- ]);
- if ($emailChanged || $passwordChanged || $currentUser->name !== $this->name) {
- $this->updateMasterDatabase($currentUser, $oldEmail, $passwordChanged);
- }
- DB::commit();
- session()->flash('message', 'Profilo aggiornato con successo!');
- $this->editMode = false;
- $this->password = '';
- } catch (\Exception $e) {
- DB::rollBack();
- Log::error('Profile update failed', [
- 'user_id' => $currentUser->id,
- 'error' => $e->getMessage(),
- 'trace' => $e->getTraceAsString()
- ]);
- session()->flash('error', 'Errore durante l\'aggiornamento: ' . $e->getMessage());
- }
- }
- /**
- * Update user information in master database
- */
- private function updateMasterDatabase($currentUser, $oldEmail, $passwordChanged)
- {
- try {
- // Store current tenant connection info
- $currentConnection = DB::getDefaultConnection();
- $currentDatabase = DB::connection()->getDatabaseName();
- Log::info('Updating master database', [
- 'current_connection' => $currentConnection,
- 'current_database' => $currentDatabase,
- 'user_id' => $currentUser->id,
- 'old_email' => $oldEmail,
- 'new_email' => $this->email
- ]);
- $masterConfig = [
- 'driver' => 'mysql',
- 'host' => env('DB_HOST', '127.0.0.1'),
- 'port' => env('DB_PORT', '3306'),
- 'database' => env('DB_DATABASE'),
- 'username' => env('DB_USERNAME'),
- 'password' => env('DB_PASSWORD'),
- 'charset' => 'utf8mb4',
- 'collation' => 'utf8mb4_unicode_ci',
- 'prefix' => '',
- 'strict' => true,
- 'engine' => null,
- ];
- config(['database.connections.master_temp' => $masterConfig]);
- $updateData = [
- 'name' => $this->name,
- 'email' => $this->email
- ];
- if ($passwordChanged) {
- $updateData['password'] = Hash::make($this->password);
- }
- $updated = DB::connection('master_temp')
- ->table('users')
- ->where('email', $oldEmail)
- ->update($updateData);
- if ($updated) {
- Log::info('Successfully updated user in master database', [
- 'old_email' => $oldEmail,
- 'new_email' => $this->email,
- 'password_changed' => $passwordChanged
- ]);
- } else {
- Log::warning('No user found in master database to update', [
- 'email' => $oldEmail
- ]);
- }
- config(['database.default' => $currentConnection]);
- DB::purge('master_temp');
- } catch (\Exception $e) {
- Log::error('Failed to update master database', [
- 'error' => $e->getMessage(),
- 'user_id' => $currentUser->id,
- 'old_email' => $oldEmail,
- 'new_email' => $this->email
- ]);
- }
- }
- public function cancel()
- {
- $this->editMode = false;
- $this->password = '';
- $this->mount();
- }
- private function resetInputFields()
- {
- $this->name = '';
- $this->cognome = '';
- $this->email = '';
- $this->telefono = '';
- $this->cellulare = '';
- $this->password = '';
- }
- public function render()
- {
- return view('livewire.profile');
- }
- }
|