1 25 package org.ofbiz.security; 26 27 import java.util.ArrayList ; 28 import java.util.Iterator ; 29 import java.util.LinkedList ; 30 import java.util.List ; 31 import java.util.Map ; 32 33 import javax.servlet.http.HttpSession ; 34 35 import org.ofbiz.base.util.Debug; 36 import org.ofbiz.base.util.UtilMisc; 37 import org.ofbiz.entity.GenericDelegator; 38 import org.ofbiz.entity.GenericEntityException; 39 import org.ofbiz.entity.GenericValue; 40 import org.ofbiz.entity.condition.EntityCondition; 41 import org.ofbiz.entity.condition.EntityConditionList; 42 import org.ofbiz.entity.condition.EntityExpr; 43 import org.ofbiz.entity.condition.EntityOperator; 44 import org.ofbiz.entity.util.EntityUtil; 45 46 57 public class OFBizSecurity extends org.ofbiz.security.Security { 58 59 public static final String module = OFBizSecurity.class.getName(); 60 61 public static final Map simpleRoleEntity = UtilMisc.toMap( 62 "ORDERMGR", UtilMisc.toMap("name", "OrderRole", "pkey", "orderId"), 63 "FACILITY", UtilMisc.toMap("name", "FacilityRole", "pkey", "facilityId"), 64 "MARKETING", UtilMisc.toMap("name", "MarketingCampaignRole", "pkey", "marketingCampaignId")); 65 66 GenericDelegator delegator = null; 67 68 protected OFBizSecurity() {} 69 70 protected OFBizSecurity(GenericDelegator delegator) { 71 this.delegator = delegator; 72 } 73 74 public GenericDelegator getDelegator() { 75 return delegator; 76 } 77 78 public void setDelegator(GenericDelegator delegator) { 79 this.delegator = delegator; 80 } 81 82 85 public Iterator findUserLoginSecurityGroupByUserLoginId(String userLoginId) { 86 List collection = (List ) userLoginSecurityGroupByUserLoginId.get(userLoginId); 87 88 if (collection == null) { 89 try { 90 collection = delegator.findByAnd("UserLoginSecurityGroup", UtilMisc.toMap("userLoginId", userLoginId), null); 91 } catch (GenericEntityException e) { 92 Debug.logWarning(e, module); 93 } 94 if (collection == null) collection = new LinkedList (); 96 userLoginSecurityGroupByUserLoginId.put(userLoginId, collection); 97 } 98 collection = EntityUtil.filterByDate(collection, true); 100 return collection.iterator(); 101 } 102 103 106 public boolean securityGroupPermissionExists(String groupId, String permission) { 107 GenericValue securityGroupPermissionValue = delegator.makeValue("SecurityGroupPermission", 108 UtilMisc.toMap("groupId", groupId, "permissionId", permission)); 109 Boolean exists = (Boolean ) securityGroupPermissionCache.get(securityGroupPermissionValue); 110 111 if (exists == null) { 112 try { 113 if (delegator.findByPrimaryKey(securityGroupPermissionValue.getPrimaryKey()) != null) 114 exists = Boolean.TRUE; 115 else 116 exists = Boolean.FALSE; 117 } catch (GenericEntityException e) { 118 exists = Boolean.FALSE; 119 Debug.logWarning(e, module); 120 } 121 securityGroupPermissionCache.put(securityGroupPermissionValue, exists); 122 } 123 return exists.booleanValue(); 124 } 125 126 129 public boolean hasPermission(String permission, HttpSession session) { 130 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 131 132 if (userLogin == null) return false; 133 134 return hasPermission(permission, userLogin); 135 } 136 137 140 public boolean hasPermission(String permission, GenericValue userLogin) { 141 if (userLogin == null) return false; 142 143 Iterator iterator = findUserLoginSecurityGroupByUserLoginId(userLogin.getString("userLoginId")); 144 GenericValue userLoginSecurityGroup = null; 145 146 while (iterator.hasNext()) { 147 userLoginSecurityGroup = (GenericValue) iterator.next(); 148 if (securityGroupPermissionExists(userLoginSecurityGroup.getString("groupId"), permission)) return true; 149 } 150 151 return false; 152 } 153 154 157 public boolean hasEntityPermission(String entity, String action, HttpSession session) { 158 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 159 160 if (userLogin == null) return false; 161 return hasEntityPermission(entity, action, userLogin); 162 } 163 164 167 public boolean hasEntityPermission(String entity, String action, GenericValue userLogin) { 168 if (userLogin == null) return false; 169 170 Iterator iterator = findUserLoginSecurityGroupByUserLoginId(userLogin.getString("userLoginId")); 172 GenericValue userLoginSecurityGroup = null; 173 174 while (iterator.hasNext()) { 175 userLoginSecurityGroup = (GenericValue) iterator.next(); 176 177 179 if (securityGroupPermissionExists(userLoginSecurityGroup.getString("groupId"), entity + "_ADMIN")) 181 return true; 182 if (securityGroupPermissionExists(userLoginSecurityGroup.getString("groupId"), entity + action)) 183 return true; 184 } 185 186 return false; 187 } 188 189 192 public boolean hasRolePermission(String application, String action, String primaryKey, String role, HttpSession session) { 193 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 194 return hasRolePermission(application, action, primaryKey, role, userLogin); 195 } 196 197 200 public boolean hasRolePermission(String application, String action, String primaryKey, String role, GenericValue userLogin) { 201 List roles = null; 202 if (role != null && !role.equals("")) 203 roles = UtilMisc.toList(role); 204 return hasRolePermission(application, action, primaryKey, roles, userLogin); 205 } 206 207 210 public boolean hasRolePermission(String application, String action, String primaryKey, List roles, HttpSession session) { 211 GenericValue userLogin = (GenericValue) session.getAttribute("userLogin"); 212 return hasRolePermission(application, action, primaryKey, roles, userLogin); 213 } 214 215 218 public boolean hasRolePermission(String application, String action, String primaryKey, List roles, GenericValue userLogin) { 219 String entityName = null; 220 EntityCondition condition = null; 221 222 if (userLogin == null) 223 return false; 224 225 if (primaryKey.equals("") && roles == null) { 227 if (hasEntityPermission(application, action, userLogin)) return true; 228 if (hasEntityPermission(application + "_ROLE", action, userLogin)) return true; 229 } 230 231 Map simpleRoleMap = (Map ) OFBizSecurity.simpleRoleEntity.get(application); 232 if (simpleRoleMap != null && roles != null) { 233 entityName = (String ) simpleRoleMap.get("name"); 234 String pkey = (String ) simpleRoleMap.get("pkey"); 235 if (pkey != null) { 236 List expressions = new ArrayList (); 237 Iterator i = roles.iterator(); 238 while (i.hasNext()) { 239 String role = (String ) i.next(); 240 expressions.add(new EntityExpr("roleTypeId", EntityOperator.EQUALS, role)); 241 } 242 EntityConditionList exprList = new EntityConditionList(expressions, EntityOperator.OR); 243 EntityExpr keyExpr = new EntityExpr(pkey, EntityOperator.EQUALS, primaryKey); 244 EntityExpr partyExpr = new EntityExpr("partyId", EntityOperator.EQUALS, userLogin.getString("partyId")); 245 List joinList = UtilMisc.toList(exprList, keyExpr, partyExpr); 246 condition = new EntityConditionList(joinList, EntityOperator.AND); 247 } 248 249 } 250 251 return hasRolePermission(application, action, entityName, condition, userLogin); 252 } 253 254 266 public boolean hasRolePermission(String application, String action, String entityName, EntityCondition condition, GenericValue userLogin) { 267 if (userLogin == null) return false; 268 269 if (hasEntityPermission(application, action, userLogin)) return true; 271 272 if (entityName == null || condition == null) return false; 274 275 if (hasEntityPermission(application + "_ROLE", action, userLogin)) { 277 List roleTest = null; 279 try { 280 roleTest = delegator.findByCondition(entityName, condition, null, null); 282 } catch (GenericEntityException e) { 283 Debug.logError(e, "Problems doing role security lookup on entity [" + entityName + "] using [" + condition + "]", module); 284 return false; 285 } 286 287 if (roleTest != null && roleTest.size() > 0) return true; 290 } 291 292 return false; 293 } 294 295 } 296 | Popular Tags |