Working with hierarchical data

1

I'm creating a system where administrators will set user permissions. As I want to do in an organized and well detailed way, I thought of creating permissions by groups, hierarchically, like this:

  
  • Item 1   
    • Register
    •   
    • Change
    •   
    • Delete
    •   
  •   
  • Item 2   
    • Same thing as Item 1   
      • But it may contain more nodes   
        • And even more if necessary:)
        •   
      •   
    •   
  •   
  • Item 3
  •   

Ok, the logic is created and the database too, however how to handle this in php?

I would not want to have to limit the amount of nodes, so it would have to be a dynamic way of looking. How can I do this?

    
asked by anonymous 31.05.2015 / 00:15

2 answers

1

I have an implementation that goes against what you want, but to explain I go in parts:

The end result would look like this: (red is not allowed in green with permission)

Wellyousaidthatthepartofthedatabaseisready,butitdidnotshowsoI'llshowyouhowIputitthereinthedatabase.

CREATETABLE'tab_modulo'('mod_id'int(11)NOTNULLAUTO_INCREMENTCOMMENT'PK','mod_tipo'varchar(45)NOTNULLCOMMENT'MENU->MENUDOSISTEMA\nMENU-SUB->SUB-MENUDOSISTEMA\nMODULO->MODULODOSISTEMA\nMODULOUP->MODULOQUECONTEMNIVEISDEACESSO\nNIVELUP->NIVELDECESSODENTRODEUMMODULO','mod_nome'textNOTNULLCOMMENT'tagparavisualizaçãonohtml','mod_apelido'varchar(45)NOTNULLCOMMENT'NomedoMenu/Sub-Menu,nomequeaparecenainterfacepermissõesetc.','mod_ref'int(11)NOTNULLCOMMENT'Usadoparareferenciarossub-menusaqualmenu(mod_id)elepertence','mod_atalho'char(1)DEFAULTNULLCOMMENT'tecladeatalho','mod_ordem'int(11)NOTNULLDEFAULT'0paraficaremcimanaordenacaoevalorespositivosirãofazerdesceroitemnaordenacao',PRIMARYKEY('mod_id'))ENGINE=InnoDBAUTO_INCREMENT=116DEFAULTCHARSET=latin1COMMENT='ModuloseMenusdosistema';

Understandingtheconcept:

  • MENU-partthatappearswithoutmouseover(thisisgrayintheimage)
  • SUB-MENU-Partwherethemodulesaregrouped(sub-titleifyouprefer)
  • MODULE-Thepagethatwillbegivenaccess(hasthecheckboxinfront)
  • UPMODULEandUPLEVEL-Specialpermissionswithinamodule(appearsasifitwereasub-itemofamodule)

    mysql>select*fromtab_modulolimit4;+--------+----------+---------------------------------------------------------------------------------------+----------------+---------+------------+-----------+|mod_id|mod_tipo|mod_nome|mod_apelido|mod_ref|mod_atalho|mod_ordem|+--------+----------+---------------------------------------------------------------------------------------+----------------+---------+------------+-----------+|1|MENU-SUB|<b>CartaFrete</b>|CartaFrete|42|c|4||2|MENU|<b>Financeiro</b>|Financeiro|0|f|1||4|MENU|<b>Administrador</b>|Administrador|0|a|6||5|MODULO|<ahref="javascript:loadContent('#conteudo','consultaFrete.php');">Consulta Carta</a> | Consulta Carta |       1 | c          |         0 |
    +--------+----------+---------------------------------------------------------------------------------------+----------------+---------+------------+-----------+
    

I left an attribute with TAG tags for when php is put on the page the menu is already set, and the one attribute with the tagless description to generate the list of permissions for me to be tagging / unchecking permissions.

In php I made a function to generate the menu like this:

    private function construirMenuPermissoes(){
    for ($i=0; $i <sizeof($this->objeto); $i++) {   
        if($this->objeto[$i]->mod_tipo == 'MENU'){    //ENTÃO ELE É UM MENU PRINCIPAL
            $this->menus[] = $this->objeto[$i];                
            $mod_id = $this->objeto[$i]->mod_id;
            $mod_apelido = $this->objeto[$i]->mod_apelido;             
            $mod_nome = $this->objeto[$i]->mod_nome;
            $menu = "<div id='p$mod_id' mod_id='$mod_id' class='div-menu'><ul style='width: 180px;'>".
                   // "<li id='".$mod_apelido."'>".
                        "<legend class='ui-widget ui-widget-header ui-corner-all legend'>$mod_nome</legend>?".
                        "<ul id='browser' class='treeview ".$mod_apelido."' style='margin-top: -10px;'>"; //cria o menu no nivel principal
            $itens = "";
            $itens = $itens.$this->getSubNiveis2($mod_id);                
            echo $menu.$itens."</ul></ul></div>";
        }
    } 
}

private function getSubNiveis2($mod_id){ //FUNCAO RECURSIVA USADA NO PERMISSOES.PHP
    for ($i=0; $i < sizeof($this->objeto); $i++) {      
        $mod_apelido = $this->objeto[$i]->mod_apelido;            
        $mod_ref = $this->objeto[$i]->mod_ref;
        $mod_tipo = $this->objeto[$i]->mod_tipo;   
        $id = $this->objeto[$i]->mod_id;
        if( $mod_ref == $mod_id){
            if($mod_tipo == 'MENU-SUB'){
                $menu = @$menu."<li id='p".$id."' mod_id='$id' ><b>".$mod_apelido."</b><ul>";
                $menu = @$menu.$this->getSubNiveis2($id)."</ul>"; //MOD_ID DO ARRAY NAO DO PARAMETRO
            }               
            else if($mod_tipo == 'MODULOUP'){                                // ACESSO ELEVADO DENTRO DE UM MODULO
                $menu = @$menu."<li id='p".$id."' style='padding-bottom: 6px; font: bold 8pt Arial;'><b>".$mod_apelido.
                        "<input class='".$id."' type='checkbox' style='float: right; cursor: pointer;'/>".
                        "</b><ul>";
                $menu = @$menu.$this->getSubNiveis2($id)."</ul>"; //MOD_ID DO ARRAY NAO DO PARAMETRO
            }
            else{                    
                $menu = @$menu."<li id='p".$id.
                        "' style='padding-bottom: 6px; font: bold 8pt Arial;'>".$mod_apelido.
                        "<input class='".$id."' mod_ref='" . $mod_ref . "'
                            type='checkbox' style='float: right; cursor: pointer;'/>"."</li>";
            }
        }
    }
    return $menu;
}

Where $ this-> object is the array of all the modules I have registered in the database. I hope it has helped you, you doubt the provision.

    
02.06.2015 / 21:36
1

Good morning Diego, I think the best way for you to do this is to separate users by status, which would be nothing more than permission groupings.

In this way you would have two streams in your system, a screen where the Administrator would create status, example (Administrator, Editor, Operator) and another screen where the System Administrator would bind permissions to each status, for example: Administrators have full control, Editors can edit and create, Operators can just view.

It should be noted that the permissions could not be created from the panel, since it would not be possible for you to predict in your system script a permission that does not exist.

Thanks, I hope I have helped. Thanks.

    
31.05.2015 / 08:31