📄 jahiausermanagerdbprovider.java
字号:
// Get a database connection Connection dbConn = getDBConnection (1000); if (dbConn == null) { return result; } // execute the SELECT query Statement statement = null; StringBuffer query = new StringBuffer (); try { statement = dbConn.createStatement(); if (statement != null) { // Construct the database query query.append ("SELECT "); query.append (FIELD_USER_NAME_USERS); query.append (" FROM "); query.append (JAHIA_USERS); query.append (" WHERE "); query.append (FIELD_SITE_ID_USERS); query.append ("="); query.append (siteID); ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (statement,query.toString()); if (rs != null) { while (rs.next()) { String name = rs.getString (FIELD_USER_NAME_USERS); if (name != null) { result.add (name); } } } // result set not needed anymore. rs = null; } } catch (SQLException ex) { toConsole ("SQL Exception occured!"); // FIXME -Fulco- : Don't know yet what to do with this exception. // It should be logged somewhere ! } finally { query = null; closeDBConnection (dbConn); closeStatement (statement); } return result; } //------------------------------------------------------------------------- /** * This method returns the list of this site's users' keys. * * @return Return a vector of strings holding the user identification key . */ public Vector getUserList (int siteID){ Vector result = new Vector(); // Get a database connection Connection dbConn = getDBConnection (1000); if (dbConn == null) { return result; } // execute the SELECT query Statement statement = null; try { statement = dbConn.createStatement(); if (statement != null) { // Get the basic user data String query = "SELECT key_jahia_users FROM jahia_users WHERE siteid_jahia_users=" + siteID; ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(statement,query); if (rs != null) { while (rs.next()) { String name = rs.getString ("key_jahia_users"); if (name != null) { result.add (name); } } } } } catch (SQLException ex) { toConsole ("SQL Exception occured!"); // FIXME -Fulco- : Don't know yet what to do with this exception. // It should be logged somewhere ! } finally { closeDBConnection (dbConn); closeStatement (statement); } return result; } //------------------------------------------------------------------------- /** * This method return all users' keys in the system. * * @return Return a vector of strings holding the user identification key . */ public Vector getUserList (){ Vector result = new Vector(); // Get a database connection Connection dbConn = getDBConnection (1000); if (dbConn == null) { return result; } // execute the SELECT query Statement statement = null; try { statement = dbConn.createStatement(); if (statement != null) { // Get the basic user data String query = "SELECT key_jahia_users FROM jahia_users "; ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(statement,query); if (rs != null) { while (rs.next()) { String name = rs.getString ("key_jahia_users"); if (name != null) { result.add (name); } } } } } catch (SQLException ex) { toConsole ("SQL Exception occured!"); // FIXME -Fulco- : Don't know yet what to do with this exception. // It should be logged somewhere ! } finally { closeDBConnection (dbConn); closeStatement (statement); } return result; } /** * Transforms a search with "*" characters into a valid LIKE statement * with "%" characters. Also escapes the string to remove all "'" and * other chars that might disturb the request construct. * @param input the original String * @return String a resulting string that has */ private String makeLIKEString(String input) { String result = JahiaTools.replacePattern(input, "*", "%"); result = JahiaTools.replacePattern(result, "'", "\\'"); result = JahiaTools.replacePattern(result, "\"", "\\\""); result = JahiaTools.replacePattern(result, "_", "\\_"); return result; } /** * Find users according to a table of name=value properties. If the left * side value is "*" for a property then it will be tested against all the * properties. ie *=test* will match every property that starts with "test" * @param siteID site identifier * @param searchCriterias a Properties object that contains search criterias * in the format name,value (for example "*"="*" or "username"="*test*") or * null to search without criterias * @return Set a set of JahiaUser elements that correspond to those * search criterias * @todo this code could be cleaner if username was a real user property * but as it isn't we have to do a lot of custom handling. */ public Set searchUsers(int siteID, Properties searchCriterias) { /** @todo implement siteID into SQL request */ Set result = new HashSet(); Set userKeys = new HashSet(); // Get a database connection Connection dbConn = getDBConnection (1000); if (dbConn == null) { return result; } if (searchCriterias == null) { searchCriterias = new Properties(); searchCriterias.setProperty("*", "*"); } boolean haveWildCardProperty = false; if (searchCriterias.getProperty("*") != null) { haveWildCardProperty = true; } // execute the SELECT query Statement statement = null; try { statement = dbConn.createStatement(); if (statement != null) { StringBuffer query; boolean onlyUserNameInSelect = false; if ((searchCriterias.getProperty(USERNAME_PROPERTY_NAME) != null) || (haveWildCardProperty) ){ String curCriteriaValue; if (haveWildCardProperty) { curCriteriaValue = makeLIKEString(searchCriterias.getProperty("*")); } else { curCriteriaValue = makeLIKEString(searchCriterias.getProperty(USERNAME_PROPERTY_NAME)); } query = new StringBuffer("SELECT DISTINCT key_jahia_users AS result_key_jahia_users FROM jahia_users,jahia_sites_users WHERE "); query.append("name_jahia_users LIKE '"); query.append(curCriteriaValue); query.append("'"); query.append("AND jahia_sites_users.siteid_sites_users="); query.append(siteID); query.append(" AND jahia_users.key_jahia_users=jahia_sites_users.userid_sites_users"); // JahiaConsole.println("JahiaUserManagerDBProvider.searchUsers", "Executing query [" + query.toString() + "]"); ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(statement,query.toString()); if (rs != null) { while (rs.next()) { String name = rs.getString ("result_key_jahia_users"); if (name != null) { userKeys.add (name); } } } if ((!haveWildCardProperty) && (searchCriterias.size() == 1)) { onlyUserNameInSelect = true; } } else { onlyUserNameInSelect = false; } if (!onlyUserNameInSelect) { query = new StringBuffer("SELECT DISTINCT jahia_users.key_jahia_users AS result_key_jahia_users FROM jahia_users, jahia_user_prop, jahia_sites_users WHERE "); Enumeration criteriaNames = searchCriterias.keys(); while (criteriaNames.hasMoreElements()) { String curCriteriaName = (String) criteriaNames.nextElement(); String curCriteriaValue = makeLIKEString(searchCriterias.getProperty(curCriteriaName)); if ("*".equals(curCriteriaName)) { // we must look in all columns, including special for // the user. query.append("(jahia_users.name_jahia_users LIKE '"); query.append(curCriteriaValue); query.append("'"); query.append(" OR jahia_user_prop.value_jahia_user_prop LIKE '"); query.append(curCriteriaValue); query.append("') "); query.append(" AND "); onlyUserNameInSelect = false; } else { if (USERNAME_PROPERTY_NAME.equals(curCriteriaName)) { // user name filter is a special case and is not // stored in the property table. } else { query.append("(jahia_user_prop.name_jahia_user_prop='"); query.append(makeLIKEString(curCriteriaName)); query.append("' AND jahia_user_prop.value_jahia_user_prop LIKE '"); query.append(curCriteriaValue); query.append("') "); query.append(" AND "); onlyUserNameInSelect = false; } } } if (!(onlyUserNameInSelect)) { if (!query.toString().endsWith(" AND ")) { query.append(" AND "); } } query.append("jahia_users.id_jahia_users=jahia_user_prop.id_jahia_users AND "); query.append("jahia_sites_users.siteid_sites_users="); query.append(siteID); query.append(" AND jahia_users.key_jahia_users=jahia_sites_users.userid_sites_users"); // JahiaConsole.println("JahiaUserManagerDBProvider.searchUsers", "Executing query [" + query.toString() + "]"); ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery(statement,query.toString()); if (rs != null) { while (rs.next()) { String name = rs.getString ("result_key_jahia_users"); if (name != null) { userKeys.add (name); } } } } } } catch (SQLException ex) { JahiaConsole.printe("JahiaUserManagerDBProvider.searchUsers", ex); } finally { closeDBConnection (dbConn); closeStatement (statement); } // now that we have all the keys, let's load all the users. Iterator userKeyEnum = userKeys.iterator(); while (userKeyEnum.hasNext()) { String curUserKey = (String) userKeyEnum.next(); JahiaUser user = lookupUser(curUserKey); result.add(user); } return result; } //-------------------------------------------------------------------------- /** * Default constructor * * @exception JahiaException The user manager need some Jahia services to be * able to run correctly. If one of these services are not instanciated then a * JahiaException exception is thrown. */ protected JahiaUserManagerDBProvider () throws JahiaException { mUserCache = new Hashtable();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -