简单的权限管理-资源,角色,用户,部门(二)

具体的action方法:

//-------权限-----------
	public String noAuth(){
		return "noAuth";
	}
	public String toMenuManage(){
		//menus=menuBusi.getAllMenu();
		BaseQuery baseQuery=new BaseQuery();
		baseQuery.setPageNo(Pagination.cpn(pageNo));
		baseQuery.setPageSize(pageSize);
		
		StringBuilder params = new StringBuilder();//分页查询的条件(扩展)		
		if(name!=null&&name!=""){
			baseQuery.setName(name);
			params.append("name=").append(name);
		}
		
		String url = "";//分页的连接
		pagination = menuBusi.getMenuWithPage(baseQuery);
		pagination.pageView(url, params.toString());
		return "toMenuManage";
	}
	public String toRoleManage(){
		//roles=roleBusi.getAllRole();
		BaseQuery baseQuery=new BaseQuery();
		baseQuery.setPageNo(Pagination.cpn(pageNo));
		baseQuery.setPageSize(pageSize);		
		StringBuilder params = new StringBuilder();//分页查询的条件(扩展)
		
		if(name!=null&&name!=""){
			baseQuery.setName(name);
			params.append("name=").append(name);
		}
		if(menuId!=null){
			baseQuery.setMenuId(menuId);
			params.append("menuId=").append(menuId);
		}
		
		String url = "";//分页的连接
		pagination = roleBusi.getRoleWithPage(baseQuery);
		pagination.pageView(url, params.toString());
		return "toRoleManage";
	}
	public String toUserManage(){
		//users=userBusi.getAllUser();
		BaseQuery baseQuery=new BaseQuery();
		baseQuery.setPageNo(Pagination.cpn(pageNo));
		baseQuery.setPageSize(pageSize);		
		StringBuilder params = new StringBuilder();//分页查询的条件(扩展)
		
		if(name!=null&&name!=""){
			baseQuery.setName(name);
			params.append("name=").append(name);
		}
		if(deptId!=null){
			baseQuery.setDeptId(deptId);
			params.append("deptId=").append(deptId);
		}
		if(menuId!=null){
			baseQuery.setDeptId(menuId);
			params.append("menuId=").append(menuId);
		}
		if(gender!=null){
			baseQuery.setGender(gender);
			params.append("gender=").append(gender);
		}
		
		String url = "";//分页的连接
		pagination = userBusi.getUserWithPage(baseQuery);
		pagination.pageView(url, params.toString());
		return "toUserManage";
	}
	public String toDeptManage(){
		BaseQuery baseQuery=new BaseQuery();
		baseQuery.setPageNo(Pagination.cpn(pageNo));
		baseQuery.setPageSize(pageSize);		
		StringBuilder params = new StringBuilder();//分页查询的条件(扩展)
		
		if(name!=null&&name!=""){
			baseQuery.setName(name);
			params.append("name=").append(name);
		}
		
		String url = "";//分页的连接
		pagination = departmentBusi.getDepartmentWithPage(baseQuery);
		pagination.pageView(url, params.toString());
		return "toDeptManage";
	}
	
	
	//添加用户
	public String toAddDept(){
		if(department!=null && department.getId()!=null){
			//查询用户
			department = departmentBusi.getDepartment(department.getId());
		}
		return "toAddDept";
	}
	public String toAddUser(){
		//加载出所有部门信息	
		departments=departmentBusi.getAllDepartment();
		if(usertemp!=null && usertemp.getId()!=null){					
			//查询用户
			usertemp = userBusi.getUser(usertemp.getId());
		}
		return "toAddUser";
	}
	public String toAddRole(){
		if(role!=null && role.getId()!=null){
			role=roleBusi.getRole(role.getId());
		}
		return "toAddRole";
	}
	public String toAddMenu(){
		if(menu!=null && menu.getId()!=null){
			menu=menuBusi.getMenu(menu.getId());
		}
		return "toAddMenu";
	}
	
	public String addDept(){
		try {			
			if(department!=null && department.getId()!=null){
				departmentBusi.update(department);
			}else{			
				departmentBusi.addDepartment(department);
			}
			flag=true;
			message="操作成功";
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "addDept";
	}
	public String addUser(){
		try {			
			if(usertemp!=null && usertemp.getId()!=null){
				userBusi.update(usertemp);
			}else{			
				userBusi.addUser(usertemp);
			}
			flag=true;
			message="操作成功";
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "addUser";
	}
	public String addRole(){
		try {
			if(role!=null && role.getId()!=null){
				roleBusi.update(role);
			}else{			
				roleBusi.addRole(role);
			}
			flag=true;
			message="操作成功";
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "addRole";
	}
	public String addMenu(){
		try {
			if(menu!=null && menu.getId()!=null){
				menuBusi.update(menu);
			}else{			
				menuBusi.addMenu(menu);
			}	
			flag=true;
			message="操作成功";
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "addMenu";
	}
	//删除
	public String deleteDept(){
		try {
			if(department!=null && department.getId()!=null){
				departmentBusi.deleteDepartment(department.getId());
				flag=true;
				message="删除部门成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}		
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "deleteDept";
	}
	public String deleteUser(){
		try {
			if(usertemp!=null && usertemp.getId()!=null){
				userBusi.deleteUser(usertemp.getId());
				flag=true;
				message="删除用户成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}		
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "deleteUser";
	}
	public String deleteRole(){
		try {
			if(role!=null && role.getId()!=null){
				//删除role的时候确认没有user进行关联
				if(!roleBusi.hasUserByRole(role.getId())){				
					roleBusi.deleteRole(role.getId());
					flag=true;
					message="删除角色成功";
				}else{
					flag=false;
					message="用户关联该角色,无法删除";
				}
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		
		return "deleteRole";
	}
	public String deleteMenu(){
		try {
			if(menu!=null && menu.getId()!=null){
				//删除menu的时候确认没有role进行关联
				if(!menuBusi.hasRoleByMenu(menu.getId())){				
					menuBusi.deleteMenu(menu.getId());
					flag=true;
					message="删除资源成功";
				}else{
					flag=false;
					message="资源关联该资源,无法删除";
				}
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "deleteMenu";
	}
	
	//禁用,启用
	public String changeDeptStatus(){
		try {
			if(department!=null && department.getId()!=null){
				departmentBusi.changeDeptStatus(department.getId());
				flag=true;
				message="状态更新成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "changeDeptStatus";
	}
	
	public String changeUserStatus(){
		try {
			if(usertemp!=null && usertemp.getId()!=null){
				userBusi.changeUserStatus(usertemp.getId());
				flag=true;
				message="状态更新成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "changeUserStatus";
	}

	public String changeRoleStatus(){
		try {
			if(role!=null && role.getId()!=null){
				roleBusi.changeRoleStatus(role.getId());
				flag=true;
				message="状态更新成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "changeRoleStatus";
	}
	
	public String changeMenuStatus(){
		try {
			if(menu!=null && menu.getId()!=null){
				menuBusi.changeMenuStatus(menu.getId());
				flag=true;
				message="状态更新成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "changeMenuStatus";
	}
	
	
	
	//角色授权
	public String toAuthRole(){
		if(role!=null && role.getId()!=null){
			role=roleBusi.getRole(role.getId());
		}
		menus=menuBusi.getAllMenu();
		return "toAuthRole";
	}	
	public String grantRole(){
		try {
			if(role!=null && role.getId()!=null){			
				roleBusi.grantMenu2Role(role.getId(),menuIds);
				flag=true;
				message="授权成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}
		return "grantRole";
	}
	//用户授权
	public String toAuthUser(){
		if(usertemp!=null && usertemp.getId()!=null){
			usertemp=userBusi.getUser(usertemp.getId());
		}
		roles=roleBusi.getAllRole();
		return "toAuthUser";
	}	
	public String grantUser(){
		try {
			if(usertemp!=null && usertemp.getId()!=null){			
				userBusi.grantRole2User(usertemp.getId(),roleIds);
				flag=true;
				message="授权成功";
			}else{
				flag=false;
				message="抱歉,您进行了非法操作";
			}
		} catch (Exception e) {
			flag=false;
			message="抱歉,服务器正忙,操作失败";
		}		
		return "grantUser";
	}
	//重置密码
	public String repassword(){
		if(usertemp!=null && usertemp.getId()!=null){
			userBusi.changePassword(usertemp.getId());
		}
		return "repassword";
	}

2.service

@Transactional
	public void grantMenu2Role(Long id,String[] menuIds){
		if(menuIds!=null && menuIds.length>0){			
			Role role = getRole(id);
			role.setMenus(null);
			Set menus=new HashSet();
			for (String menuId : menuIds) {
				menus.add(menuDao.get(Long.parseLong(menuId)));
			}
			role.setMenus(menus);
			dao.update(role);
		}
	}


你可能感兴趣的:(工作项目)