Custom Role management in cakephp

Cakephp

Posted on 10 Oct 15 by Vickey
  541



By using ACL we can manage roles in cakephp but if you dont know how it will be implement you can use a simple custom code.

$permissions=[];
$permissions['default_permissions']=array(

    'USER'=>array('ADD'=>array(1),'EDIT'=>array(1),'INDEX'=>array(1,2))

);
$controller= strtoupper($this->params['controller']);
$action =strtoupper($this->params['action']);

if( isset($permissions['default_permissions'][$controller][$action]) && !in_array($this->Auth->user('role_id'),$permissions['default_permissions'][$controller][$action]))
return $this->redirect('/');

In this code USER is controller name and 'ADD' and 'EDIT' are actions. Here I have two types of users one is admin role id 1 and other is front end user role id is 2. So here admin can use 'ADD',  'EDIT', and 'index' page but normal use can use only 'INDEX'. Action that not given in 'permissions' array can access every one 


If this post help you reduce time to develop, you can help me to maintain this website :)
www.000webhost.com