role_model = D("Common/Role"); } // 角色管理列表 public function index() { $data = $this->role_model->order(array("listorder" => "ASC", "id" => "DESC"))->select(); $this->assign("roles", $data); $this->display(); } // 添加角色 public function roleadd() { $this->display(); } // 添加角色提交 public function roleadd_post() { if (IS_POST) { if ($this->role_model->create()!==false) { if ($this->role_model->add()!==false) { $this->success("添加角色成功",U("rbac/index")); } else { $this->error("添加失败!"); } } else { $this->error($this->role_model->getError()); } } } // 删除角色 public function roledelete() { $id = I("get.id",0,'intval'); if ($id == 1) { $this->error("超级管理员角色不能被删除!"); } $role_user_model=M("RoleUser"); $count=$role_user_model->where(array('role_id'=>$id))->count(); if($count>0){ $this->error("该角色已经有用户!"); }else{ $status = $this->role_model->delete($id); if ($status!==false) { $this->success("删除成功!", U('Rbac/index')); } else { $this->error("删除失败!"); } } } // 编辑角色 public function roleedit() { $id = I("get.id",0,'intval'); if ($id == 1) { $this->error("超级管理员角色不能被修改!"); } $data = $this->role_model->where(array("id" => $id))->find(); if (!$data) { $this->error("该角色不存在!"); } $this->assign("data", $data); $this->display(); } // 编辑角色提交 public function roleedit_post() { $id = I("request.id",0,'intval'); if ($id == 1) { $this->error("超级管理员角色不能被修改!"); } if (IS_POST) { if ($this->role_model->create()!==false) { if ($this->role_model->save()!==false) { $this->success("修改成功!", U('Rbac/index')); } else { $this->error("修改失败!"); } } else { $this->error($this->role_model->getError()); } } } // 角色授权 public function authorize() { $this->auth_access_model = D("Common/AuthAccess"); //角色ID $roleid = I("get.id",0,'intval'); if (empty($roleid)) { $this->error("参数错误!"); } import("Tree"); $menu = new \Tree(); $menu->icon = array('│ ', '├─ ', '└─ '); $menu->nbsp = ' '; $result = $this->initMenu(); $newmenus=array(); $priv_data=$this->auth_access_model->where(array("role_id"=>$roleid))->getField("rule_name",true);//获取权限表数据 foreach ($result as $m){ $newmenus[$m['id']]=$m; } foreach ($result as $n => $t) { $result[$n]['checked'] = ($this->_is_checked($t, $roleid, $priv_data)) ? ' checked' : ''; $result[$n]['level'] = $this->_get_level($t['id'], $newmenus); $result[$n]['style'] = empty($t['parentid']) ? '' : 'display:none;'; $result[$n]['parentid_node'] = ($t['parentid']) ? ' class="child-of-node-' . $t['parentid'] . '"' : ''; } $str = "