0

I'm having problems to do a simple permission system on my Webapp. My DB has a table called "usuario" that has informations about the users of the system. One of these columns is called "privilegio" that has value '0' for administrators and 1 for regular users. An administrator has the power to Add and edit users on the system. Im trying to take this behavior querying my database with the cod of the logged user and getting its permission. If the user is not on the administrator group (privilegio=1) then the add/edit/delete buttons will be unset.

public function usuario() {
if($this->session->userdata('logged')){ 
  $crud = new grocery_CRUD(); 
  $crud->set_subject("Usuário"); 
  $crud->set_theme('datatables'); 
  $crud->set_table("usuario"); 
  (...) 
  $crud->field_type('privilegio','dropdown',array('0'=>'Administrador','1'=>'Usuario'));
  (...) 
  $this->db->select('privilegio');
  $this->db->get('usuario'); 
  $result = $this->db->where('cod_func',$this->session->userdata('cod_func')); 
  if(!$result){ 
     $crud->unset_add(); 
     $crud->unset_edit(); 
     $crud->unset_delete(); 
  }
(...)

The problem (and the question) is that this code only list the user that is logged on, not the others already registered on the system and stored on "usuario" table. I wonder that the list is been made by my query (what is not the behavior I would like) I hope you could undestand my doubt. Sorry for my bad english.

Thank you!

4

1 回答 1

0

您在使用活动记录功能时遇到问题...

当您使用该功能时

$this->db->get('usuario');

这转化为查询:

SELECT * FROM usuario

所以试着把你的代码改成这样:

$this->db->select('privilegio');
$this->db->from('usuario');
$this->db->where('cod_func',$this->session->userdata('cod_func')); 
$this->db->limit(1); //You're only expecting one result
$result = $this->db->get(); // Save the result to the variable result

//为评论编辑,类似于

$result = $result->first_row('array'); //Here I'm fetching only the first row into an array
$privilegio = $result['privilegio']; //Im saving the result from the query in the variable $privilegio

这转化为:

SELECT priviliegio FROM usuario WHERE cod_func = 'some_value' LIMIT 1;

然后你可以用 $result 变量做任何你想做的事情,请参考文档来看看你能做什么......

生成查询结果

于 2013-10-30T01:32:47.287 回答