Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get $this->Auth->user('id') in user model cakephp [duplicate]

I want a working on a changepassword module. I want to check the user current password from database if it is same as entered by user. I have defined a $validate array as follows.

`'old_password' => array(
                'minLength' => array(
                    'rule' => array('minLength', 1),
                    'message' => 'Current Password is  required'
                ),
                'oldPass' => array(
                    'rule' => array('oldPass',array('old_password')),
                    'message' => 'Current Password is  invalid'
                )
            ),`

and have defined a method to check the current password

`function oldPass($data){
        if(!empty($data['old_password'])) {
            $valid = false;
            $userData = $this->Auth->user(); // here I am getting error 
            $oldPass = Security::hash(Configure::read('Security.salt') . $data['old_password']);
            if ($userData['User']['password'] == $oldPass) {
                $valid = true;
            }
            return $valid;
        } else {
            return false;
        }

    }`

Here is the error I am getting Undefined property: User::$Auth [APP\models\user.php, line 194] Fatal error: Call to a member function user() on a non-object in

Basically All I want to match the current password with the password entered by user

I am using $this->User->validates() to validate. and to get error messages I am using $this->set('validationErrorsArray', $this->User->invalidFields());

Please help me how can I get the current logged in user id in model.

like image 859
deep Avatar asked May 19 '26 23:05

deep


1 Answers

There is a dry approach:

$uid = CakeSession::read("Auth.User.id");

Note that this is only for CakePHP 2.x - and still shouldn't be done. Usually you pass the session data you need into the model layer explicitly .

In 3.x you cannot statically access the session anymore, there you definitely need to pass the data into the model layer.

like image 108
mark Avatar answered May 21 '26 14:05

mark



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!