2011-09-22 73 views
4

我需要Codeigniter 2.x ACL +身份驗證庫。Codeigniter 2.x - 身份驗證+ ACL庫

我需要給3個不同的管理員用戶和2個不同的前端用戶,並希望通過數據庫動態設置所有內容。

請幫助。

回答

7

CI最受歡迎的兩個認證庫(至少在今年年初)似乎是Ion_auth和Tank_auth。既不處理您的ACL需求,但Ion_auth提供單組功能。

幾個月前,我在一個項目上開始使用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']); 
    } 
+0

你有沒有考慮在建立該項目GitHub並添加您的功能?這可能是項目願意合併的東西。這是一個非常簡單的添加,它增加了一些很好的功能。 –

+0

@AndyShinn - 雖然我仍然認爲Ion_auth是最好的開始,但是我已經把它砍到了與原始版本差不多的程度。我會考慮在我目前的項目完成後發佈它。它現在包括ACL,多郵件地址功能,Mailchimp API集成以及考慮FB/Twitter集成。我需要幫助清理它,並使後者具有可選功能,因爲它們僅用於我的項目目的。 –

+0

@MikeS。我使用Ion Auth 2和用於Ion Auth 2的https://github.com/weblogics/Codeigniter-Ion-Auth-ACL插件。但是我沒有得到如何授予權限(密鑰和值)?你能幫我理解這些代碼嗎? –