Skip to content

Instantly share code, notes, and snippets.

@patrickmaciel
Created January 12, 2012 19:54
Show Gist options
  • Save patrickmaciel/1602714 to your computer and use it in GitHub Desktop.
Save patrickmaciel/1602714 to your computer and use it in GitHub Desktop.
Erro Auth Component - Seguindo tutorial do Curso Assando Sites Básico - Aula 6 (Última aula)
<?php
class AppController extends Controller {
public $helpers = array('Html','Form','Session');
public $components = array('Session', 'Auth');
public function beforeFilter() {
if($this->isPrefix('painel')) {
$this->layout = 'painel';
$this->Auth->authenticate = array('Form', array(
'userModel' => 'Usuario',
'fields' => array(
'username' => 'usuario',
'password' => 'senha'
)
)
);
} else {
$this->Auth->allow('*');
}
parent::beforeFilter();
}
public function isPrefix($prefix) {
return isset($this->request->params['prefix']) && $this->request->params['prefix'] == $prefix;
}
}
<?php
$this->Session->flash('auth');
echo $this->Form->create(null, array('controller' => 'usuarios', 'action' => 'login', 'painel' => 'true'));
echo $this->Form->input('usuario');
echo $this->Form->input('senha', array('type' => 'password'))
echo $this->Form->end('Entrar');
?>
<?php
App::uses('AppModel', 'Model');
/**
* Usuario Model
*
*/
class Usuario extends AppModel {
/**
* Display field
*
* @var string
*/
public $displayField = 'usuario';
/**
* beforeSave method
*
* @return array
*/
public function beforeSave($options = array()) {
$senha = $this->data['Usuario']['senha'];
$senha = AuthComponent::password($senha);
$this->data['Usuario']['senha'] = $senha;
return parent::beforeSave($options);
}
}
<?php
App::uses('AppController', 'Controller');
/**
* Usuarios Controller
*
* @property Usuario $Usuario
*/
class UsuariosController extends AppController {
/**
* login method
*
* @return boolean
*/
public function painel_login() {
if($this->request->is('post')) {
if($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash('Usuário e/ou senha inválidos', 'default', array(), 'auth');
}
}
}
/**
* logout method
*
* @return void
*/
public function painel_logout() {
$this->redirect($this->Auth->logout());
}
/**
* index method
*
* @return void
*/
public function index() {
$this->Usuario->recursive = 0;
$this->set('usuarios', $this->paginate());
}
/**
* view method
*
* @param string $id
* @return void
*/
public function view($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
$this->set('usuario', $this->Usuario->read(null, $id));
}
// /**
// * add method
// *
// * @return void
// */
// public function add() {
// if ($this->request->is('post')) {
// $this->Usuario->create();
// if ($this->Usuario->save($this->request->data)) {
// $this->Session->setFlash(__('The usuario has been saved'));
// $this->redirect(array('action' => 'index'));
// } else {
// $this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
// }
// }
// }
// /**
// * edit method
// *
// * @param string $id
// * @return void
// */
// public function edit($id = null) {
// $this->Usuario->id = $id;
// if (!$this->Usuario->exists()) {
// throw new NotFoundException(__('Invalid usuario'));
// }
// if ($this->request->is('post') || $this->request->is('put')) {
// if ($this->Usuario->save($this->request->data)) {
// $this->Session->setFlash(__('The usuario has been saved'));
// $this->redirect(array('action' => 'index'));
// } else {
// $this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
// }
// } else {
// $this->request->data = $this->Usuario->read(null, $id);
// }
// }
// /**
// * delete method
// *
// * @param string $id
// * @return void
// */
// public function delete($id = null) {
// if (!$this->request->is('post')) {
// throw new MethodNotAllowedException();
// }
// $this->Usuario->id = $id;
// if (!$this->Usuario->exists()) {
// throw new NotFoundException(__('Invalid usuario'));
// }
// if ($this->Usuario->delete()) {
// $this->Session->setFlash(__('Usuario deleted'));
// $this->redirect(array('action' => 'index'));
// }
// $this->Session->setFlash(__('Usuario was not deleted'));
// $this->redirect(array('action' => 'index'));
// }
/**
* painel_index method
*
* @return void
*/
public function painel_index() {
$this->Usuario->recursive = 0;
$this->set('usuarios', $this->paginate());
}
/**
* painel_view method
*
* @param string $id
* @return void
*/
public function painel_view($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
$this->set('usuario', $this->Usuario->read(null, $id));
}
/**
* painel_add method
*
* @return void
*/
public function painel_add() {
if ($this->request->is('post')) {
$this->Usuario->create();
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
}
}
/**
* painel_edit method
*
* @param string $id
* @return void
*/
public function painel_edit($id = null) {
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->Usuario->save($this->request->data)) {
$this->Session->setFlash(__('The usuario has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The usuario could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->Usuario->read(null, $id);
}
}
/**
* painel_delete method
*
* @param string $id
* @return void
*/
public function painel_delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->Usuario->id = $id;
if (!$this->Usuario->exists()) {
throw new NotFoundException(__('Invalid usuario'));
}
if ($this->Usuario->delete()) {
$this->Session->setFlash(__('Usuario deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('Usuario was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment