001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.minilang.method.ifops;
019:
020: import java.util.LinkedList;
021: import java.util.List;
022:
023: import org.ofbiz.base.util.UtilXml;
024: import org.ofbiz.base.util.string.FlexibleStringExpander;
025: import org.ofbiz.entity.GenericValue;
026: import org.ofbiz.minilang.SimpleMethod;
027: import org.ofbiz.minilang.method.MethodContext;
028: import org.ofbiz.minilang.method.MethodOperation;
029: import org.ofbiz.security.Security;
030: import org.w3c.dom.Element;
031:
032: /**
033: * Iff the user has the specified permission, process the sub-operations. Otherwise
034: * process else operations if specified.
035: */
036: public class IfHasPermission extends MethodOperation {
037:
038: protected List subOps = new LinkedList();
039: protected List elseSubOps = null;
040:
041: protected FlexibleStringExpander permissionExdr;
042: protected FlexibleStringExpander actionExdr;
043:
044: public IfHasPermission(Element element, SimpleMethod simpleMethod) {
045: super (element, simpleMethod);
046: this .permissionExdr = new FlexibleStringExpander(element
047: .getAttribute("permission"));
048: this .actionExdr = new FlexibleStringExpander(element
049: .getAttribute("action"));
050:
051: SimpleMethod.readOperations(element, subOps, simpleMethod);
052:
053: Element elseElement = UtilXml
054: .firstChildElement(element, "else");
055: if (elseElement != null) {
056: elseSubOps = new LinkedList();
057: SimpleMethod.readOperations(elseElement, elseSubOps,
058: simpleMethod);
059: }
060: }
061:
062: public boolean exec(MethodContext methodContext) {
063: // if conditions fails, always return true; if a sub-op returns false
064: // return false and stop, otherwise return true
065: // return true;
066:
067: // only run subOps if element is empty/null
068: boolean runSubOps = false;
069:
070: // if no user is logged in, treat as if the user does not have permission: do not run subops
071: GenericValue userLogin = methodContext.getUserLogin();
072: if (userLogin != null) {
073: String permission = methodContext
074: .expandString(permissionExdr);
075: String action = methodContext.expandString(actionExdr);
076:
077: Security security = methodContext.getSecurity();
078: if (action != null && action.length() > 0) {
079: // run hasEntityPermission
080: if (security.hasEntityPermission(permission, action,
081: userLogin)) {
082: runSubOps = true;
083: }
084: } else {
085: // run hasPermission
086: if (security.hasPermission(permission, userLogin)) {
087: runSubOps = true;
088: }
089: }
090: }
091:
092: if (runSubOps) {
093: return SimpleMethod.runSubOps(subOps, methodContext);
094: } else {
095: if (elseSubOps != null) {
096: return SimpleMethod
097: .runSubOps(elseSubOps, methodContext);
098: } else {
099: return true;
100: }
101: }
102: }
103:
104: public String rawString() {
105: return "<if-has-permission permission=\"" + this .permissionExdr
106: + "\" action=\"" + this .actionExdr + "\"/>";
107: }
108:
109: public String expandedString(MethodContext methodContext) {
110: // TODO: something more than a stub/dummy
111: return this.rawString();
112: }
113: }
|