User.php 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Illuminate\Support\Facades\Log;
  4. use Illuminate\Support\Facades\DB;
  5. use Livewire\Component;
  6. use App\Http\Middleware\TenantMiddleware;
  7. use Illuminate\Support\Facades\Auth;
  8. class User extends Component
  9. {
  10. public function boot()
  11. {
  12. app(TenantMiddleware::class)->setupTenantConnection();
  13. $this->logCurrentDatabase('After tenant connection setup in boot()');
  14. }
  15. public $records, $name, $cognome, $email, $password, $oldPassword, $level, $enabled, $dataId, $update = false, $add = false;
  16. public $userExists = false;
  17. protected $rules = [
  18. 'name' => 'required',
  19. 'cognome' => 'required',
  20. 'email' => 'required',
  21. 'password' => 'required'
  22. ];
  23. protected $messages = [
  24. 'name.required' => 'Il nome è obbligatorio',
  25. 'cognome.required' => 'Il cognome è obbligatorio',
  26. 'email.required' => 'La mail è obbligatoria',
  27. 'password.required' => 'La password è obbligatoria',
  28. ];
  29. /**
  30. * Helper method to log current database information
  31. */
  32. private function logCurrentDatabase($context = '')
  33. {
  34. try {
  35. $currentConnection = DB::getDefaultConnection();
  36. $currentDatabase = DB::connection()->getDatabaseName();
  37. $user = Auth::user();
  38. Log::info('Database Connection Info', [
  39. 'context' => $context,
  40. 'current_connection' => $currentConnection,
  41. 'current_database' => $currentDatabase,
  42. 'user_id' => $user ? $user->id : null,
  43. 'user_tenant_database' => $user ? $user->tenant_database : null,
  44. 'user_tenant_username' => $user ? $user->tenant_username : null,
  45. ]);
  46. } catch (\Exception $e) {
  47. Log::error('Failed to get database info', [
  48. 'context' => $context,
  49. 'error' => $e->getMessage()
  50. ]);
  51. }
  52. }
  53. public function resetFields()
  54. {
  55. $this->name = '';
  56. $this->cognome = '';
  57. $this->email = '';
  58. $this->password = '';
  59. $this->oldPassword = '';
  60. $this->level = 0;
  61. $this->enabled = true;
  62. $this->emit('load-data-table');
  63. }
  64. public function render()
  65. {
  66. $this->logCurrentDatabase('Before fetching users in render()');
  67. $this->records = \App\Models\User::select('id', 'name', 'cognome', 'email', 'password', 'level', 'enabled')->get();
  68. $this->logCurrentDatabase('After fetching users in render()');
  69. return view('livewire.user');
  70. }
  71. public function add()
  72. {
  73. $this->logCurrentDatabase('In add() method');
  74. $this->resetFields();
  75. $this->add = true;
  76. $this->update = false;
  77. $this->enabled = true;
  78. $this->userExists = false;
  79. }
  80. public function store()
  81. {
  82. $this->logCurrentDatabase('Start of store() method');
  83. Log::info('User store', [
  84. 'name' => $this->name,
  85. 'cognome' => $this->cognome,
  86. 'email' => $this->email,
  87. 'level' => $this->level,
  88. 'enabled' => $this->enabled
  89. ]);
  90. $rules = [
  91. 'name' => 'required',
  92. 'cognome' => 'required',
  93. 'email' => 'required|email|unique:users,email',
  94. 'password' => 'required|min:6'
  95. ];
  96. $messages = [
  97. 'name.required' => 'Il nome è obbligatorio',
  98. 'cognome.required' => 'Il cognome è obbligatorio',
  99. 'email.required' => 'La mail è obbligatoria',
  100. 'email.email' => 'La mail deve essere un indirizzo valido',
  101. 'email.unique' => 'Questa mail è già stata utilizzata',
  102. 'password.required' => 'La password è obbligatoria',
  103. 'password.min' => 'La password deve essere di almeno 6 caratteri'
  104. ];
  105. $this->validate($rules, $messages);
  106. $this->logCurrentDatabase('Before creating user in store()');
  107. try {
  108. $user = \App\Models\User::create([
  109. 'name' => $this->name,
  110. 'cognome' => $this->cognome,
  111. 'email' => $this->email,
  112. 'password' => bcrypt($this->password),
  113. 'level' => $this->level,
  114. 'enabled' => $this->enabled
  115. ]);
  116. $this->logCurrentDatabase('After creating user in store()');
  117. Log::info('User created successfully', [
  118. 'user_id' => $user->id,
  119. 'name' => $this->name,
  120. 'cognome' => $this->cognome,
  121. 'email' => $this->email,
  122. 'level' => $this->level,
  123. 'enabled' => $this->enabled,
  124. 'database' => DB::connection()->getDatabaseName()
  125. ]);
  126. session()->flash('success', 'Utente creato');
  127. $this->resetFields();
  128. $this->add = false;
  129. } catch (\Exception $ex) {
  130. $this->logCurrentDatabase('Error in store() method');
  131. Log::error('User creation failed', [
  132. 'error' => $ex->getMessage(),
  133. 'database' => DB::connection()->getDatabaseName(),
  134. 'user_data' => [
  135. 'name' => $this->name,
  136. 'cognome' => $this->cognome,
  137. 'email' => $this->email,
  138. 'level' => $this->level,
  139. 'enabled' => $this->enabled
  140. ]
  141. ]);
  142. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  143. }
  144. }
  145. public function edit($id)
  146. {
  147. $this->logCurrentDatabase('Start of edit() method');
  148. try {
  149. $user = \App\Models\User::findOrFail($id);
  150. $this->logCurrentDatabase('After finding user in edit()');
  151. if (!$user) {
  152. session()->flash('error', 'Dato non trovato');
  153. } else {
  154. $this->name = $user->name;
  155. $this->cognome = $user->cognome;
  156. $this->email = $user->email;
  157. $this->level = $user->level;
  158. $this->dataId = $user->id;
  159. $this->update = true;
  160. $this->add = false;
  161. $this->enabled = $user->enabled;
  162. $this->userExists = true;
  163. }
  164. Log::info('User edit loaded', [
  165. 'user_id' => $id,
  166. 'name' => $this->name,
  167. 'cognome' => $this->cognome,
  168. 'email' => $this->email,
  169. 'level' => $this->level,
  170. 'database' => DB::connection()->getDatabaseName()
  171. ]);
  172. } catch (\Exception $ex) {
  173. $this->logCurrentDatabase('Error in edit() method');
  174. Log::error('User edit failed', [
  175. 'user_id' => $id,
  176. 'error' => $ex->getMessage(),
  177. 'database' => DB::connection()->getDatabaseName()
  178. ]);
  179. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  180. }
  181. }
  182. public function update()
  183. {
  184. $this->logCurrentDatabase('Start of update() method');
  185. $this->validate();
  186. try {
  187. \App\Models\User::whereId($this->dataId)->update([
  188. 'name' => $this->name,
  189. 'cognome' => $this->cognome,
  190. 'email' => $this->email,
  191. 'password' => bcrypt($this->password),
  192. 'level' => $this->level,
  193. 'enabled' => $this->enabled
  194. ]);
  195. $this->logCurrentDatabase('After updating user');
  196. Log::info('User updated successfully', [
  197. 'user_id' => $this->dataId,
  198. 'name' => $this->name,
  199. 'cognome' => $this->cognome,
  200. 'email' => $this->email,
  201. 'level' => $this->level,
  202. 'enabled' => $this->enabled,
  203. 'database' => DB::connection()->getDatabaseName()
  204. ]);
  205. session()->flash('success', 'Dato aggiornato');
  206. $this->resetFields();
  207. $this->update = false;
  208. } catch (\Exception $ex) {
  209. $this->logCurrentDatabase('Error in update() method');
  210. Log::error('User update failed', [
  211. 'user_id' => $this->dataId,
  212. 'error' => $ex->getMessage(),
  213. 'database' => DB::connection()->getDatabaseName()
  214. ]);
  215. session()->flash('error', 'Errore (' . $ex->getMessage() . ')');
  216. }
  217. }
  218. public function cancel()
  219. {
  220. $this->logCurrentDatabase('In cancel() method');
  221. $this->resetFields();
  222. $this->add = false;
  223. $this->update = false;
  224. $this->userExists = false;
  225. $this->enabled = false;
  226. }
  227. public function delete($id)
  228. {
  229. $this->logCurrentDatabase('Start of delete() method');
  230. try {
  231. \App\Models\User::find($id)->delete();
  232. $this->logCurrentDatabase('After deleting user');
  233. Log::info('User deleted successfully', [
  234. 'user_id' => $id,
  235. 'database' => DB::connection()->getDatabaseName()
  236. ]);
  237. session()->flash('success', "Dato eliminato");
  238. } catch (\Exception $e) {
  239. $this->logCurrentDatabase('Error in delete() method');
  240. Log::error('User deletion failed', [
  241. 'user_id' => $id,
  242. 'error' => $e->getMessage(),
  243. 'database' => DB::connection()->getDatabaseName()
  244. ]);
  245. session()->flash('error', 'Errore (' . $e->getMessage() . ')');
  246. }
  247. }
  248. }