001: /*
002: * $Id: CheckPermissionTransform.java,v 1.3 2004/01/13 06:16:29 byersa Exp $
003: *
004: * Copyright (c) 2001-2003 The Open For Business Project - www.ofbiz.org
005: *
006: * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal
007: * in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
008: * copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
009: *
010: * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
011: *
012: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
013: * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
014: * 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.
015: *
016: */
017: package org.ofbiz.content.webapp.ftl;
018:
019: import java.io.IOException;
020: import java.io.Writer;
021: import java.sql.Timestamp;
022: import java.util.List;
023: import java.util.Locale;
024: import java.util.Map;
025: import java.util.ArrayList;
026: import javax.servlet.http.HttpServletRequest;
027:
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.base.util.GeneralException;
030: import org.ofbiz.base.util.UtilDateTime;
031: import org.ofbiz.base.util.UtilMisc;
032: import org.ofbiz.base.util.UtilValidate;
033: import org.ofbiz.content.content.ContentServicesComplex;
034: import org.ofbiz.content.content.ContentPermissionServices;
035: import org.ofbiz.content.content.ContentWorker;
036: import org.ofbiz.entity.GenericDelegator;
037: import org.ofbiz.entity.GenericEntityException;
038: import org.ofbiz.entity.GenericValue;
039: import org.ofbiz.minilang.MiniLangException;
040: import org.ofbiz.service.ModelService;
041: import org.ofbiz.security.Security;
042:
043: import freemarker.template.Environment;
044: import freemarker.template.SimpleHash;
045: import freemarker.template.TemplateHashModel;
046: import freemarker.template.TemplateTransformModel;
047: import freemarker.template.TransformControl;
048: import freemarker.template.TemplateModelException;
049:
050: /**
051: * CheckPermissionTransform - Freemarker Transform for URLs (links)
052: *
053: * @author <a href="mailto:byersa@automationgroups.com">Al Byers</a>
054: * @version $Revision: 1.3 $
055: * @since 3.0
056: */
057: public class CheckPermissionTransform implements TemplateTransformModel {
058:
059: public static final String module = CheckPermissionTransform.class
060: .getName();
061:
062: public static final String[] saveKeyNames = { "globalNodeTrail",
063: "nodeTrail" };
064: public static final String[] removeKeyNames = {};
065:
066: /**
067: * A wrapper for the FreeMarkerWorker version.
068: */
069: public static Object getWrappedObject(String varName,
070: Environment env) {
071: return FreeMarkerWorker.getWrappedObject(varName, env);
072: }
073:
074: public static String getArg(Map args, String key, Environment env) {
075: return FreeMarkerWorker.getArg(args, key, env);
076: }
077:
078: public static String getArg(Map args, String key, Map ctx) {
079: return FreeMarkerWorker.getArg(args, key, ctx);
080: }
081:
082: public Writer getWriter(final Writer out, Map args) {
083: final StringBuffer buf = new StringBuffer();
084: final Environment env = Environment.getCurrentEnvironment();
085: final Map templateCtx = (Map) FreeMarkerWorker
086: .getWrappedObject("context", env);
087: //FreeMarkerWorker.convertContext(templateCtx);
088: final GenericDelegator delegator = (GenericDelegator) FreeMarkerWorker
089: .getWrappedObject("delegator", env);
090: final HttpServletRequest request = (HttpServletRequest) FreeMarkerWorker
091: .getWrappedObject("request", env);
092: FreeMarkerWorker.getSiteParameters(request, templateCtx);
093: //templateCtx.put("buf", buf);
094: if (Debug.verboseOn())
095: Debug.logVerbose(FreeMarkerWorker.logMap("(C)before save",
096: templateCtx, 0), module);
097: final Map savedValues = FreeMarkerWorker.saveValues(
098: templateCtx, saveKeyNames);
099: if (Debug.verboseOn())
100: Debug
101: .logVerbose("(C-0)savedValues: " + savedValues,
102: module);
103: FreeMarkerWorker.overrideWithArgs(templateCtx, args);
104: if (Debug.verboseOn())
105: Debug.logVerbose(FreeMarkerWorker.logMap(
106: "(C)after overrride", templateCtx, 0), module);
107:
108: return new LoopWriter(out) {
109:
110: public void write(char cbuf[], int off, int len) {
111: buf.append(cbuf, off, len);
112: if (Debug.verboseOn())
113: Debug.logVerbose("in CheckPerm, buf:"
114: + buf.toString(), module);
115: }
116:
117: public void flush() throws IOException {
118: out.flush();
119: }
120:
121: public int onStart() throws TemplateModelException,
122: IOException {
123: List trail = (List) templateCtx.get("globalNodeTrail");
124: List passedGlobalNodeTrail = null;
125: if (trail != null)
126: passedGlobalNodeTrail = new ArrayList(trail);
127: else
128: passedGlobalNodeTrail = new ArrayList();
129: int sz = passedGlobalNodeTrail.size();
130:
131: GenericValue view = null;
132: String contentId = (String) templateCtx
133: .get("contentId");
134: String subContentId = (String) templateCtx
135: .get("subContentId");
136: if (UtilValidate.isNotEmpty(subContentId)
137: || UtilValidate.isNotEmpty(contentId)) {
138: String this ContentId = contentId;
139: if (UtilValidate.isEmpty(this ContentId))
140: this ContentId = subContentId;
141:
142: if (UtilValidate.isNotEmpty(this ContentId)) {
143:
144: try {
145: view = delegator
146: .findByPrimaryKeyCache("Content",
147: UtilMisc.toMap("contentId",
148: this ContentId));
149: } catch (GenericEntityException e) {
150: Debug
151: .logError(
152: e,
153: "Error getting sub-content",
154: module);
155: throw new RuntimeException(e.getMessage());
156: }
157: passedGlobalNodeTrail.add(FreeMarkerWorker
158: .makeNode(view));
159: }
160: } else {
161: if (sz > 0) {
162: view = (GenericValue) passedGlobalNodeTrail
163: .get(sz - 1);
164: }
165: }
166: if (view == null) {
167: view = delegator.makeValue("Content", null);
168: view.put("ownerContentId", templateCtx
169: .get("ownerContentId"));
170: }
171:
172: GenericValue userLogin = null;
173: Security security = null;
174: if (request != null) {
175: userLogin = (GenericValue) request.getSession()
176: .getAttribute("userLogin");
177: security = (Security) request
178: .getAttribute("security");
179: }
180: if (userLogin == null) {
181: userLogin = (GenericValue) templateCtx
182: .get("userLogin");
183: }
184:
185: String statusId = (String) templateCtx.get("statusId");
186: String targetPurpose = (String) templateCtx
187: .get("purposeTypeId");
188: List purposeList = new ArrayList();
189: if (UtilValidate.isNotEmpty(targetPurpose))
190: purposeList.add(targetPurpose);
191: String entityAction = (String) templateCtx
192: .get("entityAction");
193: String targetOperation = (String) templateCtx
194: .get("targetOperation");
195: if (UtilValidate.isEmpty(targetOperation)) {
196: if (UtilValidate.isNotEmpty(entityAction))
197: targetOperation = "CONTENT" + entityAction;
198: }
199: List targetOperationList = new ArrayList();
200: if (UtilValidate.isNotEmpty(targetOperation))
201: targetOperationList.add(targetOperation);
202: List roleList = new ArrayList();
203:
204: if (Debug.verboseOn())
205: Debug.logVerbose("in CheckPermission, view" + view,
206: module);
207: Map results = ContentPermissionServices
208: .checkPermission(view, statusId, userLogin,
209: purposeList, targetOperationList,
210: roleList, delegator, security,
211: entityAction);
212: if (Debug.verboseOn())
213: Debug.logVerbose("in CheckPermission, results"
214: + results, module);
215:
216: boolean isError = ModelService.RESPOND_ERROR
217: .equals(results
218: .get(ModelService.RESPONSE_MESSAGE));
219: if (isError) {
220: throw new IOException(ModelService.RESPONSE_MESSAGE);
221: }
222:
223: String permissionStatus = (String) results
224: .get("permissionStatus");
225:
226: if (permissionStatus != null
227: && permissionStatus.equalsIgnoreCase("granted")) {
228: if (Debug.verboseOn())
229: Debug.logVerbose(
230: "in CheckPermission, permissionStatus"
231: + permissionStatus, module);
232: return TransformControl.EVALUATE_BODY;
233: } else {
234: return TransformControl.SKIP_BODY;
235: }
236: }
237:
238: public void close() throws IOException {
239: String wrappedFTL = buf.toString();
240: if (Debug.verboseOn())
241: Debug.logVerbose("in CheckPerm, wrappedFTL:"
242: + wrappedFTL, module);
243: out.write(wrappedFTL);
244: if (Debug.verboseOn())
245: Debug
246: .logVerbose(
247: FreeMarkerWorker.logMap(
248: "(C)before remove",
249: templateCtx, 0), module);
250: FreeMarkerWorker.removeValues(templateCtx,
251: removeKeyNames);
252: if (Debug.verboseOn())
253: Debug.logVerbose(FreeMarkerWorker.logMap(
254: "(C)after remove", templateCtx, 0), module);
255: FreeMarkerWorker.reloadValues(templateCtx, savedValues);
256: if (Debug.verboseOn())
257: Debug.logVerbose(FreeMarkerWorker.logMap(
258: "(C)after reload", templateCtx, 0), module);
259: }
260: };
261: }
262: }
|