KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > minilang > method > conditional > HasPermissionCondition


1 /*
2  * $Id: HasPermissionCondition.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2001-2005 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.conditional;
25
26 import org.ofbiz.base.util.UtilValidate;
27 import org.ofbiz.entity.GenericValue;
28 import org.ofbiz.minilang.SimpleMethod;
29 import org.ofbiz.minilang.method.MethodContext;
30 import org.ofbiz.security.Security;
31 import org.w3c.dom.Element JavaDoc;
32
33 /**
34  * Implements compare to a constant condition.
35  *
36  * @author <a HREF="mailto:jonesde@ofbiz.org">David E. Jones</a>
37  * @version $Rev: 5462 $
38  * @since 2.1
39  */

40 public class HasPermissionCondition implements Conditional {
41     
42     SimpleMethod simpleMethod;
43     
44     String JavaDoc permission;
45     String JavaDoc action;
46     
47     public HasPermissionCondition(Element JavaDoc element, SimpleMethod simpleMethod) {
48         this.simpleMethod = simpleMethod;
49         
50         this.permission = element.getAttribute("permission");
51         this.action = element.getAttribute("action");
52     }
53
54     public boolean checkCondition(MethodContext methodContext) {
55         // only run subOps if element is empty/null
56
boolean runSubOps = false;
57
58         // if no user is logged in, treat as if the user does not have permission: do not run subops
59
GenericValue userLogin = methodContext.getUserLogin();
60         if (userLogin != null) {
61             String JavaDoc permission = methodContext.expandString(this.permission);
62             String JavaDoc action = methodContext.expandString(this.action);
63             
64             Security security = methodContext.getSecurity();
65             if (action != null && action.length() > 0) {
66                 // run hasEntityPermission
67
if (security.hasEntityPermission(permission, action, userLogin)) {
68                     runSubOps = true;
69                 }
70             } else {
71                 // run hasPermission
72
if (security.hasPermission(permission, userLogin)) {
73                     runSubOps = true;
74                 }
75             }
76         }
77         
78         return runSubOps;
79     }
80
81     public void prettyPrint(StringBuffer JavaDoc messageBuffer, MethodContext methodContext) {
82         messageBuffer.append("has-permission[");
83         messageBuffer.append(this.permission);
84         if (UtilValidate.isNotEmpty(this.action)) {
85             messageBuffer.append(":");
86             messageBuffer.append(this.action);
87         }
88         messageBuffer.append("]");
89     }
90 }
91
Popular Tags