2012-05-10 8 views
1

私のプロジェクトでzend ACLを実装しようとしていますが、私は3つの問題に直面しています。 これはコードで問題を説明するには、次のzend aclモジュール式の実装ですか?

マイライブラリのプラグインクラス

class Mylib_Controller_Plugin_AccessCheck extends Zend_Controller_Plugin_Abstract { 

    private $_acl = null; 
    private $_auth = null; 

    public function __construct(Zend_Acl $acl, Zend_Auth $auth) { 
     $this->_acl = $acl; 
     $this->auth = $auth; 
    } 

    public function preDispatch(Zend_Controller_Request_Abstract $request) { 
     $module = $request->getModuleName(); 
     $recourse = $request->getControllerName(); 
     $action = $request->getActionName(); 
      $identity = $this->auth->getStorage()->read(); 
    if (!isset($identity->Role)) { 

      $role = 'default'; 
     } else { 
     $role = $identity->Role; 
     } 

     if (!$this->_acl->isAllowed($role, $module, $recourse)) { 
      $request->setModuleName('Admin') 
        ->setControllerName('User') 
        ->setActionName('index'); 
     } 


    } 

} 

これはこれはbootstarpに_initAppAutoloadあるモデルフォルダ

class Application_Model_DbTable_LibraryAcl extends Zend_Acl { 

    public function __construct() { 
     $this->addRole(new Zend_acl_Role('default')); 
     $this->addRole(new Zend_acl_Role('User')); 
     $this->addRole(new Zend_acl_Role('Admin'), 'User'); 



     $this->add(new Zend_Acl_Resource('Admin')) 
       ->add(new Zend_Acl_Resource('default')) 

     ; 



     $this->allow('Admin') 
       ->deny(array('User', 'default')); 

    } 

} 

の私のACLクラスです

$acl = new Application_Model_DbTable_LibraryAcl(); 

     $auth = Zend_Auth::getInstance(); 


     $fc = Zend_Controller_Front::getInstance(); 

     $fc->setControllerDirectory(array('default' => '/../application/modules/default/controllers', 
      'Admin' => '/../application/modules/Admin/controllers')); 
     $fc->registerPlugin(new Hyderlib_Controller_Plugin_AccessCheck($acl, $auth)); 

1)最初の問題は、どのようにA pplication_Model_DbTable_LibraryAcl私は管理者とデフォルトのフォルダを持つモジュール化された実装を持っていますか、またはどのように各モジュールのリソースツリーを作成できますか?

2)私のデータベースにはデフォルトの役割はありませんが、私はこのデフォルトのユーザーにアカウントを作成せずに前提を設定したいと思っています(そのため、私は役割のIDをチェックし、それはデフォルトになります)。そうすること、あるいは論理的にすることがベストプラクティスですか?

3)モジュールとコントローラだけでなく、アクションの_isAllowedメソッドでMylib_Controller_Plugin_AccessCheckクラスをチェックインするにはどうすればよいですか?また、リダイレクトのこの方法はまた、私のエラーを与えている

は、ここで適切に

答えて

0

をリダイレクトされていない行く、

1)第一の問題は、私はその私 Application_Model_DbTable_LibraryAclに指定することができる方法であります管理フォルダとデフォルトフォルダを使用して モジュラを実装しているか、 モジュールごとにリソースツリーを作成するにはどうすればよいですか?

あなたは既にかなり接近しているが、基本的な実装のために:

class Application_Model_DbTable_LibraryAcl extends Zend_Acl { 

public function __construct() { 
    //add Roles 
    //default role has very limited access 
    $this->addRole(new Zend_acl_Role('default')); 
    //User inherits all default access 
    $this->addRole(new Zend_acl_Role('User'), 'default'); 
    //Admin inherits all User and Default acces 
    $this->addRole(new Zend_acl_Role('Admin'), 'User'); 

    //add resources, caps don't seem to be a problem. 
    //add Admin module resource 
    $this->add(new Zend_Acl_Resource('admin')); 
    //add Admin module Index controller resource, specify admin as parent 
    $this->add(new Zend_Acl_Resource('index'), 'admin'); 
    //add default module access 
    $this->add(new Zend_Acl_Resource('default')); 

    //add access rules 
    //default in Zend_Acl is to deny all 

    //everyone has access to the front page and the error page 
    $this->allow(NULL, 'default', array('index', 'error')); 
    $this->allow(NULL, 'default', array('index', 'index')); 

    //add default user rules 
    //allow default access to login logout 
    $this->allow('default', 'default', array('login', 'logout')); 

    //add crud access for User 
    $this->allow('User', 'default', array('add', 'update')); 

    //admin can do all 
    $this->allow('Admin', NULL); 
    } 

} 

これはおそらく、完璧ではないですが、あなたは何をすべきかのアイデアを与える必要があります。必要に応じて結果をテストすることができます。

2)私は、データベース内のデフォルトの役割を持っていないが、私は アカウントを作成せずに、いくつかのpreviligaesを持っているために、この デフォルトのユーザーを作りたい(私は役割の身元を確認した理由ですし、それならばis no 私はそれをデフォルトに設定します)。そうするベストプラクティスか、 論理ですか?

私のために働く、本当にあなたが誰であるかを知るまで、ユーザーの役割をチェックするのは難しいです。チェックしてください。私は で私Mylib_Controller_Plugin_AccessCheckクラスでアクションの_isAllowed方法過ぎないだけでモジュールと コントローラを確認することができますどのように

3)。?

public function preDispatch(Zend_Controller_Request_Abstract $request) { 
     $module = $request->getModuleName(); 
     $recourse = $request->getControllerName(); 
     $action = $request->getActionName(); 
      $identity = $this->auth->getStorage()->read(); 
    if (!isset($identity->Role)) { 
      $role = 'default'; 
     } else { 
      $role = $identity->Role; 
     } 
     //default role is default, if role is not allowed and not set to default send to error controller. 
     if (!$this->_acl->isAllowed($role, $module, $recourse, $action)) { 
      if ($role == 'default'){ 
       $request->setModuleName('default') 
         ->setControllerName('index') 
         ->setActionName('login'); 
      } else { 
       $request->setModuleName('default') 
         ->setControllerName('error') 
         ->setActionName('noauth'); 
     } 

isAllowed()にアクション名を追加する私のアプリのために働くようだが、テストは非常に広範ベンしていません。したがって、注意して使用してください。私はあなたがまだこれらのコンセプトの周りに頭を抱えようとしているようなものです。

関連する問題