Thinkphp3.2 cms之角色开发
一、角色管理开发
<?php namespace Admin\Controller; use Think\Controller; class SystemController extends CommonController{ public function Role_Index(){ $Role_list=D('role')->select(); $this->assign('Role_list',$Role_list); $this->display("role"); } /*** * 角色添加 */ public function Role_Add(){ $data['r_name']=I('post.r_name','','htmlspecialchars'); $data['r_pid']=I('post.r_name','0','htmlspecialchars'); $Role_add=D('role')->add($data); if(isset($Role_add)){ $this->success("添加成功"); }else{ $this->error("添加失败"); } } /** * @param int $id * 删除角色 */ public function Role_Del($id=0){ $r_del=D('role')->where(array('r_id'=>$id))->delete(); if(isset($r_del)){ $this->success("删除成功"); }else{ $this->error("删除失败"); } } public function Role_Update($id=0){ if(IS_POST) { $data['r_name'] = I('post.r_name', '', 'htmlspecialchars'); $data['r_pid'] = I('post.r_name', '0', 'htmlspecialchars'); $r_save = D('role')->where(array('r_id' => I('post.id')))->save($data); if(isset($r_save)){ $this->success("修改成功"); }else{ $this->error("修改失败"); } }else{ $role_update=D('role')->where(array('r_id'=>$id))->find(); $this->assign('role_update',$role_update); $this->display('RoleEdit'); } } /** * 权限管理 */ public function Auth_Index(){ $Auth_node=D('node')->select(); $this->assign('auth_node',$Auth_node); $this->display('auth'); } /** * 权限添加 */ public function Auth_Add(){ $data=$this->Auth_Data(); $auth_add=D('node')->add($data); if($auth_add){ $this->success("添加成功"); }else{ $this->error("添加失败"); } } public function Auth_Data(){ $data['n_name']=I('post.n_name','','htmlspecialchars'); $data['n_g']=ucfirst(I('post.n_g','','htmlspecialchars')); $data['n_m']=ucfirst(I('post.n_m','','htmlspecialchars')); $data['n_a']=ucfirst(I('post.n_a','','htmlspecialchars')); $data['n_pid']=I('post.n_pid'); $data['n_sort']=I('post.n_sort'); return $data; } /** * 角色权限设置 */ public function AuthRole_Setting($id=0){ $Auth_node=D('node')->select(); $Role_list=D('role')->where(array('r_id'=>$id))->find(); $Node_Access=M('node_access')->where(array('r_id'=>$id))->find(); $this->assign('auth_node',$Auth_node); $this->assign('role_list',$Role_list); $this->assign('node_access',$Node_Access); $this->display('AuthSetting'); } /** * 角色权限添加 */ public function AuthRoleSetting_Add(){ $data['r_id']=I('post.r_id'); $n_id=I('post.new_rules'); $data['n_id'] = implode(',', $n_id); $Node_role_find=D('node_access')->where(array('r_id'=>$data['r_id']))->find(); $Node_role_access=empty($Node_role_find)?D('node_access')->add($data):D('node_access')->where(array('r_id'=>$data['r_id']))->save($data); if($Node_role_access!=false){ $this->success("配置成功"); }else{ $this->error("配置失败"); } } /** * 权限的删除 */ public function Auth_Del($id=0){ $auth_del=M('node')->where(array('n_id'=>$id))->delete(); } /** * 权限修改 */ public function Auth_SaveIndex() { $this->display("authsave"); } /** * 测试 */ // public function disubs(){ // $data=D('node')->field('n_id,n_name')->where('n_pid=0')->select(); // foreach($data as $k=>$v){ // $data[$k]['sub']=D('node')->field('n_id,n_name')->where('n_pid='.$v['n_id'])->select(); // } // var_dump($data); // // } public function User_Index(){ $Role_list=D('role')->select(); //角色id $User_list=M('login')->select(); //用户id $Role_user=M('role_user')->select(); //角色和用户共同id foreach($User_list as $k=>$v){ $role=M('role_user')->where(array('user_id'=>$v['uid']))->join('m_role on m_role_user.role_id=m_role.r_id')->select(); $User_list[$k]['role_name']=$role[0]['r_name']; } $this->assign('role_list',$Role_list); $this->assign('user_list',$User_list); $this->assign('role_user',$Role_user); $this->display("UserIndex"); } public function User_Data(){ $data['uname']=I('post.uname','','htmlspecialchars'); $data['upasswd']=I('post.upasswd','','htmlspecialchars'); $data['state']=I('post.state','0','htmlspecialchars'); return $data; } public function User_Add(){ if(IS_POST){ $data=$this->User_Data(); $User_Add=M('login')->add($data); if($User_Add){ $role['role_id']= I('post.role_id'); $role['user_id']=$User_Add; M('role_user')->add($role); $this->success("添加成功"); } } } public function User_Del($id=0){ $User_del=M('login')->where(array('uid'=>I('get.uid')))->delete(); if($User_del){ $this->success("删除成功"); }else{ $this->error("删除失败"); } } public function ceshi(){ $User_list=M('login')->select(); //用户id foreach($User_list as $k=>$v){ $role=M('role_user')->where(array('user_id'=>$v['uid']))->join('m_role on m_role_user.role_id=m_role.r_id')->select(); $User_list[$k]['role_name']=$role[0]['r_name']; } $this->admin=M('node_access')->where(array('r_id'=>2))->find(); foreach($this->admin as $val){ $map=explode(",",$val); } for($i=0;$i<count($map);$i++) { $menu[]=D('node')->where(array("n_id"=>$map[$i]))->select(); } } }