【问题标题】:Codeigniter 2.x - Authentication + ACL libraryCodeigniter 2.x - 身份验证 + ACL 库
【发布时间】:2011-09-22 23:06:14
【问题描述】:

我需要一个 Codeigniter 2.x ACL + 身份验证库。

我需要给 3 个不同的管理员用户和 2 个不同的前端用户,并希望通过数据库动态设置所有内容。

请帮忙。

【问题讨论】:

    标签: php codeigniter codeigniter-2


    【解决方案1】:

    两个最流行的 CI 身份验证库(至少在今年年初)似乎是 Ion_auth 和 Tank_auth。尽管 Ion_auth 提供单组功能,但两者都不能满足您的 ACL 需求。

    几个月前,我在一个项目中开始使用 Tank_auth,但当我需要更大的灵活性时,我改用了 Ion_auth。借助它包含的功能,我添加了一个 user_groups 表以及必要的库和模型函数,以允许每个用户拥有多个组成员身份。

    数据结构:

    mysql> describe auth_users_groups;
    +------------+-----------------------+------+-----+-------------------+----------------+
    | Field      | Type                  | Null | Key | Default           | Extra          |
    +------------+-----------------------+------+-----+-------------------+----------------+
    | id         | int(11) unsigned      | NO   | PRI | NULL              | auto_increment |
    | user_id    | mediumint(8) unsigned | NO   |     | NULL              |                |
    | group_id   | mediumint(8) unsigned | NO   |     | NULL              |                |
    | dt_updated | timestamp             | NO   |     | CURRENT_TIMESTAMP |                |
    +------------+-----------------------+------+-----+-------------------+----------------+
    4 rows in set (0.00 sec)
    

    库中添加的部分代码:

       public function get_user_groups($user_id = NULL)
       {
          if ($user_id === NULL) $user_id = $this->get_user()->id;
          return $this->ci->ion_auth_model->get_user_groups($user_id)->result();
       }
    
       /**
        * is_member - checks for group membership via user_groups table
        *
        * @param string $group_name
        * @return bool
        **/
       public function is_member($group_name)
       {
          $user_groups = $this->ci->session->userdata('groups');
    
          if ($user_groups)
          {
             // Go through the groups to see if we have a match..
             foreach ($user_groups as $group)
             {
                if ($group->name == $group_name)
                {
                   return true;
                }
             }
          }
          // No match was found:
          return false;
       }
    

    部分型号代码:

       public function get_user_groups($user_id = NULL)
       {
          if ($user_id === NULL) return false;
          return $this->db->select('group_id, name, description, dt_updated')
                      ->join($this->tables['groups'], 'group_id = '.$this->tables['groups'].'.id', 'left')
                      ->where('user_id', $user_id)
                      ->get($this->tables['users_groups']);
       }
    
       public function set_group($user_id, $group_id)
       {
          $values = array('user_id'=>$user_id, 'group_id'=>$group_id);
           $hits = $this->db->where($values)->count_all_results($this->tables['users_groups']);
          if ($hits > 0)
          {
             return NULL;
          }
          return $this->db->insert($this->tables['users_groups'], $values);
       }
    
       public function remove_groups($user_id, $group_ids)
       {
          $this->db->where('user_id', $user_id);
          $this->db->where_in('group_id', $group_ids);
          return $this->db->delete($this->tables['users_groups']);
       }
    

    【讨论】:

    • 您是否考虑过在 GitHub 上分叉项目并添加您的功能?它可能是项目愿意合并的东西。这是一个非常简单的添加,增加了一些很好的功能。
    • @AndyShinn - 虽然我仍然认为 Ion_auth 是最好的起点,但我已经将它修改到与原版几乎不一样的程度。我会考虑在我当前的项目完成后发布它。它现在包括 ACL、多电子邮件地址功能、Mailchimp API 集成以及考虑 FB/Twitter 集成。我需要帮助清理它并使后面的功能成为可选功能,因为它们只是为了我的项目而“启用”。
    • @MikeS。我正在使用 Ion Auth 2 和用于 Ion Auth 2 的 github.com/weblogics/Codeigniter-Ion-Auth-ACL 插件。但我不知道如何授予权限(键和值)?你能帮我理解那段代码吗?
    • @GaneshAher,我在六年前分叉了 Ion Auth,并没有回头。抱歉,我帮不上忙。
    • @MikeS。好的,谢谢回复。
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2010-10-15
    • 1970-01-01
    • 2011-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多