从零开始一步一步做论坛------抛砖引玉,欢迎拍砖[六]----权限

      上节谈了版区权限,这次来谈谈个人权限,也就是用户权限.这个系统有一个初始化的Action,当执行这个Action的时候,会对系统的[权限--角色--用户组]有一个初始化的功能.代码如下:

//添加用户权限类
public class SystemInitAction extends ActionSupport{

	private PermissionService permissionService;
	private RoleService roleService;
	private UserGroupService userGroupService;
	private UserInfoService userInfoService;
	private UserLevelService userLevelService;

	
	public String execute() {
		ExportDB.CreateDB();
		
		/***************************构造角色****************************/
		// 注册用户角色
		Role registerRole = new Role();
		registerRole.setRoleName("普通注册用户");
		registerRole.setTypeID(0);
		Set registerSet = new HashSet();
		registerSet.add(SystemInit.p101);
		registerSet.add(SystemInit.p102);
		registerSet.add(SystemInit.p103);
		registerSet.add(SystemInit.p201);
		registerSet.add(SystemInit.p202);
		registerSet.add(SystemInit.p203);
		registerRole.setPromissions(registerSet);
		
		this.getRoleService().saveRole(registerRole);
		
		//版主角色
		Role boardMasterRole = new Role();
		boardMasterRole.setRoleName("版主角色");
		boardMasterRole.setTypeID(1);
		Set boardMasterSet = new HashSet();
		boardMasterSet.add(SystemInit.p401);
		boardMasterSet.add(SystemInit.p402);
		boardMasterSet.add(SystemInit.p403);
		boardMasterSet.add(SystemInit.p404);
		boardMasterRole.setPromissions(boardMasterSet);
	
		this.getRoleService().saveRole(boardMasterRole);
		
		//超级管理员角色
		Role adminRole = new Role();
		adminRole.setRoleName("超级管理员");
		adminRole.setTypeID(1);
		Set adminSet = new HashSet();
		adminSet.add(SystemInit.p000);
		adminSet.add(SystemInit.p101);
		adminSet.add(SystemInit.p102);
		adminSet.add(SystemInit.p103);
		adminSet.add(SystemInit.p201);
		adminSet.add(SystemInit.p202);
		adminSet.add(SystemInit.p203);
		adminSet.add(SystemInit.p401);
		adminSet.add(SystemInit.p402);
		adminSet.add(SystemInit.p403);
		adminSet.add(SystemInit.p404);
		adminRole.setPromissions(adminSet);
		this.getRoleService().saveRole(adminRole);
		

		/***************************构造用户组****************************/
		//游客用户组
		UserGroup guestGroup = new UserGroup();
		guestGroup.setGroupName("游客用户组");
		guestGroup.setGroupDesc("只有少部分权限");
		guestGroup.setType(Constant.GROUP_GUEST);
		Set guestSet = new HashSet();
		guestSet.add(registerRole);
		guestGroup.setRoles(guestSet);
		this.getUserGroupService().saveUserGroup(guestGroup);
		
		
		// 注册用户组
		UserGroup registerGroup = new UserGroup();
		registerGroup.setGroupName("注册用户组");
		registerGroup.setGroupDesc("拥有注册用户所有权限");
		registerGroup.setType(Constant.GROUP_REGISTER);
		Set register = new HashSet();
		register.add(registerRole);
		registerGroup.setRoles(register);	
		this.getUserGroupService().saveUserGroup(registerGroup);
		
		//系统超级管理员
		UserGroup adminGroup = new UserGroup();
		adminGroup.setGroupName("超级管理员");
		adminGroup.setGroupDesc("系统超级管理员,不解释");
		adminGroup.setType(Constant.GROUP_ADMINISTRATOR);
		Set superRole = new HashSet();
		superRole.add(adminRole);
		adminGroup.setRoles(superRole);
		this.getUserGroupService().saveUserGroup(adminGroup);

		
		//构造系统超级管理员
		UserInfo admin = new UserInfo();
		admin.setUserName("admin");
		admin.setPassword("123456");
		admin.setUserLevel(this.getUserLevelService().getUserLevelByValue(100000));
		admin.setUserGroup(adminGroup);
		admin.setRegTime(1317651595390L);
		this.getUserInfoService().saveUserInfo(admin);
		return SUCCESS;
	}

}
 

      大家看到这时,请看看以前几个中的图,权限和角色多对多关联,角色和用户组多对多关联,但是用户和用户组之间是多对一关联,一个用户只能属于一个用户组,这就限定死了每个人的权限,或者说限定死了每个用户组对应的权限.当然,用户可以在后台编辑管理这些表之间的对应关系.本来给系统的用户组定义了6种类型,但是在开发中只实现了四种,不过以后可以慢慢完善,主要是系统框架搭好了,完善倒是很容易.当一个用户注册的时候,将这个用户加入注册用户组即可以了.reg方法中只需要一句话,注意UserGroup是有个type的值的,指示不同用户组有不同值,这个值在配置文件中已经写死了.reg方法中加入以下语句:

ui.setUserGroup(this.getUserGroupService().getUserGroupByType(Constant.GROUP_REGISTER));

       对于没有注册的用户,他来论坛也必须有一定的权限,所以对于UserLoginInterceptor这个拦截器来说,如果用户登录,就通过,如果没有登录,构造一个虚拟的游客身份,同事给予游客对应的权限,或者说将其和游客用户组绑定,代码如下:

public class UserLoginInterceptor  extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();
		
		HttpServletRequest request = (HttpServletRequest) ac.get(ServletActionContext.HTTP_REQUEST);
		HttpServletResponse response = (HttpServletResponse) ac.get(ServletActionContext.HTTP_RESPONSE);
		
		ServletContext servletContext = (ServletContext) ac.get(ServletActionContext.SERVLET_CONTEXT);
		WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		UserInfoService userInfoService = (UserInfoService) wc.getBean("userInfoService");
		UserGroupService userGroupService = (UserGroupService) wc.getBean("userGroupService");
		
		
		UserSession us = (UserSession) ac.getSession().get(Constant.USERSESSION);
		
		if(us!=null){//登录
			return invocation.invoke();
		}else{//游客
			ac.getSession().put(Constant.USERSESSION, this.createGuestUserSession(userInfoService, userGroupService));
			return invocation.invoke();
		}
	}
	
	private UserSession createGuestUserSession(UserInfoService userInfoService, UserGroupService userGroupService){
		UserSession us = new UserSession();
		
		us.setUserName("guest");
		UserGroup ug = userGroupService.getUserGroupByType(Constant.GROUP_GUEST);
		us.setGroupID(ug.getId());
		us.setIsGuest(1);
		Map[] permissionMap = userInfoService.getUserPermissions(ug);
		us.setUserPermissionArray(permissionMap);
		return us;
	}

}

 最后对所有用户进行权限拦截,如果有权限则放行,没有权限拦截.代码如下:

public class UserPermissionInterceptor extends AbstractInterceptor{

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		ActionContext ac = invocation.getInvocationContext();
		
		String actionName = "/"+ac.getName();
		String saction = "";
		
		Map map = ac.getParameters();
		
		String[] _saction = (String[]) map.get("action");
		if(_saction!=null){
			saction = _saction[0];
		}
		
		boolean havaPermission = false;
		
		ServletContext servletContext = (ServletContext)ac.get(ServletActionContext.SERVLET_CONTEXT);
		WebApplicationContext wc = WebApplicationContextUtils.getWebApplicationContext(servletContext);
		
		if(wc==null){
			return "null";//此处需要修改
		}else{
			UserSession us = (UserSession) ac.getSession().get(Constant.USERSESSION);
	    	
			Map m = us.getUserPermission();
	    
			Permission permission = (Permission) us.getUserPermission().get(actionName + "?action=*");		
			if(permission!=null){
				havaPermission = true;
				System.out.println("全权通过");
			}else{
				permission = (Permission) us.getUserPermission().get(actionName+"?action="+saction);
				System.out.println("权限拦截器执行:"+actionName+"?action="+saction);
				if(permission !=null){
					havaPermission = true;
				}else{
					havaPermission = false;
				}
			}
			if(havaPermission){
				System.out.println("权限拦截器执行:顺利通过");
				return invocation.invoke();
			}else{
				//此处需要添加
				System.out.println("权限拦截器执行:未通过");
				return null;
			}
			
		}
		
	}

}

      这个拦截器还不是很全面,有许多地方需要改进.

      个人感觉其实开发一个系统,前期搭好框架不是很难,但是后期完善才是最花费时间的.虽然各种Service--Action已经写完了,但是要实现某个小功能,就有可能修改Action中的方法,所以大概实现了框架,那么剩下来的时间就是慢慢的修改Action和页面了,还有各种配置.总之,开发一个系统一个人是很不容易的.关键一定要坚持下来.

原创首发,谢谢支持!

猜你喜欢

转载自feiyang404.iteye.com/blog/1189943
今日推荐