dongyi3616 2014-02-10 15:08
浏览 101
已采纳

CodeIgniter Ion Auth:is_admin()是如何工作的?

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.

  • 写回答

2条回答 默认 最新

  • doubi3929 2014-02-10 17:58
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?