Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace App\Controller;
- use App\Controller\AppController;
- use Cake\Event\Event;
- /**
- * 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
- {
- public function beforeFilter(Event $event){
- parent::beforeFilter($event);
- $this->Auth->allow('add');
- }
- /**
- * Index method
- *
- * @return \Cake\Http\Response|void
- */
- public function index()
- {
- $this->paginate = [
- 'contain' => ['Roles']
- ];
- $users = $this->paginate($this->Users);
- $this->set(compact('users'));
- }
- /**
- * 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' => ['Roles']
- ]);
- $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.'));
- }
- $roles = $this->Users->Roles->find('list', ['limit' => 200]);
- $this->set(compact('user', 'roles'));
- }
- /**
- * Edit method
- *
- * @param string|null $id User id.
- * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise.
- * @throws \Cake\Network\Exception\NotFoundException 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.'));
- }
- $roles = $this->Users->Roles->find('list', ['limit' => 200]);
- $this->set(compact('user', 'roles'));
- }
- /**
- * 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 login(){
- if($this->request->is('post')) {
- $user = $this->Auth->identify();
- if ($user) {
- $this->Auth->setUser($user);
- return $this->redirect($hits->Auth->redirectUrl());
- }
- $this->Flash->error(_('Login inválido'));
- }
- }
- public function logout(){
- return $this->redirect($this->Auth->logout());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement