Web Interfaces of PROSUME

UsersController.php 4.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. namespace App\Controller;
  3. use App\Controller\AppController;
  4. use Cake\Event\Event;
  5. use App\Utility\EprosumeModel;
  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. * Index method
  17. *
  18. * @return \Cake\Http\Response|void
  19. */
  20. public function index()
  21. {
  22. $users = $this->paginate($this->Users);
  23. $this->set(compact('users'));
  24. $this->set(['meterList' => $this->getMeterList()]);
  25. }
  26. /**
  27. * View method
  28. *
  29. * @param string|null $id User id.
  30. * @return \Cake\Http\Response|void
  31. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  32. */
  33. public function view($id = null)
  34. {
  35. $user = $this->Users->get($id, [
  36. 'contain' => []
  37. ]);
  38. $this->set('user', $user);
  39. }
  40. /**
  41. * Add method
  42. *
  43. * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
  44. */
  45. public function add()
  46. {
  47. $user = $this->Users->newEntity();
  48. if ($this->request->is('post')) {
  49. $user = $this->Users->patchEntity($user, $this->request->getData());
  50. if ($this->Users->save($user)) {
  51. $this->Flash->success(__('The user has been saved.'));
  52. return $this->redirect(['action' => 'index']);
  53. }
  54. $this->Flash->error(__('The user could not be saved. Please, try again.'));
  55. }
  56. $this->set(compact('user'));
  57. $this->set(['meterList' => $this->getMeterList()]);
  58. }
  59. /**
  60. * Edit method
  61. *
  62. * @param string|null $id User id.
  63. * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
  64. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  65. */
  66. public function edit($id = null)
  67. {
  68. $user = $this->Users->get($id, [
  69. 'contain' => []
  70. ]);
  71. if ($this->request->is(['patch', 'post', 'put'])) {
  72. $user = $this->Users->patchEntity($user, $this->request->getData());
  73. if ($this->Users->save($user)) {
  74. $this->Flash->success(__('The user has been saved.'));
  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. $this->set(['meterList' => $this->getMeterList()]);
  81. }
  82. /**
  83. * Delete method
  84. *
  85. * @param string|null $id User id.
  86. * @return \Cake\Http\Response|null Redirects to index.
  87. * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
  88. */
  89. public function delete($id = null)
  90. {
  91. $this->request->allowMethod(['post', 'delete']);
  92. $user = $this->Users->get($id);
  93. if ($this->Users->delete($user)) {
  94. $this->Flash->success(__('The user has been deleted.'));
  95. } else {
  96. $this->Flash->error(__('The user could not be deleted. Please, try again.'));
  97. }
  98. return $this->redirect(['action' => 'index']);
  99. }
  100. public function beforeFilter(Event $event)
  101. {
  102. parent::beforeFilter($event);
  103. $this->Auth->allow(['logout']);
  104. }
  105. public function login()
  106. {
  107. if ($this->request->is('post')) {
  108. $user = $this->Auth->identify();
  109. if ($user) {
  110. $this->Auth->setUser($user);
  111. return $this->redirect($this->Auth->redirectUrl());
  112. }
  113. $this->Flash->error(__('Invalid username or password, try again'));
  114. }
  115. }
  116. public function logout()
  117. {
  118. return $this->redirect($this->Auth->logout());
  119. }
  120. public function isAuthorized($user)
  121. {
  122. return $this->request->getParam('action') === 'login'
  123. || $this->request->getParam('action') === 'logout'
  124. || $user["role"] === "admin";
  125. }
  126. private function getMeterList() {
  127. $EprosumeModel = new EprosumeModel();
  128. $meterList = $EprosumeModel->meterList();
  129. return array_combine(
  130. array_map(
  131. function($el) {
  132. return $el["id"];
  133. }, $meterList
  134. ),
  135. array_map(
  136. function($el) {
  137. return $el["meter"];
  138. }, $meterList
  139. )
  140. );
  141. }
  142. }