| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299 |
- <?php
- namespace App\Http\Livewire;
- use Illuminate\Support\Facades\Log;
- use Illuminate\Support\Facades\DB;
- use Livewire\Component;
- use App\Http\Middleware\TenantMiddleware;
- use Illuminate\Support\Facades\Auth;
- class User extends Component
- {
- public function boot()
- {
- app(TenantMiddleware::class)->setupTenantConnection();
- $this->logCurrentDatabase('After tenant connection setup in boot()');
- }
- public $records, $name, $cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
- public $userExists = false;
- protected $rules = [
- 'name' => 'required',
- 'cognome' => 'required',
- 'email' => 'required',
- 'password' => 'required'
- ];
- protected $messages = [
- 'name.required' => 'Il nome è obbligatorio',
- 'cognome.required' => 'Il cognome è obbligatorio',
- 'email.required' => 'La mail è obbligatoria',
- 'password.required' => 'La password è obbligatoria',
- ];
- /**
- * Helper method to log current database information
- */
- private function logCurrentDatabase($context = '')
- {
- try {
- $currentConnection = DB::getDefaultConnection();
- $currentDatabase = DB::connection()->getDatabaseName();
- $user = Auth::user();
- Log::info('Database Connection Info', [
- 'context' => $context,
- 'current_connection' => $currentConnection,
- 'current_database' => $currentDatabase,
- 'user_id' => $user ? $user->id : null,
- 'user_tenant_database' => $user ? $user->tenant_database : null,
- 'user_tenant_username' => $user ? $user->tenant_username : null,
- ]);
- } catch (\Exception $e) {
- Log::error('Failed to get database info', [
- 'context' => $context,
- 'error' => $e->getMessage()
- ]);
- }
- }
- public function resetFields()
- {
- $this->name = '';
- $this->cognome = '';
- $this->email = '';
- $this->password = '';
- $this->oldPassword = '';
- $this->level = 0;
- $this->enabled = true;
- $this->emit('load-data-table');
- }
- public function render()
- {
- $this->logCurrentDatabase('Before fetching users in render()');
- $this->records = \App\Models\User::select('id', 'name', 'cognome', 'email', 'password', 'level', 'enabled')->get();
- $this->logCurrentDatabase('After fetching users in render()');
- return view('livewire.user');
- }
- public function add()
- {
- $this->logCurrentDatabase('In add() method');
- $this->resetFields();
- $this->add = true;
- $this->update = false;
- $this->enabled = true;
- $this->userExists = false;
- }
- public function store()
- {
- $this->logCurrentDatabase('Start of store() method');
- Log::info('User store', [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- $rules = [
- 'name' => 'required',
- 'cognome' => 'required',
- 'email' => 'required|email|unique:users,email',
- 'password' => 'required|min:6'
- ];
- $messages = [
- 'name.required' => 'Il nome è obbligatorio',
- 'cognome.required' => 'Il cognome è obbligatorio',
- 'email.required' => 'La mail è obbligatoria',
- 'email.email' => 'La mail deve essere un indirizzo valido',
- 'email.unique' => 'Questa mail è già stata utilizzata',
- 'password.required' => 'La password è obbligatoria',
- 'password.min' => 'La password deve essere di almeno 6 caratteri'
- ];
- $this->validate($rules, $messages);
- $this->logCurrentDatabase('Before creating user in store()');
- try {
- $user = \App\Models\User::create([
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- $this->logCurrentDatabase('After creating user in store()');
- Log::info('User created successfully', [
- 'user_id' => $user->id,
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled,
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('success', 'Utente creato');
- $this->resetFields();
- $this->add = false;
- } catch (\Exception $ex) {
- $this->logCurrentDatabase('Error in store() method');
- Log::error('User creation failed', [
- 'error' => $ex->getMessage(),
- 'database' => DB::connection()->getDatabaseName(),
- 'user_data' => [
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]
- ]);
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
- }
- }
- public function edit($id)
- {
- $this->logCurrentDatabase('Start of edit() method');
- try {
- $user = \App\Models\User::findOrFail($id);
- $this->logCurrentDatabase('After finding user in edit()');
- if (!$user) {
- session()->flash('error', 'Dato non trovato');
- } else {
- $this->name = $user->name;
- $this->cognome = $user->cognome;
- $this->email = $user->email;
- $this->level = $user->level;
- $this->dataId = $user->id;
- $this->update = true;
- $this->add = false;
- $this->enabled = $user->enabled;
- $this->userExists = true;
- }
- Log::info('User edit loaded', [
- 'user_id' => $id,
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'database' => DB::connection()->getDatabaseName()
- ]);
- } catch (\Exception $ex) {
- $this->logCurrentDatabase('Error in edit() method');
- Log::error('User edit failed', [
- 'user_id' => $id,
- 'error' => $ex->getMessage(),
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
- }
- }
- public function update()
- {
- $this->logCurrentDatabase('Start of update() method');
- $this->validate();
- try {
- \App\Models\User::whereId($this->dataId)->update([
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'password' => bcrypt($this->password),
- 'level' => $this->level,
- 'enabled' => $this->enabled
- ]);
- $this->logCurrentDatabase('After updating user');
- Log::info('User updated successfully', [
- 'user_id' => $this->dataId,
- 'name' => $this->name,
- 'cognome' => $this->cognome,
- 'email' => $this->email,
- 'level' => $this->level,
- 'enabled' => $this->enabled,
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('success', 'Dato aggiornato');
- $this->resetFields();
- $this->update = false;
- } catch (\Exception $ex) {
- $this->logCurrentDatabase('Error in update() method');
- Log::error('User update failed', [
- 'user_id' => $this->dataId,
- 'error' => $ex->getMessage(),
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
- }
- }
- public function cancel()
- {
- $this->logCurrentDatabase('In cancel() method');
- $this->resetFields();
- $this->add = false;
- $this->update = false;
- $this->userExists = false;
- $this->enabled = false;
- }
- public function delete($id)
- {
- $this->logCurrentDatabase('Start of delete() method');
- try {
- \App\Models\User::find($id)->delete();
- $this->logCurrentDatabase('After deleting user');
- Log::info('User deleted successfully', [
- 'user_id' => $id,
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('success', "Dato eliminato");
- } catch (\Exception $e) {
- $this->logCurrentDatabase('Error in delete() method');
- Log::error('User deletion failed', [
- 'user_id' => $id,
- 'error' => $e->getMessage(),
- 'database' => DB::connection()->getDatabaseName()
- ]);
- session()->flash('error', 'Errore (' . $e->getMessage() . ')');
- }
- }
- }
|