User.php 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. <?php
  2. namespace App\Http\Livewire;
  3. use Livewire\Component;
  4. use Livewire\WithPagination;
  5. use Hash;
  6. use Illuminate\Support\Facades\Hash as FacadesHash;
  7. class User extends Component
  8. {
  9. use WithPagination;
  10. protected $paginationTheme = 'bootstrap';
  11. public $title = 'Utenti';
  12. public $groups = [];
  13. public $search, $firstname, $lastname, $username, $email, $group_id, $password, $old_password, $dataId, $update = false, $add = false;
  14. protected $rules = [
  15. 'firstname' => 'required',
  16. 'lastname' => 'required',
  17. 'username' => 'required',
  18. 'email' => 'required'
  19. ];
  20. protected $messages = [
  21. 'firstname.required' => 'Il nome è obbligatorio',
  22. 'lastname.required' => 'Il cognome è obbligatorio',
  23. 'username.required' => 'Lo username è obbligatorio',
  24. 'email.required' => 'Il nome è obbligatorio',
  25. 'password.min' => 'La password deve essere di almeno 6 caratteri',
  26. ];
  27. public function resetFields(){
  28. $this->firstname = '';
  29. $this->lastname = '';
  30. $this->username = '';
  31. $this->email = '';
  32. $this->password = '';
  33. $this->old_password = '';
  34. $this->group_id = null;
  35. $this->resetErrorBag();
  36. }
  37. public function updatingSearch()
  38. {
  39. $this->resetPage();
  40. }
  41. public function updatedPassword()
  42. {
  43. $this->resetErrorBag('password');
  44. }
  45. public function render()
  46. {
  47. $this->groups = \App\Models\UserGroup::orderBy('name')->get();
  48. $rows = \App\Models\User::where('firstname', 'like', '%'.$this->search.'%')->orWhere('lastname', 'like', '%'.$this->search.'%')->orderBy('firstname')->paginate(10);
  49. return view('livewire.users', ['records' => $rows]);
  50. }
  51. public function add()
  52. {
  53. $this->resetFields();
  54. $this->add = true;
  55. $this->update = false;
  56. }
  57. public function store()
  58. {
  59. $this->validate();
  60. if (!$this->validatePassword()) {
  61. return;
  62. }
  63. try {
  64. $u = \App\Models\User::create([
  65. 'firstname' => $this->firstname,
  66. 'lastname' => $this->lastname,
  67. 'username' => $this->username,
  68. 'email' => $this->email,
  69. 'password' => FacadesHash::make($this->password),
  70. ]);
  71. \App\Models\UserUserGroup::create([
  72. 'group_id' => $this->group_id,
  73. 'user_id' => $u->id
  74. ]);
  75. session()->flash('success','Record creato');
  76. $this->resetFields();
  77. $this->add = false;
  78. } catch (\Exception $ex) {
  79. session()->flash('error','Errore in fase di salvataggio' . $ex->getMessage());
  80. }
  81. }
  82. public function validatePassword()
  83. {
  84. $this->resetErrorBag('password');
  85. if ($this->add && empty($this->password)) {
  86. $this->addError('password', 'La password è obbligatoria');
  87. return false;
  88. }
  89. if (!empty($this->password) && strlen($this->password) < 6) {
  90. $this->addError('password', 'La password deve essere di almeno 6 caratteri');
  91. return false;
  92. }
  93. return true;
  94. }
  95. public function edit($id){
  96. try {
  97. $record = \App\Models\User::findOrFail($id);
  98. if( !$record) {
  99. session()->flash('error','Record non trovato');
  100. } else {
  101. $this->firstname = $record->firstname;
  102. $this->lastname = $record->lastname;
  103. $this->username = $record->username;
  104. $this->email = $record->email;
  105. $this->old_password = $record->password;
  106. $this->dataId = $record->id;
  107. $this->update = true;
  108. $this->add = false;
  109. $this->group_id = \App\Models\UserUserGroup::where('user_id', $this->dataId)->first()->group_id;
  110. }
  111. } catch (\Exception $ex) {
  112. session()->flash('error','Errore');
  113. }
  114. }
  115. public function update()
  116. {
  117. $this->validate();
  118. if (!$this->validatePassword()) {
  119. return;
  120. }
  121. try {
  122. $updateData = [
  123. 'firstname' => $this->firstname,
  124. 'lastname' => $this->lastname,
  125. 'username' => $this->username,
  126. 'email' => $this->email,
  127. ];
  128. if ($this->password !== '') {
  129. $updateData['password'] = FacadesHash::make($this->password);
  130. }
  131. \App\Models\User::whereId($this->dataId)->update($updateData);
  132. \App\Models\UserUserGroup::where('user_id', $this->dataId)->delete();
  133. \App\Models\UserUserGroup::create([
  134. 'group_id' => $this->group_id,
  135. 'user_id' => $this->dataId
  136. ]);
  137. session()->flash('success','Record aggiornato');
  138. $this->resetFields();
  139. $this->update = false;
  140. } catch (\Exception $ex) {
  141. session()->flash('error','Errore ' . $ex->getMessage());
  142. }
  143. }
  144. public function cancel()
  145. {
  146. $this->add = false;
  147. $this->update = false;
  148. $this->resetFields();
  149. }
  150. public function delete($id)
  151. {
  152. try{
  153. \App\Models\User::find($id)->delete();
  154. session()->flash('success',"Record eliminato");
  155. }catch(\Exception $e){
  156. session()->flash('error',"Errore");
  157. }
  158. }
  159. }