Question

I'm using the Ion_auth for CodeIgniter in my project auth library. I'm trying to make a similar function as is_admin() for other user types (there are three user types other than the admin) to check whether the logging in user is of one of those three types so I can redirect them to their respective pages. Ion_auth is using an event called is_admin but I can't understand where this is happening in the Ion_auth_model.php and how it checks whether the logged in user is admin. So how can I make other functions/events similar to this one?

I know I can just do something like query the database and check the user group but I want to do the way Ion_auth has done. That way I can also satisfy myself by actually understanding the stock code.

Was it helpful?

Solution

I am just looking at source code on github.

As lots of auth libraries store, information you are looking for in session and library itself checks session variables.

your function you are looking for is on line 447 in file application/libraries/Ion_auth.php

public function is_admin($id=false)
    {
        $this->ion_auth_model->trigger_events('is_admin');

        $admin_group = $this->config->item('admin_group', 'ion_auth');

        return $this->in_group($admin_group, $id);
    }

In order to create your own I suggest you to reverse engeneer model (ion_auth_model.php)

But! Ion already has a workaround for you, this method is what you are looking for

in_group()

$group = 'gangstas';
    if (!$this->ion_auth->in_group($group))
    {
        $this->session->set_flashdata('message', 'You must be a gangsta to view this page');
        redirect('welcome/index');
    }

source

OTHER TIPS

The function is_admin() refers to the admin group defined in the file /application/config/ion_auth.php: $config['admin_group'] = 'admin'; // Default administrators group, use name.

The admin_group name itself is stored in the ion_auth database field groups.name.

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