我們使用的是Auth組件。我們目前能夠阻止未登錄的用戶訪問我們的「管理員」頁面(adminhome.ctp)。但我們無法弄清楚如何讓isAuthorized()
阻止非管理員訪問該頁面。CakePHP:如何爲特定頁面要求管理員角色?
的AppController的內部:
public function beforeFilter() {
$this->Auth->allow('index', 'view', 'login', 'logout', 'display');
$this->Auth->authorize = array('Controller');
//$this->Auth->autoRedirect = false;
}
public function isAuthorized($user_id) {
$this->loadModel('User');
$user = $this->User->findById($this->Auth->user());
if ($user['User']['role'] === 'admin') {
$this->Session->setFlash('isAuthorized');
return true;
}
$this->Session->setFlash('!isAuthorized');
return false;
}
這裏beforeFilter()在PagesController:
function beforeFilter() {
$this->Auth->deny('adminhome');
}
什麼是我們做錯了什麼?
我們使用管理路由。它在所有控制器上都能很好地工作,但它似乎不適用於頁面,因爲正如您所指出的那樣,'adminhome'不是一種方法。我會嘗試你的版本,但我很困惑它將如何解決我的特定問題,這涉及到由PagesController呈現的特定頁面。換句話說'$ this-> Auth-> deny()'不能引用特定的頁面,可以嗎? – emersonthis 2013-04-26 00:23:59
我想我很困惑你的答案,因爲第一個塊(使用管理員前綴,因爲我們想)似乎並沒有參考網頁。 – emersonthis 2013-04-26 00:26:03
如果您已經使用管理路由,那麼不要考慮答案的第一部分。是的,$ this-> Auth-> deny()可能不能引用特定的頁面,這就是爲什麼你必須在你的情況下使用display()和isAuthorized()方法,就像我在第二部分中指出的那樣。 – vcanales 2013-04-26 00:33:05