2016-05-23 46 views
0

我想拒絕訪問我的網站上的私人區域。但我不知道我做錯了什麼。拒絕訪問私人區域Phalcon PHP ACL

我不想使用Acl::DENY爲默認規則。 相反,我使用Acl::ALLOW作爲全局規則並拒絕訪問私有資源。

這裏是我的代碼:

<?php 
use Phalcon\Acl; 
use Phalcon\Acl\Role; 
use Phalcon\Acl\Resource; 
use Phalcon\Events\Event; 
use Phalcon\Mvc\User\Plugin; 
use Phalcon\Mvc\Dispatcher; 
use Phalcon\Acl\Adapter\Memory as AclList; 


class SecurityPlugin extends Plugin { 

    public function getAcl() { 
     if (!isset($this->persistent->acl)) { 

      $acl = new AclList(); 
      $acl->setDefaultAction(Acl::ALLOW); 

      $roles = array(
       'admin' => new Role('Administrators'), 
       'guests' => new Role('Guests') 
      ); 
      foreach ($roles as $role) { 
       $acl->addRole($role); 
      } 

      //Private area resources 
      $privateResources = array(
       'admin'  => array('index'), 
       'products'  => array('index', 'search', 'new'); 

      foreach ($privateResources as $resource => $actions) { 
       $acl->addResource(new Resource($resource), $actions); 
      } 

      foreach ($privateResources as $resource => $actions) { 
       foreach ($actions as $action) { 
        $acl->deny('Guests', $resource, $action); 
       } 
      } 

     } 

     return $this->persistent->acl; 
    } 


    public function beforeDispatch(Event $event, Dispatcher $dispatcher) { 

     $auth = $this->session->get('auth'); 
     if (!$auth) { 
      $role = 'Guests'; 
     } else { 
      $role = 'Admin'; 
     } 

     $controller = $dispatcher->getControllerName(); 
     $action = $dispatcher->getActionName(); 

     $acl = $this->getAcl(); 

     $allowed = $acl->isAllowed($role, $controller, $action); 
     if ($allowed != Acl::ALLOW) { 
      $dispatcher->forward(array(
       'controller' => 'errors', 
       'action'  => 'show401' 
      )); 
      $this->session->destroy(); 
      return false; 
     } 
    } 
} 

謝謝你,試圖幫助我。

回答

0

你忘了其實你指定的ACL定義到$this->persistent->acl

public function getAcl() { 
    if (!isset($this->persistent->acl)) { 

     $acl = new AclList(); 

     ... 

     //The acl is stored in session 
     $this->persistent->acl = $acl; 
    } 

    return $this->persistent->acl; 
} 

通過看你的代碼,我猜你使用這個SecurityPlugin的爾康INVO例子嗎? 如果是這樣,請參閱line 88。如果沒有,this是一個很好的例子,可以幫助你。

+0

哦,男人,謝謝你,我很盲目^^,thx爲你的例證現在我明白我的錯誤... – Falundrim