Advertisement
raphaelluiz128

userscontroller

Jun 21st, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.89 KB | None | 0 0
  1. <?php
  2. namespace App\Controller;
  3.  
  4. use App\Controller\AppController;
  5. use Cake\Event\Event;
  6. /**
  7. * Users Controller
  8. *
  9. * @property \App\Model\Table\UsersTable $Users
  10. *
  11. * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
  12. */
  13. class UsersController extends AppController
  14. {
  15.  
  16. public function beforeFilter(Event $event){
  17. parent::beforeFilter($event);
  18. $this->Auth->allow('add');
  19. }
  20.  
  21. /**
  22. * Index method
  23. *
  24. * @return \Cake\Http\Response|void
  25. */
  26. public function index()
  27. {
  28. $this->paginate = [
  29. 'contain' => ['Roles']
  30. ];
  31. $users = $this->paginate($this->Users);
  32.  
  33. $this->set(compact('users'));
  34. }
  35.  
  36. /**
  37. * View method
  38. *
  39. * @param string|null $id User id.
  40. * @return \Cake\Http\Response|void
  41. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  42. */
  43. public function view($id = null)
  44. {
  45. $user = $this->Users->get($id, [
  46. 'contain' => ['Roles']
  47. ]);
  48.  
  49. $this->set('user', $user);
  50. }
  51.  
  52. /**
  53. * Add method
  54. *
  55. * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
  56. */
  57. public function add()
  58. {
  59. $user = $this->Users->newEntity();
  60. if ($this->request->is('post')) {
  61. $user = $this->Users->patchEntity($user, $this->request->getData());
  62. if ($this->Users->save($user)) {
  63. $this->Flash->success(__('The user has been saved.'));
  64.  
  65. return $this->redirect(['action' => 'index']);
  66. }
  67. $this->Flash->error(__('The user could not be saved. Please, try again.'));
  68. }
  69. $roles = $this->Users->Roles->find('list', ['limit' => 200]);
  70. $this->set(compact('user', 'roles'));
  71. }
  72.  
  73. /**
  74. * Edit method
  75. *
  76. * @param string|null $id User id.
  77. * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
  78. * @throws \Cake\Network\Exception\NotFoundException When record not found.
  79. */
  80. public function edit($id = null)
  81. {
  82. $user = $this->Users->get($id, [
  83. 'contain' => []
  84. ]);
  85. if ($this->request->is(['patch', 'post', 'put'])) {
  86. $user = $this->Users->patchEntity($user, $this->request->getData());
  87. if ($this->Users->save($user)) {
  88. $this->Flash->success(__('The user has been saved.'));
  89.  
  90. return $this->redirect(['action' => 'index']);
  91. }
  92. $this->Flash->error(__('The user could not be saved. Please, try again.'));
  93. }
  94. $roles = $this->Users->Roles->find('list', ['limit' => 200]);
  95. $this->set(compact('user', 'roles'));
  96. }
  97.  
  98. /**
  99. * Delete method
  100. *
  101. * @param string|null $id User id.
  102. * @return \Cake\Http\Response|null Redirects to index.
  103. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  104. */
  105. public function delete($id = null)
  106. {
  107. $this->request->allowMethod(['post', 'delete']);
  108. $user = $this->Users->get($id);
  109. if ($this->Users->delete($user)) {
  110. $this->Flash->success(__('The user has been deleted.'));
  111. } else {
  112. $this->Flash->error(__('The user could not be deleted. Please, try again.'));
  113. }
  114.  
  115. return $this->redirect(['action' => 'index']);
  116. }
  117.  
  118. public function login(){
  119. if($this->request->is('post')) {
  120. $user = $this->Auth->identify();
  121. if ($user) {
  122. $this->Auth->setUser($user);
  123. return $this->redirect($hits->Auth->redirectUrl());
  124. }
  125. $this->Flash->error(_('Login inválido'));
  126. }
  127.  
  128.  
  129. }
  130.  
  131.  
  132. public function logout(){
  133. return $this->redirect($this->Auth->logout());
  134. }
  135.  
  136. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement