Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

CakePHP plugin. Allows to display only the links that can be accessed with Acl

branch: master

Fetching latest commit…

Octocat-spinner-32-eaf2f5

Cannot retrieve the latest commit at this time

Octocat-spinner-32 views
Octocat-spinner-32 README.md
Octocat-spinner-32 acl_utilities_app_controller.php
Octocat-spinner-32 acl_utilities_app_model.php
README.md

AclUtilities Plugin for CakePHP

AclUtilities permits to translarently display or hide links in view according to Acl rules.

Installation

  • in the views, replace $this->Html->link() by $this->Acl->link()

example:

<?php echo $this->Html->link(__('List Posts', true), array('action' => 'index')); ?>

<?php echo $this->Html->link(__('Edit User', true), array('controller'=>'User','action' => 'edit')); ?>

replaced by:

<?php echo $this->Acl->link(__('List Posts', true), array('action' => 'index')); ?>

<?php echo $this->Acl->link(__('Edit User', true), array('controller'=>'User','action' => 'edit')); ?>

Be sure to use an array format for the URL.

  • in the AppController, add the helper AclUtilities.Acl

    var $helpers = array(/*...,*/ 'AclUtilities.Acl');
    
  • If you are using Auth->allowedActions or Auth->allow()
    Then you have to move them all into AppController::beforeFilter() like the following:

    function beforeFilter() { //[...] // $allowedActions = array([Controller]=>array([action1],[action2]); Configure::write('AclUtilities.allowedActions', $allowedActions = array( 'News' => array('*'), // access to all the actions 'Groups' => array(), // no access to groups 'Pages' => array('display'), 'Posts' => array('index', 'view'), 'Users' => array('login','register'), ));

      // now, we need to allow the action for the current module
      if (isset($allowedActions[$this->name]))
          $this->Auth->allow($allowedActions[$this->name]);
    

    }

And this is it; your links are now only displayed when they can be accessed!

More Examples

  • use of the option wrapper:

    • <?php echo $this->Html->link(__('List Posts', true), array('action' => 'index')); ?>
        • <?php echo $this->Acl->link(__('List Posts', true) ,array('action' => 'index') ,array('wrapper'=>'li'); ?>
      • another use of the wrapper

        Html->link(__('List Posts', true), array('action' => 'index')); ?>

        <?php echo $this->Acl->link(__('List Posts', true) ,array('action' => 'index') ,array('wrapper'=>'

        %s
        '); ?>
      • use of $this->Acl->check()

        Acl->check(array('action' => 'index'))): ?>
        Html->link(__('List Posts', true), array('action' => 'index')); ?>
          <?php endif; ?>
        

      • use of blocks (only display a block if a link is successful):

        Acl->startBlock() ?>
        Acl->link(__('List Posts', true), array('action' => 'index')); ?>
        Acl->endBlock() ?>

      Use a different Model

      In AppController::beforeFilter() add this:

      Configure::write('AclUtilities.modelName','Group');
      Configure::write('AclUtilities.modelKey','Auth.User.group_id');
      
    Something went wrong with that request. Please try again.