Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Filters seem to have done the trick.</p> <p>Assuming you've got a base controller defined in protected/components/Controller.php, add the following function to it to define a filter. If for some reason you don't have a base Controller class, define the filter in a separate class, as described in <a href="http://www.yiiframework.com/doc/guide/1.1/en/basics.controller#filter" rel="nofollow">the Controller documentation</a>. It might be better (more reusable) to do it that way anyway.</p> <pre><code>public function filterAccessControl($filterChain) { $controller = Yii::app()-&gt;controller-&gt;id; $action = Yii::app()-&gt;controller-&gt;action-&gt;id; //The RBAC admin module I'm using creates entries for operations as, e.g. Post:Create // You may need to change this to match whatever entry format you have in your AuthItem table $operation = ucfirst($controller) . ':' . ucfirst($action); Yii::log('Checking auth for user: ' . Yii::app()-&gt;user-&gt;id. ' to operation: ' . $operation, 'info'); if(Yii::app()-&gt;user-&gt;checkAccess($operation)) { Yii::log('User authorised', 'info'); $filterChain-&gt;run(); return true; } else { Yii::log('Unauthorised user!!!!!', 'info'); throw new CHttpException(401, 'You are not authorized to perform this action.'); return false; } } </code></pre> <p>(I'm using <a href="http://www.yiiframework.com/extension/rbam/" rel="nofollow">RBAM</a> to administer the Auth tables in the DB, which stores the operation name as ControllerName:ActionName, hence the ucfirst).</p> <p>To run this filter for every action in every controller, apply the filter by adding a <em>filters()</em> function, again in your Controller.php: </p> <pre><code>public function filters() { return array( 'accessControl', ); } </code></pre> <p>Some caveats: </p> <ul> <li>if you have existing filters in any of your controllers, you'll need to apply this filter there instead, since that controller <em>filters()</em> function will override this one</li> <li>It might be safer to do it in each controller individually anyway, since if necessary you can then limit what actions the filter applies to if for some reason you want some exceptions. Plus if you only do it here, then forget and later add other filters to some controller, you'll lose your access control for those!</li> <li><strong>Before doing this</strong>, ensure you've configured your default guest role in your <em>authManager</em> config, and that role has access to /site/login. Otherwise you won't be able to log in! (If this happens, just disable the filter until you've sorted it out)</li> </ul>
    singulars
    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.
    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