📄 acldbutils.java
字号:
// $Id: AclDBUtils.java,v 1.8 2002/07/05 17:42:09 pmartin Exp $//// ____.// __/\ ______| |__/\. _______// __ .____| | \ | +----+ \// _______| /--| | | - \ _ | : - \_________// \\______: :---| : : | : | \________>// |__\---\_____________:______: :____|____:_____\// /_____|//// . . . i n j a h i a w e t r u s t . . .//package org.jahia.services.acl;import java.sql.*;import java.util.*;import org.jahia.data.JahiaDBDOMObject;import org.jahia.data.JahiaDOMObject;import org.jahia.data.cache.JahiaSimpleCache;import org.jahia.exceptions.JahiaException;import org.jahia.exceptions.database.JahiaDatabaseException;import org.jahia.registries.ServicesRegistry;import org.jahia.services.cache.JahiaCacheFactory;import org.jahia.utils.DBRowDataFilter;import org.jahia.utils.JahiaConsole;/** * This is a static class providing the necessary database utilities used by * the ACL Manager Service. This class is a singelton and only one instance * can be obtained throught the <code>getInstance()</code> method. * * @author Fulco Houkes * @author MAP * @version 1.1 */class AclDBUtils implements ACLInfo{ /** Jahia acl database table name */ private static String JAHIA_ACL = "jahia_acl"; /** acl ID field name */ private static String FIELD_ACL_ID = "id_jahia_acl"; /** acl parent ID field name */ private static String FIELD_PARENT_ID = "parent_id_jahia_acl"; /** ACL inheritance field name */ private static String FIELD_INHERITANCE = "inheritance_jahia_acl"; /** Jahia acl entry database table name */ private static String JAHIA_ACL_ENTRIES = "jahia_acl_entries"; /** ACL ID column name of the jahia acl entries table */ private static String FIELD_ENTRY_ACL_ID = "id_jahia_acl"; /** entry target type column name of the jahia acl entries table */ private static String FIELD_ENTRY_TYPE = "type_jahia_acl_entries"; /** entry target key column name of the jahia acl entries table */ private static String FIELD_ENTRY_TARGET = "target_jahia_acl_entries"; /** entry state column name of the jahia acl entries table */ private static String FIELD_ENTRY_STATE = "entry_state_jahia_acl_entries"; /** entry tristate column name of the jahia acl entries table */ private static String FIELD_ENTRY_TRISTATE = "entry_trist_jahia_acl_entries"; /** Reference of the unique instance of this object */ static private AclDBUtils mObject = null; JahiaSimpleCache cacheACLEntries = null; //------------------------------------------------------------------------- // FH 4 Apr. 2001 /** * Default constructor * * @throws JahiaDatabaseException */ private AclDBUtils () throws JahiaDatabaseException { // THE CACHE _MUST_ CACHE AN _UNLIMITED_ NUMBER OF ENTRIES!!! cacheACLEntries = JahiaCacheFactory.getInstance().createJahiaSimpleCache ("ACLEntriesCache","ACL Entries DB cache",JahiaSimpleCache.UNLIMITED); cacheAllACLEntries(); } //------------------------------------------------------------------------- // FH 4 Apr. 2001 /** * Return the unique instance of this class. * * @return * Return the unique instance of this class. */ static public AclDBUtils getInstance () { try { if (mObject == null) { mObject = new AclDBUtils (); } } catch (JahiaDatabaseException e) { JahiaConsole.println ("AclDBUtils", "!!!!!!! Problem reading ACL entries from database!"); } return mObject; } //------------------------------------------------------------------------- // DJ 06/05/02 /** * Read all ACLs from the database and construct the JahiaACL tree structure. * * @return * All Acls from the database * @throws JahiaException */ public Hashtable readAllACLs() throws JahiaException { Hashtable allACLs = new Hashtable(); // Get a database connection Connection dbConn = getDBConnection (); if (dbConn == null) { return allACLs; } Statement statement = null; StringBuffer query = new StringBuffer (); try { // get aclentriesID statement = dbConn.createStatement(); query.append ("SELECT * "); query.append ("FROM "); query.append (JAHIA_ACL); query.append (" ORDER BY "); query.append (FIELD_PARENT_ID); query.append (", "); query.append (FIELD_ACL_ID); ResultSet rs = ServicesRegistry.getInstance().getDBPoolService().executeQuery (statement, query.toString()); // Cache all ACL if possible, otherwise construct relicat Hashtable relicatACL = new Hashtable(); if (rs != null) { while (rs.next()) { int aclID = rs.getInt(FIELD_ACL_ID); int parentID = rs.getInt(FIELD_PARENT_ID); JahiaACL parentACL; // Find the parent if exists. if (allACLs.get(new Integer(parentID)) == null && parentID > 0) { // Parent actually not in JahiaACL parentACL = null; relicatACL.put(new Integer(aclID), new Integer(parentID)); } else { // Parent is in JahiaACL parentACL = parentID <= 0 ? null : (JahiaACL)allACLs.get(new Integer(parentID)); } JahiaACL acl = new JahiaACL(aclID, parentACL, rs.getInt(FIELD_INHERITANCE)); acl.setUserEntries(getACLEntries(aclID, USER_TYPE_ENTRY)); acl.setGroupEntries(getACLEntries(aclID, GROUP_TYPE_ENTRY)); allACLs.put(new Integer(aclID), acl); } } // Let look about relicat while (!relicatACL.isEmpty()) { Enumeration enum = relicatACL.keys(); int rest = relicatACL.size(); while (enum.hasMoreElements()) { Integer aclID = (Integer)enum.nextElement(); Integer parentID = (Integer)relicatACL.get(aclID); JahiaACL parentACL = (JahiaACL)allACLs.get(parentID); // The parent exists now if (parentACL != null) { ((JahiaACL)allACLs.get(aclID)).setParentACL(parentACL, false); relicatACL.remove(aclID); } // Test if some ACL are orpheans if (!enum.hasMoreElements() && rest == relicatACL.size()) { relicatACL.remove(aclID); JahiaConsole.println("AclDBUtils.readAllACLs", "WARNING !!! Parent ACL #" + parentID.toString() + " does not exist for ACL #" + aclID.toString()); } } } relicatACL = null; } catch (SQLException ex) { // no exception, the acls are simply not cached... JahiaConsole.println ("AclDBUtils.readAllACLs", "!!! DB error, can't cache acls : "+ex); } finally { query = null; closeDBConnection (dbConn); closeStatement (statement); } return allACLs; } //------------------------------------------------------------------------- // FH 4 Apr. 2001 /** * Add an ACL entry in the database. * * @param aclID * ACL's indentification number. * @param typeID * Entry type. See constants defined int <code>ACLInfo</code>. * @param targetID * Identification of the target to which the entry has to be applied. * Basically this is the user or group identification key or the IP * number. * @param state * ACL entry permissions state. * @param tristate * ACL entry permissions tristate. * * @return * Return <code>true</code> on success. * * @exception JahiaDatabaseException * Throws this error on any database failure. */ public boolean addACLEntry (int aclID, int typeID, String targetID, int state, int tristate) throws JahiaDatabaseException { StringBuffer query = new StringBuffer (); query.append ("INSERT INTO "); query.append (JAHIA_ACL_ENTRIES); query.append (" ("); query.append (FIELD_ENTRY_ACL_ID); query.append (","); query.append (FIELD_ENTRY_TYPE); query.append (","); query.append (FIELD_ENTRY_TARGET); query.append (","+FIELD_ENTRY_STATE); query.append (","); query.append (FIELD_ENTRY_TRISTATE); query.append (")"); query.append (" VALUES ("); query.append (aclID); query.append (","); query.append (typeID); query.append (",'"); query.append (targetID); query.append ("',"); query.append (state); query.append (","); query.append (tristate); query.append (")"); boolean result = makeQuery (query.toString()); if (result) { JahiaACLEntry entry = new JahiaACLEntry (state, tristate); Hashtable cachedType = null; Hashtable cachedACL = (Hashtable)cacheACLEntries.getValue(new Integer(aclID)); if (cachedACL==null) { cachedACL = new Hashtable(); cacheACLEntries.setValue(cachedACL, new Integer(aclID)); } cachedType=(Hashtable)cachedACL.get(new Integer(typeID)); if (cachedType == null) { cachedType = new Hashtable(); cachedACL.put(new Integer(typeID), cachedType); } cachedType.put (targetID, entry); } query = null; return result; } //------------------------------------------------------------------------- // FH 4 Apr. 2001 /** * Add an ACL entry into the database. * * @param aclID * ACL's indentification number. * @param typeID * Entry type. See constants defined int <code>ACLInfo</code>. * @param targetID * Identification of the target to which the entry has to be applied. * Basically this is the user or group identification key or the IP * number. * @param state * ACL entry permissions state. * @param tristate * ACL entry permissions tristate. * * @return * Return <code>true</code> on success. * * @exception JahiaDatabaseException * Throws this error on any database failure. */ public boolean updateACLEntry (int aclID, int typeID, String targetID, int state, int tristate) throws JahiaDatabaseException { StringBuffer query = new StringBuffer ("UPDATE "); query.append (JAHIA_ACL_ENTRIES); query.append (" SET "); query.append (FIELD_ENTRY_STATE); query.append ("="); query.append (state); query.append (","); query.append (FIELD_ENTRY_TRISTATE); query.append ("="); query.append (tristate); query.append (" WHERE (("); query.append (FIELD_ENTRY_ACL_ID); query.append ("="); query.append (aclID); query.append (")"); query.append (" AND ("); query.append (FIELD_ENTRY_TYPE); query.append ("="); query.append (typeID); query.append (")"); query.append (" AND ("); query.append (FIELD_ENTRY_TARGET); query.append ("='"); query.append (targetID); query.append ("'))"); boolean result = makeQuery (query.toString()); if (result) { JahiaACLEntry entry = new JahiaACLEntry (state, tristate); Hashtable cachedType = null; Hashtable cachedACL = (Hashtable)cacheACLEntries.getValue(new Integer(aclID)); if (cachedACL==null) { cachedACL = new Hashtable(); cacheACLEntries.setValue(cachedACL, new Integer(aclID)); } cachedType=(Hashtable)cachedACL.get(new Integer(typeID)); if (cachedType == null) { cachedType = new Hashtable(); cachedACL.put(new Integer(typeID), cachedType); } cachedType.put (targetID, entry); } query = null; return result; } //------------------------------------------------------------------------- // FH 4 Apr. 2001 /** * Remove the specified ACL entry associated with the specified ACL and * target IDs from the database. * * @param aclID * ACL's indentification number. * @param typeID * Entry type. See constants defined int <code>ACLInfo</code>. * @param targetID * Identification of the target to which the entry has to be applied. * Basically this is the user or group identification key or the IP * number. * * @return * Return <code>true</code> on success. * * @exception JahiaDatabaseException * Throws this error on any database failure. */ public boolean removeACLEntry (int aclID, int typeID, String targetID) throws JahiaDatabaseException { // only do database access if the acl exists in cache, else it shouldn't // exist in the database anyway! Hashtable cachedType = null; Hashtable cachedACL = (Hashtable)cacheACLEntries.getValue(new Integer(aclID)); boolean result = true; if (cachedACL!=null) { cachedType=(Hashtable)cachedACL.get(new Integer(typeID)); if (cachedType != null) { StringBuffer query = new StringBuffer ("DELETE FROM "); query.append (JAHIA_ACL_ENTRIES); query.append (" WHERE "); query.append (FIELD_ENTRY_ACL_ID); query.append ("="); query.append (aclID); query.append (" AND "); query.append (FIELD_ENTRY_TYPE); query.append ("="); query.append (typeID); query.append (" AND "); query.append (FIELD_ENTRY_TARGET); query.append ("='"); query.append (targetID); query.append ("'"); result = makeQuery (query.toString());
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -