001: /*
002: * $Id: ModelPermission.java,v 1.1 2004/02/11 16:49:36 ajzeneski Exp $
003: *
004: * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a
007: * copy of this software and associated documentation files (the "Software"),
008: * to deal in the Software without restriction, including without limitation
009: * the rights to use, copy, modify, merge, publish, distribute, sublicense,
010: * and/or sell copies of the Software, and to permit persons to whom the
011: * Software is furnished to do so, subject to the following conditions:
012: *
013: * The above copyright notice and this permission notice shall be included
014: * in all copies or substantial portions of the Software.
015: *
016: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
017: * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
018: * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
019: * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
020: * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
021: * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
022: * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
023: *
024: */
025: package org.ofbiz.service;
026:
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.entity.GenericDelegator;
029: import org.ofbiz.entity.GenericEntityException;
030: import org.ofbiz.entity.util.EntityUtil;
031: import org.ofbiz.security.Security;
032: import org.ofbiz.base.util.UtilMisc;
033: import org.ofbiz.base.util.Debug;
034:
035: import java.util.List;
036:
037: /**
038: * Service Permission Model Class
039: *
040: * @author <a href="mailto:jaz@ofbiz.org">Andy Zeneski</a>
041: * @version $Revision: 1.1 $
042: * @since 3.0
043: */
044: public class ModelPermission {
045:
046: public static final String module = ModelPermission.class.getName();
047:
048: public static final int PERMISSION = 1;
049: public static final int ENTITY_PERMISSION = 2;
050: public static final int ROLE_MEMBER = 3;
051:
052: public ModelService serviceModel = null;
053: public int permissionType = 0;
054: public String nameOrRole = null;
055: public String action = null;
056:
057: public boolean evalPermission(Security security,
058: GenericValue userLogin) {
059: if (userLogin == null) {
060: Debug
061: .logInfo(
062: "Secure service requested with no userLogin object",
063: module);
064: return false;
065: }
066: switch (permissionType) {
067: case 1:
068: return evalSimplePermission(security, userLogin);
069: case 2:
070: return evalEntityPermission(security, userLogin);
071: case 3:
072: return evalRoleMember(userLogin);
073: default:
074: Debug.logWarning(
075: "Invalid permission type [" + permissionType
076: + "] for permission named : " + nameOrRole
077: + " on service : " + serviceModel.name,
078: module);
079: return false;
080: }
081: }
082:
083: private boolean evalSimplePermission(Security security,
084: GenericValue userLogin) {
085: if (nameOrRole == null) {
086: Debug.logWarning(
087: "Null permission name passed for evaluation",
088: module);
089: return false;
090: }
091: return security.hasPermission(nameOrRole, userLogin);
092: }
093:
094: private boolean evalEntityPermission(Security security,
095: GenericValue userLogin) {
096: if (nameOrRole == null) {
097: Debug.logWarning(
098: "Null permission name passed for evaluation",
099: module);
100: return false;
101: }
102: if (action == null) {
103: Debug.logWarning("Null action passed for evaluation",
104: module);
105: }
106: return security.hasEntityPermission(nameOrRole, action,
107: userLogin);
108: }
109:
110: private boolean evalRoleMember(GenericValue userLogin) {
111: if (nameOrRole == null) {
112: Debug
113: .logWarning(
114: "Null role type name passed for evaluation",
115: module);
116: return false;
117: }
118: GenericDelegator delegator = userLogin.getDelegator();
119: List partyRoles = null;
120: try {
121: partyRoles = delegator.findByAnd("PartyRole", UtilMisc
122: .toMap("roleTypeId", nameOrRole, "partyId",
123: userLogin.get("partyId")));
124: } catch (GenericEntityException e) {
125: Debug.logError(e, "Unable to lookup PartyRole records",
126: module);
127: }
128:
129: if (partyRoles != null && partyRoles.size() > 0) {
130: partyRoles = EntityUtil.filterByDate(partyRoles);
131: if (partyRoles != null && partyRoles.size() > 0) {
132: return true;
133: }
134: }
135: return false;
136: }
137: }
|