你不想让一个隐藏(GROUP_ID)字段在您的形式,因为它可以被操纵。
只需将其设置为group_id的默认值并通过管理进行更改即可。
基本思路/实施:
添加到您的用户表
`gid` smallint unsigned not null default 0, //or members default value
-
alter table `users` add index(`gid`);
alter table `users` foreign key(`gid`) references groups(`id`) on delete restrict on update restrict;
- 您可以在这里正常化的权限列,并有多重选择
create table `groups`(
id smallint unsigned not null auto_increment primary key,
`name` varchar(20) not null,
`permissions` varchar(255) not null, //JSON object '["read", "edit", "delete", "admin", "super"]'
created_at datetime
)engine=innodb;//or whatever engine you like
-
关闭我的头顶
class MY_Controller extends CI_Controller{
protected $_user;
protected $_permissions=array();
protected $_group;
public function __construct()
{
parent::__construct();
//check for a user logged in
$this->_user = ($user) ? $user : NULL;
//if user, get group and permissions
if($this->_user !== NULL)
{
$this->_get_group();
$this->_get_permissions();
}
}
public function _get_group(){
return $this->_group = $this->_user->group->name; //need to work this bit out
}
public function _get_permissions(){
return $this->_permissions = json_decode($this->_user->permissions, TRUE);
}
public function can_read(){
return in_array('read', $this->_permissions);
}
/// and so on etc
}