All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.quincy.auth.service.impl.AuthorizationAbstract Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.quincy.auth.service.impl;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.Map.Entry;

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

import org.springframework.beans.factory.annotation.Autowired;

import com.quincy.auth.entity.Menu;
import com.quincy.auth.entity.Permission;
import com.quincy.auth.entity.Role;
import com.quincy.auth.mapper.AuthMapper;
import com.quincy.auth.o.DSession;
import com.quincy.auth.o.User;
import com.quincy.auth.service.AuthorizationService;

public abstract class AuthorizationAbstract implements AuthorizationService {
	protected abstract Object getUserObject(HttpServletRequest request) throws Exception;
	protected abstract void saveVcode(HttpServletRequest request, String vcode) throws Exception;

	public DSession getSession(HttpServletRequest request) throws Exception {
		Object obj = this.getUserObject(request);
		return obj==null?null:(DSession)obj;
	}

	private final static int width = 88;
	private final static int height = 40;
	private final static int lines = 5;
	
	private final static String VCODE_COMBINATION_FROM = "23456789abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ";

	public void vcode(HttpServletRequest request, HttpServletResponse response, int length) throws Exception {
		Random random = new Random();
		StringBuilder sb = new StringBuilder(length);
		for(int i=0;i roleList = authMapper.findRolesByUserId(userId);
		Map roleMap = new HashMap(roleList.size());
		for(Role role:roleList) {//去重
			roleMap.put(role.getId(), role.getName());
		}
		List roles = new ArrayList(roleMap.size());
		roles.addAll(roleMap.values());
		session.setRoles(roles);
		//权限
		List permissionList = authMapper.findPermissionsByUserId(userId);
		Map permissionMap = new HashMap(permissionList.size());
		for(Permission permission:permissionList) {//去重
			permissionMap.put(permission.getId(), permission.getName());
		}
		List permissions = new ArrayList(permissionMap.size());
		permissions.addAll(permissionMap.values());
		session.setPermissions(permissions);
		//菜单
		List rootMenus = this.findMenusByUserId(userId);
		session.setMenus(rootMenus);
		return session;
	}

	protected DSession createSession(User user) {
		DSession session = this.createSession(user.getId());
		session.setUser(user);
		return session;
	}

	private List findMenusByUserId(Long userId) {
		List allMenus = authMapper.findMenusByUserId(userId);
		Map duplicateRemovedMenus = new HashMap(allMenus.size());
		for(Menu menu:allMenus) {
			duplicateRemovedMenus.put(menu.getId(), menu);
		}
		List rootMenus = new ArrayList(duplicateRemovedMenus.size());
		Set> entrySet = duplicateRemovedMenus.entrySet();
		for(Entry entry:entrySet) {
			Menu menu = entry.getValue();
			if(menu.getPId()==null) {
				rootMenus.add(menu);
				this.loadChildrenMenus(menu, entrySet);
			}
		}
		return rootMenus;
	}

	private void loadChildrenMenus(Menu parent, Set> entrySet) {
		for(Entry entry:entrySet) {
			Menu menu = entry.getValue();
			if(parent.getId()==menu.getPId()) {
				if(parent.getChildren()==null)
					parent.setChildren(new ArrayList(10));
				parent.getChildren().add(menu);
			}
		}
		if(parent.getChildren()!=null&&parent.getChildren().size()>0) {
			for(Menu child:parent.getChildren()) {
				this.loadChildrenMenus(child, entrySet);
			}
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy