你不想讓一個隱藏(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
}