Advertisement
Guest User

Untitled

a guest
Apr 19th, 2020
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.19 KB | None | 0 0
  1. <?php
  2. declare(strict_types=1);
  3.  
  4.  
  5. namespace App\Controller;
  6. use Authentication\AuthenticationService;
  7. use Authentication\AuthenticationServiceInterface;
  8. use Authentication\AuthenticationServiceProviderInterface;
  9. use Authentication\Middleware\AuthenticationMiddleware;
  10. use Psr\Http\Message\ServerRequestInterface;
  11. use Cake\Auth\DefaultPasswordHasher;
  12.  
  13. /**
  14. * Users Controller
  15. *
  16. * @property \App\Model\Table\UsersTable $Users
  17. * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
  18. */
  19. class UsersController extends AppController
  20. {
  21. public function beforeFilter(\Cake\Event\EventInterface $event)
  22. {
  23. parent::beforeFilter($event);
  24. // Configure the login action to not require authentication, preventing
  25. // the infinite redirect loop issue
  26. $this->Authentication->addUnauthenticatedActions(['login', 'add', 'index']);
  27. }
  28. /**
  29. * Index method
  30. *
  31. * @return \Cake\Http\Response|null|void Renders view
  32. */
  33. public function index()
  34. {
  35. $users = $this->paginate($this->Users);
  36.  
  37. $this->set(compact('users'));
  38. }
  39.  
  40. public function login()
  41. {
  42. $result = $this->Authentication->getResult();
  43. // If the user is logged in send them away.
  44. if ($result->isValid()) {
  45. $target = $this->Authentication->getLoginRedirect() ?? '/products';
  46. return $this->redirect($target);
  47. }
  48. if ($this->request->is('post') && !$result->isValid()) {
  49. $this->Flash->error('Invalid username or password');
  50. }
  51. }
  52. public function logout()
  53. {
  54. $result = $this->Authentication->getResult();
  55. // regardless of POST or GET, redirect if user is logged in
  56. if ($result->isValid()) {
  57. $this->Authentication->logout();
  58. return $this->redirect(['controller' => 'Users', 'action' => 'login']);
  59. }
  60. }
  61.  
  62. /**
  63. * Add method
  64. *
  65. * @return \Cake\Http\Response|null|void Redirects on successful add, renders view otherwise.
  66. */
  67. public function add()
  68. {
  69. $user = $this->Users->newEmptyEntity();
  70. if ($this->request->is('post')) {
  71. $user = $this->Users->patchEntity($user, $this->request->getData());
  72. if ($this->Users->save($user)) {
  73. $this->Flash->success(__('The user has been saved.'));
  74.  
  75. return $this->redirect(['action' => 'index']);
  76. }
  77. $this->Flash->error(__('The user could not be saved. Please, try again.'));
  78. }
  79. $this->set(compact('user'));
  80. }
  81.  
  82. /**
  83. * Edit method
  84. *
  85. * @param string|null $id User id.
  86. * @return \Cake\Http\Response|null|void Redirects on successful edit, renders view otherwise.
  87. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  88. */
  89. public function edit($id = null)
  90. {
  91. $user = $this->Users->get($id, [
  92. 'contain' => [],
  93. ]);
  94. if ($this->request->is(['patch', 'post', 'put'])) {
  95. $user = $this->Users->patchEntity($user, $this->request->getData());
  96. if ($this->Users->save($user)) {
  97. $this->Flash->success(__('The user has been saved.'));
  98.  
  99. return $this->redirect(['action' => 'index']);
  100. }
  101. $this->Flash->error(__('The user could not be saved. Please, try again.'));
  102. }
  103. $this->set(compact('user'));
  104. }
  105.  
  106. /**
  107. * Delete method
  108. *
  109. * @param string|null $id User id.
  110. * @return \Cake\Http\Response|null|void Redirects to index.
  111. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  112. */
  113. public function delete($id = null)
  114. {
  115. $this->request->allowMethod(['post', 'delete']);
  116. $user = $this->Users->get($id);
  117. if ($this->Users->delete($user)) {
  118. $this->Flash->success(__('The user has been deleted.'));
  119. } else {
  120. $this->Flash->error(__('The user could not be deleted. Please, try again.'));
  121. }
  122.  
  123. return $this->redirect(['action' => 'index']);
  124. }
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement