acl(访问控制列表)codeigniter按用户id而不是按角色

9fkzdhlc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(404)

我试图创建acl(访问控制列表),但不是按角色创建,而是按用户id创建,因为客户端需要相同的级别,但拥有不同的权限
如何检查访问数据库中没有权限的方法或控制器的用户
下面是表权限结构

+------------------------+--------------+------+-----+---------+----------------+
| Field                  | Type         | Null | Key | Default | Extra          |
+------------------------+--------------+------+-----+---------+----------------+
| permission_id          | int(11)      | NO   | PRI | NULL    | auto_increment |
| permission_name        | varchar(255) | NO   |     | NULL    |                |
| permission_desc        | text         | YES  |     | NULL    |                |
| permission_created_at  | datetime     | YES  |     | NULL    |                |
| permission_modified_at | datetime     | YES  |     | NULL    |                |
+------------------------+--------------+------+-----+---------+----------------+

然后,权限表和权限角色表有关系,这里是权限角色的结构

+--------------------+---------+------+-----+---------+----------------+
| Field              | Type    | Null | Key | Default | Extra          |
+--------------------+---------+------+-----+---------+----------------+
| permission_role_id | int(11) | NO   | PRI | NULL    | auto_increment |
| user_id            | int(11) | NO   | MUL | NULL    |                |
| permission_id      | int(11) | NO   | MUL | NULL    |                |
+--------------------+---------+------+-----+---------+----------------+

现在,我很困惑,如果用户访问的控制器用户没有权限访问它,我怎么能检查它?如果按路由或uri检查,但我的数据库没有保存类控制器。。。有什么解决办法吗?
谢谢你,对不起我的英语不好

1hdlvixo

1hdlvixo1#

假设 Branch 等等。是控制器和 view , edit 对于存储系统,您必须执行以下操作:

class Branch extends CI_Controller {

     public function view {
         $this->acl->can_access(6);
     }

     public function edit {
         $this->acl->can_access(9);
     }
}

acl模型:

class Acl extends CI_Model {

     public function can_access($permission_id = null) {
         $uid = $this->session->userdata('user_id');
         if (!is_null($uid) && !is_null($permission_id)) {
             $this->db->where('user_id', $uid);
             $this->db->where('permission_id', $permission_id);
             $this->db->from('permissions_role');
             if ($this->db->count_all_results() > 0) {
                 return;
             }
         }
         show_error('Not allowed'); // function exits
     }

}

如果您重构了数据库结构以在 permission 你不必包括的table can_access 在每个auth方法中,可以让控制器扩展 MY_Controller 代码如下:

class MY_Controller extends CI_Controller {

    public function __construct() {
        parent::__construct();
        $this->can_access();
    }

    private function can_access() {
        $controller = $this->router->class;
        $method = $this->router->method;
        $curr_user = $this->session->userdata('user_id');
        // do join between permissions and permissions role...
        // check if num_rows > 0 for current user given the controller/method
        // if num_rows is not greater than 0 (user doesn't have permission)
        // then show error. otherwise do nothing (user has permission)
    }

}

相关问题