Snippets
Sergio de EOM CakePHP 3.1 Login for custom (Model + Form + Bootstrap + Bootstrap for Spanish in Inflector)
Revised by
Sergio N.
77f10fc
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 | <?php
/**
* CakePHP(tm) Path File: \App\Controller\AppController.php
*/
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Event\Event;
/**
* Application Controller
* Add your application-wide methods in the class below, your controllers
* will inherit them.
* @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller
*/
class AppController extends Controller
{
/**
* Initialization hook method.
* Use this method to add common initialization code like loading components.
* e.g. `$this->loadComponent('Security');`
* @return void
*/
public function initialize()
{
parent::initialize();
$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
// Set Full Auth via Form
$this->loadComponent('Auth', [
'authorize' => ['Controller'],
'loginRedirect' => [
'controller' => 'Pages', // @todo Mi Controller segun PROYECTO a modo de demo
'action' => 'display',
'home'
],
'logoutRedirect' => [
'controller' => 'Usuarios', // @todo Mi Controller segun PROYECTO
'action' => 'login'
],
'loginAction' => [
'controller' => 'Usuarios', // @todo Mi Controller segun PROYECTO
'action' => 'login'
],
'authenticate' => [
'Form' => [
//'passwordHasher' => 'Blowfish',
'userModel' => 'Usuarios', // @todo Mi TABLA segun DB
'fields' => ['username' => 'email', 'password' => 'password'], // @todo mis campos personalizados segun DB
'scope' => ['Usuarios.habilitado' => 1] // @todo Filtro para bloquiar ingresos de usuarios activos segun DB
]
],
'authError' => '¿De verdad crees que se le permita ver eso?',
'storage' => 'Session'
]);
}
/**
* @param $usuario
*
* @return bool
*/
public function isAuthorized($usuario = array())
{
// Tipo de permiso es Admin..?
if (true === isset($usuario['perfiles_id']) && $usuario['perfiles_id'] === '1') { // @todo Codigo a cambiar segun DB y PROYECTO
// Permitir
return true;
} else {
// Denegar
return false;
}
}
/**
* @param Event $event An Event instance
*
* @return void
*/
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
// Bloquiar todo
$this->Auth->deny();
// Es Admin..?
if ($this->Auth->user('perfiles_id') === 1) { // @todo Codigo a cambiar segun DB y PROYECTO
// Permitir todo al Admin
$this->Auth->allow();
} else {
// Es anonimo..?
$this->Auth->allow(['index', 'view', 'display', 'contactarnos', 'registrarce', 'logout']); // @todo Codigo a cambiar segun DB y PROYECTO
}
}
/**
* Before render callback.
*
* @param \Cake\Event\Event $event The beforeRender event.
*
* @return void
*/
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 | <?php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Event\Event;
/**
* Path File: \App\Controller\UsuariosController.php
* Usuarios Controller
*
* @property \App\Model\Table\UsuariosTable $Usuarios
*/
class UsuariosController extends AppController
{
public function logout(){
return $this->redirect($this->Auth->logout());
}
public function login(){
if ($this->request->is('post')) {
// Existe el usuario ..?
$user = $this->Auth->identify();
if ($user != false) {
// Set Storage
$this->Auth->setUser($user);
if ($this->Auth->authenticationProvider()->needsPasswordRehash()) {
$user = $this->Users->get($this->Auth->user('id'));
$user->password = $this->request->data('password');
// Save Usuario
$this->Users->save($user);
}
return $this->redirect($this->Auth->redirectUrl());
}else{
// Set Flash Auth
$this->Flash->error( __('Usuario o Clave es incorrecta'), ['key'=>'auth']);
}
}
}
/**
* Index method
*
* @return void
*/
public function index()
{
$this->paginate = [
'contain' => ['Perfiles']
];
$this->set('usuarios', $this->paginate($this->Usuarios));
$this->set('_serialize', ['usuarios']);
}
/**
* View method
*
* @param string|null $id Usuario id.
* @return void
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function view($id = null)
{
$usuario = $this->Usuarios->get($id, [
'contain' => ['Perfiles']
]);
$this->set('usuario', $usuario);
$this->set('_serialize', ['usuario']);
}
/**
* Add method
*
* @return void Redirects on successful add, renders view otherwise.
*/
public function add()
{
$usuario = $this->Usuarios->newEntity();
if ($this->request->is('post')) {
$usuario = $this->Usuarios->patchEntity($usuario, $this->request->data);
if ($this->Usuarios->save($usuario)) {
$this->Flash->success(__('The usuario has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The usuario could not be saved. Please, try again.'));
}
}
$perfiles = $this->Usuarios->Perfiles->find('list', ['limit' => 200]);
$this->set(compact('usuario', 'perfiles'));
$this->set('_serialize', ['usuario']);
}
/**
* Edit method
*
* @param string|null $id Usuario id.
* @return void Redirects on successful edit, renders view otherwise.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function edit($id = null)
{
$usuario = $this->Usuarios->get($id, [
'contain' => []
]);
if ($this->request->is(['patch', 'post', 'put'])) {
$usuario = $this->Usuarios->patchEntity($usuario, $this->request->data);
if ($this->Usuarios->save($usuario)) {
$this->Flash->success(__('The usuario has been saved.'));
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error(__('The usuario could not be saved. Please, try again.'));
}
}
$perfiles = $this->Usuarios->Perfiles->find('list', ['limit' => 200]);
$this->set(compact('usuario', 'perfiles'));
$this->set('_serialize', ['usuario']);
}
/**
* Delete method
*
* @param string|null $id Usuario id.
* @return void Redirects to index.
* @throws \Cake\Network\Exception\NotFoundException When record not found.
*/
public function delete($id = null)
{
$this->request->allowMethod(['post', 'delete']);
$usuario = $this->Usuarios->get($id);
if ($this->Usuarios->delete($usuario)) {
$this->Flash->success(__('The usuario has been deleted.'));
} else {
$this->Flash->error(__('The usuario could not be deleted. Please, try again.'));
}
return $this->redirect(['action' => 'index']);
}
}
|
You can clone a snippet to your computer for local editing. Learn more.