123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- <?php
- namespace App\Controller;
-
- use App\Controller\AppController;
- use Cake\Event\Event;
-
- use App\Utility\EprosumeModel;
-
- /**
- * Users Controller
- *
- * @property \App\Model\Table\UsersTable $Users
- *
- * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = [])
- */
- class UsersController extends AppController
- {
-
- /**
- * Index method
- *
- * @return \Cake\Http\Response|void
- */
- public function index()
- {
- $users = $this->paginate($this->Users);
-
- $this->set(compact('users'));
- $this->set(['meterList' => $this->getMeterList()]);
- }
-
- /**
- * View method
- *
- * @param string|null $id User id.
- * @return \Cake\Http\Response|void
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function view($id = null)
- {
- $user = $this->Users->get($id, [
- 'contain' => []
- ]);
-
- $this->set('user', $user);
- }
-
- /**
- * Add method
- *
- * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise.
- */
- public function add()
- {
- $user = $this->Users->newEntity();
- if ($this->request->is('post')) {
- $user = $this->Users->patchEntity($user, $this->request->getData());
- if ($this->Users->save($user)) {
- $this->Flash->success(__('The user has been saved.'));
-
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The user could not be saved. Please, try again.'));
- }
- $this->set(compact('user'));
- $this->set(['meterList' => $this->getMeterList()]);
- }
-
- /**
- * Edit method
- *
- * @param string|null $id User id.
- * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function edit($id = null)
- {
- $user = $this->Users->get($id, [
- 'contain' => []
- ]);
- if ($this->request->is(['patch', 'post', 'put'])) {
- $user = $this->Users->patchEntity($user, $this->request->getData());
- if ($this->Users->save($user)) {
- $this->Flash->success(__('The user has been saved.'));
-
- return $this->redirect(['action' => 'index']);
- }
- $this->Flash->error(__('The user could not be saved. Please, try again.'));
- }
- $this->set(compact('user'));
- $this->set(['meterList' => $this->getMeterList()]);
- }
-
- /**
- * Delete method
- *
- * @param string|null $id User id.
- * @return \Cake\Http\Response|null Redirects to index.
- * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found.
- */
- public function delete($id = null)
- {
- $this->request->allowMethod(['post', 'delete']);
- $user = $this->Users->get($id);
- if ($this->Users->delete($user)) {
- $this->Flash->success(__('The user has been deleted.'));
- } else {
- $this->Flash->error(__('The user could not be deleted. Please, try again.'));
- }
-
- return $this->redirect(['action' => 'index']);
- }
-
- public function beforeFilter(Event $event)
- {
- parent::beforeFilter($event);
- $this->Auth->allow(['logout']);
- }
-
- public function login()
- {
- if ($this->request->is('post')) {
- $user = $this->Auth->identify();
- if ($user) {
- $this->Auth->setUser($user);
- return $this->redirect($this->Auth->redirectUrl());
- }
- $this->Flash->error(__('Invalid username or password, try again'));
- }
- }
-
- public function logout()
- {
- return $this->redirect($this->Auth->logout());
- }
-
- public function isAuthorized($user)
- {
- return $this->request->getParam('action') === 'login'
- || $this->request->getParam('action') === 'logout'
- || $user["role"] === "admin";
- }
-
- private function getMeterList() {
- $EprosumeModel = new EprosumeModel();
- $meterList = $EprosumeModel->meterList();
- return array_combine(
- array_map(
- function($el) {
- return $el["id"];
- }, $meterList
- ),
- array_map(
- function($el) {
- return $el["meter"];
- }, $meterList
- )
- );
- }
- }
|