我尝试创建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 | |
+------------------------+--------------+------+-----+---------+----------------+然后,permission_role权限表与表有关联,下面是permission_role's结构
+--------------------+---------+------+-----+---------+----------------+
| 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进行检查,但我的数据库没有保存类控制器.有什么解决办法吗?
谢谢,很抱歉我的英语很差
发布于 2018-12-10 15:06:08
假设Branch .etc。是控制器和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
}
}如果重构db结构以在permission表中同时包含控制器/方法,则不必在每个auth方法中包含can_access,只需让控制器用如下代码扩展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)
}
}https://stackoverflow.com/questions/53701788
复制相似问题