1 22 package org.jahia.services.usermanager; 23 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.exceptions.database.JahiaDatabaseConnectionException; 26 import org.jahia.exceptions.database.JahiaDatabaseException; 27 import org.jahia.registries.ServicesRegistry; 28 29 import java.security.Principal ; 30 import java.security.acl.Group ; 31 import java.sql.Connection ; 32 import java.sql.SQLException ; 33 import java.sql.Statement ; 34 import java.util.Enumeration ; 35 import java.util.Hashtable ; 36 import java.util.Properties ; 37 38 44 class JahiaDBGroup extends JahiaGroup { 45 46 protected int mID; 47 48 49 protected static int mUSERTYPE = 1; 50 51 52 protected static int mGROUPTYPE = 2; 53 54 55 private static final String mHOMEPAGE_PROP = "group_homepage"; 56 57 58 private Properties mProperties = new Properties (); 59 60 73 protected JahiaDBGroup (int id, String groupname, String groupKey, int siteID, 74 Hashtable members, Properties properties) 75 throws JahiaException { 76 ServicesRegistry registry = ServicesRegistry.getInstance (); 77 if (registry == null) { 78 throw new JahiaException ("Jahia Internal Error", 79 "JahiaDBGroup Could not get the Service Registry instance", 80 JahiaException.SERVICE_ERROR, JahiaException.CRITICAL_SEVERITY); 81 } 82 83 mID = id; 84 mGroupname = groupname; 85 mGroupKey = groupKey; 86 mSiteID = siteID; 87 88 if (members != null) { 89 mMembers = members; 90 } 91 92 if (properties != null) { 93 mProperties = properties; 94 } 95 96 } 97 98 99 public boolean addMember (Principal principal) { 101 102 boolean result = false; 103 104 if (principal != null) { 105 if (!isMember (principal)) { 106 try { 107 108 String query = null; 109 110 if (principal instanceof Group ) { 111 112 query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)" + 113 " VALUES ('" + ((JahiaGroup) principal).getName () + 114 "','" + this.getName () + "'," + mGROUPTYPE + ")"; 115 } else { 116 117 query = "INSERT INTO jahia_grp_access (id_jahia_member,id_jahia_grps,membertype_grp_access)" + 118 " VALUES ('" + ((JahiaUser) principal).getName () + 119 "','" + this.getName () + "'," + mUSERTYPE + ")"; 120 } 121 122 if (makeQuery (query)) { 123 mMembers.put (principal.getName (), principal); 124 result = true; 125 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this); 126 } 127 } catch (JahiaDatabaseConnectionException ex) { 128 } 130 } 131 } 132 return result; 133 } 134 135 136 142 public int getGroupID () { 143 return mID; 144 } 145 146 153 public int getHomepageID () { 154 155 if (mProperties != null) { 156 157 try { 158 String value = mProperties.getProperty (mHOMEPAGE_PROP); 159 if (value == null) 160 return -1; 161 return Integer.parseInt (value); 162 } catch (Throwable t) { 163 t.printStackTrace (); 164 } 165 } 166 return -1; 167 } 168 169 177 public boolean setHomepageID (int id) { 178 179 183 return setProperty (mHOMEPAGE_PROP, String.valueOf (id)); 184 } 185 186 public Properties getProperties () { 188 return mProperties; 189 } 190 191 192 public String getProperty (String key) { 194 195 if ((mProperties != null) && (key != null)) { 196 return mProperties.getProperty (key); 197 } 198 return null; 199 } 200 201 209 public synchronized boolean removeProperty (String key) { 210 boolean result = false; 211 212 if ((key != null) && (key.length () > 0)) { 213 JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance (); 214 if (utils != null) { 215 try { 216 result = utils.removeProperty (key, mID, getProviderName (), getGroupKey ()); 217 } 218 catch (JahiaDatabaseException ex) { 220 } 222 223 catch (JahiaDatabaseConnectionException ex) { 225 } 227 228 catch (JahiaException ex) { 230 } 232 } 233 } 234 235 if (result) { 236 mProperties.remove (key); 237 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this); 238 } 239 240 return result; 241 } 242 243 253 public synchronized boolean setProperty (String key, String value) { 254 boolean result = false; 255 256 if ((key != null) && (value != null)) { 257 258 JahiaGroupDBUtils utils = JahiaGroupDBUtils.getInstance (); 259 if (utils != null) { 260 try { 261 if (getProperty (key) == null) { 262 result = 263 utils.addProperty (key, value, mID, getProviderName (), 264 getGroupKey ()); 265 } else { 266 result = 267 utils.updateProperty (key, value, mID, getProviderName (), 268 getGroupKey ()); 269 } 270 } 271 catch (JahiaDatabaseException ex) { 273 } 275 276 catch (JahiaDatabaseConnectionException ex) { 278 } 280 281 catch (JahiaException ex) { 283 } 285 } 286 287 if (result) { 288 mProperties.setProperty (key, value); 289 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this); 290 } 291 } 292 return result; 293 } 294 295 296 public boolean equals (Object another) { 297 if (another instanceof Principal ) { 298 if (another != null) { 299 return (getName ().equals (((Principal ) another).getName ())); 300 } 301 } 302 return false; 303 } 304 305 public int hashCode () { 307 return ("group" + mID).hashCode (); 308 } 309 310 311 public boolean removeMember (Principal principal) { 313 314 boolean result = false; 315 if (principal != null) { 316 317 String query = ""; 318 if (principal instanceof Group ) { 319 320 query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='" + 321 ((JahiaDBGroup) principal).getName () + 322 "' AND id_jahia_grps='" + this.getName () + 323 "' AND membertype_grp_access=" + mGROUPTYPE; 324 } else { 325 326 if ((((JahiaUser) principal).isRoot ()) && 327 (getGroupname ().equals ( 328 JahiaGroupManagerService.ADMINISTRATORS_GROUPNAME))) { 329 return false; 330 } 331 332 query = "DELETE FROM jahia_grp_access WHERE id_jahia_member='" + 333 ((JahiaUser) principal).getName () + 334 "' AND id_jahia_grps='" + this.getName () + 335 "' AND membertype_grp_access=" + mUSERTYPE; 336 } 337 338 try { 339 if (makeQuery (query)) { 340 mMembers.remove (principal.getName ()); 341 result = true; 342 ServicesRegistry.getInstance().getJahiaGroupManagerService().updateCache(this); 343 } 344 } catch (JahiaDatabaseConnectionException ex) { 345 } 347 } 348 return result; 349 } 350 351 352 public String toString () { 354 355 StringBuffer output = new StringBuffer ("Details of group [" + mGroupname + "] :\n"); 356 357 output.append (" - ID : " + Integer.toString (mID) + "\n"); 358 359 output.append (" - properties :"); 360 361 Enumeration names = mProperties.propertyNames (); 362 String name; 363 if (names.hasMoreElements ()) { 364 output.append ("\n"); 365 while (names.hasMoreElements ()) { 366 name = (String ) names.nextElement (); 367 output.append ( 368 " " + name + " -> [" + (String ) mProperties.getProperty (name) + "]\n"); 369 } 370 } else { 371 output.append (" -no properties-\n"); 372 } 373 374 output.append (" - members : "); 376 names = mMembers.keys (); 377 if (names.hasMoreElements ()) { 378 while (names.hasMoreElements ()) { 379 output.append (names.nextElement () + "/"); 380 } 381 } else { 382 output.append (" -no members-\n"); 383 } 384 385 return output.toString (); 386 } 387 388 389 398 private boolean makeQuery (String query) 399 throws JahiaDatabaseConnectionException { 400 boolean result = true; 401 Connection dbConn = null; 402 Statement statement = null; 403 404 try { 405 dbConn = org.jahia.services.database.ConnectionDispenser.getConnection (); 406 if (dbConn == null) { 407 throw new JahiaDatabaseConnectionException ( 408 "JahiaDBGroup could not get a DB connection"); 409 } 410 411 statement = dbConn.createStatement (); 412 synchronized (this) { 413 statement.executeUpdate (query); 414 } 415 } catch (SQLException sqlEx) { 416 result = false; 418 } finally { 419 try { 420 if (statement != null) { 421 statement.close (); 422 } 423 424 } catch (SQLException sqlEx) { 425 result = false; 427 } 428 } 429 430 return result; 431 } 432 433 438 public String getProviderName () { 439 return JahiaGroupManagerDBProvider.PROVIDER_NAME; 440 } 441 442 } 443 | Popular Tags |