01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17: package org.apache.cocoon.portal.tools.transformation;
18:
19: import org.apache.avalon.framework.activity.Disposable;
20: import org.apache.avalon.framework.service.ServiceException;
21: import org.apache.avalon.framework.service.ServiceManager;
22: import org.apache.cocoon.portal.tools.PortalToolManager;
23: import org.apache.cocoon.portal.tools.service.UserRightsService;
24: import org.apache.cocoon.transformation.AbstractSAXTransformer;
25: import org.apache.cocoon.xml.IncludeXMLConsumer;
26: import org.w3c.dom.DocumentFragment;
27: import org.xml.sax.Attributes;
28: import org.xml.sax.SAXException;
29:
30: /**
31: *
32: * @version $Id: CheckAccessTransformer.java 433543 2006-08-22 06:22:54Z crossley $
33: */
34: public class CheckAccessTransformer extends AbstractSAXTransformer
35: implements Disposable {
36:
37: public static final String RIGHTS_NAMESPACE_URI = "http://apache.org/cocoon/portal/tools/rights/1.0";
38:
39: public static final String ACCESS_TAG = "access";
40: public static final String RIGHT_ID = "id";
41:
42: private UserRightsService urs;
43: private PortalToolManager ptm;
44:
45: /* (non-Javadoc)
46: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#service(org.apache.avalon.framework.service.ServiceManager)
47: */
48: public void service(ServiceManager manager) throws ServiceException {
49: super .service(manager);
50: ptm = (PortalToolManager) this .manager
51: .lookup(PortalToolManager.ROLE);
52: urs = ptm.getUserRightsService();
53: }
54:
55: /* (non-Javadoc)
56: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#startElement(java.lang.String, java.lang.String, java.lang.String, org.xml.sax.Attributes)
57: */
58: public void startElement(String uri, String name, String raw,
59: Attributes attr) throws SAXException {
60: if (RIGHTS_NAMESPACE_URI.equals(uri) && ACCESS_TAG.equals(name)) {
61: String id = attr.getValue(RIGHT_ID);
62: if (!urs.userIsAllowed(id, this .ptm.getPortalObjects()
63: .getProfileManager().getUser())) {
64: this .stack.push(new Boolean(false));
65: }
66: this .startRecording();
67: } else {
68: super .startElement(uri, name, raw, attr);
69: }
70: }
71:
72: /* (non-Javadoc)
73: * @see org.apache.cocoon.transformation.AbstractSAXTransformer#endElement(java.lang.String, java.lang.String, java.lang.String)
74: */
75: public void endElement(String uri, String name, String raw)
76: throws SAXException {
77: if (RIGHTS_NAMESPACE_URI.equals(uri) && ACCESS_TAG.equals(name)) {
78: DocumentFragment frag = this .endRecording();
79: Boolean ignore = (Boolean) this .stack.pop();
80: if (!ignore.booleanValue())
81: IncludeXMLConsumer.includeNode(frag,
82: this .contentHandler, this .lexicalHandler);
83: } else {
84: super .endElement(uri, name, raw);
85: }
86: }
87:
88: /* (non-Javadoc)
89: * @see org.apache.avalon.framework.activity.Disposable#dispose()
90: */
91: public void dispose() {
92: if (this.manager != null) {
93: this.manager.release(ptm);
94: ptm = null;
95: this.manager = null;
96: }
97: super.dispose();
98: }
99: }
|