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条)

报告相同问题?

悬赏问题

  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计