KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > ifops > IfHasPermission


1 /*
2  * $Id: IfHasPermission.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001, 2002 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */

24 package org.ofbiz.minilang.method.ifops;
25
26 import java.util.LinkedList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import org.ofbiz.base.util.UtilXml;
30 import org.ofbiz.base.util.string.FlexibleStringExpander;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.minilang.SimpleMethod;
33 import org.ofbiz.minilang.method.MethodContext;
34 import org.ofbiz.minilang.method.MethodOperation;
35 import org.ofbiz.security.Security;
36 import org.w3c.dom.Element JavaDoc;
37
38 /**
39  * Iff the user has the specified permission, process the sub-operations. Otherwise
40  * process else operations if specified.
41  *
42  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
43  * @version $Rev: 5462 $
44  * @since 2.0
45  */

46 public class IfHasPermission extends MethodOperation {
47
48     protected List JavaDoc subOps = new LinkedList JavaDoc();
49     protected List JavaDoc elseSubOps = null;
50
51     protected FlexibleStringExpander permissionExdr;
52     protected FlexibleStringExpander actionExdr;
53
54     public IfHasPermission(Element JavaDoc element, SimpleMethod simpleMethod) {
55         super(element, simpleMethod);
56         this.permissionExdr = new FlexibleStringExpander(element.getAttribute("permission"));
57         this.actionExdr = new FlexibleStringExpander(element.getAttribute("action"));
58
59         SimpleMethod.readOperations(element, subOps, simpleMethod);
60
61         Element JavaDoc elseElement = UtilXml.firstChildElement(element, "else");
62         if (elseElement != null) {
63             elseSubOps = new LinkedList JavaDoc();
64             SimpleMethod.readOperations(elseElement, elseSubOps, simpleMethod);
65         }
66     }
67
68     public boolean exec(MethodContext methodContext) {
69         // if conditions fails, always return true; if a sub-op returns false
70
// return false and stop, otherwise return true
71
// return true;
72

73         // only run subOps if element is empty/null
74
boolean runSubOps = false;
75
76         // if no user is logged in, treat as if the user does not have permission: do not run subops
77
GenericValue userLogin = methodContext.getUserLogin();
78         if (userLogin != null) {
79             String JavaDoc permission = methodContext.expandString(permissionExdr);
80             String JavaDoc action = methodContext.expandString(actionExdr);
81             
82             Security security = methodContext.getSecurity();
83             if (action != null && action.length() > 0) {
84                 // run hasEntityPermission
85
if (security.hasEntityPermission(permission, action, userLogin)) {
86                     runSubOps = true;
87                 }
88             } else {
89                 // run hasPermission
90
if (security.hasPermission(permission, userLogin)) {
91                     runSubOps = true;
92                 }
93             }
94         }
95
96         if (runSubOps) {
97             return SimpleMethod.runSubOps(subOps, methodContext);
98         } else {
99             if (elseSubOps != null) {
100                 return SimpleMethod.runSubOps(elseSubOps, methodContext);
101             } else {
102                 return true;
103             }
104         }
105     }
106
107     public String JavaDoc rawString() {
108         return "<if-has-permission permission=\"" + this.permissionExdr + "\" action=\"" + this.actionExdr + "\"/>";
109     }
110     public String JavaDoc expandedString(MethodContext methodContext) {
111         // TODO: something more than a stub/dummy
112
return this.rawString();
113     }
114 }
115
Popular Tags