Codeigniter 2.x - 身份验证+ ACL库

时间:2011-09-22 23:06:15

标签: php codeigniter codeigniter-2

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

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

请帮忙。

1 个答案:

答案 0 :(得分:7)

两个最受欢迎的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']);
   }