1 13 18 package org.jahia.engines.shared; 19 20 import java.security.Principal ; 21 import java.util.ArrayList ; 22 import java.util.Enumeration ; 23 import java.util.HashMap ; 24 import java.util.HashSet ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 import java.util.Map ; 28 import java.util.Properties ; 29 import java.util.Set ; 30 import java.util.Vector ; 31 32 import javax.servlet.http.HttpSession ; 33 34 import org.jahia.data.applications.ApplicationBean; 35 import org.jahia.data.applications.ApplicationContext; 36 import org.jahia.data.containers.JahiaContainer; 37 import org.jahia.data.fields.JahiaField; 38 import org.jahia.engines.JahiaEngine; 39 import org.jahia.engines.JahiaEngineTools; 40 import org.jahia.engines.users.SelectUG_Engine; 41 import org.jahia.exceptions.JahiaException; 42 import org.jahia.params.ParamBean; 43 import org.jahia.registries.ServicesRegistry; 44 import org.jahia.services.acl.JahiaBaseACL; 45 import org.jahia.services.applications.JahiaApplicationsDispatchingService; 46 import org.jahia.services.usermanager.JahiaGroup; 47 import org.jahia.services.usermanager.JahiaGroupManagerService; 48 import org.jahia.services.usermanager.JahiaUser; 49 50 public class Application_Field { 51 52 private static org.apache.log4j.Logger logger = 53 org.apache.log4j.Logger.getLogger(Application_Field.class); 54 55 private static Application_Field theObject = null; 56 private static final String JSP_FILE = "/jsp/jahia/engines/shared/application_field.jsp"; 57 private static final String READONLY_JSP = "/jsp/jahia/engines/shared/readonly_application_field.jsp"; 58 public static final String APPLICATION_ROLES = "applicationRoles"; 59 60 65 public static synchronized Application_Field getInstance() { 66 if (theObject == null) { 67 theObject = new Application_Field(); 68 } 69 return theObject; 70 } 71 72 83 public boolean handleField( ParamBean jParams, Integer modeInt, HashMap engineMap ) 84 throws JahiaException { 85 86 int mode = modeInt.intValue(); 87 JahiaField theField = (JahiaField) engineMap.get( "theField" ); 88 if ( theField == null ){ 89 logger.debug("the field is null"); 90 } 91 switch (mode) { 92 case (JahiaEngine.LOAD_MODE): 93 return composeEngineMap(jParams, engineMap, theField); 94 case (JahiaEngine.UPDATE_MODE): 95 return getFormData(jParams, engineMap, theField); 96 case (JahiaEngine.SAVE_MODE): 97 return saveData(jParams, engineMap, theField); 98 } 99 return false; 100 } 101 102 111 private boolean getFormData( ParamBean jParams, HashMap engineMap, JahiaField theField ) 112 { 113 logger.debug("started"); 114 String fieldValue = jParams.getRequest().getParameter( 115 "_" + new Integer (theField.getID()).toString() ); 116 if (fieldValue != null) { 117 theField.setObject( fieldValue ); 118 } 119 Integer oldAppID = (Integer )engineMap.get("appID"); 120 if ((oldAppID == null) || (oldAppID.intValue() == -1)) { 121 return true; 122 } 123 ApplicationContext appContext = null; 124 try { 125 appContext = ServicesRegistry.getInstance() 126 .getJahiaApplicationContextService() 127 .getApplicationContext(oldAppID. 128 intValue()); 129 } catch ( Throwable t ){ 130 logger.debug("Cannot access app context",t); 131 return true; 133 } 134 Vector roles = appContext.getRoles(); 135 ArrayList roleMembersList = new ArrayList (); 137 if (roles != null) { 138 for (int roleNb = 0, size = roles.size(); roleNb < size; roleNb++) { 139 Set roleMembers = getFormMembers(jParams, roleNb); 140 roleMembersList.add(roleMembers); 141 } 142 } 143 Map appRoleMembers = new HashMap (); 144 appRoleMembers.put(oldAppID, roleMembersList); 145 engineMap.put(APPLICATION_ROLES, appRoleMembers); 146 return true; 147 } 148 149 158 private boolean saveData (ParamBean jParams, HashMap engineMap, 159 JahiaField theField) 160 throws JahiaException { 161 162 logger.debug("started"); 163 164 boolean success = true; 165 if (engineMap.get("createApplication_" + 166 theField.getDefinition().getName()) != null) { 167 theField.setValue( (String ) theField.getObject()); 168 172 Integer appID = null; 174 try { 175 appID = new Integer (theField.getValue()); 176 } catch (NumberFormatException nfe) { 177 return true; 180 } 181 Map appRoleMembers = (Map )engineMap.get(APPLICATION_ROLES); 183 List roleMembersList = (List )appRoleMembers.get(appID); 184 if (roleMembersList == null) { 185 return false; 186 } 187 success = theField.save(jParams); 188 ApplicationContext appContext = null; 189 try { 190 appContext = ServicesRegistry.getInstance() 191 .getJahiaApplicationContextService() 192 .getApplicationContext(appID.intValue()); 193 } catch ( Throwable t ){ 194 return true; 197 } 198 Vector roles = appContext.getRoles(); 199 if (roles != null) { 201 JahiaGroupManagerService gMgr = ServicesRegistry.getInstance() 202 .getJahiaGroupManagerService(); 203 String role = null; 204 for (int roleNb = 0, size = roles.size(); roleNb < size; roleNb++) { 205 role = (String ) roles.get(roleNb); 206 String gName = appID + "_" + theField.getID() + "_" + role; 207 JahiaGroup grp = gMgr.lookupGroup(0, gName); 208 if (grp == null) { 209 grp = gMgr.createGroup(0, gName, new Properties ()); 211 } else { 212 Set membersSet = (Set )roleMembersList.get(roleNb); 213 Enumeration members = grp.members(); 214 Principal p = null; 215 while (members.hasMoreElements()) { 216 p = (Principal ) members.nextElement(); 217 if (p instanceof JahiaUser) { 218 if (!membersSet.contains(p)) { 219 logger.debug("removed member=" + p.getName()); 220 grp.removeMember(p); 221 } else { 222 membersSet.remove(p); 223 } 224 } else if (p instanceof JahiaGroup) { 225 if (!membersSet.contains(p)) { 226 logger.debug("removed member=" + p.getName()); 227 grp.removeMember(p); 228 } else { 229 membersSet.remove(p); 230 } 231 } 232 } 233 Iterator it = membersSet.iterator(); 234 while (it.hasNext()) { 235 p = (Principal ) it.next(); 236 if (p instanceof JahiaUser) { 237 grp.addMember(p); 238 } else if (p instanceof JahiaGroup) { 239 grp.addMember(p); 240 } 241 } 242 } 243 } 244 } 245 Boolean flushApp = Boolean.FALSE; 246 if (engineMap.get("flushApp") != null) { 247 flushApp = (Boolean ) engineMap.get("flushApp"); 248 if (flushApp.booleanValue()) { 249 HttpSession session = jParams.getRequest().getSession(); 250 JahiaApplicationsDispatchingService dispatcher = 251 ServicesRegistry.getInstance(). 252 getJahiaApplicationsDispatchingService(); 253 if (dispatcher != null) { 254 dispatcher.flushAllSessionsCaches(session); 255 } 256 } 257 } 258 } 259 engineMap.remove(APPLICATION_ROLES); 260 return success; 261 } 262 263 273 private boolean composeEngineMap( ParamBean jParams, HashMap engineMap, JahiaField theField ) 274 throws JahiaException { 275 276 logger.debug("started"); 277 engineMap.put( "createApplication_" + theField.getDefinition().getName(), Boolean.TRUE ); 282 283 Vector appList = ServicesRegistry.getInstance().getJahiaApplicationsManagerService().getApplications(); 285 286 Vector authAppList = new Vector (); 288 for (int i = 0, size = appList.size(); i < size; i++) { 289 ApplicationBean app = (ApplicationBean)appList.get(i); 290 if (app.getVisibleStatus() == 1 && 291 (app.isShared() || app.getJahiaID() == jParams.getSiteID() || 292 ServicesRegistry.getInstance().getAppsShareService().getShare(jParams.getSite(), app) != null)) { 293 294 authAppList.add(app); 306 } 307 } 308 309 if (authAppList == null) { authAppList = new Vector (); } 310 engineMap.put( "appList", authAppList.elements() ); 311 312 Integer appID; 313 try { 314 appID = new Integer ( (String ) theField.getObject()); 315 } catch (NumberFormatException nfe) { 319 appID = new Integer ( -1); 320 } 321 ApplicationBean appBean = ServicesRegistry.getInstance().getJahiaApplicationsManagerService().getApplication(appID.intValue()); 322 if (appBean == null) { 323 appID = new Integer (-1); 325 } 326 if (appID.intValue() == -1) { 327 if (authAppList.size() > 0) { 329 appID = new Integer ( ( (ApplicationBean) authAppList.get(0)). 330 getID()); 331 } 332 } 333 engineMap.put( "appID", appID ); 334 Vector roles = new Vector (); 335 if (appID.intValue() >= 0) { 336 try { 337 ApplicationContext appContext = ServicesRegistry.getInstance(). 338 getJahiaApplicationContextService(). 339 getApplicationContext(appID. 340 intValue()); 341 roles = appContext.getRoles(); 342 } catch ( Throwable t ){ 343 logger.debug("Error retrieving application context appID[" + appID + "]"); 344 } 345 } 346 engineMap.put( "roles", roles ); 347 348 Map appRoleMembers = (Map )engineMap.get(APPLICATION_ROLES); 350 if (appRoleMembers == null) { 351 appRoleMembers = new HashMap (); 352 } 353 List roleMembersList = (List )appRoleMembers.get(appID); 354 if (roleMembersList == null) { 355 roleMembersList = new ArrayList (); 356 JahiaGroupManagerService gMgr = ServicesRegistry.getInstance() 358 .getJahiaGroupManagerService(); 359 for (int i = 0, size = roles.size(); i < size; i++) { 360 String role = (String )roles.get(i); 361 JahiaGroup grp = gMgr.lookupGroup(0, 362 appID + "_" + theField.getID() + "_" + role); 363 if (grp != null) 364 roleMembersList.add(getAppMembers(grp)); 365 else 366 roleMembersList.add(new HashSet ()); 367 } 368 appRoleMembers.put(appID, roleMembersList); 369 } 370 engineMap.put(APPLICATION_ROLES, appRoleMembers); 371 engineMap.put("selectUsrGrp", 372 SelectUG_Engine.getInstance().renderLink(jParams, "")); 373 374 379 382 boolean editable = false; 383 JahiaContainer theContainer = (JahiaContainer)engineMap.get("theContainer"); 384 if (theContainer == null) { 385 editable = true; 387 } else { 388 HashMap ctnListFieldAcls = (HashMap ) engineMap 389 .get("ctnListFieldAcls"); 390 391 if (theContainer.getListID() != 0 && ctnListFieldAcls != null 392 && ctnListFieldAcls.size() > 0) { 393 JahiaBaseACL acl = JahiaEngineTools.getCtnListFieldACL( 394 ctnListFieldAcls, theField.getID()); 395 if (acl != null) { 396 editable = acl.getPermission(jParams.getUser(), 397 JahiaBaseACL.WRITE_RIGHTS, true); 398 } 399 } else { 400 editable = true; 401 } 402 } 403 String output = ""; 404 if ( editable ){ 405 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, JSP_FILE ); 406 } else { 407 output = ServicesRegistry.getInstance().getJahiaFetcherService().fetchServlet( jParams, READONLY_JSP ); 408 } 409 engineMap.put( "fieldForm", output ); 410 return true; 411 } 412 413 private HashSet getFormMembers(ParamBean jParams, int roleNb) { 414 String [] authMembersStr = jParams.getRequest().getParameterValues("authMembers" + roleNb); 415 HashSet membersSet = new HashSet (); 416 if (authMembersStr != null) { 417 for (int i = 0; i < authMembersStr.length; i++) { 418 if (authMembersStr[i].charAt(0) == 'u') { 419 JahiaUser user = ServicesRegistry.getInstance(). 420 getJahiaUserManagerService(). 421 lookupUser(authMembersStr[i].substring(1)); 422 membersSet.add(user); 423 } else { 424 JahiaGroup group = ServicesRegistry.getInstance(). 425 getJahiaGroupManagerService(). 426 lookupGroup(authMembersStr[i].substring(1)); 427 membersSet.add(group); 428 } 429 } 430 } 431 return membersSet; 432 } 433 434 private HashSet getAppMembers(JahiaGroup grp) { 435 HashSet membersSet = new HashSet (); 436 Enumeration enumeration = grp.members(); 437 while (enumeration.hasMoreElements()) { 438 membersSet.add(enumeration.nextElement()); 439 } 440 return membersSet; 441 } 442 } 443 | Popular Tags |