Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 11th, 2012  |  syntax: None  |  size: 2.58 KB  |  hits: 11  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Hashing passwords and AuthComponent
  2. // Users Model
  3. public function beforeSave ($options = array ()) {
  4.     $this->data['User']['password'] = AuthComponent::password($this->data['User']['password']);
  5.     return true;
  6. }
  7.  
  8. // Users Controller
  9. public $components = array ('Acl', 'Session',
  10.     'Auth' => array (
  11.     'authenticate' => array (
  12.         // login e logout sono di default i seguenti controller e views
  13.         // 'loginRedirect' => array ('controller' => 'users', 'action' => 'login'),
  14.         // 'logoutRedirect' => array ('controller' => 'users', 'action' => 'logout'),
  15.         'Form' => array (
  16.             'fields' => array (
  17.             // il valore default
  18.                 'username' => 'email'
  19.             ),
  20.             'scope' => array (
  21.                 'User.active' => 1
  22.             )
  23.         )
  24.     ),
  25.     'authError' => 'Login error message I get'
  26. ));
  27.  
  28. public function login () {
  29.     if ($this->request->is('post')) { // if the request came from post data and not via http (useful for security)
  30.          // the password is hashed in User Model in beforeSave method as read on documentation
  31.          // debug ($this->data);
  32.          if ($this->Auth->login()) {
  33.              $id = $this->Auth->user('id');
  34.              return $this->redirect(array('controller'=>'users', 'action'=>$id, $this->Auth->user('username')));
  35.          } else {
  36.              $this->Session->setFlash('Login error message', 'default', array(), 'auth');
  37.          }
  38.     }
  39. }
  40.        
  41. // the view login.ctp
  42. echo $this->Form->text('User.email', array('id'=>'email', 'value'=>'your@email.com'));
  43. echo $this->Form->password('User.password', array('id'=>'password', 'value'=>'password'));
  44.        
  45. // in the controller
  46. debug($this->data);
  47. // in the view
  48. Array
  49. (
  50.     [User] => Array
  51.     (
  52.         [email] => the@email.com
  53.         [password] => thepass // not hashed
  54.     )
  55. )
  56.        
  57. echo $this->Layout->sessionFlash();
  58.        
  59. public $components = array(
  60.     'Session',
  61.     'Cookie',
  62.     'Acl',
  63.     /**
  64.      * Default is authorize option is ActionsAuthorize.
  65.      * In this case, system uses AclComponent to check for permissions on an action level.
  66.      * learn more: http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#authorization
  67.      */
  68.     'Auth'=> array(
  69.         'authorize' => array(
  70.             'Actions' => array('actionPath' => 'controllers')
  71.         ),
  72.         'authenticate' => array(
  73.             'Form' => array(
  74.                 'fields' => array('username' => 'email', 'password' => 'password')
  75.             )
  76.         )
  77.     )
  78. );
  79.        
  80. if ($this->request->is('post')) {
  81.         if ($this->Auth->login()) {
  82.             // recirect stuffs