Display menu according to user permission - Codeigniter - PHP

Asked

Viewed 199 times

0

Hello!

I am developing a panel to create and change user permission using the plugin jstree.

inserir a descrição da imagem aqui

This works well for me once I use a table called treeview to mount the tree of jstree and use the table tb_permission, to determine the user’s permission.

treeview
inserir a descrição da imagem aqui

tb_permission
inserir a descrição da imagem aqui

Relating the field name table treeview and the countryside function table tb_permission, I check the state of the field allowed table tb_permission and if it is TRUE, then the user has permission for such a function, if it is equal to FALSE, the user does not have permission.

DOUBT

My question arises at the time of displaying the MENU on the sidebar, respecting the permission that the user has.

I tried to add a field in the table tb_permission, and create the following rule.

First get the field result parent_id table tb_permission, whose the type is equal to 2 and allowed be equal to TRUE.

Then for each of the parent_id, I check how many results I got and if the value is greater than zero, I mark the field visible as TRUE.

And the countryside visible, i would use as parameter to display the MENU on the sidebar in case of TRUE

But in addition to not working, I believe I have other ways to do.


// in test - check only type functions start
$parent = $this->db->select('parent_id')->from('tb_permission')->where('role_id', $role_id)->where('type', 2)->where('allowed', TRUE)->get()->result();
foreach($parent as $key => $item)
{
    //$this->db->where('menu_id', $item->parent_id)->where('role_id', $role_id)->update('tb_permission', ['visible' => TRUE]);
    $allowed     = $this->db->select('*')->from('tb_permission')->where('parent_id', $item->parent_id)->where('role_id', $role_id)->where('type !=', 0)->where('allowed', TRUE)->get()->num_rows();

    $visible     = $allowed > 0 ? TRUE : '';
    if ($visible)
        $this->db->where('menu_id', $item->parent_id)->where('type !=', 2)->where('role_id', $role_id)->update('tb_permission', ['visible' => TRUE]);
}
// in test - check only type functions end

Sidebar
inserir a descrição da imagem aqui

1 answer

0


I arrived at the expected result using the following:

I still think there may be other options, if anyone can disclose, I’d appreciate.


// in test - check only type functions start
$this->db->where('role_id', $role_id)->update('tb_permission', ['visible' => FALSE]);
parent = $this->db->select('parent_id')->from('tb_permission')->where('role_id', $role_id)->where('type !=', 0)->where('allowed', TRUE)->get()->result();
foreach($parent as $key => $item)
{            
     $allowed     = $this->db->select('*')->from('tb_permission')->where('parent_id', $item->parent_id)->where('role_id', $role_id)->where('type !=', 0)->where('allowed', TRUE)->get()->num_rows();

     $visible     = $allowed > 0 ? TRUE : '';
     if ($visible)
         $this->db->where('parent_id', $item->parent_id)->where('type', 0)->where('role_id', $role_id)->update('tb_permission', ['visible' => TRUE]);
         $this->db->where('parent_id', $item->parent_id)->where('type', 1)->where('role_id', $role_id)->where('allowed', TRUE)->update('tb_permission', ['visible' => TRUE]);
}
// in test - check only type functions end

Browser other questions tagged

You are not signed in. Login or sign up in order to post.