1 25 package org.ofbiz.content.content; 26 27 import java.util.ArrayList ; 28 import java.util.HashMap ; 29 import java.util.Iterator ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import org.ofbiz.base.util.UtilProperties; 34 import org.ofbiz.entity.GenericValue; 35 36 45 public class PermissionRecorder { 46 47 public static final int PRE_PURPOSE = 0; 48 public static final int PRE_ROLE = 1; 49 public static final int WITH_ROLES = 2; 50 51 protected boolean isOn = false; 52 protected GenericValue userLogin; 53 protected List permCheckResults = new ArrayList (); 54 protected boolean entityPermCheckResult = false; 55 protected String currentContentId = ""; 56 protected Map currentContentMap; 57 protected String privilegeEnumId; 58 protected int currentCheckMode; 59 protected GenericValue [] contentPurposeOperations; 60 protected String [] statusTargets; 61 protected String [] targetOperations; 62 63 public static final String module = PermissionRecorder.class.getName(); 64 65 public static final String [] opFields = { "contentPurposeTypeId", "contentOperationId", "roleTypeId", "statusId", "privilegeEnumId"}; 66 public static final String [] fieldTitles = { "Purpose", "Operation", "Role", "Status", "Privilege"}; 67 68 public PermissionRecorder() { 69 isOn = UtilProperties.propertyValueEqualsIgnoreCase("content.properties", "permissionRecorderOn", "true"); 70 } 71 72 public void setCheckMode(int val) { 73 currentCheckMode = val; 74 } 75 76 public int getCheckMode() { 77 return currentCheckMode; 78 } 79 80 public boolean isOn() { 81 return isOn; 82 } 83 84 public void setOn(boolean b) { 85 isOn = b; 86 } 87 88 public void setUserLogin(GenericValue user) { 89 userLogin = user; 90 } 91 92 public GenericValue getUserLogin() { 93 return userLogin; 94 } 95 96 public boolean getEntityPermCheckResult() { 97 return entityPermCheckResult; 98 } 99 100 public void setEntityPermCheckResult(boolean b) { 101 entityPermCheckResult = b; 102 } 103 104 public GenericValue [] getContentPurposeOperations() { 105 return contentPurposeOperations; 106 } 107 108 public void setContentPurposeOperations(List opList) { 109 contentPurposeOperations = (GenericValue [])opList.toArray(); 110 } 111 112 public void setPrivilegeEnumId(String id) { 113 privilegeEnumId = id; 114 } 115 116 public String getPrivilegeEnumId() { 117 return privilegeEnumId; 118 } 119 120 public String [] getStatusTargets() { 121 return statusTargets; 122 } 123 124 public void setStatusTargets(List opList) { 125 statusTargets = (String [])opList.toArray(); 126 } 127 128 public String [] getTargetOperations() { 129 return targetOperations; 130 } 131 132 public void setTargetOperations(List opList) { 133 targetOperations = (String [])opList.toArray(); 134 } 135 136 public void setCurrentContentId(String id) { 137 if (!currentContentId.equals(id)) { 138 currentContentMap = new HashMap (); 139 permCheckResults.add(currentContentMap); 140 currentContentMap.put("contentId", id); 141 currentContentMap.put("checkResults", new ArrayList ()); 142 } 143 currentContentId = id; 144 } 145 146 public String getCurrentContentId() { 147 return currentContentId; 148 } 149 150 public void setRoles(List roles) { 151 if (currentContentMap != null) { 152 if (roles != null) 153 currentContentMap.put("roles", roles.toArray()); 154 else 155 currentContentMap.put("roles", null); 156 } 157 } 158 159 public void setPurposes(List purposes) { 160 if (currentContentMap != null) { 161 if (purposes != null) 162 currentContentMap.put("purposes", purposes.toArray()); 163 else 164 currentContentMap.put("purposes", null); 165 } 166 } 167 168 public void startMatchGroup(List targetOperations, List purposes, List roles, List targStatusList, String targPrivilegeEnumId, String contentId) { 169 170 currentContentMap = new HashMap (); 171 permCheckResults.add(currentContentMap); 172 String s = null; 173 if (targetOperations != null) { 174 s = targetOperations.toString(); 176 currentContentMap.put("contentOperationId", s); 178 } 179 if (purposes != null) { 180 s = purposes.toString(); 182 currentContentMap.put("contentPurposeTypeId", s); 184 } 185 if (roles != null) { 186 s = roles.toString(); 188 currentContentMap.put("roleTypeId", s); 190 } 191 if (targStatusList != null) { 192 s = targStatusList.toString(); 194 currentContentMap.put("statusId", s); 196 } 197 currentContentMap.put("privilegeEnumId", privilegeEnumId); 198 currentContentMap.put("contentId", contentId); 199 currentContentMap.put("checkResultList", new ArrayList ()); 200 currentContentMap.put("matches", null); 201 currentContentId = contentId; 202 } 204 205 public void record(GenericValue purposeOp, boolean targetOpCond, boolean purposeCond, boolean statusCond, boolean privilegeCond, boolean roleCond) { 206 207 Map map = new HashMap (purposeOp); 208 map.put("contentOperationIdCond", new Boolean (targetOpCond)); 209 map.put("contentPurposeTypeIdCond", new Boolean (purposeCond)); 210 map.put("statusIdCond", new Boolean (statusCond)); 211 map.put("privilegeEnumIdCond", new Boolean (privilegeCond)); 212 map.put("roleTypeIdCond", new Boolean (roleCond)); 213 map.put("contentId", currentContentId); 214 ((List )currentContentMap.get("checkResultList")).add(map); 215 } 217 218 public String toHtml() { 219 StringBuffer sb = new StringBuffer (); 220 sb.append("<style type=\"text/css\">"); 221 sb.append(".pass {background-color:lime; font-family:Verdana,Arial,sans-serif; font-size:10px; }"); 222 sb.append(".fail {background-color:red; font-family:Verdana,Arial,sans-serif; font-size:10px; }"); 223 sb.append(".target {background-color:lightgrey; font-family:Verdana,Arial,sans-serif; font-size:10px; }"); 224 sb.append(".headr {background-color:white; font-weight:bold; font-family:Verdana,Arial,sans-serif; font-size:12px; }"); 225 sb.append("</style>"); 226 227 sb.append("<table border=\"1\" >"); 229 sb.append("<tr>"); 231 232 sb.append("<td class=\"headr\">"); 233 sb.append("Content Id"); 234 sb.append("</td>"); 235 236 String str = null; 238 String s = null; 239 for (int i=0; i < fieldTitles.length; i++) { 240 String opField = (String )fieldTitles[i]; 241 sb.append("<td class=\"headr\">"); 242 sb.append(opField); 243 sb.append("</td>"); 244 } 245 sb.append("<td class=\"headr\" >Pass/Fail</td>"); 246 sb.append("</tr>"); 247 248 Iterator iter = permCheckResults.iterator(); 249 while (iter.hasNext()) { 250 Map cMap = (Map )iter.next(); 251 sb.append(renderCurrentContentMapHtml(cMap)); 252 } 253 sb.append("</table>"); 254 return sb.toString(); 255 } 256 257 public String renderCurrentContentMapHtml(Map cMap) { 258 StringBuffer sb = new StringBuffer (); 259 List resultList = (List )cMap.get("checkResultList"); 260 Iterator iter = resultList.iterator(); 261 while (iter.hasNext()) { 262 Map rMap = (Map )iter.next(); 263 sb.append(renderResultRowHtml(rMap, cMap)); 265 } 266 267 return sb.toString(); 268 } 269 270 272 public String renderResultRowHtml(Map rMap, Map currentContentResultMap ) { 273 StringBuffer sb = new StringBuffer (); 274 275 sb.append("<tr>"); 277 278 sb.append("<td class=\"target\">"); 279 sb.append((String )rMap.get("contentId")); 280 sb.append("</td>"); 281 282 String str = null; 284 String s = null; 285 for (int i=0; i < opFields.length; i++) { 286 String opField = (String )opFields[i]; 287 sb.append("<td class=\"target\">"); 288 s = (String )currentContentResultMap.get(opField); 289 if (s != null) 290 str = s; 291 else 292 str = " "; 293 sb.append(str); 294 sb.append("</td>"); 295 } 296 sb.append("<td class=\"target\" > </td>"); 297 sb.append("</tr>"); 298 299 sb.append("<tr>"); 302 303 sb.append("<td class=\"target\">"); 304 sb.append((String )currentContentResultMap.get("contentId")); 305 sb.append("</td>"); 306 307 boolean isPass = true; 308 for (int i=0; i < opFields.length; i++) { 309 String opField = (String )opFields[i]; 310 Boolean bool = (Boolean )rMap.get(opField + "Cond"); 311 String cls = (bool.booleanValue()) ? "pass" : "fail"; 312 if (!bool.booleanValue()) 313 isPass = false; 314 sb.append("<td class=\"" + cls + "\">"); 315 s = (String )rMap.get(opField); 317 sb.append(s); 319 sb.append("</td>"); 320 } 321 String passFailCls = (isPass) ? "pass" : "fail"; 322 sb.append("<td class=\"" + passFailCls +"\">" + passFailCls.toUpperCase() + "</td>"); 323 sb.append("</tr>"); 324 326 return sb.toString(); 327 } 328 } 329 330 | Popular Tags |