PHP code example of alessandrominoccheri / userpermissions

1. Go to this page and download the library: Download alessandrominoccheri/userpermissions library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

alessandrominoccheri / userpermissions example snippets


public $components = array(
    'UserPermissions.UserPermissions'
);

$rules = array(
	'user_type' => $user_type,
	'redirect' => '/projects/',
	'message' => 'You do not have permission to access this page',
	'action' =>  $this->request->params['action'],
	'controller' =>  $this,
	'groups' => array(
		'guest' => array('register', 'logout', 'login'),
		'admin' => array('*'), 
		'admin-team' => array('register', 'add', 'logout', 'index', 'edit'),
		'user' => array('register', 'add', 'logout', 'index')
	),
	'views' => array(
		'edit' => 'checkEdit',
		'delete' => 'checkDelete',
	),
);

$this->UserPermissions->allow($rules);

public function beforeFilter () {
	parent::beforeFilter(); 
	//default user_type if not logged
	$user_type = 'guest';
        
    //if you have stored field group inside session
	if($this->Session->read('is_logged')){
		$auth_user = $this->Auth->user();
		$user_type = $auth_user['group'];
	}
        
    //pass user type to the plugin
	$rules = array(
		'user_type' => $user_type,
		'redirect' => '/projects/',
		'message' => 'No permission',
		'action' =>  $this->request->params['action'],
		'controller' =>  $this,
		'groups' => array(
			'guest' => array('register', 'logout', 'login'),
			'admin' => array('*'), 
			'admin-team' => array('register', 'add', 'logout', 'index', 'edit'),
			'user' => array('register', 'add', 'logout', 'index')
		),
		'views' => array(
			'edit' => 'checkEdit',
			'delete' => 'checkDelete',
		),
	);

	$this->UserPermissions->allow($rules);
}

$user_type = $this->getUsernameOfuserLogged();
$rules = array(
	'user_type' => $user_type,
	'redirect' => '/projects/',
	'message' => 'No permission',
	'action' =>  $this->request->params['action'],
	'controller' =>  $this,
	'groups' => array(
		'guest' => array('register', 'logout', 'login'),
		'user1' => array('*'), 
		'user2' => array('register', 'add', 'logout', 'index', 'edit'),
		'user3' => array('register', 'add', 'logout', 'index')
	),
	'views' => array(
		'edit' => 'checkEdit',
		'delete' => 'checkDelete',
	),
);

$user_type = $this->getUsernameOfuserLogged();
$rules = array(
	'user_type' 	=> $user_type,
	'message' 		=> 'No permission',
	'action' 		=>  $this->request->params['action'],
	'controller' 	=>  $this,
	'groups' 		=> array(
		'guest' => array('register', 'logout', 'login'),
		'user1' => array('*'), 
		'user2' => array('register', 'add', 'logout', 'index', 'edit'),
		'user3' => array('register', 'add', 'logout', 'index')
	),
	'views' 		=> array(
		'edit' 		=> 'checkEdit',
		'delete' 	=> 'checkDelete',
	),
);

'action' =>  $this->request->params['action'],

'controller' =>  $this->request->params['controller'],

'groups' => array(
	'guest' => array('register', 'login'),
	'admin' => array('*'), 
	'admin-team' => array('register', 'add', 'logout', 'index', 'edit'),
	'user' => array('register', 'add', 'logout', 'index')
),

'views' => array(
	'edit' 		=> 'checkEdit',
	'delete' 	=> 'checkDelete',
),

public function beforeFilter () {
		parent::beforeFilter(); 
        
        //default user_type if not logged
		$user_type = 'guest';
        
        //if you have stored filed group inside session
		if($this->Session->read('is_logged')){
			$auth_user = $this->Auth->user();
			$user_type = $auth_user['group'];
		}
        
        //pass user type to the plugin
		$rules = array(
			'user_type' => $user_type,
			'redirect' => '/projects/',
			'message' => 'No permission',
			'action' =>  $this->request->params['action'],
			'controller' =>  $this,
			'groups' => array(
				'guest' => array('register', 'logout', 'login'),
				'admin' => array('*'), 
				'admin-team' => array('register', 'add', 'logout', 'index', 'edit'),
				'user' => array('register', 'add', 'logout', 'index')
			),
			'views' => array(
				'edit' => 'checkEdit',
				'delete' => 'checkDelete',
			),
		);

		$this->UserPermissions->allow($rules);
	}
	
	public function checkEdit(){
		$auth_user = $this->Auth->user();
		$user_id = $auth_user['id'];
		echo($user_id.' - '.$_GET['id']);
		if($user_id == $_GET['id']){
			return true;
		}	
		else{
			return false;
		}
	}

	public function checkDelete(){
		if($this->Session->read('id') == $_GET['id']){
			return true;
		}	
		else{
			return false;
		}
	}