Rss Feed
  1. Cakephp Working with password

    Saturday, 13 October 2012

    To hash password in model

    Security::hash($password, 'sha1', true) ;
    AuthComponent::password($password) ;

     To check password with confirm password and to reset password

    public $validate = array(
        'name' => array(
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.',
                    'last' => true,
                ),
                'validName' => array(
                    'rule' => '_validName',
                    'message' => 'This field must be alphanumeric',
                    'last' => true,
                ),
            ),
            'username' => array(
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'The username has already been taken.',
                    'last' => true,
                ),
                'notEmpty' => array(
                    'rule' => 'notEmpty',
                    'message' => 'This field cannot be left blank.',
                    'last' => true,
                ),
                'validAlias' => array(
                    'rule' => '_validAlias',
                    'message' => 'This field must be alphanumeric',
                    'last' => true,
                ),
            ),
           
            'pwd' => array(                                                                           // password field
                'rule' => array('minLength', 6),
                'message' => 'Passwords must be at least 6 characters long.',
            ),
            'password2'  => array('rule' =>'checkpasswords','message' => 'Passwords dont match'), //confirm password field
            'oldpwd'  => array('rule' =>'checkoldnewpasswords','message' => 'Old and New Passwords dont match'), // oldpassword that to be changed
            'email' => array(
                'email' => array(
                    'rule' => 'email',
                    'message' => 'Please provide a valid email address.',
                    'last' => true,
                ),
                'isUnique' => array(
                    'rule' => 'isUnique',
                    'message' => 'Email address already in use.',
                    'last' => true,
                ),
            ),
               
           
           
        );
       

        function checkpasswords()     // to check pasword and confirm password
        {  //print_r($this->data['User']['id']);
        if(strcmp($this->data['User']['pwd'],$this->data['User']['password2']) == 0 )
        {
            return true;
        }
        return false;
        }
       
        function checkoldnewpasswords()   // to check password and old password
        {
          
            $this->id = $this->data['User']['id'];
            $user_data = $this->field('password');      
            print_r(Security::hash($this->data['User']['oldpwd'], 'sha1', true));
           if ($user_data == (Security::hash($this->data['User']['oldpwd'], 'sha1', true)))
           {
         return true;
         }else{
         return false;
         }
            }
             


    To assign password to correct database field name
    Here using   pwd i getting password in view so assigning this to database password field.

    function beforeSave() {
        parent::beforeSave();
        //print_r(($this->data['User']['newPassword']));
        //if (isset($this->data['User']['pwd']) && !empty($this->data['User']['pwd'])){
           // $this->data['User']['password'] = Security::hash($this->data['User']['pwd'], 'sha1', true);
          
            if (!empty($this->data[$this->alias]['pwd'])) {                 // hashing password with sha1 and cake salt
            $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['pwd']);
       
            }
        return true;
        }




    In view

    <?php echo $this->Form->input('oldpwd', array('type' => 'password','label' => '', 'class' => 'my-text-field-gen-11')); ?>

    <?php echo $this->Form->input('pwd', array('type' => 'password','label' => '', 'class' => 'my-text-field-gen-11')); ?>

    <?php echo $this->Form->input('password2', array('type' => 'password', 'label' => '','class' => 'my-text-field-gen-11')); ?> 


    Refer://http://www.dereuromark.de/2011/08/25/working-with-passwords-in-cakephp/


  2. 0 comments:

    Post a Comment