📄 userrightmanager.java
字号:
package com.corp.bisc.ebiz.member;
import com.corp.bisc.ebiz.base.*;
import com.corp.bisc.ebiz.security.*;
import com.corp.bisc.ebiz.exception.*;
import java.sql.*;
import java.util.*;
/**
* 权限管理辅助类。
* 权限全部存放在内存中,实现高效
* 创建日期:(2002-5-23 17:35:59)
* @author:PangWei
*/
public class UserRightManager {
//蔡楚煌,归零处理,在数据库手工插入-1ID记录
public static final int TOPFORDER=-1;
//操作类型
public static final int OPERATION_QUERY = 0;
public static final int OPERATION_ADD = 1;
public static final int OPERATION_DELETE = 2;
public static final int OPERATION_EDIT = 3;
public static final int OPERATION_DETAIL = 4;
//范围类型,组(1),单元(0),全部(3)
public static final int SCOPE_GRP = 1;
public static final int SCOPE_UNIT = 0;
public static final int SCOPE_ALL = 3;
//
private static Hashtable hashRights = new Hashtable();
private static UserRightManager handler = null;
/**
* UserRightManager 构造子注解。
*/
public UserRightManager() {
super();
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-17 20:22:45)
* @return java.lang.String
* @param context com.corp.bisc.ebiz.base.RequestContext
* @param scopetype int
*/
public String addRight(RequestContext context, int scopetype,Connection conn) throws Exception{
UserRight userRight;
String addresult = "";
if(scopetype == UserRightManager.SCOPE_GRP){
userRight = new UserRightGroup();
addresult = userRight.fromRequest(context,conn,0);
}else{
userRight = new UserRight();
userRight.fromRequest(context,conn,0);
}
System.out.println("UserRightManager- addRight- addresult="+addresult);
return addresult;
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-17 20:33:44)
*/
public void deleteRights(String selectedid,Connection conn) throws SQLException{
StringTokenizer st = new StringTokenizer(selectedid,"@@");
while(st.hasMoreTokens()){
String id = (String)st.nextElement();
long lid = new Long(id).longValue();
UserRight ur = (UserRight)UserRightManager.findPrincipalById(lid,conn);
if(ur != null)
ur.deleteFromDB(conn);
}
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-16 14:33:18)
* @return boolean
* @param rightName java.lang.String
*/
public static boolean existRight(String rightName,Connection conn) throws SQLException{
String sql = "select 1 from S_PRINCIPAL where name='"+rightName+"'";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
if(rs.next())
return true;
return false;
}
/**
* 根据id查找相应的UserRight
* 创建日期:(2002-5-23 19:15:20)
* 修改日期:2002-08-08;蔡楚煌
* @return com.corp.bisc.ebiz.security.WebPrincipal
* @param id long
*/
public static WebPrincipal findPrincipalById(long id,Connection conn) throws SQLException{
System.out.println("***** UserRightManagement WebPrincipal findPrincipalById ");
try{
UserRight user = null;
Statement stat = conn.createStatement();
String sql="select FUNCID, NAME, DESCRIPTION, ISFOLDER from S_PRINCIPAL WHERE FUNCID="+new Long(id).toString();
//System.out.println("WebPrincipal findPrincipalById sql="+sql);
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
int isFolder = rs.getInt(4);
if(isFolder == 0)
user = new UserRight();
else
user = new UserRightGroup();
user.setId(rs.getLong(1));
user.setName(rs.getString(2));
user.setDescription(rs.getString(3));
user.setFolder(isFolder);
if(isFolder ==1){
((UserRightGroup)user).loadSubRights(conn);
System.out.println("isFolder ==1");
}
}
rs.close();
stat.close();
return user;
}catch(SQLException e){
System.out.println("SQLException e="+e);
throw e;
}
}
public static UserRightManager getHandle(Connection conn)
{
if(handler == null){
handler = new UserRightManager();
}
return handler;
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-15 16:33:34)
* @return int
* @param operation java.lang.String
*/
public static int getOperationType(String operation) {
int operationtype = 0;
if(operation.equalsIgnoreCase("query"))
operationtype = UserRightManager.OPERATION_QUERY;
else if(operation.equalsIgnoreCase("add"))
operationtype = UserRightManager.OPERATION_ADD;
else if(operation.equalsIgnoreCase("delete"))
operationtype = UserRightManager.OPERATION_DELETE;
else if(operation.equalsIgnoreCase("update"))
operationtype = UserRightManager.OPERATION_EDIT;
else if(operation.equalsIgnoreCase("detail"))
operationtype = UserRightManager.OPERATION_DETAIL;
else
operationtype = -1;
return operationtype;
}
public Hashtable getRights()
{
return hashRights;
}
/**
* 此处插入方法描述。
* 创建日期:(2002-7-15 16:33:34)
* @return int
* @param operation java.lang.String
*/
public static int getScopeType(String scope) {
int scopetype = 0;
if(scope.equalsIgnoreCase("group"))
scopetype = UserRightManager.SCOPE_GRP;
else if(scope.equalsIgnoreCase("unit"))
scopetype = UserRightManager.SCOPE_UNIT;
else if(scope.equalsIgnoreCase("all"))
scopetype = UserRightManager.SCOPE_ALL;
else
scopetype = -1;
return scopetype;
}
/**
* 查找所有角色
* 创建日期:(2002-8-10 20:14:27)
* 蔡楚煌,返回所有角色类矢量表
* @return java.util.Vector
*
*/
//角色类指处于权限第三层的权限组(其父类的父归零)
public static Vector getTypeUsers(Connection conn) throws SQLException {
Vector vect=new Vector();
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select FUNCID, NAME, DESCRIPTION, ISFOLDER from S_PRINCIPAL "
+" where funcid in("+
"select subfunc from S_PRINCIPALGROUP where funcid in ("+
"select subfunc from S_PRINCIPALGROUP where funcid="+TOPFORDER+" ) )" );
while(rs.next())
{
UserRight user = null;
int isFolder = rs.getInt(4);
//if(isFolder == 0)
// user = new UserRight();
// else
user = new UserRightGroup();
user.setId(rs.getLong(1));
user.setName(rs.getString(2));
user.setDescription(rs.getString(3));
user.setFolder(isFolder);
vect.add(user);
//System.out.println("right id:->" + user.getID() + " right name:->" + user.getName());
}
stmt.close();
/*PreparedStatement ps = UserRightGroup.getInitialStatement(conn);
Enumeration enum = hashRights.elements();
while(enum.hasMoreElements())
{
Object o = enum.nextElement();
if(o instanceof UserRightGroup)
{
UserRightGroup grp = (UserRightGroup)o;
grp.initializeStructure(ps);
}
}
ps.close();*/
}
catch(SQLException e)
{
System.out.println("getTypeUsers SQLException e= "+e);
throw e;
}
return vect;
}
/**
* 返回权限组或者权限单元的权限。
* 创建日期:(2002-7-15 20:14:27)
* @return java.util.Vector
* @param type int
*/
public static Vector getTypeUsers(Connection conn,int type) throws SQLException {
Vector vect = new Vector();
String sql = "select FUNCID, NAME, DESCRIPTION, ISFOLDER from S_PRINCIPAL where ISFOLDER=?";
try{
PreparedStatement pstat = conn.prepareStatement(sql);
pstat.setInt(1,type);
ResultSet rs = pstat.executeQuery();
while(rs.next()){
UserRight user = null;
int isFolder = rs.getInt(4);
if(isFolder ==1)
user = new UserRightGroup();
else
user = new UserRight();
user.setId(rs.getLong(1));
user.setName(rs.getString(2));
user.setDescription(rs.getString(3));
user.setFolder(isFolder);
vect.add(user);
}
rs.close();
pstat.close();
}catch(SQLException e){
System.out.println("getTypeUsers SQLException e="+e);
e.printStackTrace(System.out);
vect = null;
throw e;
}
return vect;
}
/**
* 查找所有角色
* 创建日期:(2002-8-10 20:14:27)
* 蔡楚煌,返回指定部门的角色类矢量表
* @return java.util.Vector
*
*/
//角色类指处于权限第三层的权限组(其父类的父归零)
public static Vector getTypeUsers(Connection conn,String deptype) throws SQLException {
Vector vect=new Vector();
try
{
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select FUNCID, NAME, DESCRIPTION, ISFOLDER from S_PRINCIPAL "
+" where funcid in("+
"select subfunc from S_PRINCIPALGROUP where funcid ="+deptype+")" );
while(rs.next())
{
UserRight user = null;
int isFolder = rs.getInt(4);
//if(isFolder == 0)
// user = new UserRight();
// else
user = new UserRightGroup();
user.setId(rs.getLong(1));
user.setName(rs.getString(2));
user.setDescription(rs.getString(3));
user.setFolder(isFolder);
vect.add(user);
//System.out.println("right id:->" + user.getID() + " right name:->" + user.getName());
}
stmt.close();
/*PreparedStatement ps = UserRightGroup.getInitialStatement(conn);
Enumeration enum = hashRights.elements();
while(enum.hasMoreElements())
{
Object o = enum.nextElement();
if(o instanceof UserRightGroup)
{
UserRightGroup grp = (UserRightGroup)o;
grp.initializeStructure(ps);
}
}
ps.close();*/
}
catch(SQLException e)
{
System.out.println("getTypeUsers SQLException e= "+e);
throw e;
}
return vect;
}
/**
* 根据用户的编号返回用户的权限组合。
* 所有权限的名称,包含权限组和权限组下的子权限
* 创建日期:(2002-9-14 11:50:11)
* @return java.util.Vector
* @param userid int
* @param conn java.sql.Connection
*/
public static Vector getUserRights(String userid, Connection conn) throws SQLException{
Vector vect = new Vector();
String sql = "select funcid from S_USERPRINCIPAL where userid = '" + userid + "'";
Statement stat = conn.createStatement();
ResultSet rs = stat.executeQuery(sql);
while(rs.next()){
long id = rs.getLong(1);
UserRight ur = (UserRight)UserRightManager.findPrincipalById(id,conn);
if(ur != null){
vect.add(ur);
//System.out.println("User "+userid+"'s rights are as below");
//ur.dump(System.out);
}
}
rs.close();
stat.close();
return vect;
}
/**
* 修改权限项,
* 创建日期:(2002-8-8 20:22:45)
* @return java.lang.String
* @param context com.corp.bisc.ebiz.base.RequestContext
* @param scopetype int
* @param conn Connection
*/
//修改:蔡楚煌,
public String updateRight(RequestContext context, UserRight userRight,Connection conn) throws Exception{
int scopetype=userRight.getFolder();
String addresult = "";
if(scopetype == UserRightManager.SCOPE_GRP){
System.out.println("updateRight---scopetype == UserRightManager.SCOPE_GRP");
addresult = userRight.fromRequest(context,conn,1);
}else{
userRight.fromRequest(context,conn,1);
}
return addresult;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -