Last active
August 29, 2015 14:00
-
-
Save swthate/11178754 to your computer and use it in GitHub Desktop.
User auth.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- File: app/View/Users/add.ctp --> | |
<h1>Add User</h1> | |
<div class="users form"> | |
<?php echo $this->Form->create('user'); ?> | |
<fieldset> | |
<legend><?php echo __('Add User'); ?></legend> | |
<?php | |
echo $this->Form->input('username'); | |
echo $this->Form->input('password'); | |
?> | |
</fieldset> | |
<?php echo $this->Form->end(__('Submit')); ?> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// app/Model/User.php | |
App::uses('AppModel', 'Model'); | |
App::uses('SimplePasswordHasher', 'Controller/Component/Auth'); | |
class User extends AppModel | |
{ | |
public $validate = array( | |
'username' => array( | |
'required' => array( | |
'rule' => array('notEmpty'), | |
'message' => 'A username is required.' | |
) | |
), | |
'password' => array( | |
'required' => array( | |
'rule' => array('notEmpty'), | |
'message' => 'A password is required.' | |
) | |
) | |
); | |
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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// File: app/Controller/UsersConterller.php | |
class UsersController extends AppController | |
{ | |
# ----------------------------------------- # | |
# - BEFORE FILTER ------------------------- # | |
# ----------------------------------------- # | |
public function beforeFilter() | |
{ | |
parent::beforeFilter(); | |
// Allow users to register and logout. | |
$this->Auth->allow('add', 'logout'); | |
} | |
# ----------------------------------------- # | |
# - LOGIN action -------------------------- # | |
# ----------------------------------------- # | |
public function login() | |
{ | |
if ($this->request->is('post')) | |
{ | |
if ($this->Auth->login()) | |
{ | |
return $this->redirect($this->Auth->redirect()); | |
} | |
$this->Session->setFlash(__('Invalid username or password, try again')); | |
} | |
} | |
# ----------------------------------------- # | |
# - LOGOUT action ------------------------- # | |
# ----------------------------------------- # | |
public function logout() | |
{ | |
return $this->redirect($this->Auth->logout()); | |
} | |
# ----------------------------------------- # | |
# - INDEX action -------------------------- # | |
# ----------------------------------------- # | |
public function index() | |
{ | |
$this->User->recursive = 0; | |
$this->set('users', $this->paginate()); | |
} | |
# ----------------------------------------- # | |
# - VIEW action --------------------------- # | |
# ----------------------------------------- # | |
public function view($id = null) | |
{ | |
$thi->User->id = $id; | |
if (!$this->User->exists()) | |
{ | |
throw new NotFoundException(__('Invalid User')); | |
} | |
$this->set('user', $this->User->read(null, $id)); | |
} | |
# ----------------------------------------- # | |
# - ADD action ---------------------------- # | |
# ----------------------------------------- # | |
public function add() | |
{ | |
if ($this->request->is('post')) | |
{ | |
$this->User->create(); | |
if ($this->User->save($this->request->data)) | |
{ | |
$this->Session->setFlash(__('The user has been saved')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('The user could not be saved. Please, try again.')); | |
} | |
} | |
# ----------------------------------------- # | |
# - EDIT action --------------------------- # | |
# ----------------------------------------- # | |
public function edit($id = null) | |
{ | |
$this->User->id = $id; | |
if (!$this->User->exists()) | |
{ | |
throw new NotFoundException(__('Invalid user')); | |
} | |
if ($this->request->is('post') || $this->request->is('put')) | |
{ | |
if ($this->User->save($this->request->data)) | |
{ | |
$this->Session->setFlash(__('The user has been saved')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('The user could not be saved. Please, try again.')); | |
} else { | |
$this->request->data = $this->User->read(null, $id); | |
unset($this->request->data['User']['password']); | |
} | |
} | |
# ----------------------------------------- # | |
# - DELETE action ------------------------- # | |
# ----------------------------------------- # | |
public function delete($id = null) | |
{ | |
$this->request->onlyAllow('post'); | |
$this->User->id = $id; | |
if (!$this->User->exists()) | |
{ | |
throw new NotFoundException(__('Invalid user')); | |
} | |
if ($this->User->delete()) | |
{ | |
$this->Session->setFlash(__('User deleted')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
$this->Session->setFlash(__('User was not deleted.')); | |
return $this->redirect(array('action' => 'index')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment