首页 > 代码库 > 用户权限设计(一)

用户权限设计(一)

我是springmvc加mysql加hibernate的环境,基础配置的东西就不写出来的,只把重点的写出来

1.数据表:一个三个表

  (1) 用户表:

技术分享技术分享

技术分享技术分享

  (2)对象表

技术分享技术分享

技术分享技术分享

  (3)用户关于对象对应的权限表

技术分享技术分享

技术分享技术分享

2.model对象

UserAuth.java

public class UserAuth {

	private String userName;

	private Map<String, List<String>> userAuthMap;

	public String getUserName() {
		return userName;
	}

	public void setUserName(String userName) {
		this.userName = userName;
	}

	public Map<String, List<String>> getUserAuthMap() {
		return userAuthMap;
	}

	public void setUserAuthMap(Map<String, List<String>> userAuthMap) {
		this.userAuthMap = userAuthMap;
	}

}

AuthAcc.java

@Entity
@Table(name = "auth_acc")
public class AuthAcc {
	@Id
	private String id;

	@Column
	private String userId;

	@Column
	private String objId;

	@Column
	private String authOperate;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getUserId() {
		return userId;
	}

	public void setUserId(String userId) {
		this.userId = userId;
	}

	public String getObjId() {
		return objId;
	}

	public void setObjId(String objId) {
		this.objId = objId;
	}

	public String getAuthOperate() {
		return authOperate;
	}

	public void setAuthOperate(String authOperate) {
		this.authOperate = authOperate;
	}
}
AuthObj.java

@Entity
@Table(name = "auth_obj")
public class AuthObj {
	@Id
	private String id;

	@Column
	private String name;

	@Column
	private String operate;

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getOperate() {
		return operate;
	}

	public void setOperate(String operate) {
		this.operate = operate;
	}
}

3.Controller
	@RequestMapping("authDemo")
	public String authDemo(Map<String, Object> map) {

		Map<String, Object> authObjMap = new HashMap<String, Object>();// 保存所有对象的所有操作权限

		Map<String, String> objNmMap = new HashMap<String, String>();// 保存对象id对应的对象名,用于画面显示

		List<AuthObj> authObjLst = testService.getAuthObjLst();// 得到有权限管理的所有对象

		String operateKey = null;// 操作权限内容字母
		
		String operate = null;// 操作权限内容

		for (AuthObj mAuthObj : authObjLst) {
			Map<String, String> operateMap = new HashMap<String, String>();// 操作权限内容Map
			
			operate = mAuthObj.getOperate();// 操作权限内容
			
			for (int i = 0; i < operate.length(); i++) {
				operateKey = operate.substring(i, i + 1);
				operateMap.put(operateKey, Constants.OPERATEMAP.get(operateKey));//把每个字母对应的名字保存在map中
			}
			
			authObjMap.put(mAuthObj.getId(), operateMap);//以对象的id为key,操作权限内容Map为value
			objNmMap.put(mAuthObj.getId(), mAuthObj.getName());// 保存对象id对应的对象名,用于画面显示
		}

		map.put("mp", authObjMap);// 保存所有对象的所有操作权限

		map.put("nmMp", objNmMap);// 保存对象id对应的对象名,用于画面显示

		Map<String, List<String>> authAccMap = new HashMap<String, List<String>>();

		String userId = "004";// 假设当前用户id为004

		List<AuthAcc> authAccLst = testService.getAuthAccLstByUserid(userId);// 获取当前用户对应对象的权限

		String authOperate = null;//当前用户的权限

		for (AuthAcc mAuthAcc : authAccLst) {
			
			List<String> authLst = new ArrayList<String>();//当前用户的权限,用一个list保存
			
			authOperate = mAuthAcc.getAuthOperate();//当前用户的权限
			
			for (int i = 0; i < authOperate.length(); i++) {
				authLst.add(authOperate.substring(i, i + 1));//遍历权限,保存在一个list里
			}
			
			authAccMap.put(mAuthAcc.getObjId(), authLst);//以对象id为key,当前用户的权限为value,保存在一个map里
		}

		UserAuth mUserAuth = new UserAuth();

		mUserAuth.setUserAuthMap(authAccMap);

		map.put("vm", mUserAuth);

		return "authDemoVw";
	}
}

4.Constants.java

public class Constants {
	/**
	 * 定义每个字母对应的权限名称
	 */
	public final static Map<String, String> OPERATEMAP = new HashMap<String, String>();  
	static {  
		OPERATEMAP.put("c", "增加");  
		OPERATEMAP.put("r", "查询");  
		OPERATEMAP.put("u", "更改"); 
		OPERATEMAP.put("d", "删除"); 
	}  
}

5.authDemoVw.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@taglib uri="http://www.springframework.org/tags/form" prefix="form"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
	<form:form action="" method="post" commandName="vm">
		<table>
			<c:forEach var="entry" items="${mp}">
				<tr>
					<td>${nmMp[entry.key]}</td>
					<td><form:checkboxes path="userAuthMap[${entry.key}]"
							items="${entry.value}" /></td>
				</tr>
			</c:forEach>
		</table>
	</form:form>
</body>
</html>

6.效果图

技术分享

技术分享

这篇文章只是先把用户所拥有的权限列表显示出来,权限的更新和权限的判断在后面的文章里会写





用户权限设计(一)