KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > content > webapp > ftl > CheckPermissionTransform


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

17 package org.ofbiz.content.webapp.ftl;
18
19 import java.io.IOException JavaDoc;
20 import java.io.Writer JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.List JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27
28 import org.ofbiz.base.util.GeneralException;
29 import org.ofbiz.base.util.StringUtil;
30 import org.ofbiz.base.util.UtilValidate;
31 import org.ofbiz.base.util.template.FreeMarkerWorker;
32 import org.ofbiz.content.content.ContentWorker;
33 import org.ofbiz.content.content.PermissionRecorder;
34 import org.ofbiz.entity.GenericDelegator;
35 import org.ofbiz.entity.GenericValue;
36 import org.ofbiz.entityext.permission.EntityPermissionChecker;
37 import org.ofbiz.security.Security;
38 import org.ofbiz.service.ModelService;
39 import org.ofbiz.webapp.ftl.LoopWriter;
40
41 import freemarker.core.Environment;
42 import freemarker.template.TemplateModelException;
43 import freemarker.template.TemplateTransformModel;
44 import freemarker.template.TransformControl;
45
46 /**
47  * CheckPermissionTransform - Freemarker Transform for URLs (links)
48  *
49  * @author <a HREF="mailto:byersa@automationgroups.com">Al Byers</a>
50  * @version $Rev: 5462 $
51  * @since 3.0
52  */

53 public class CheckPermissionTransform implements TemplateTransformModel {
54
55     public static final String JavaDoc module = CheckPermissionTransform.class.getName();
56
57     public static final String JavaDoc [] saveKeyNames = {"globalNodeTrail", "nodeTrail", "mode", "purposeTypeId", "statusId", "entityOperation", "targetOperation" };
58     public static final String JavaDoc [] removeKeyNames = {};
59
60     /**
61      * A wrapper for the FreeMarkerWorker version.
62      */

63     public static Object JavaDoc getWrappedObject(String JavaDoc varName, Environment env) {
64         return FreeMarkerWorker.getWrappedObject(varName, env);
65     }
66
67     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Environment env) {
68         return FreeMarkerWorker.getArg(args, key, env);
69     }
70
71     public static String JavaDoc getArg(Map JavaDoc args, String JavaDoc key, Map JavaDoc ctx) {
72         return FreeMarkerWorker.getArg(args, key, ctx);
73     }
74
75
76     public Writer JavaDoc getWriter(final Writer JavaDoc out, Map JavaDoc args) {
77         final StringBuffer JavaDoc buf = new StringBuffer JavaDoc();
78         final Environment env = Environment.getCurrentEnvironment();
79         final Map JavaDoc templateCtx = FreeMarkerWorker.createEnvironmentMap(env);
80         //FreeMarkerWorker.convertContext(templateCtx);
81
final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker.getWrappedObject("delegator", env);
82         final HttpServletRequest JavaDoc request = (HttpServletRequest JavaDoc) FreeMarkerWorker.getWrappedObject("request", env);
83         final GenericValue userLogin = (GenericValue) FreeMarkerWorker.getWrappedObject("userLogin", env);
84         FreeMarkerWorker.getSiteParameters(request, templateCtx);
85         FreeMarkerWorker.overrideWithArgs(templateCtx, args);
86         final String JavaDoc mode = (String JavaDoc)templateCtx.get("mode");
87         final String JavaDoc quickCheckContentId = (String JavaDoc)templateCtx.get("quickCheckContentId");
88         final Map JavaDoc savedValues = new HashMap JavaDoc();
89                     //Debug.logInfo("in CheckPermission, contentId(1):" + templateCtx.get("contentId"),"");
90
//Debug.logInfo("in CheckPermission, subContentId(1):" + templateCtx.get("subContentId"),"");
91

92         return new LoopWriter(out) {
93
94             public void write(char cbuf[], int off, int len) {
95                 buf.append(cbuf, off, len);
96             }
97
98             public void flush() throws IOException JavaDoc {
99                 out.flush();
100             }
101
102             public int onStart() throws TemplateModelException, IOException JavaDoc {
103                 List JavaDoc trail = (List JavaDoc)templateCtx.get("globalNodeTrail");
104                 String JavaDoc trailCsv = ContentWorker.nodeTrailToCsv(trail);
105                     //Debug.logInfo("in CheckPermission, trailCsv(2):" + trailCsv,"");
106
//Debug.logInfo("in CheckPermission, contentId(2):" + templateCtx.get("contentId"),"");
107
//Debug.logInfo("in CheckPermission, subContentId(2):" + templateCtx.get("subContentId"),"");
108

109                 GenericValue currentContent = null;
110         String JavaDoc contentAssocPredicateId = (String JavaDoc)templateCtx.get("contentAssocPredicateId");
111                 String JavaDoc strNullThruDatesOnly = (String JavaDoc)templateCtx.get("nullThruDatesOnly");
112                 Boolean JavaDoc nullThruDatesOnly = (strNullThruDatesOnly != null && strNullThruDatesOnly.equalsIgnoreCase("true")) ? new Boolean JavaDoc(true) :new Boolean JavaDoc(false);
113                 GenericValue val = null;
114                 try {
115                     val = ContentWorker.getCurrentContent(delegator, trail, userLogin, templateCtx, nullThruDatesOnly, contentAssocPredicateId);
116                 } catch(GeneralException e) {
117                     throw new RuntimeException JavaDoc("Error getting current content. " + e.toString());
118                 }
119                 final GenericValue view = val;
120                 currentContent = val;
121                 if (currentContent != null) {
122                     //Debug.logInfo("in CheckPermission, currentContent(0):" + currentContent.get("contentId"),"");
123
}
124
125                 if (currentContent == null) {
126                     currentContent = delegator.makeValue("Content", null);
127                     currentContent.put("ownerContentId", templateCtx.get("ownerContentId"));
128                 }
129                     //Debug.logInfo("in CheckPermission, currentContent(1):" + currentContent.get("contentId"),"");
130

131                 Security security = null;
132                 if (request != null) {
133                     security = (Security) request.getAttribute("security");
134                 }
135              
136                 String JavaDoc statusId = (String JavaDoc)currentContent.get("statusId");
137                 String JavaDoc passedStatusId = (String JavaDoc)templateCtx.get("statusId");
138                 List JavaDoc statusList = StringUtil.split(passedStatusId, "|");
139                 if (statusList == null)
140                     statusList = new ArrayList JavaDoc();
141                 if (UtilValidate.isNotEmpty(statusId) && !statusList.contains(statusId)) {
142                     statusList.add(statusId);
143                 }
144                 String JavaDoc targetPurpose = (String JavaDoc)templateCtx.get("contentPurposeList");
145                 List JavaDoc purposeList = StringUtil.split(targetPurpose, "|");
146                 String JavaDoc entityOperation = (String JavaDoc)templateCtx.get("entityOperation");
147                 String JavaDoc targetOperation = (String JavaDoc)templateCtx.get("targetOperation");
148                 if (UtilValidate.isEmpty(targetOperation)) {
149                     if (UtilValidate.isNotEmpty(entityOperation))
150                         targetOperation = "CONTENT" + entityOperation;
151                 }
152                 List JavaDoc targetOperationList = StringUtil.split(targetOperation, "|");
153                 if (targetOperationList.size() == 0) {
154                     //Debug.logInfo("in CheckPermission, entityOperation:" + entityOperation,"");
155
//Debug.logInfo("in CheckPermission, templateCtx:" + templateCtx,"");
156
throw new IOException JavaDoc("targetOperationList has zero size.");
157                 }
158                 List JavaDoc roleList = new ArrayList JavaDoc();
159         
160                 String JavaDoc privilegeEnumId = (String JavaDoc)currentContent.get("privilegeEnumId");
161                 Map JavaDoc results = EntityPermissionChecker.checkPermission(currentContent, statusList, userLogin, purposeList, targetOperationList, roleList, delegator, security, entityOperation, privilegeEnumId, quickCheckContentId);
162
163                 boolean isError = ModelService.RESPOND_ERROR.equals(results.get(ModelService.RESPONSE_MESSAGE));
164                 if (isError) {
165                     throw new IOException JavaDoc(ModelService.RESPONSE_MESSAGE);
166                 }
167
168                 String JavaDoc permissionStatus = (String JavaDoc) results.get("permissionStatus");
169
170                 if (UtilValidate.isEmpty(permissionStatus) || !permissionStatus.equals("granted")) {
171                 
172                     String JavaDoc errorMessage = "Permission to add response is denied (2)";
173                     PermissionRecorder recorder = (PermissionRecorder)results.get("permissionRecorder");
174                         //Debug.logInfo("recorder(0):" + recorder, "");
175
if (recorder != null) {
176                         String JavaDoc permissionMessage = recorder.toHtml();
177                         //Debug.logInfo("permissionMessage(0):" + permissionMessage, "");
178
errorMessage += " \n " + permissionMessage;
179                     }
180                     templateCtx.put("permissionErrorMsg", errorMessage);
181                 }
182
183
184                 if (permissionStatus != null && permissionStatus.equalsIgnoreCase("granted")) {
185                     FreeMarkerWorker.saveContextValues(templateCtx, saveKeyNames, savedValues);
186                     if (mode == null || !mode.equalsIgnoreCase("not-equals"))
187                         return TransformControl.EVALUATE_BODY;
188                     else
189                         return TransformControl.SKIP_BODY;
190                 } else {
191                     if (mode == null || !mode.equalsIgnoreCase("not-equals"))
192                         return TransformControl.SKIP_BODY;
193                     else
194                         return TransformControl.EVALUATE_BODY;
195                 }
196             }
197
198
199             public void close() throws IOException JavaDoc {
200                 FreeMarkerWorker.reloadValues(templateCtx, savedValues, env);
201                 String JavaDoc wrappedContent = buf.toString();
202                 out.write(wrappedContent);
203             }
204         };
205     }
206 }
207
Popular Tags