|
|
@@ -3,12 +3,22 @@
|
|
|
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 $records, $name,$cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
|
|
|
+ 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',
|
|
|
@@ -23,7 +33,34 @@ class User extends Component
|
|
|
'password.required' => 'La password è obbligatoria',
|
|
|
];
|
|
|
|
|
|
- public function resetFields(){
|
|
|
+ /**
|
|
|
+ * 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 = '';
|
|
|
@@ -36,12 +73,19 @@ class User extends Component
|
|
|
|
|
|
public function render()
|
|
|
{
|
|
|
- $this->records = \App\Models\User::select('id', 'name','cognome' ,'email', 'password', 'level', 'enabled')->get();
|
|
|
+ $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;
|
|
|
@@ -51,6 +95,8 @@ class User extends Component
|
|
|
|
|
|
public function store()
|
|
|
{
|
|
|
+ $this->logCurrentDatabase('Start of store() method');
|
|
|
+
|
|
|
Log::info('User store', [
|
|
|
'name' => $this->name,
|
|
|
'cognome' => $this->cognome,
|
|
|
@@ -58,6 +104,7 @@ class User extends Component
|
|
|
'level' => $this->level,
|
|
|
'enabled' => $this->enabled
|
|
|
]);
|
|
|
+
|
|
|
$rules = [
|
|
|
'name' => 'required',
|
|
|
'cognome' => 'required',
|
|
|
@@ -74,16 +121,13 @@ class User extends Component
|
|
|
'password.required' => 'La password è obbligatoria',
|
|
|
'password.min' => 'La password deve essere di almeno 6 caratteri'
|
|
|
];
|
|
|
+
|
|
|
$this->validate($rules, $messages);
|
|
|
- Log::info('User store', [
|
|
|
- 'name' => $this->name,
|
|
|
- 'cognome' => $this->cognome,
|
|
|
- 'email' => $this->email,
|
|
|
- 'level' => $this->level,
|
|
|
- 'enabled' => $this->enabled
|
|
|
- ]);
|
|
|
+
|
|
|
+ $this->logCurrentDatabase('Before creating user in store()');
|
|
|
+
|
|
|
try {
|
|
|
- \App\Models\User::create([
|
|
|
+ $user = \App\Models\User::create([
|
|
|
'name' => $this->name,
|
|
|
'cognome' => $this->cognome,
|
|
|
'email' => $this->email,
|
|
|
@@ -91,26 +135,52 @@ class User extends Component
|
|
|
'level' => $this->level,
|
|
|
'enabled' => $this->enabled
|
|
|
]);
|
|
|
- Log::info('User created', [
|
|
|
+
|
|
|
+ $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
|
|
|
+ 'enabled' => $this->enabled,
|
|
|
+ 'database' => DB::connection()->getDatabaseName()
|
|
|
]);
|
|
|
- session()->flash('success','Utente creato');
|
|
|
+
|
|
|
+ session()->flash('success', 'Utente creato');
|
|
|
$this->resetFields();
|
|
|
$this->add = false;
|
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
+ $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){
|
|
|
+ public function edit($id)
|
|
|
+ {
|
|
|
+ $this->logCurrentDatabase('Start of edit() method');
|
|
|
+
|
|
|
try {
|
|
|
$user = \App\Models\User::findOrFail($id);
|
|
|
- if( !$user) {
|
|
|
- session()->flash('error','Dato non trovato');
|
|
|
+
|
|
|
+ $this->logCurrentDatabase('After finding user in edit()');
|
|
|
+
|
|
|
+ if (!$user) {
|
|
|
+ session()->flash('error', 'Dato non trovato');
|
|
|
} else {
|
|
|
$this->name = $user->name;
|
|
|
$this->cognome = $user->cognome;
|
|
|
@@ -122,22 +192,35 @@ class User extends Component
|
|
|
$this->enabled = $user->enabled;
|
|
|
$this->userExists = true;
|
|
|
}
|
|
|
- Log::info('User edit', [
|
|
|
+
|
|
|
+ Log::info('User edit loaded', [
|
|
|
+ 'user_id' => $id,
|
|
|
'name' => $this->name,
|
|
|
'cognome' => $this->cognome,
|
|
|
'email' => $this->email,
|
|
|
- 'level' => $this->level
|
|
|
+ 'level' => $this->level,
|
|
|
+ 'database' => DB::connection()->getDatabaseName()
|
|
|
]);
|
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
+ $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 {
|
|
|
- if ($this->pa)
|
|
|
\App\Models\User::whereId($this->dataId)->update([
|
|
|
'name' => $this->name,
|
|
|
'cognome' => $this->cognome,
|
|
|
@@ -146,23 +229,39 @@ class User extends Component
|
|
|
'level' => $this->level,
|
|
|
'enabled' => $this->enabled
|
|
|
]);
|
|
|
- Log::info('User updated', [
|
|
|
+
|
|
|
+ $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
|
|
|
+ 'enabled' => $this->enabled,
|
|
|
+ 'database' => DB::connection()->getDatabaseName()
|
|
|
]);
|
|
|
- session()->flash('success','Dato aggiornato');
|
|
|
+
|
|
|
+ session()->flash('success', 'Dato aggiornato');
|
|
|
$this->resetFields();
|
|
|
$this->update = false;
|
|
|
} catch (\Exception $ex) {
|
|
|
- session()->flash('error','Errore (' . $ex->getMessage() . ')');
|
|
|
+ $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;
|
|
|
@@ -172,11 +271,29 @@ class User extends Component
|
|
|
|
|
|
public function delete($id)
|
|
|
{
|
|
|
- try{
|
|
|
+ $this->logCurrentDatabase('Start of delete() method');
|
|
|
+
|
|
|
+ try {
|
|
|
\App\Models\User::find($id)->delete();
|
|
|
- session()->flash('success',"Dato eliminato");
|
|
|
- }catch(\Exception $e){
|
|
|
- session()->flash('error','Errore (' . $e->getMessage() . ')');
|
|
|
+
|
|
|
+ $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() . ')');
|
|
|
}
|
|
|
}
|
|
|
}
|