Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I use a modified version of what's in the "Zend Framework in Action" book from Manning Press (available as PDF download if you need it now). I think you can just download the accompanying code from the book's site. You want to look at the Chapter 7 code.</p> <p>Overview:</p> <p>The controller is the resource, and the action is the privilege. Put your allows &amp; denys in the controller's init method. I'm also using a customized version of their Controller_Action_Helper_Acl.</p> <p>Every controller has a public static getAcls method:</p> <pre><code>public static function getAcls($actionName) { $acls = array(); $acls['roles'] = array('guest'); $acls['privileges'] = array('index','list','view'); return $acls; } </code></pre> <p>This lets other controllers ask about this controller's permissions. Every controller init method calls $this->_initAcls(), which is defined in my own base controller:</p> <pre><code>public function init() { parent::init(); // sets up ACLs } </code></pre> <p>The parent looks like this:</p> <pre><code>public function init() { $this-&gt;_initAcls(); // init access control lists. } protected function _initAcls() { $to_call = array(get_class($this), 'getAcls'); $acls = call_user_func($to_call, $this-&gt;getRequest()-&gt;getActionName()); // i.e. PageController::getAcls($this-&gt;getRequest()-&gt;getActionName()); if(isset($acls['roles']) &amp;&amp; is_array($acls['roles'])) { if(count($acls['roles'])==0) { $acls['roles'] = null; } if(count($acls['privileges'])==0){ $acls['privileges'] = null; } $this-&gt;_helper-&gt;acl-&gt;allow($acls['roles'], $acls['privileges']); } } </code></pre> <p>Then I just have a function called: </p> <pre><code>aclink($link_text, $link_url, $module, $resource, $privilege); </code></pre> <p>It calls {$resource}Controller::getAcls() and does permission checks against them. If they have permission, it returns the link, otherwise it returns ''.</p> <pre><code>function aclink($link_text, $link_url, $module, $resource, $privilege) { $auth = Zend_Auth::getInstance(); $acl = new Acl(); //wrapper for Zend_Acl if(!$acl-&gt;has($resource)) { $acl-&gt;add(new Zend_Acl_Resource($resource)); } require_once ROOT.'/application/'.$module.'/controllers/'.ucwords($resource).'Controller.php'; $to_call = array(ucwords($resource).'Controller', 'getAcls'); $acls = call_user_func($to_call, $privilege); if(isset($acls['roles']) &amp;&amp; is_array($acls['roles'])) { if(count($acls['roles'])==0) { $acls['roles'] = null; } if(count($acls['privileges'])==0){ $acls['privileges'] = null; } $acl-&gt;allow($acls['roles'], $resource, $acls['privileges']); } $result = $acl-&gt;isAllowed($auth, $resource, $privilege); if($result) { return '&lt;a href="'.$link_url.'" class="aclink"&gt;'.$link_text.'&lt;/a&gt;'; } else { return ''; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload