Logic for access permission and registration of permissions in the database Using JStree and Codeigniter

0

Hello,

I'm trying to develop a logic to register permissões de acessos , using codeigniter and plugin jstree

At the beginning to add a new permission, the following table is loaded into the database.

CREATE TABLE IF NOT EXISTS 'sys_jstree' (
'id' INT(11) NOT NULL,
'texto' VARCHAR(100) NOT NULL,
'tipo' VARCHAR(100) NOT NULL,
'parent_id' VARCHAR(20) NOT NULL,
'menu_id' INT(11) NOT NULL,
PRIMARY KEY ('id'))
ENGINE = InnoDB
DEFAULT CHARSET = utf8;

INSERT INTO 'sys_jstree' ('id', 'texto', 'tipo', 'parent_id', 'menu_id')
VALUES
(1, 'Dashboard', 'grupo', '#', 0),
(2, 'Configuração', 'grupo', '#', 0),
(3, 'Usuário', 'funcao', 2, 0),
(4, 'Criar', 'acao', 3, 0),
(5, 'Editar', 'acao', 3, 0),
(6, 'Excluir', 'acao', 3, 0),
(8, 'Visualizar', 'acao', 3, 0);

The sys_jstree table is only used to popular jstree by clicking Nova Permissão .

Forpopular%%usethefollowingmethod.Controller

//get_all_jstreepublicfunctionget_all_jstree_funcoes(){$jstree=$this->permissao->get_all_jstree_funcoes();echojson_encode($jstree,JSON_NUMERIC_CHECK);}

Model

//get_all_jstree_funcoespublicfunctionget_all_jstree_funcoes(){$this->db->select('id,textotext,tipotype,parent_idparent');$this->db->from('sys_jstree');$query=$this->db->get();returnarray_map(function($item){return$item;},$query->result_array());}

ThewayIhavedone,sofar,everythingisfine.TheproblemstartswhenIclickSave.

TosaveIamusingtwotables,onetosavethejstreeandthenomeoftheregistercalledgrupo.

CREATETABLEIFNOTEXISTS'tb_nivel'('id'INT(11)NOTNULLAUTO_INCREMENT,'nome'VARCHAR(80)NOTNULL,'grupo_id'INT(11)NOTNULL,PRIMARYKEY('id'))ENGINE=InnoDBDEFAULTCHARSET=utf8;INSERTINTO'tb_nivel'('id','nome','grupo_id')VALUES(1,'Administrador',1),(2,'Usuário',2);

Andanothertabletb_nivel

CREATETABLEIFNOTEXISTS'tb_jstree'('id'INT(11)NOTNULL,'texto'VARCHAR(100)NOTNULL,'tipo'VARCHAR(100)NOTNULL,'acesso'INT(11)NOTNULL,--BIT(1)NOTNULLDEFAULT1,'parent_id'VARCHAR(20)NOTNULL,'menu_id'INT(11)NOTNULL,'permissao_id'INT(11)NOTNULL,PRIMARYKEY('id'))ENGINE=InnoDBDEFAULTCHARSET=utf8;INSERTINTO'tb_jstree'('id','texto','tipo','acesso','parent_id',menu_id','permissao_id')VALUES(1,'Dashboard','grupo',1,'#',1,1),(2,'Configuração','grupo',0,'#',2,1),(3,'Usuário','funcao',0,2,3,1),(4,'Criar','acao',1,3,4,1),(5,'Editar','acao',1,3,59,1),(6,'Excluir','acao',0,3,60,1),(8,'Visualizar','acao',1,3,83,1),(9,'Dashboard','grupo',1,'#',1,2),(10,'Configuração','grupo',0,'#',2,2),(11,'Usuário','funcao',0,10,1,2),(12,'Criar','acao',0,11,4,2),(13,'Editar','acao',0,11,59,2),(14,'Excluir','acao',0,11,60,2),(15,'Visualizar','acao',0,11,83,2);
Thistb_jstreetablewillstoretheaccesspermissions,havethesamefieldsasthejstreetableandsomeadditionalfields,whicharethesys_jstreefieldsthatwillbeacessoortrue,andthefalse,whichwilltakepermissao_idoftheregisteredpermission.

ToaddanewpermissionIamusingthemethodsbelow.

Controller

//setpublicfunctionset_objeto(){$objeto=$_POST['objeto'];$js_tree=$_POST['jstree'];$objeto_decode=json_decode($objeto);$jstree_decode=json_decode($js_tree);$id=$objeto_decode->id;/***lançaoobjeto***/foreach($jstree_decodeas$key=>$value){$jstree_decode[$key]=(array)$value;}/***exibeoresultado***///print_r($jstree_decode);if($id==0){$permissao_id=$this->permissao->add($objeto_decode);$this->permissao->add_jstree($jstree_decode,$permissao_id);}else{$permissao_id=$objeto_decode->id;$this->permissao->edit(array('id'=>$permissao_id),$objeto_decode);$this->permissao->edit_jstree($jstree_decode);}$objeto_encode=json_encode($objeto_decode);echo($objeto_encode);}

Model

//addpublicfunctionadd($dados){$permissao=['nome'=>$dados->nome,'grupo_id'=>$dados->grupo_id,];$permissao_id=$this->db->insert($this->tabela,$permissao);return$permissao_id;}//add_jstreepublicfunctionadd_jstree($dados,$permissao_id){print_r($dados);$array=(array)$dados;foreach($dadosas$js){$jstree=['acesso'=>$js['state']->selected,'permissao_id'=>$permissao_id,];$jstree_id=$this->db->insert('tb_jstree',$permissao);return$jstree_id;}}

Thequestionnowis:

  • AstheIDoftheINSERTtableshows,foreachrecord,Iwillhavetorepeateveryjs_treetreeandfetchthedatabythejstreefield.
  • Isthereanyotherwaytoregisterapermissionwithouthavingtoregistersomethingthatalreadyexistsinthedatabase?Seerecord1and9,forexample.
  • Ifthereisnootherway,howcanIaddanewtree.since,beforeregisteringthejstree,thefieldslikeIDandparent_idarealreadyfilled?SoIdonothavecontrolsonthesefields,whenIshouldhave,sincewheneverIaddanewpermission,thetreeIDwillalwaysstartwith1,andtherearesomeparent_idthatwillbeID'schild.

ThebiggestproblemisbecausetheID=1,2,3...alreadyexistsinthedatabase.

Togetanidea,seehowthejstreearrayiscoming,whenIclickonsavenew.

pastebin array_jstree

    
asked by anonymous 29.03.2018 / 17:23

0 answers