John Davidson

php - Displaying only specified roles based on session role

0 comments
Message:


Currently I have a tab where I'm displaying all the roles of a person which is fetching them from my database. Now I've added an extra column in the database name can_manage. This column basically shows what role a specific person can control, for example a digital director will be able to control the digital team. Currently my database looks like this:



Now by default I'm showing all my records from database in View class:


<?php if($roles) foreach($roles as $key => $role):?>
<a data-filter=".roles<?php echo $key ?>"><?php echo $role ?></a>
<?php endforeach;?>

Model Class:


function get_roles_idpair()
{
$idpair=array();
$this->db->from("crm_client_roles");
$query = $this->db->get();
foreach($query->result_array() as $row):
$idpair[$row['roles_id']]=$row['role'];
endforeach;
return $idpair;
}

Now everytime a user logs in they are assigned a roles_id from the database. Now I want it so that suppose the digital director logs in, he can only be able to see the people assigned to them in the can_manage part and their own role. And all the people with 0 as can_manage can only see the role that they currently are.



My Answer:
To achieve this, you can modify your View class to only display the roles based on the logged in user's role and can_manage value. Here's an example of how you can do this:

1. Modify your Model class to include a method to get the logged in user's role and can_manage value:

php
function get_user_role($user_id)
{
$this->db->select('role, can_manage');
$this->db->from('crm_client_roles');
$this->db->where('roles_id', $user_id);
$query = $this->db->get();
return $query->row_array();
}


2. In your Controller class, get the logged in user's role and can_manage value and pass it to the View:

php
$user_id = // get the logged in user's ID
$user_role = $this->your_model->get_user_role($user_id);
$data['user_role'] = $user_role;


3. Modify your View class to only display the roles based on the logged in user's role and can_manage value:

php
$role): ?>






With this implementation, only the roles that the logged in user can manage or their own role will be displayed in the View. Make sure to replace 'your_model' with the actual name of your Model class.

Rate this post

5 of 5 based on 2939 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us