Code Monkey home page Code Monkey logo

acl_utilities's Introduction

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:

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

      <div class="myClass">
      	<?php echo $this->Html->link(__('List Posts', true), array('action' => 'index')); ?>
      </div>
    
      <?php echo $this->Acl->link(__('List Posts', true)
      							,array('action' => 'index')
      							,array('wrapper'=>'<div class="myClass">%s</div>'); ?>
    
  • use of $this->Acl->check()

    	<div>
      	<?php if ($this->Acl->check(array('action' => 'index'))): ?>
      		<div class="myClass">
      			<?php echo $this->Html->link(__('List Posts', true), array('action' => 'index')); ?>
      		</div>
      	<?php endif; ?>
      </div>
    
  • use of blocks (only display a block if a link is successful):

      <div>
      	<?php $this->Acl->startBlock() ?>
      		<div class="myClass">
      			<?php echo $this->Acl->link(__('List Posts', true), array('action' => 'index')); ?>
      		</div>
      	<?php $this->Acl->endBlock() ?>
      </div>
    

Use a different Model

In AppController::beforeFilter() add this:

	Configure::write('AclUtilities.modelName','Group');
	Configure::write('AclUtilities.modelKey','Auth.User.group_id');

acl_utilities's People

Contributors

robmaurer avatar geromey avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.