1 14 package org.jahia.data.viewhelper.principal; 15 16 import java.lang.reflect.Method ; 17 import java.security.Principal ; 18 import java.util.ArrayList ; 19 import java.util.Enumeration ; 20 import java.util.HashSet ; 21 import java.util.Properties ; 22 import java.util.Set ; 23 import java.util.StringTokenizer ; 24 import java.util.HashMap ; 25 import java.util.Vector ; 26 import javax.servlet.http.HttpServletRequest ; 27 28 import org.jahia.registries.ServicesRegistry; 29 import org.jahia.services.acl.JahiaBaseACL; 30 import org.jahia.services.usermanager.JahiaGroup; 31 import org.jahia.services.usermanager.JahiaUser; 32 import org.jahia.services.usermanager.JahiaUserManagerService; 33 import org.jahia.services.usermanager.JahiaGroupManagerService; 34 import org.jahia.utils.JahiaString; 35 import org.jahia.utils.JahiaTools; 36 import java.io.Serializable ; 37 38 63 public class PrincipalViewHelper implements Serializable { 64 65 public static String PRINCIPAL = "Principal"; 66 public static String PERMISSIONS = "Permissions"; 67 public static String PROVIDER = "Provider"; 68 public static String NAME = "Name"; 69 public static String PROPERTIES = "Properties"; 70 71 84 public PrincipalViewHelper(String [] textFormat) { 85 for (int i = 0; i < textFormat.length; i++) { 86 StringTokenizer st = new StringTokenizer (textFormat[i], ","); 87 String fieldToDisplay = (String )st.nextElement(); 88 if (selectBoxFieldsHeading.contains(fieldToDisplay)) { 89 if (st.hasMoreElements()) { 90 selectBoxFieldsSize.add(Integer.valueOf(((String )st.nextElement()).trim())); 91 } else { 92 selectBoxFieldsSize.add(new Integer (-1)); 93 } 94 try { 95 selectBoxFieldsMethod.add(PrincipalViewHelper.class.getMethod( 96 "get" + fieldToDisplay, new Class [] {Principal .class, Integer .class})); 97 } catch (java.lang.NoSuchMethodException nsme) { 98 logger.fatal("Internal class error ! Please check Jahia code", nsme); 99 } 100 } else { 101 } 103 } 104 } 105 106 113 public String getPrincipalTextOption(Principal p) { 114 String authUserText = ""; 115 for (int i = 0; i < selectBoxFieldsMethod.size(); i++) { 116 Method m = (Method )selectBoxFieldsMethod.get(i); 117 Integer size = (Integer )selectBoxFieldsSize.get(i); 118 Object [] args = { p, size }; 119 try { 120 authUserText += (String )m.invoke(this, args); 121 } catch (java.lang.reflect.InvocationTargetException ite) { 122 logger.fatal("Internal class error !", ite); 123 } catch (java.lang.IllegalAccessException iae) { 124 logger.fatal("Internal class error !", iae); 125 } 126 if (i + 1 < selectBoxFieldsHeading.size()) { 127 authUserText += " "; 128 } 129 } 130 return JahiaTools.replacePattern(authUserText, " ", " "); 131 } 132 133 140 public String getPrincipalValueOption(Principal p) { 141 if (p instanceof JahiaUser) { 142 return "u" + ((JahiaUser)p).getUserKey(); 143 } else { 144 return "g" + ((JahiaGroup)p).getGroupKey(); 145 } 146 } 147 148 154 public static String getName(Principal p, Integer size) { 155 if (p instanceof JahiaUser) { 156 return JahiaString.adjustStringSize(((JahiaUser)p).getUsername(), size.intValue()); 157 } else { 158 return JahiaString.adjustStringSize(((JahiaGroup)p).getGroupname(), size.intValue()); 159 } 160 } 161 162 168 public static String getProvider(Principal p, Integer size) { 169 if (p instanceof JahiaUser) { 170 return JahiaString.adjustStringSize(((JahiaUser)p).getProviderName(), 171 size.intValue()); 172 } else { 173 return JahiaString.adjustStringSize(((JahiaGroup)p).getProviderName(), 174 size.intValue()); 175 } 176 } 177 178 184 public static String getPrincipal(Principal p, Integer size) { 185 if (p instanceof JahiaUser) { 186 return "u"; 187 } else { 188 return "g"; 189 } 190 } 191 192 201 public static String getProperties(Principal p, Integer size) { 202 String properties = ""; 203 if (p instanceof JahiaUser) { 204 JahiaUser user = (JahiaUser)p; 205 String firstname = user.getProperty("firstname"); 207 String lastname = user.getProperty("lastname"); 208 if (firstname != null) { 209 properties += firstname; 210 if (firstname.length() < size.intValue()) { 211 properties += " "; 212 } 213 } 214 if (lastname != null) 215 properties += lastname; 216 if ("".equals(properties)) { 217 String email = user.getProperty("email"); 218 if (email != null) 219 properties += email; 220 } 221 return JahiaString.adjustStringSize(properties, size.intValue()); 222 } else { 223 JahiaGroup group = (JahiaGroup)p; 224 Enumeration grpMembers = group.members(); 226 String members = "("; 227 while (grpMembers.hasMoreElements()) { 228 Object obj = (Object )grpMembers.nextElement(); 229 if (obj instanceof JahiaUser) { 230 JahiaUser tmpUser = (JahiaUser)obj; 231 members += tmpUser.getUsername(); 232 } else { 233 JahiaGroup tmpGroup = (JahiaGroup)obj; 234 members += tmpGroup.getGroupname(); 235 } 236 if (members.length() > 20) 237 break; 238 if (grpMembers.hasMoreElements()) 239 members += ", "; 240 } 241 members += ")"; 242 return JahiaString.adjustStringSize(members, 20); 243 } 244 } 245 246 252 public String getPermissions(Principal p, Integer size) { 253 Integer permissions = (Integer ) perms.get(p); 254 255 String permStr = ""; 256 257 boolean inherited = (permissions.intValue() >> 3 & 1) != 0; 258 if (inherited) { 259 permStr += (permissions.intValue() >> JahiaBaseACL.READ_RIGHTS & 1) != 0 ? "r" : "-"; 260 permStr += (permissions.intValue() >> JahiaBaseACL.WRITE_RIGHTS & 1) != 0 ? "w" : "-"; 261 permStr += (permissions.intValue() >> JahiaBaseACL.ADMIN_RIGHTS & 1) != 0 ? "A*" : "-*"; 262 } else { 263 permStr += (permissions.intValue() >> JahiaBaseACL.READ_RIGHTS & 1) != 0 ? "r" : "-"; 264 permStr += (permissions.intValue() >> JahiaBaseACL.WRITE_RIGHTS & 1) != 0 ? "w" : "-"; 265 permStr += (permissions.intValue() >> JahiaBaseACL.ADMIN_RIGHTS & 1) != 0 ? "A " : "- "; 266 } 267 268 return permStr; 269 } 270 271 private HashMap perms; 272 public void setPermissions(HashMap perms) { 273 this.perms = perms; 274 } 275 276 277 291 public static Set getSearchResult(HttpServletRequest request, int siteID) { 292 293 String searchString = request.getParameter("searchString"); 294 String searchIn = request.getParameter("searchIn"); 295 String [] searchInProps = request.getParameterValues("properties"); 296 String storedOn = request.getParameter("storedOn"); 297 String [] providers = request.getParameterValues("providers"); 298 299 return getSearchResult(searchString, searchIn, searchInProps, storedOn, providers, siteID); 300 } 301 302 public static Set getSearchResult(String searchString, 303 String searchIn, 304 String [] searchInProps, 305 String storedOn, 306 String [] providers, 307 int siteID) { 308 309 JahiaUserManagerService jahiaUserManagerService = 310 ServicesRegistry.getInstance().getJahiaUserManagerService(); 311 Properties searchParameters = new Properties (); 312 Set searchResults = new HashSet (); 313 if (searchIn == null) { logger.debug("No formular transmited. Finding all Jahia DB users."); 315 searchParameters.setProperty("*", "*"); 316 searchResults.addAll(jahiaUserManagerService. 317 searchUsers("jahia_db", siteID, searchParameters)); 318 } else { 319 if ("".equals(searchString)) { 321 searchString = "*"; 322 } 323 if ("allProps".equals(searchIn) || searchInProps == null) { 324 searchParameters.setProperty("*", searchString); 325 } else { 326 for (int i = 0; i < searchInProps.length; i++) { 327 searchParameters.setProperty(searchInProps[i], searchString); 328 } 329 } 330 if ("everywhere".equals(storedOn) || providers == null) { 331 searchResults.addAll(jahiaUserManagerService. 332 searchUsers(siteID, searchParameters)); 333 } else { 334 for (int i = 0; i < providers.length; i++) { 335 String curServer = providers[i]; 336 Set curSearchResults = jahiaUserManagerService. 337 searchUsers(curServer, siteID, searchParameters); 338 if (curSearchResults != null) { 339 searchResults.addAll(curSearchResults); 340 } 341 } 342 } 343 } 344 return searchResults; 345 } 346 347 348 362 public static Set getGroupSearchResult(HttpServletRequest request, int siteID) { 363 364 String searchString = request.getParameter("searchString"); 365 String searchIn = request.getParameter("searchIn"); 366 String [] searchInProps = request.getParameterValues("properties"); 367 String storedOn = request.getParameter("storedOn"); 368 String [] providers = request.getParameterValues("providers"); 369 370 return getGroupSearchResult(searchString, searchIn, searchInProps, storedOn, providers, siteID); 371 372 } 373 374 public static Set getGroupSearchResult(String searchString, String searchIn, String [] searchInProps, String storedOn, String [] providers, int siteID) { 375 376 JahiaGroupManagerService jahiaGroupManagerService = 377 ServicesRegistry.getInstance().getJahiaGroupManagerService(); 378 Properties searchParameters = new Properties (); 379 Set searchResults = new HashSet (); 380 if (searchIn == null) { logger.debug("No formular transmited. Finding all Jahia DB users."); 382 searchParameters.setProperty("*", "*"); 383 searchResults.addAll(jahiaGroupManagerService. 384 searchGroups("jahia_db", siteID, searchParameters)); 385 } else { 386 if ("".equals(searchString)) { 388 searchString = "*"; 389 } 390 if ("allProps".equals(searchIn) || searchInProps == null) { 391 searchParameters.setProperty("*", searchString); 392 } else { 393 for (int i = 0; i < searchInProps.length; i++) { 394 searchParameters.setProperty(searchInProps[i], searchString); 395 } 396 } 397 if ("everywhere".equals(storedOn) || providers == null) { 398 searchResults.addAll(jahiaGroupManagerService. 399 searchGroups(siteID, searchParameters)); 400 } else { 401 for (int i = 0; i < providers.length; i++) { 402 String curServer = providers[i]; 403 Set curSearchResults = jahiaGroupManagerService. 404 searchGroups(curServer, siteID, searchParameters); 405 if (curSearchResults != null) { 406 searchResults.addAll(curSearchResults); 407 } 408 } 409 } 410 } 411 return searchResults; 412 } 413 414 422 public static Set removeJahiaAdministrators(Set users) { 423 Set usersWithoutJahiaAdmin = new HashSet (users); 424 JahiaGroup jahiaAdminGroup = ServicesRegistry.getInstance().getJahiaGroupManagerService().getAdministratorGroup(0); 425 Enumeration memberEnum = jahiaAdminGroup.members(); 426 while (memberEnum.hasMoreElements()) { 427 Object curMemberObject = memberEnum.nextElement(); 428 if (curMemberObject instanceof JahiaUser) { 429 usersWithoutJahiaAdmin.remove((JahiaUser) curMemberObject); 430 } 431 } 432 return usersWithoutJahiaAdmin; 433 } 434 435 public static Set getUserGroupMembership(JahiaUser usr, int site) { 436 Set groups = new HashSet (); 437 JahiaGroupManagerService jahiaGroupManagerService = ServicesRegistry.getInstance().getJahiaGroupManagerService(); 438 Vector v = jahiaGroupManagerService.getUserMembership(usr); 439 Enumeration en = v.elements(); 440 while (en.hasMoreElements()) { 441 String gname = (String ) en.nextElement(); 442 JahiaGroup g = jahiaGroupManagerService.lookupGroup(gname); 443 if (g != null && (g.getSiteID() == site || g.getSiteID() ==0)) { 444 groups.add(g); 445 } 446 } 447 return groups; 448 } 449 450 private static HashSet selectBoxFieldsHeading = new HashSet (); 451 private ArrayList selectBoxFieldsSize = new ArrayList (); 452 private ArrayList selectBoxFieldsMethod = new ArrayList (); 453 454 private static org.apache.log4j.Logger logger = 455 org.apache.log4j.Logger.getLogger(PrincipalViewHelper.class); 456 457 static { 458 selectBoxFieldsHeading.add(PRINCIPAL); 459 selectBoxFieldsHeading.add(PERMISSIONS); 460 selectBoxFieldsHeading.add(PROVIDER); 461 selectBoxFieldsHeading.add(NAME); 462 selectBoxFieldsHeading.add(PROPERTIES); 463 } 464 465 } 466 | Popular Tags |