Skip to content

Instantly share code, notes, and snippets.

@julienhay
Created May 7, 2014 13:46
Show Gist options
  • Select an option

  • Save julienhay/91f320d1f774400180b4 to your computer and use it in GitHub Desktop.

Select an option

Save julienhay/91f320d1f774400180b4 to your computer and use it in GitHub Desktop.
Auth Init Cakephp
class AppController extends Controller {
public $components = array(
'Auth',
'Session',
);
<?php
$this->assign('description', 'Log in');
?>
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User', array('class' => 'form-horizontal'));?>
<div class="form-group">
<label for="UserUsername" class="col-sm-2 control-label"><?php echo __('Username'); ?></label>
<?php echo $this->Form->input('username', array(
'label' => false,
'class' => 'form-control',
'div' => array('class' => 'col-sm-10')
)); ?>
</div>
<div class="form-group">
<label for="UserPassword" class="col-sm-2 control-label"><?php echo __('Password'); ?></label>
<?php echo $this->Form->input('password', array(
'label' => false,
'class' => 'form-control',
'div' => array('class' => 'col-sm-10')
)); ?>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<?= $this->Form->submit(__('Log in'), array('class' => 'btn btn-success')); ?>
</div>
</div>
<?= $this->Form->end();?>
<?php
App::uses('SimplePasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
public $name = 'User';
public $validate = array(
'username' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Un nom d\'user est requis'
),
'unique' => array(
'rule' => array('isUnique'),
'message' => 'Nom d\'utilisateur déjà pris'
)
),
'password' => array(
'required' => array(
'rule' => array('notEmpty'),
'message' => 'Un mot de passe est requis',
'on' => 'create'
)
),
'role' => array(
'valid' => array(
'rule' => array('inList', array('admin', 'utilisateur')),
'message' => 'Merci de rentrer un rôle valide',
'allowEmpty' => false
)
)
);
public function beforeSave($options = array()) {
if (isset($this->data[$this->alias]['password'])) {
$passwordHasher = new SimplePasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this->alias]['password']);
}
return true;
}
}
CREATE TABLE IF NOT EXISTS `users` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(50) DEFAULT NULL,
`password` varchar(50) DEFAULT NULL,
`created` datetime DEFAULT NULL,
`modified` datetime DEFAULT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 ;
<?php
class UsersController extends AppController {
public function isAuthorized($user) {
if (isset($user['role'])) {
return true;
}
return parent::isAuthorized($user);
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('logout', 'login');
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
$this->Session->setFlash(__('Successfully logged in'),'default',array('class' => 'alert alert-success'));
return $this->redirect($this->Auth->redirect());
} else {
$this->Session->setFlash(__("Bad username or password, please try again."),'default',array('class' => 'alert alert-danger'));
}
}
}
public function logout() {
$this->Session->setFlash(__('Successfully logged out'),'default',array('class' => 'alert alert-success'));
return $this->redirect($this->Auth->logout());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment