Question

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!

Was it helpful?

Solution

you're having trouble with the active record functions...

When you use the function

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

This translates to the query:

SELECT * FROM usuario

So try changing your code to something like this:

$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

//Edited for the comment, something like

$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

This translates to:

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

Then you can do whatever you want with the $result variable, please refer to documentation to see what you can do...

Generating Query Results

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top