User.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  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. }
  36. public function updatingSearch()
  37. {
  38. $this->resetPage();
  39. }
  40. public function render()
  41. {
  42. $this->groups = \App\Models\UserGroup::orderBy('name')->get();
  43. $rows = \App\Models\User::where('firstname', 'like', '%'.$this->search.'%')->orWhere('lastname', 'like', '%'.$this->search.'%')->orderBy('firstname')->paginate(10);
  44. return view('livewire.users', ['records' => $rows]);
  45. }
  46. public function add()
  47. {
  48. $this->resetFields();
  49. $this->add = true;
  50. $this->update = false;
  51. }
  52. public function store()
  53. {
  54. $this->validate();
  55. if (!$this->validatePassword()) {
  56. return;
  57. }
  58. try {
  59. $u = \App\Models\User::create([
  60. 'firstname' => $this->firstname,
  61. 'lastname' => $this->lastname,
  62. 'username' => $this->username,
  63. 'email' => $this->email,
  64. 'password' => FacadesHash::make($this->password),
  65. ]);
  66. \App\Models\UserUserGroup::create([
  67. 'group_id' => $this->group_id,
  68. 'user_id' => $u->id
  69. ]);
  70. session()->flash('success','Record creato');
  71. $this->resetFields();
  72. $this->add = false;
  73. } catch (\Exception $ex) {
  74. session()->flash('error','Errore in fase di salvataggio' . $ex->getMessage());
  75. }
  76. }
  77. public function validatePassword()
  78. {
  79. if ($this->add && empty($this->password)) {
  80. $this->addError('password', 'La password è obbligatoria');
  81. return false;
  82. }
  83. if (!empty($this->password) && strlen($this->password) < 6) {
  84. $this->addError('password', 'La password deve essere di almeno 6 caratteri');
  85. return false;
  86. }
  87. return true;
  88. }
  89. public function edit($id){
  90. try {
  91. $record = \App\Models\User::findOrFail($id);
  92. if( !$record) {
  93. session()->flash('error','Record non trovato');
  94. } else {
  95. $this->firstname = $record->firstname;
  96. $this->lastname = $record->lastname;
  97. $this->username = $record->username;
  98. $this->email = $record->email;
  99. $this->old_password = $record->password;
  100. $this->dataId = $record->id;
  101. $this->update = true;
  102. $this->add = false;
  103. $this->group_id = \App\Models\UserUserGroup::where('user_id', $this->dataId)->first()->group_id;
  104. }
  105. } catch (\Exception $ex) {
  106. session()->flash('error','Errore');
  107. }
  108. }
  109. public function update()
  110. {
  111. $this->validate();
  112. if (!$this->validatePassword()) {
  113. return;
  114. }
  115. try {
  116. $updateData = [
  117. 'firstname' => $this->firstname,
  118. 'lastname' => $this->lastname,
  119. 'username' => $this->username,
  120. 'email' => $this->email,
  121. ];
  122. if ($this->password !== '') {
  123. $updateData['password'] = FacadesHash::make($this->password);
  124. }
  125. \App\Models\User::whereId($this->dataId)->update($updateData);
  126. \App\Models\UserUserGroup::where('user_id', $this->dataId)->delete();
  127. \App\Models\UserUserGroup::create([
  128. 'group_id' => $this->group_id,
  129. 'user_id' => $this->dataId
  130. ]);
  131. session()->flash('success','Record aggiornato');
  132. $this->resetFields();
  133. $this->update = false;
  134. } catch (\Exception $ex) {
  135. session()->flash('error','Errore ' . $ex->getMessage());
  136. }
  137. }
  138. public function cancel()
  139. {
  140. $this->add = false;
  141. $this->update = false;
  142. $this->resetFields();
  143. }
  144. public function delete($id)
  145. {
  146. try{
  147. \App\Models\User::find($id)->delete();
  148. session()->flash('success',"Record eliminato");
  149. }catch(\Exception $e){
  150. session()->flash('error',"Errore");
  151. }
  152. }
  153. }