Skip to content

Instantly share code, notes, and snippets.

@alexi190
Last active August 29, 2015 14:23
Show Gist options
  • Save alexi190/fb5c6f65014241cde429 to your computer and use it in GitHub Desktop.
Save alexi190/fb5c6f65014241cde429 to your computer and use it in GitHub Desktop.
<?php
use App\Controller\AppController;
use Cake\Event\Event;
use Cake\Network\Exception\NotFoundException;
class UsersController extends AppController {
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
// Allow users to register and logout.
// You should not add the "login" action to allow list. Doing so would
// cause problems with normal functioning of AuthComponent.
$this->Auth->allow(['add', 'logout']);
}
public function index() {
$this->set('users', $this->Users->find('all'));
}
public function view($id) {
if (!$id) {
throw new NotFoundException(__('Invalid user'));
}
$user = $this->Users->get($id);
$this->set(compact('user'));
}
public function add() {
$user = $this->Users->newEntity();
if ($this->request->is('post')) {
$user = $this->Users->patchEntity($user, $this->request->data);
if ($this->Users->save($user)) {
$this->Flash->success(__('The user has been saved.'));
return $this->redirect(['action' => 'add']);
}
$this->Flash->error(__('Unable to add the user.'));
}
$this->set('user', $user);
}
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());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment