📄 dbiofficefactory.java
字号:
/*
* DbIofficeFactory.java
*
* Created on 2001年7月3日, 下午2:40
*/
package com.gs.db.dbimp;
import com.gs.db.*;
import com.gs.util.*;
import java.sql.*;
import java.util.*;
import java.io.*;
//////////////////////////////////////
import java.net.InetAddress;
import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
////////////////////////////////////////
public class DbIofficeFactory extends IofficeFactory {
private static final String GET_PERM_INFO =
"SELECT * FROM gsMenues where type!=0";
private static final String COUNT_PERM_INFO =
"SELECT max(IDMenue) FROM gsMenues";
private static final String GET_USER_PERMS =
"SELECT IDMenue FROM gsUserMenu WHERE userid=?" ;
private static final String USERS_WITH_PERM =
"SELECT userid FROM gsUserMenu WHERE IDMenue=?";
private static final String GET_GROUP_PERMS =
"SELECT IDMenue from gsGroupMenu WHERE groupid=?";
private static final String GROUPS_WITH_PERM =
"SELECT groupid FROM gsGroupMenu WHERE IDMenue=?";
private static final String ADD_USER_PERM =
"INSERT INTO gsUserMenu(userid,IDMenue) VALUES(?,?)";
private static final String REMOVE_USER_PERM =
"DELETE FROM gsUserMenu WHERE userid=? AND IDMenue=?";
private static final String ADD_GROUP_PERM =
"INSERT INTO gsGroupMenu(groupid,IDMenue) VALUES(?,?)";
private static final String REMOVE_GROUP_PERM =
"DELETE FROM gsGroupMenu WHERE groupid=? AND IDMenue=?";
private static final String GROUP_PERMISSION =
"SELECT IDMenue FROM gsGroupMenu WHERE groupid=? ORDER BY IDMenue ASC";
private static final String USER_PERMISSION =
"SELECT IDMenue FROM gsUserMenu WHERE userid=? ORDER BY IDMenue ASC";
private static final String UPDATE_LOGIN =
"UPDATE gsLogins set ipaddr=?, lastpulse=?,doingwhat=? WHERE userid=?";
private static final String DELETE_LOGIN_RECORD=
"DELETE FROM gsLogins WHERE userid=?";
private static final String LOAD_ANNM_ACC_COMPS =
"SELECT * FROM gsAnnmCompAccess";
private static final String FIND_USER_COMP_ACCESS1 =
"select gsGroupUser.groupid "
+"from gsGroupUser inner join gsGroupCompAccess "
+ "on gsGroupUser.groupid = gsGroupCompAccess.groupid "
+ "where gsGroupUser.userid = ? "
+ "AND (gsGroupCompAccess.componentname=? OR gsGroupCompAccess.componentname ='ALL')";
private static final String FIND_USER_COMP_ACCESS2 =
"select gsGroupUser.groupid "
+"from gsGroupUser inner join iofficegroupcompaccess "
+ "on gsGroupUser.groupid = iofficegroupcompaccess.groupid "
+ "where gsGroupUser.userid = ? "
+ "AND (gsGroupCompAccess.componentname=? OR gsGroupCompAccess.componentname ='ALL' OR gsGroupCompAccess.componentname ='DEFAULTS')";
private static final String INSERT_GROUP_COMP_ACCESS =
"INSERT INTO gsGroupCompAccess(componentname, groupid) VALUES(?,?)";
private static final String DELETE_GROUP_COMP_ACCESS =
"DELETE FROM gsGroupCompAccess WHERE componentName = ? AND groupID=? ";
private static final String CHECK_GROUP_COMP_ACCESS =
"SELECT groupid FROM gsGroupCompAccess WHERE componentname = ? AND groupid=? ";
protected DbCacheManager cacheManager;
private ProfileManager profileManager;
private DbComponentManager compManager = null;
private Hashtable permNameToTypeTable=null;
private Hashtable permTypeToNameTable=null;
private ArrayList annmComp=null;
/** Creates new DbIofficeFactory */
public DbIofficeFactory() {
cacheManager = new DbCacheManager();
profileManager = new DbProfileManager(this);
getMaxPermissionID();
loadPermNameTable();
//clearTooOldNotifications();
loadAnnmCompAccessTable();
compManager = new DbComponentManager( this );
boolean isDebug = false;
String isDebugStr = PropertyManager.getProperty("isDebug");
if(isDebugStr != null)
{
isDebug = Boolean.getBoolean(isDebugStr);
}
}
public void refreshPerm(){
getMaxPermissionID();
loadPermNameTable();
}
private void loadAnnmCompAccessTable()
{
Connection con = null;
PreparedStatement pstmt = null;
ArrayList list = new ArrayList();
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(LOAD_ANNM_ACC_COMPS);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
list.add( rs.getString("componentname"));
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
annmComp = list;
}
public ComponentManager getComponentManager ()
{
return compManager;
}
public ProfileManager getProfileManager() {
return profileManager;
}
public DbCacheManager getCacheManager() {
return cacheManager;
}
private void loadPermNameTable()
{
Connection con = null;
PreparedStatement pstmt = null;
Hashtable mytable = new Hashtable(50);
Hashtable mytable2 = new Hashtable(50);
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_PERM_INFO);
ResultSet rs = pstmt.executeQuery();
while (rs.next()) {
String name = rs.getString("Name");
int permtype = rs.getInt("IDMenue");
mytable.put( name, new Integer(permtype) );
mytable2.put( new Integer(permtype) , new String(name) );
}
this.permNameToTypeTable = mytable;
this.permTypeToNameTable = mytable2;
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
}
public int[] usersWithPermission(int permissionType)
throws UnauthorizedException
{
int [] users = new int[0];
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(USERS_WITH_PERM);
pstmt.setInt(1,permissionType);
ResultSet rs = pstmt.executeQuery();
ArrayList userList = new ArrayList();
while (rs.next()) {
userList.add(new Integer(rs.getInt("userid")));
}
users = new int[userList.size()];
for (int i=0; i<users.length; i++) {
users[i] = ((Integer)userList.get(i)).intValue();
}
}
catch( SQLException sqle ) {
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return users;
}
public int[] groupsWithPermission(int permissionType)
throws UnauthorizedException
{
int [] groups = new int[0];
Connection con = null;
PreparedStatement pstmt = null;
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GROUPS_WITH_PERM);
pstmt.setInt(1,permissionType);
ResultSet rs = pstmt.executeQuery();
ArrayList groupList = new ArrayList();
while (rs.next()) {
groupList.add(new Integer(rs.getInt("groupid")));
}
groups = new int[groupList.size()];
for (int i=0; i<groups.length; i++) {
groups[i] = ((Integer)groupList.get(i)).intValue();
}
}
catch( SQLException sqle ) {
System.err.println("Error in DbIofficeFactory.groupsWithPermission:" + sqle);
sqle.printStackTrace();
}
finally {
try { pstmt.close(); }
catch (Exception e) { e.printStackTrace(); }
try { con.close(); }
catch (Exception e) { e.printStackTrace(); }
}
return groups;
}
public IofficePermissions getPermissions(Authorization authorization) {
int userID = authorization.getUserID();
return getFinalPermissions( userID) ;
}
public boolean testUserFinalPermission( int userID, int permType )
{
IofficePermissions perms = getFinalPermissions( userID);
return perms.get( permType );
}
private IofficePermissions getFinalPermissions(int userID) {
Cache userPermCache = cacheManager.getCache( DbCacheManager.USER_PERMS_CACHE );
IofficePermissions permissions =(IofficePermissions) userPermCache.get( new Integer(userID) );
if (permissions != null) {
return permissions;
}
//Not so simple case: cache is not turned on or the user permissions
//have not been cached yet.
boolean isAnonymous = (userID == -1);
boolean isUser = !isAnonymous;
IofficePermissions finalPermissions = IofficePermissions.none();
//Step 1 - Get permissions for the User. This includes anonymous
//perms, "special user" perms, and the specific perms for the user.
if (isUser) {
IofficePermissions userPermissions = getUserPermissions(userID);
//Combine permissions
finalPermissions = new IofficePermissions(finalPermissions, userPermissions);
}
//Add in anonymous perms.
IofficePermissions anonyPermissions = null;
anonyPermissions = (IofficePermissions)userPermCache.get(new Integer(-1));
//Otherwise, do our own lookup.
if (anonyPermissions == null) {
anonyPermissions = getUserPermissions(-1);
//Add to cache so it will be there next time.
userPermCache.add(new Integer(-1), anonyPermissions);
}
//Combine permissions
finalPermissions = new IofficePermissions(finalPermissions, anonyPermissions);
//If they are a valid user, figure out "any user" permissions.
if (isUser) {
IofficePermissions specialUserPermissions = null;
//Check for cache
specialUserPermissions = (IofficePermissions)userPermCache.get(new Integer(0));
//Otherwise, do our own lookup.
if (specialUserPermissions == null) {
specialUserPermissions = getUserPermissions(0);
//Add to cache so it will be there next time.
userPermCache.add(new Integer(0), specialUserPermissions);
}
//Combine permissions
finalPermissions = new IofficePermissions(finalPermissions, specialUserPermissions);
}
//Step 2 -- get Permissions for all groups the user is in.
int [] groups = ((DbProfileManager)getProfileManager()).getUserGroups(userID);
for (int i=0; i<groups.length; i++) {
IofficePermissions groupPermissions = getGroupPermissions(groups[i]);
finalPermissions = new IofficePermissions(finalPermissions, groupPermissions);
}
//Finally, add user to cache so it will be there next time.
if (isUser && userPermCache != null) {
userPermCache.add(new Integer(userID), finalPermissions);
}
return finalPermissions;
}
public boolean hasPermission(int type) {
return true;
}
protected IofficePermissions getUserPermissions(int userID ) {
Connection con = null;
PreparedStatement pstmt = null;
//Initialize a permissions array with no permissions.
boolean [] permissions = new boolean[IofficePermissions.MAX_PREV_NUM];
for (int i=0; i<permissions.length; i++) {
permissions[i] = false;
}
try {
con = DbConnectionManager.getConnection();
pstmt = con.prepareStatement(GET_USER_PERMS);
pstmt.setInt(1, userID);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
int newPerm = rs.getInt("IDMenue");
permissions[newPerm] = true;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -