easyui权限问题


今天我们在上一次的基础上完成用户的权限问题。
思路:一个用户对应多个菜单,然后一个菜单可以对应多个用户,其实这就是user与menu的多对多的关系。我们通过一个用户权限中间表,将用户的ID和其所有的一些权限进行一一对应。

MenuDao

package com.shl.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.shl.entity.TreeNode;
import com.shl.util.JsonBaseDao;
import com.shl.util.JsonUtils;
import com.shl.util.PageBean;
import com.shl.util.StringUtils;

public class MenuDao extends JsonBaseDao {
	/**
	 * 
	 * @param map  req.getparameterMap
	 * @param pb  分页
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<TreeNode> list(Map<String, String[]> map,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		List<Map<String, Object>> listMenu=this.listMenuSef(map, pb);//先查大权限
		List<TreeNode> tnlist=new ArrayList<>();
		menuList2TreeNodeList(listMenu,tnlist);//查大权限对应的一些小权限
		return tnlist;
	}
	
	//查出用户所有的大权限
	public List<Map<String, Object>> listMenuSef(Map<String, String[]> map,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_menu where true";
		String id=JsonUtils.getParamVal(map, "menuHid");
		if(StringUtils.isNotBlank(id)) {
			sql=sql+" and menuid in ("+id+") ";
		}
		else {
			sql=sql+" and menuid = -1";
		}
		return super.executeQuery(sql, pb);
	}
	
	/**
	 * 查询menu表的数据(大权限对应的一些小权限)
	 * @param map
	 * @param pb
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> listMenu(Map<String, String[]> map,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_menu where true";
		String id=JsonUtils.getParamVal(map, "id");
		if(StringUtils.isNotBlank(id)) {
			sql=sql+" and parentid in ("+id+") ";
		}
		else {
			sql=sql+" and parentid = -1";
		}
		return super.executeQuery(sql, pb);
	}
	
	/**
	 * {Menuid:1}-->{id:1}
	 * 
	 * menu表中的数据不符合easyUI树形展示的数据格式
	 * 需要转换成easyUI所能识别的数据格式
	 * @param map
	 * @param tn
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	private void menu2TreeNode(Map<String, Object> map,TreeNode tn) throws InstantiationException, IllegalAccessException, SQLException {
		tn.setId(map.get("Menuid").toString());
		tn.setText(map.get("Menuname").toString());
		tn.setAttributes(map);
		
		//tn.setChildren(children);
		Map<String, String[]> jspMap=new HashMap<>();
		jspMap.put("id", new String[] {tn.getId()});
		List<Map<String, Object>> listMenu = this.listMenu(jspMap, null);
		List<TreeNode> tnlist=new ArrayList<>();
		menuList2TreeNodeList(listMenu,tnlist);
		tn.setChildren(tnlist);
	}
	
	/**
	 * [{Menuid:1},{Menuid:2}.....]-->[{id:1},{id:2}....]
	 * @param maplist
	 * @param tnlist
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	private void menuList2TreeNodeList(List<Map<String, Object>> maplist,List<TreeNode> tnlist) throws InstantiationException, IllegalAccessException, SQLException {
		TreeNode tn=null;
		for (Map<String, Object> map : maplist) {
			tn=new TreeNode();
			menu2TreeNode(map,tn);
			tnlist.add(tn);
		}
	}
}

login.jsp

<form action="${pageContext.request.contextPath}/userAction.action?methodName=login" method="post">
	uid:<input type="text" name="uid"><br>
	upwd:<input type="text" name="upwd"><br>
	<input type="submit">
</form>

UserDao

package com.shl.dao;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import com.shl.util.JsonBaseDao;
import com.shl.util.JsonUtils;
import com.shl.util.PageBean;
import com.shl.util.StringUtils;

public class UserDao extends JsonBaseDao{
	/**
	 * 登录查询用户表    登录
	 * @return
	 * @throws SQLException 
	 * @throws IllegalAccessException 
	 * @throws InstantiationException 
	 */
	public List<Map<String, Object>> list(Map<String, String[]> paMap,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_user_version2 where true ";
		String uid=JsonUtils.getParamVal(paMap, "uid");
		String upwd=JsonUtils.getParamVal(paMap, "upwd");
		if(StringUtils.isNotBlank(uid)) {
			sql=sql+" and uid = "+uid;
		}
		if(StringUtils.isNotBlank(upwd)) {
			sql=sql+" and upwd = "+upwd;
		}
		return super.executeQuery(sql, pb);
	}
	
	/**
	 * 通过中间表查询登录用户所对应的权限
	 * @param paMap
	 * @param pb
	 * @return
	 * @throws InstantiationException
	 * @throws IllegalAccessException
	 * @throws SQLException
	 */
	public List<Map<String, Object>> listMenu(String uid,PageBean pb) throws InstantiationException, IllegalAccessException, SQLException{
		String sql="select * from t_easyui_usermenu where true ";
		if(StringUtils.isNotBlank(uid)) {
			sql=sql+" and uid = "+uid;
		}
		return super.executeQuery(sql, pb);
	}
}

UserAction

package com.shl.web;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.shl.dao.UserDao;
import com.zking.framework.ActionSupport;

public class UserAction extends ActionSupport {
	
	private UserDao ud=new UserDao();
	
	public String login(HttpServletRequest req,HttpServletResponse resp) throws InstantiationException, IllegalAccessException, SQLException {
		List<Map<String, Object>> list = this.ud.list(req.getParameterMap(), null);
		if(list!=null&&list.size()>0) {
			List<Map<String, Object>> listMenu = this.ud.listMenu(req.getParameter("uid"), null);//查用户有的权限
			StringBuilder sb=new StringBuilder();
			for (Map<String, Object> map : listMenu) {
				sb.append(","+map.get("menuId"));
			}
			//,001,002,.....
			req.setAttribute("menuHid", sb.substring(1));//将用户有的权限拼接存入req中
		}
		else {
			return "login";
		}
		return "index";
	}
}

然后我们在index.jsp中将用户有的权限从req中取出来,放入影藏域。

index.js

因为这次不是查所有的权限了,所有要把用户所有的权限当做参数传入方法中

$(function(){
	$("#tt").tree({
		url:'menuAction.action?methodName=treeMenu&&menuHid='+$("#menuHid").val(),
		onClick:function(node){
			//content为跳转界面的路径
			var content = '<iframe scrolling="no" frameborder="0" src="'+node.attributes.menuURL+'" width="99%" height="99%"></iframe>';
			if($('#menuTabs').tabs('exists',node.text)){//如果界面已经存在,则让那个界面选中
				$('#menuTabs').tabs('select',node.text);
			}
			else{//如果不存在,那就新增一个页面
				$('#menuTabs').tabs('add',{    
					title:node.text,    
					content:content,    
					closable:true,    
					tools:[{    
						iconCls:'icon-mini-refresh',    
						handler:function(){    
							alert('refresh');    
						}    
					}]    
				});  
			}
		}
	})
})

效果1:
在这里插入图片描述
在这里插入图片描述
效果二:
在这里插入图片描述
在这里插入图片描述
不同的用户有不同的权限

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值