[eluser]alekz[/eluser]
Hi, i made a simple controller/function based login by group...
this is an improved version
i made this using ideas of other ACL projects, is simple and effective, this is an example of use:
group_id type
-------------------
1 Admin
2 Publisher
3 Subscriber
add this to the of redux_auth.php config file
Code:
//----------------------------------------
//
// Permissions begin here
//
//----------------------------------------
$config['auth']['permissions']['/controller1/function1'] = array(1, 2);
$config['auth']['permissions']['/controller1/function2'] = array(1, 3);
$config['auth']['permissions']['/controller2/'] = array(3);
we are giving access to id_groups 1 and 2 to the controller1/function1
we are giving access to id_groups 1 and 3 to the controller1/function2
we are giving access to id_group 3 to the controller2 and all their functions
if you want that everybody that is logged in, use your controller/function just need to do this.
Code:
$config['auth']['permissions']['/controller2/'] = 0;
Put this three lines of code, in every constructor of your controllers
Code:
class Controller1 extends Controller()
{
function Controller1() //this is the constructor
{
parent::Controller(); //you just need to put
if(!$this->redux_auth->has_permissions()) //this three lines
redirect(''); //of code...
}
}
So just put this code in every controller of your project to protect it by controller or by function, and only will be accessed by the groups you want
if you want than a controller/function be accessed by everybody just dont write it in the config file...
to use this you need to add this to redux_auth.php library after logged_in() function
Code:
protected function _get_permissions($group_ids)
{
if($this->ci->session->userdata('id')) //check if is logged....
{
if($group_ids[0] != 0)
{
return $var = (in_array($this->ci->session->userdata('group_id'), $group_ids)) ? true : false; //check if the group
}
else return true;
}
else
return false;
}
public function has_permissions()
{
$controller = '/'.$this->ci->uri->rsegment(1).'/';
$function = $controller.$this->ci->uri->rsegment(2);
if(isset($this->permissions[$controller]))
return $this->_get_permissions($this->permissions[$controller]);
else if(isset($this->permissions[$function]))
return $this->_get_permissions($this->permissions[$function]);
else return -1;
}
and add this line in function _login
Code:
protected function _login ($users, $banned, $email)
{
$i = $this->ci->db->select($users.'.password, '.
$users.'.hash, '.
$users.'.id, '.
$users.'.activation_code, '.
$users.'.group_id, '. // add this line
$banned.'.reason')
->from($users)
->join($banned, $users.'.banned_id = '.$banned.'.id', 'left')
->where($users.'.email', $email)
->limit(1)
->get();
return $var = ($i->num_rows() > 0) ? $i->row() : false;
}
and add this line in the login function to set in the session the group_id after successful login
Code:
public function login ($email, $password)
{
//.....
//....
//.....
if ($password === $result->password)
{
$this->ci->session->set_userdata(array('id'=> $result->id));
$this->ci->session->set_userdata(array('group_id'=> $result->group_id)); //add this line
return true;
}
//....
//....
i hope this can help you, this is simple but effective for small applications...
sorry for my english
Best Regards from Mexico...