1 18 package org.jahia.services.usermanager; 19 20 import org.apache.regexp.RE; 21 import org.apache.regexp.RESyntaxException; 22 import org.jahia.data.fields.JahiaField; 23 import org.jahia.data.fields.LoadFlags; 24 import org.jahia.exceptions.JahiaException; 25 import org.jahia.registries.ServicesRegistry; 26 import org.jahia.services.pages.JahiaPage; 27 28 import java.util.Enumeration ; 29 import java.util.HashMap ; 30 import java.util.Vector ; 31 32 33 38 39 40 public class GroupRoleUtils { 41 private static org.apache.log4j.Logger logger = 42 org.apache.log4j.Logger.getLogger (GroupRoleUtils.class); 43 44 private static RE groupNameRE; 45 private static ServicesRegistry sReg; 46 private static JahiaGroupManagerService gMgr; 47 48 static { 49 try { 50 51 String groupNamePattern = "^([0-9]+)_([0-9]+)_(.+)$"; 55 groupNameRE = new RE (groupNamePattern); 56 57 } catch (RESyntaxException e) { 58 String errorMsg = "RE exception : " + e.getMessage (); 59 logger.error (errorMsg); 60 } 61 } 62 63 64 71 public static String translate (String groupname) { 72 if ((groupname != null) && (groupNameRE.match (groupname))) { 73 HashMap roleInfo = decompose (groupname); 74 StringBuffer buf = new StringBuffer (); 75 buf.append ((String ) roleInfo.get ("fieldName")); 76 buf.append (":"); 77 buf.append ((String ) roleInfo.get ("appName")); 78 buf.append (":"); 79 buf.append ((String ) roleInfo.get ("roleName")); 80 81 return buf.toString (); 82 } else { 83 return groupname; 84 } 85 } 86 87 88 95 public static int getfieldID (String groupname) { 96 int fieldID = 0; 97 if ((groupname != null) && (groupNameRE.match (groupname))) { 98 HashMap groupInfo = decompose (groupname); 99 Integer fieldIDObj = (Integer ) groupInfo.get ("fieldID"); 100 fieldID = fieldIDObj.intValue (); 101 } 102 return fieldID; 103 } 104 105 106 113 public static Vector getRolesForApp (int appID) { 114 Vector rolesList = new Vector (); 115 116 ServicesRegistry sReg = ServicesRegistry.getInstance (); 118 if (sReg != null) { 119 gMgr = sReg.getJahiaGroupManagerService (); 120 if (gMgr != null) { 121 Vector groupnames = gMgr.getGroupnameList (); 122 Enumeration enumeration = groupnames.elements (); 123 124 while (enumeration.hasMoreElements ()) { 126 String groupname = (String ) enumeration.nextElement (); 127 if (isRole (groupname)) { 128 if (getAppID (groupname) == appID) { 129 rolesList.add (groupname); 130 } 131 } 132 } 133 } 134 } 135 return rolesList; 136 } 137 138 139 146 public static Vector getRolesForApp (String appName) { 147 Vector rolesList = new Vector (); 148 149 sReg = ServicesRegistry.getInstance (); 151 if (sReg != null) { 152 gMgr = sReg.getJahiaGroupManagerService (); 153 if (gMgr != null) { 154 Vector groupnames = gMgr.getGroupnameList (); 155 Enumeration enumeration = groupnames.elements (); 156 157 while (enumeration.hasMoreElements ()) { 159 String groupname = (String ) enumeration.nextElement (); 160 if (isRole (groupname)) { 161 if (getAppName (groupname).equals (appName)) { 162 rolesList.add (groupname); 163 } 164 } 165 } 166 } 167 } 168 return rolesList; 169 } 170 171 172 179 public static int getAppID (String groupname) { 180 int appID = 0; 181 if ((groupname != null) && (groupNameRE.match (groupname))) { 182 HashMap groupInfo = decompose (groupname); 183 Integer appIDObj = (Integer ) groupInfo.get ("appID"); 184 appID = appIDObj.intValue (); 185 } 186 return appID; 187 } 188 189 190 197 public static String getFieldName (String groupname) { 198 String fieldName = "unknown"; 199 if ((groupname != null) && (groupNameRE.match (groupname))) { 200 HashMap groupInfo = decompose (groupname); 201 fieldName = (String ) groupInfo.get ("fieldName"); 202 } 203 return fieldName; 204 } 205 206 207 214 public static String getAppName (String groupname) { 215 String appName = "unknown"; 216 if ((groupname != null) && (groupNameRE.match (groupname))) { 217 HashMap groupInfo = decompose (groupname); 218 appName = (String ) groupInfo.get ("appName"); 219 } 220 return appName; 221 } 222 223 224 231 public static String getRoleName (String groupname) { 232 String roleName = "unknown"; 233 if ((groupname != null) && (groupNameRE.match (groupname))) { 234 HashMap groupInfo = decompose (groupname); 235 roleName = (String ) groupInfo.get ("roleName"); 236 } 237 return roleName; 238 } 239 240 241 249 private static HashMap decompose (String groupname) { 250 HashMap roleInfo = new HashMap (); 252 253 String appIDStr = groupname.substring (0, groupname.indexOf ("_")); 255 256 String remains = groupname.substring ((groupname.indexOf ("_") + 1)); 257 258 String fieldIDStr = remains.substring (0, remains.indexOf ("_")); 260 261 try { 262 263 String appName = ServicesRegistry.getInstance ().getJahiaApplicationsManagerService () 265 .getApplication (Integer.parseInt (appIDStr)) 266 .getName (); 267 268 String fieldName = null; 270 int fieldID = Integer.parseInt (fieldIDStr); 271 JahiaField field = ServicesRegistry.getInstance ().getJahiaFieldService ().loadField ( 272 fieldID, LoadFlags.NOTHING); 273 if (field == null) { 274 fieldName = "unknown"; 275 } else { 276 int pageID = field.getPageID (); 277 JahiaPage page = ServicesRegistry.getInstance ().getJahiaPageService () 278 .lookupPage (pageID); 279 fieldName = page.getTitle (); 280 } 281 282 String roleName = remains.substring ((remains.indexOf ("_") + 1)); 284 285 roleInfo.put ("appID", new Integer (appIDStr)); 286 roleInfo.put ("fieldID", new Integer (fieldID)); 287 roleInfo.put ("appName", appName); 288 roleInfo.put ("fieldName", fieldName); 289 roleInfo.put ("roleName", roleName); 290 291 } catch (JahiaException je) { 292 } catch (NumberFormatException nfe) { 294 } 296 297 return roleInfo; 298 } 299 300 301 308 public static boolean isRole (String groupname) { 309 return (groupname != null && groupNameRE.match (groupname)); 310 } 311 312 } 313 | Popular Tags |