1 25 package org.ofbiz.service; 26 27 import org.ofbiz.entity.GenericValue; 28 import org.ofbiz.entity.GenericDelegator; 29 import org.ofbiz.entity.GenericEntityException; 30 import org.ofbiz.entity.util.EntityUtil; 31 import org.ofbiz.security.Security; 32 import org.ofbiz.base.util.UtilMisc; 33 import org.ofbiz.base.util.Debug; 34 35 import java.util.List ; 36 import java.io.Serializable ; 37 38 45 public class ModelPermission implements Serializable { 46 47 public static final String module = ModelPermission.class.getName(); 48 49 public static final int PERMISSION = 1; 50 public static final int ENTITY_PERMISSION = 2; 51 public static final int ROLE_MEMBER = 3; 52 53 public ModelService serviceModel = null; 54 public int permissionType = 0; 55 public String nameOrRole = null; 56 public String action = null; 57 58 public boolean evalPermission(Security security, GenericValue userLogin) { 59 if (userLogin == null) { 60 Debug.logInfo("Secure service requested with no userLogin object", module); 61 return false; 62 } 63 switch (permissionType) { 64 case 1: 65 return evalSimplePermission(security, userLogin); 66 case 2: 67 return evalEntityPermission(security, userLogin); 68 case 3: 69 return evalRoleMember(userLogin); 70 default: 71 Debug.logWarning("Invalid permission type [" + permissionType + "] for permission named : " + nameOrRole + " on service : " + serviceModel.name, module); 72 return false; 73 } 74 } 75 76 private boolean evalSimplePermission(Security security, GenericValue userLogin) { 77 if (nameOrRole == null) { 78 Debug.logWarning("Null permission name passed for evaluation", module); 79 return false; 80 } 81 return security.hasPermission(nameOrRole, userLogin); 82 } 83 84 private boolean evalEntityPermission(Security security, GenericValue userLogin) { 85 if (nameOrRole == null) { 86 Debug.logWarning("Null permission name passed for evaluation", module); 87 return false; 88 } 89 if (action == null) { 90 Debug.logWarning("Null action passed for evaluation", module); 91 } 92 return security.hasEntityPermission(nameOrRole, action, userLogin); 93 } 94 95 private boolean evalRoleMember(GenericValue userLogin) { 96 if (nameOrRole == null) { 97 Debug.logWarning("Null role type name passed for evaluation", module); 98 return false; 99 } 100 GenericDelegator delegator = userLogin.getDelegator(); 101 List partyRoles = null; 102 try { 103 partyRoles = delegator.findByAnd("PartyRole", UtilMisc.toMap("roleTypeId", nameOrRole, "partyId", userLogin.get("partyId"))); 104 } catch (GenericEntityException e) { 105 Debug.logError(e, "Unable to lookup PartyRole records", module); 106 } 107 108 if (partyRoles != null && partyRoles.size() > 0) { 109 partyRoles = EntityUtil.filterByDate(partyRoles); 110 if (partyRoles != null && partyRoles.size() > 0) { 111 return true; 112 } 113 } 114 return false; 115 } 116 } 117 | Popular Tags |