2014-10-08 74 views
1

我一直在試圖讓CakePHP ACL與我的新應用程序一起工作,這讓我感到痛苦萬分。由於某些原因ACL似乎沒有工作,但教程是垃圾,並沒有很好地解釋每個組件。例如如何一個ACO鏈接到一個控制器/功能/視圖CakePHP 2.x ACL使其工作的問題

我得到的ACL正常工作,直到獲取頁面,以瞭解用戶是否被允許查看它,也是菜單項,他們可以/不能看到相同的問題。

我注意到,如果我這個代碼添加到我的網頁數組顯示羣組爲空白:

$user = $this->Auth->user(); 
pr($user); 

數組返回:

Array 
(
    [id] => 80 
    [first_name] => Bob 
    [last_name] => Test 
    [email] => [email protected] 
    [username] => TestAdmin 
    [tokenhash] => cleared 
    [is_active] => 1 
    [created] => 2014-10-03 16:32:45 
    [modified] => 2014-10-03 16:32:45 
    [token_expires_at] => 
    [group_id] => 3 
    [Group] => Array 
     (
      [id] => 
      [name] => 
      [enabled] => 
      [created] => 
      [modified] => 
     ) 

) 

該網站基本上是一個門戶網站,訪問者應該只能訪問登錄/註冊。和用戶組都可以訪問儀表板,但它最終在一個連續的循環,除非我允許所有人訪問儀表板(由於該組未被識別,我猜)

任何幫助將不勝感激,我意識到你可能需要我發佈代碼,所以請讓我知道你會需要什麼。

在此先感謝

編輯:

我有如下更新了我的AppController,它已開始顯示陣列中的組,因爲它應該!奇怪的感謝推向正確的方向。

AppController.php

<?php 

App::uses('Controller', 'Controller'); 

class AppController extends Controller { 
    public function beforeRender() { 
     if((($this->params['controller']==='Users') || ($this->params['controller']==='users'))&&(($this->params['action']=='login') || ($this->params['action']=='register') || ($this->params['action']=='success') || ($this->params['action']=='forgot_password') || ($this->params['action']=='reset_password'))){ 
      $this->theme = 'DataHouseLogin'; 
     }else{ 
      $this->theme = 'DataHouse'; 
     } 
     parent::beforeRender(); 
    } 

    public $components = array(
     'Acl', 
     'RequestHandler', 
     'DebugKit.Toolbar' => array('panels' => array('history' => false)), 
     'Session', 
     'Auth' => array(
      'authorize' => array(
       'Actions' => array(
        'actionPath' => 'controllers' 
        ) 
      ), 
      'loginAction' => array(
       'controller' => 'Users', 
       'action' => 'login' 
      ), 
      'loginRedirect' => array(
       'controller' => 'Dashboard', 
       'action' => 'index' 
      ), 
      'logoutRedirect' => array(
       'controller' => 'Users', 
       'action' => 'login' 
      ), 
      'authError' => 'Did you really think you are allowed to see that?', 
      'authenticate' => array(
       'Form' => array(
        'passwordHasher' => 'Blowfish' 
       ) 
      ) 
     ) 
    ); 

    public function beforeFilter() { 
     //$this->Auth->allowedActions = array('display','index','register'); 
     $this->set('user', $this->Auth->user());  
     $this->set('acl', $this->Acl); 
     $this->Auth->authorize = array(
      'Controller', 
      'Actions' => array('actionPath' => 'controllers') 
     ); 


     parent::beforeFilter(); 
    } 
    public function isAuthorized($user) { 
     // Default deny 
     return false; 
    } 
} 
+0

你有沒有配置ACL和Auth組件?讓我看看你的AppController-> beforeFilter()函數。 – 2014-10-08 10:33:28

+0

嗨Grzegorz,我已經添加了beforeFilter。 – 2014-10-08 11:17:32

回答

1

我想你應該嘗試正確配置驗證組件。嘗試把這個代碼在你的AppController:

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

public function beforeFilter() { 
    $this->Auth->authorize = array(
     'Controller', 
     'Actions' => array('actionPath' => 'controllers') 
);  
    $this->Auth->authenticate = array('Form' => array('fields' => array('username' => 'name', 'password' => 'password'))); 
    $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false); 
    $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => false, 'plugin' => false); 
} 

public function isAuthorized($user) { 
    // Default deny 
    return false; 
} 
} 

編輯: 中的usermodel和GroupModel添加充當物業:

public $actsAs = array('Acl' => array('type' => 'requester')); 
中的usermodel設置parentNode功能

public function parentNode() { 
    if (!$this->id && empty($this->data)) { 
     return null; 
    } 
    if (isset($this->data['User']['group_id'])) { 
     $groupId = $this->data['User']['group_id']; 
    } else { 
     $groupId = $this->field('group_id'); 
    } 
    if (!$groupId) { 
     return null; 
    } else { 
     return array('Group' => array('id' => $groupId)); 
    } 
} 
+0

我已更改,現在無法再登錄到應用程序 – 2014-10-08 11:36:37

+0

您應該先正確設置您的ACL,可以使用沒有ACL的Auth組件,但在這種情況下,您必須手動將授權邏輯置於isAuthorised函數中。閱讀[this - auth示例](http://book.cakephp.org/2.0/en/tutorials-and-examples/blog-auth-example/auth.html)文章,它會更清晰地給你 – 2014-10-08 11:44:09

+0

hmm是好吧,我確實遵循了這一點,在我開始實施ACL之前,這一切都工作得很好,然後ACL似乎並不適用。 – 2014-10-08 11:55:51