cakephp2用户密码确认然后保存新密码

时间:2015-02-28 12:20:10

标签: cakephp-2.0

我想确认用户密码,然后保存新密码。我可以通过此代码验证用户名,但是当我验证密码时,它不起作用。 是我缺少的东西或我做错了。我正在使用cakephp2.5这是我的代码......

CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) unsigned NOT NULL AUTO_INCREMENT,
`username` varchar(40) NOT NULL,
`password` varchar(40) NOT NULL,
`name` varchar(100) NOT NULL,
`email` varchar(100) NOT NULL,
`picture` varchar(100) NOT NULL,
`role_id` int(11) NOT NULL,
`created` datetime NOT NULL,
`modified` datetime NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=18 ;

changepassword.ctp(view)

<div class="row">
<div class="col-lg-6">
    <?php echo $this->Form->create('User', array('role'=>'form')); ?>       
    <fieldset>
        <legend><h1>Change Password&nbsp;</h1></legend>
        <?php
            echo $this->Form->input('oldpassword',array('label'=>'Old Password','type'=>'password','class'=>'form-control'));
            echo $this->Form->input('password',array('value'=>'password','label'=>'New Password','class'=>'form-control'));
            echo $this->Form->input('password_confirmation',array('type'=>'password','class'=>'form-control','value'=>'password','label'=>'Confirm new Password'));
            echo "<br>";
        ?>
    </fieldset>
    <?php
        $options = array('label' => 'Submit','class' => 'btn btn-default');
        echo $this->Form->end($options);
    ?>
</div>  

UsersController.php(controller)

 <?php
 App::uses('AppController', 'Controller');

class UsersController extends AppController {
public function changepswrd() {
    if ($this->request->is('post')) {
        $this->User->create();
        $data=$this->request->data['User'];
        $data['id']=AuthComponent::user('id');

        if ($this->User->save($data)) {
            $this->Session->setFlash(__('Password changed sucessfully'));
            return $this->redirect(array('controller' => 'users', 'action' => 'viewList'));
        } else {
            $this->Session->setFlash(__('Error!! please try again'));
        }           
    }
}

}
?>

User.php(Model)

  <?php
 App::uses('AppModel', 'Model');

 class User extends AppModel {

public $displayField = 'name';

public $validate = array(
    'password' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
        ),
        'matchPasswords'=>array(
            'rule'=>array('matchPasswords'),
            'message' => 'Both password must be equal',
        ),
    ),
    'password_confirmation' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
        ),
    ),
    'oldpassword' => array(
        'notEmpty' => array(
            'rule' => array('notEmpty'),
            'message' => 'Please Enter old password',
        ),
        'matching'=>array(
            'rule'=>array('matching'),
            'message' => 'wrong password',
        ),
    ),
);

public function matchPasswords($data){
    if($data['password']==$this->data['User']['password_confirmation']){
        return true;
    }
    $this->invalidate('password_confirmation','Both password must be equal');
    return false;
}

public function matching($data){
    $pswrd=AuthComponent::user('password');
    if($pswrd==$this->data['User']['oldpassword']){
        return true;
    }
    return false;
}

public function beforeSave($options = array()){
    if(isset($this->data['User']['password'])){
        $this->data['User']['password']=AuthComponent::password($this->data['User']['password']);
    }
}   

}

1 个答案:

答案 0 :(得分:0)

我这样做的方式:

function oldPasswordCheck($check){  
        $user = $this->find(
            'count',
            array('conditions' => array(
                'User.id' => $this->session_id,
                'User.password' => md5($this->data['User']['old_password'])
            ))
        );
        if(!$user){
            return false;
        }else {
            return true;
        }
    }

其他帮助完整链接

<强> Click me

希望此信息可以帮助您。