1 package org.jahia.services.usermanager; 2 3 import org.jahia.exceptions.JahiaException; 4 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 5 import org.jahia.exceptions.database.JahiaDatabaseException; 6 import org.jahia.registries.ServicesRegistry; 7 8 import java.security.Principal ; 9 import java.util.*; 10 11 12 21 22 public class JahiaLDAPGroup extends JahiaGroup { 23 24 static final public String GROUPKEY_LDAP_PREFIX = "{ldap}"; 25 26 27 protected int mID; 28 29 30 protected static int mUSERTYPE = 1; 31 32 33 protected static int mGROUPTYPE = 2; 34 35 36 private static final String mHOMEPAGE_PROP = "group_homepage"; 37 38 39 private Properties mProperties = new Properties (); 40 41 private boolean dynamic; 43 44 private boolean preloadedGroups; 45 46 private Set notMembers = new HashSet(); 47 48 49 59 protected JahiaLDAPGroup(int id, String groupname, String groupKey, int siteID, 60 Hashtable members, Properties properties, boolean dynamic, boolean preloadedGroups) 61 throws JahiaException { 62 ServicesRegistry registry = ServicesRegistry.getInstance (); 63 if (registry == null) { 64 throw new JahiaException ("Jahia Internal Error", 65 "JahiaLDAPGroup Could not get the Service Registry instance", 66 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 67 } 68 69 mID = id; 70 mGroupname = groupname; 71 mGroupKey = GROUPKEY_LDAP_PREFIX + groupKey; 72 mSiteID = siteID; 73 74 if (members != null) { 75 mMembers = members; 76 } 77 78 if (properties != null) { 79 mProperties = properties; 80 } 81 this.dynamic = dynamic; 82 this.preloadedGroups = preloadedGroups; 83 } 84 85 86 92 93 public int getHomepageID () { 94 if (mProperties != null) { 95 96 try { 97 String value = mProperties.getProperty (mHOMEPAGE_PROP); 100 101 if (value == null) 102 return -1; 103 return Integer.parseInt (value); 104 } catch (Throwable t) { 105 t.printStackTrace (); 106 } 107 } 108 return -1; 109 110 } 111 112 public boolean setHomepageID (int id) { 113 if (!removeProperty (mHOMEPAGE_PROP)) 114 return false; 115 return setProperty (mHOMEPAGE_PROP, String.valueOf (id)); 118 119 } 120 121 public synchronized boolean removeProperty (String key) { 122 boolean result = false; 123 124 if ((key != null) && (key.length () > 0)) { 125 JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance (); 127 if (utils != null) { 128 try { 129 result = utils.removeProperty (key, -1, getProviderName (), getGroupKey ()); 130 } 131 catch (JahiaDatabaseException ex) { 133 } 135 136 catch (JahiaDatabaseConnectionException ex) { 138 } 140 141 catch (JahiaException ex) { 143 } 145 } 146 } 147 148 if (result) { 149 mProperties.remove (key); 150 } 151 return result; 153 154 } 155 156 public Properties getProperties () { 157 return mProperties; 158 } 159 160 public boolean removeMember (Principal user) { 161 162 return false; 163 } 164 165 public String getProperty (String key) { 166 if ((mProperties != null) && (key != null)) { 167 return mProperties.getProperty (key); 168 } 169 return null; 170 171 } 172 173 public boolean addMember (Principal user) { 174 175 return false; 176 } 177 178 public synchronized boolean setProperty (String key, String value) { 179 boolean result = false; 180 181 if ((key != null) && (value != null)) { 182 JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance (); 184 if (utils != null) { 185 try { 186 if (getProperty (key) == null) { 187 result = 188 utils.addProperty (key, value, -1, getProviderName (), 189 getGroupKey ()); 190 } else { 191 result = 192 utils.updateProperty (key, value, -1, getProviderName (), 193 getGroupKey ()); 194 } 195 } 196 catch (JahiaDatabaseException ex) { 198 } 200 201 catch (JahiaDatabaseConnectionException ex) { 203 } 205 206 catch (JahiaException ex) { 208 } 210 } 211 if (result) { 213 mProperties.setProperty (key, value); 214 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this); 215 } 216 } 217 return result; 218 219 } 220 221 public String getProviderName () { 222 return JahiaGroupManagerLDAPProvider.PROVIDER_NAME; 223 } 224 225 public boolean isDynamic() { 226 return dynamic; 227 } 228 229 public String toString () { 230 231 StringBuffer output = new StringBuffer ("Details of group [" + mGroupname + "] :\n"); 232 233 output.append (" - ID : " + Integer.toString (mID) + "\n"); 234 235 output.append (" - properties :"); 236 237 Enumeration names = mProperties.propertyNames (); 238 String name; 239 if (names.hasMoreElements ()) { 240 output.append ("\n"); 241 while (names.hasMoreElements ()) { 242 name = (String ) names.nextElement (); 243 output.append ( 244 " " + name + " -> [" + (String ) mProperties.getProperty (name) + "]\n"); 245 } 246 } else { 247 output.append (" -no properties-\n"); 248 } 249 250 output.append (" - members : "); 252 names = mMembers.keys (); 253 if (names.hasMoreElements ()) { 254 while (names.hasMoreElements ()) { 255 output.append (names.nextElement () + "/"); 256 } 257 } else { 258 output.append (" -no members-\n"); 259 } 260 261 return output.toString (); 262 } 263 264 public boolean equals (Object another) { 265 if (another instanceof Principal ) { 266 if (another != null) { 267 return (getName ().equals (((Principal ) another).getName ())); 268 } 269 } 270 return false; 271 } 272 273 public int hashCode () { 275 return mID; 276 } 277 278 279 public void setSiteID (int id) { 280 mSiteID = id; 281 } 282 283 public boolean isMember(Principal principal) { 284 if (super.isMember(principal)) { 285 return true; 286 } 287 if (!preloadedGroups && principal instanceof JahiaUser) { 288 if (notMembers.contains(principal.getName())) { 289 return false; 290 } 291 if (JahiaGroupManagerLDAPProvider.getInstance().getUserMembership((JahiaUser)principal).contains(getGroupKey())) { 292 mMembers.put(principal.getName(), principal); 293 return true; 294 } else { 295 notMembers.add(principal.getName()); 296 } 297 } 298 return false; 299 } 300 } 301 | Popular Tags |