2012-04-30 93 views
2

我下面http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/part-two.html教程和我有點稍微不同的組(組4和5)4是遊客和5是管理員CakePHP的ACL教程initdb的功能警告

function initDB() { 
10   $group = $this->User->Group; 
11   //Allow admins to everything 
12   $group->id = 5;  
13   $this->Acl->allow($group, 'controllers'); 
14  //^doesnt work 
15  //$this->Acl->allow(array('model' => 'Group', 'foreign_key' => 5), 'controllers'); 
16   
17   
18   $group->id = 4; 
19   $this->Acl->deny($group, 'controllers'); 
20   $this->Acl->allow($group, 'controllers/User/login'); 
21   $this->Acl->allow($group, 'controllers/User/logout'); 
22  /* 
23  $this->Acl->deny(array('model' => 'Group', 'foreign_key' => 4), 'controllers'); 
24  $this->Acl->allow(array('model' => 'Group', 'foreign_key' => 4), 'controllers/User/login'); 
25  $this->Acl->allow(array('model' => 'Group', 'foreign_key' => 4), 'controllers/User/logout'); 
26 */  
27   
28   
29   echo "all done"; 
30   exit(); 
31  } 

但是當我運行這個功能,我得到以下錯誤

Warning (512): DbAcl::allow() - Invalid node [CORE/Cake/Controller/Component/AclComponent.php, line 387] 
Warning (512): DbAcl::allow() - Invalid node [CORE/Cake/Controller/Component/AclComponent.php, line 387]all done 

什麼給??

編輯 - 我的應用程序控制器看起來像這樣

<?php 

class AppController extends Controller { 
    public $components = array(
      'Acl', 
      'Auth' => array(
       'authorize' => array(
        'Actions' => array('actionPath' => 'controllers') 
        ) 
       ), 
      'Session' 
      ); 
    public $helpers = array('Html', 'Form', 'Session'); 

    public function beforeFilter() { 
     $this->Auth->actionPath = 'controllers/'; 
     //Configure AuthComponent 
     $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login'); 
     $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login'); 
     $this->Auth->loginRedirect = array('controller' => 'images', 'action' => 'index'); 
     $this->Auth->allow('display'); 
    } 
} 


?> 

編輯2: 已滿警告看起來像這樣

Warning (512): DbAcl::allow() - Invalid node [CORE/Cake/Controller/Component/AclComponent.php, line 387] 
Code Context 
DbAcl::allow() - CORE/Cake/Controller/Component/AclComponent.php, line 387 
AclComponent::allow() - CORE/Cake/Controller/Component/AclComponent.php, line 128 
UsersController::initDB() - APP/Controller/UsersController.php, line 20 
ReflectionMethod::invokeArgs() - [internal], line ?? 
Controller::invokeAction() - CORE/Cake/Controller/Controller.php, line 473 
Dispatcher::_invoke() - CORE/Cake/Routing/Dispatcher.php, line 107 
Dispatcher::dispatch() - CORE/Cake/Routing/Dispatcher.php, line 89 
[main] - APP/webroot/index.php, line 96 

讓我知道如果你要我張貼任何其他代碼。

+0

你安裝了'AclExtras'插件和運行'./Console/cake AclExtras.AclExtras aco_sync'命令? – Saanch

+0

是的,我做到了! – CodeCrack

+0

我已將代碼更新爲我的實際代碼。 – CodeCrack

回答

4

按照警告,這樣的事實:你打電話3次$this->Acl->allow()一旦$this->Acl->deny(),但似乎只得到2警告,我認爲這給予警告線路如下:

$this->Acl->allow($group, 'controllers/User/login'); 
$this->Acl->allow($group, 'controllers/User/logout'); 

我不使用AclExtras將acos表與現有操作同步,但AFAIK在檢查權限時AclComponent要求將acos表中的控制器別名進行復用。所以我假設你的acos表不包含任何帶有別名User的記錄,而是包含別名Users的記錄。

這可以解釋爲什麼您的acos表中沒有任何節點可以通過'controllers/User/login''controllers/User/logout'來識別。

如果我的假設是正確的,下面的代碼將工作:

$this->Acl->allow($group, 'controllers/Users/login'); 
$this->Acl->allow($group, 'controllers/Users/logout'); 
+0

你是對的!爲什麼它會使用用戶而不是用戶? – CodeCrack

+0

這些路徑導致控制器中的操作。在Cake中,控制器的名稱由多元化的模型名稱(用戶模型'UsersController')組成。我想這裏遵循相同的邏輯。謝謝你接受我的答案的方式:-) – nIcO