Skip to content

Instantly share code, notes, and snippets.

@archanavhire
Last active August 29, 2015 14:02
Show Gist options
  • Save archanavhire/1460ebc16a7510d8d589 to your computer and use it in GitHub Desktop.
Save archanavhire/1460ebc16a7510d8d589 to your computer and use it in GitHub Desktop.
validation in cakephp 3.0
<?php
/**
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* For full copyright and license information, please see the LICENSE.txt
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 0.2.9
* @license http://www.opensource.org/licenses/mit-license.php MIT License
*/
namespace App\Controller;
use Cake\Controller\Controller;
use Cake\Controller\Component\Auth;
use Cake\Controller\Component\Auth\BlowfishPasswordHasher;
/**
* 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 {
public $components = [
'Session',
'Auth' => [
'loginRedirect' => [
'controller' => 'articles',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'pages',
'action' => 'display',
'home'
],
/* 'authenticate' => [
'Form' => [
'passwordHasher' => 'Blowfish'
]
] */
]
];
public function beforeFilter() {
$this->Auth->allow(['add', 'logout']);
$this->Auth->config('authenticate', ['Form'=>['username'=>'email','password'=>'password']]);
//$pass = (new BlowfishPasswordHasher)->hash($this->request->data['password']);
//$this->request->data['password'] = $pass;
//pr($pass);exit;
}
}
<?php
//App\Model\Entity\User.php
namespace App\Model\Entity;
use Cake\ORM\Entity;
use Cake\Controller\Component\Auth\BlowfishPasswordHasher;
class User extends Entity {
public function setPassword($password) {
return (new BlowfishPasswordHasher)->hash($password);
}
}
?>
<?php
//App\Controller/UsersController.php
namespace App\Controller;
use App\Controller\AppController;
use Cake\Error\NotFoundException;
class UsersController extends AppController {
public $helpers = ['Form', 'Html'];
public function beforeFilter(Event $event) {
parent::beforeFilter($event);
$this->Auth->allow('add');
$this->Auth->authError = "Login failed. Invalid email or password.";
}
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($this->request->data);
if ($this->request->is('post')) {
if ($this->Users->save($user)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(['action' => 'index']);
}
$this->Session->setFlash(__('Unable to add the user.'));
}
$this->set('user', $user);
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
} else {
$this->Session->setFlash(
__('email or password is incorrect'),
'default',
array(),
'auth'
);
}
}
}
public function logout() {
return $this->redirect($this->Auth->logout());
}
}
?>
<?php
//App\Model\Table\UsersTable.php
namespace App\Model\Table;
use Cake\ORM\Table;
use Cake\Validation\Validator;
$validator = new Validator();
class UsersTable extends Table {
public function initialize(array $config) {
$this->addBehavior('Timestamp');
}
public function validationDefault(Validator $validator) {
return $validator->notEmpty('email', 'A email is required')
->notEmpty('password', 'A password is required')
->notEmpty('role', 'A password is required')
/* ->add('role', [
'rule' => ['inList', ['admin', 'author']],
'message' => 'Please enter a valid role'
]) */;
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment