2013-02-08 46 views
0

我試圖執行審計追蹤插件 - https://github.com/robwilkerson/CakePHP-Audit-Log-PluginCakePHP的審計日誌插件用戶認證

這一切的偉大工程,但我不能讓用戶認證通過下面我得到以下錯誤的說明工作 -

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

我已經加入

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

並添加

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

我的appController,有沒有人實現過這個或識別錯誤?

+0

CakeErrorController可能不會擴展AppController,因此沒有此方法。 – mark 2013-02-08 15:11:09

+0

我的CakeErrorController包含'App :: uses('AppController','Controller');',還有其他想法嗎? – user195257 2013-02-08 15:15:04

回答

0

不要將currentUser()函數添加到您的AppController,它必須在您的AppModel中。下面是我的currentUser()函數看起來像使用CakePHP 2.3:

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

對於CakePHP的2.4,你必須做一些改變,以便與驗證組件的工作:

在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']; 
} 

現在在你的AppController中: 真的是你不需要在你的控制器中做任何事情,它只是爲了防止一些問題。所以,可選

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; 
    } 

這對我的作品。

+0

剛剛'public function currentUser(){return AuthComponent :: user();而不是。正如已經建議的那樣,控制器實際上並不需要任何東西 – Fr0zenFyr 2015-09-09 11:56:50