user195257
user195257

Reputation: 3316

CakePHP Audit Log Plugin user authentication

I'm trying to implement the Audit Trail plugin - https://github.com/robwilkerson/CakePHP-Audit-Log-Plugin

It all works great, however i can't get the user authentication working by following the instructions i get the following error -

Fatal error: Call to undefined method CakeErrorController::currentUser()

I have followed the instructions by adding

protected function currentUser() {
      $user = $this->Auth->user();
      return $user[$this->Auth->userModel]; # Return the complete user array
}

and adding

public function beforeFilter() {
        ...
        if( !empty( $this->data ) && empty( $this->data[$this->Auth->userModel] ) ) {
          $this->data[$this->Auth->userModel] = $this->currentUser();
        }
   }

to my appController, has anyone implemented this before or recognise the error?

Upvotes: 0

Views: 1640

Answers (2)

sosoriov
sosoriov

Reputation: 11

For Cakephp 2.4 you have to do some changes in order to work with the Auth component:

In the AppModel:

public function currentUser() {
  $userId = AuthComponent::user('id');

  //Get the information of the user
  $currentUser = $this->importModel('User')->find('first', array(
      'conditions'=>array('User.id'=>$userId),
  ));

  //Return all the User
  return $currentUser['User'];
}

And now in your AppController: The true is you don't need to do anything else in your controller, it's just to prevent some problem. So, OPTIONAL:

  if( !empty( $this->request->data ) && empty( $this->request->data[$this->Auth->userModel] ) ) {
        $user['User']['id'] = $this->Auth->user('id');
        $this->request->data[$this->Auth->userModel] =  $user;
    }

It works for me.

Upvotes: 1

Hoff
Hoff

Reputation: 1772

Don't add the currentUser() function to your AppController, it has to be in your AppModel. Here's what my currentUser() function looks like using CakePHP 2.3:

public function currentUser() {
    return array('id' => AuthComponent::user('id'));
}

Upvotes: 0

Related Questions