Creating Custom User Role Management in Codeigniter (Part-1)

No votes yet.
Please wait...

Many of us have pondered over how to develop a custom User Role Management in Codeigniter. Well let me tell you there are a lot of stuff available over the internet but identifying the correct working code is simply a tough task. So today PHP Hurdles has taken the initiative to educate our fellow developers with self-explanatory custom working code which will help you to deal with the user management concepts which you can use in any of your CRM’s which is build over CI.

In this part, we will be going to simplify the equation by using FUNCTION BASED Access Control. With help of 3 MAGIC CONSTANTS functions namely __CLASS__, __FUCTION__ & __FILE__ .

So let us start

Lets us perform the operations in steps:

1. Agenda: To Save all the class name and their functions into the database without any replications.
For performing the first step we need to create 2 tables in our database for storing the controller class name & for the function name.
a. Create Controller table.

CREATE TABLE IF NOT EXISTS `controller_table` (
`controller_id` int(11) NOT NULL,
`controller_name` varchar(255) NOT NULL,
`controller_alias_name` varchar(255) NOT NULL,
`controller_satus` int(2) NOT NULL,
`added_by` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

b. Create Function table.

CREATE TABLE IF NOT EXISTS `functions_store` (
`functions_ID` int(11) NOT NULL AUTO_INCREMENT,
`class_name` int(11) NOT NULL,
`functions_name` varchar(255) NOT NULL,
`function_alias_name` varchar(255) NOT NULL,
`functions_status` varchar(10) NOT NULL,
PRIMARY KEY (`functions_ID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=90 ;

2. Agenda: Extract the controller and its functions name and add it to database without any Controller replication i.e one of the 2 controllers can’t have same name.

We can perform this by using __CLASS__, __FUNCTION__ & __FILE__ MAGIC CONSTANTS

Let us start

<?php  defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

include_once(dirname(__FILE__).”/Access_control.php”);

  /*Include above code in all controller’s where you want to set the permission*/

*/ Including Access_control.php controller file where actual insert operations will happen */

***************************************************

(To be included in every function at the top)

$var_controller = __CLASS__;    (Get controller class name)

$function_name =  __FUNCTION__;     (Get  the functions name of the controller)

$this->set_access_function($function_name,$var_controller);

(Calling set_access_function defined in  Access_control.php controller file)

/***************************Ends***************************/

?>

Add extracted Class name and Function name in database by creating  Access_control controller

Here we go:

<?php

defined(‘BASEPATH’) OR exit(‘No direct script access allowed’);

class Access_control extends CI_Controller {

public function __construct()

{

parent::__construct();

$this->load->model(‘Management_model’,’manage’);

}

public function set_access_function($function_name,$var_controller){

$data_controller = array(‘controller_name’=>,$var_controller,

‘controller_satus’=>’1’);

$result = $this->manage->verify($var_controller);  *(To check if controller name has already been entered into the database)*

if($result)

(If yes)

foreach($result as $row)

{   (Take out controller_id)

$controller_id = $row->controller_id;

$varify_data = array(‘controller_id’=>$controller_id,

‘functions_name’=>$function_name);

$test = $this->manage->varify_function($verify_data);  (Check if function having same controller id exist into the database)

if($test)

{(if yes then do nothing or pop some message) }else { (Insert it)

$function_store = array(‘controller_id’=>$controller_id,’functions_name’=>$function_name,’functions_status’=>’1’);

//echo “<pre>”; print_r($function_store); exit;

$this->manage->insert_functions_record($function_store);

} } }

else

{      (If controller doesn’t exist in database then enter it)

$this->manage->insert_record($data_controller);

} } }

?>

Management Model code for insertion related queries

<?php if (!defined(‘BASEPATH’)) exit(‘No direct script access allowed’);

class Management_model extends CI_Model {

public function __construct(){

parent::__construct(); }

public function insert_record($data)   (For storing Controller class name into databse){

$result = $this->db->insert(‘controller_table’,$data);

return $result;}

public function verify($var_controller) (For verifying Controller existing availability into database){

$this->db->select(‘*’);

$this->db->from(‘controller_table’);

$this->db->where(‘controller_name’,$var_controller);

$query = $this->db->get();

return $query->result(); }

public function verify_function($verify_data) (For verifying Function name existing availability into database){

$this->db->select(‘*’);

$this->db->from(‘functions_store’);

$this->db->where($varify_data);

$query = $this->db->get();

return $query->result();}

public function insert_functions_record($function_store) (For storing Controller availability into database){

$result = $this->db->insert(‘functions_store’,$function_store);

return $result;} }

?>

So till now we have managed to enter the entire controller class name and their functions into the database. Now the further steps will be to list down all the controller with their respective function name and assign roles to user which we will discuss in Part-2 of our Custom User Role Management In Codeigniter.

Till then stay tuned we are here to help our developer mates.

One Comment

  1. Deepak Shah said:

    thankyou for the appreciation.

    you have all the rights to comment anytime anywhere .its all your appreciations and love which makes us able to write good

    Rating: 5.0. From 1 vote.
    Please wait...
    June 2, 2017
    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Solve : *
4 ⁄ 2 =