001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: /* $Id: AccessControlTest.java 408702 2006-05-22 16:03:49Z andreas $ */
020:
021: package org.apache.lenya.ac.impl;
022:
023: import java.io.File;
024:
025: import org.apache.avalon.framework.service.ServiceSelector;
026: import org.apache.lenya.ac.AccessControlException;
027: import org.apache.lenya.ac.AccessControllerResolver;
028: import org.apache.lenya.ac.Accreditable;
029: import org.apache.lenya.ac.AccreditableManager;
030: import org.apache.lenya.ac.Identity;
031: import org.apache.lenya.ac.PolicyManager;
032: import org.apache.lenya.ac.User;
033: import org.apache.lenya.ac.file.FileAccreditableManager;
034: import org.apache.lenya.cms.LenyaTestCase;
035: import org.apache.lenya.cms.ac.PublicationAccessControllerResolver;
036: import org.apache.lenya.cms.publication.DocumentFactory;
037: import org.apache.lenya.cms.publication.DocumentUtil;
038: import org.apache.lenya.cms.publication.Publication;
039: import org.apache.lenya.cms.publication.PublicationException;
040: import org.apache.lenya.cms.repository.Session;
041: import org.apache.lenya.cms.repository.SessionImpl;
042:
043: /**
044: * To change the template for this generated type comment go to
045: * Window>Preferences>Java>Code Generation>Code and Comments
046: */
047: public class AbstractAccessControlTest extends LenyaTestCase {
048:
049: protected static final String TEST_PUB_ID = "test";
050: private ServiceSelector accessControllerResolverSelector;
051: private AccessControllerResolver accessControllerResolver;
052: private DefaultAccessController accessController;
053:
054: protected org.apache.lenya.cms.repository.Session login(
055: String userId) throws AccessControlException {
056:
057: Session session = new SessionImpl(null, true, getManager(),
058: getLogger());
059:
060: DefaultAccessController ac = getAccessController(session,
061: TEST_PUB_ID);
062: AccreditableManager acMgr = ac.getAccreditableManager();
063: User user = acMgr.getUserManager().getUser(userId);
064:
065: if (user == null) {
066: throw new AccessControlException("The user [" + userId
067: + "] does not exist!");
068: }
069:
070: ac.setupIdentity(getRequest());
071:
072: org.apache.cocoon.environment.Session cocoonSession = getRequest()
073: .getSession();
074: Identity identity = (Identity) cocoonSession
075: .getAttribute(Identity.class.getName());
076:
077: if (!identity.contains(user)) {
078: User oldUser = identity.getUser();
079: if (oldUser != null) {
080: if (getLogger().isDebugEnabled()) {
081: getLogger().debug(
082: "Removing user [" + oldUser
083: + "] from identity.");
084: }
085: identity.removeIdentifiable(oldUser);
086: }
087: identity.addIdentifiable(user);
088: }
089:
090: ac.authorize(getRequest());
091:
092: Accreditable[] accrs = identity.getAccreditables();
093: for (int i = 0; i < accrs.length; i++) {
094: getLogger().info("Accreditable: " + accrs[i]);
095: }
096:
097: session.setIdentity(identity);
098: return session;
099: }
100:
101: protected DefaultAccessController getAccessController() {
102: return getAccessController(getSession(), TEST_PUB_ID);
103: }
104:
105: protected DefaultAccessController getAccessController(
106: Session session, String pubId) {
107: DefaultAccessController controller;
108: try {
109: this .accessControllerResolverSelector = (ServiceSelector) getManager()
110: .lookup(AccessControllerResolver.ROLE + "Selector");
111: assertNotNull(this .accessControllerResolverSelector);
112:
113: this .accessControllerResolver = (AccessControllerResolver) this .accessControllerResolverSelector
114: .select(AccessControllerResolver.DEFAULT_RESOLVER);
115:
116: assertNotNull(this .accessControllerResolver);
117: getLogger().info(
118: "Using access controller resolver: ["
119: + this .accessControllerResolver.getClass()
120: + "]");
121:
122: Publication pub = getPublication(session, pubId);
123: getLogger().info("Resolve access controller");
124: getLogger().info(
125: "Publication directory: ["
126: + pub.getDirectory().getAbsolutePath()
127: + "]");
128:
129: String url = "/" + pubId + "/authoring/index.html";
130: controller = (DefaultAccessController) ((PublicationAccessControllerResolver) this .accessControllerResolver)
131: .resolveAccessController(url);
132:
133: assertNotNull(controller);
134: getLogger().info(
135: "Resolved access controller: ["
136: + controller.getClass() + "]");
137: } catch (Exception e) {
138: throw new RuntimeException(e);
139: }
140: return controller;
141: }
142:
143: /**
144: * The teardown method for JUnit
145: * @exception Exception if an error occurs
146: */
147: public void tearDown() throws Exception {
148:
149: if (this .accessControllerResolverSelector != null) {
150: if (this .accessControllerResolver != null) {
151: if (this .accessController != null) {
152: this .accessControllerResolver
153: .release(this .accessController);
154: }
155: this .accessControllerResolverSelector
156: .release(this .accessControllerResolver);
157: }
158: getManager().release(this .accessControllerResolverSelector);
159: }
160: super .tearDown();
161: }
162:
163: protected static final String USERNAME = "lenya";
164:
165: /**
166: * Returns the policy manager.
167: * @return A policy manager.
168: */
169: protected PolicyManager getPolicyManager() {
170: return getAccessController().getPolicyManager();
171: }
172:
173: /**
174: * Returns the accreditable manager.
175: * @return An accreditable manager.
176: */
177: protected AccreditableManager getAccreditableManager() {
178: return getAccessController().getAccreditableManager();
179: }
180:
181: protected File getAccreditablesDirectory()
182: throws AccessControlException {
183: FileAccreditableManager accrMgr = (FileAccreditableManager) getAccreditableManager();
184: return accrMgr.getConfigurationDirectory();
185: }
186:
187: protected DocumentFactory getFactory() {
188: return DocumentUtil.createDocumentFactory(getManager(),
189: getSession());
190: }
191:
192: protected DocumentFactory getFactory(Session session) {
193: return DocumentUtil
194: .createDocumentFactory(getManager(), session);
195: }
196:
197: private Session session;
198:
199: protected Session getSession() {
200: if (this .session == null) {
201: try {
202: this .session = login("lenya");
203: } catch (AccessControlException e) {
204: throw new RuntimeException(e);
205: }
206: }
207: return this .session;
208: }
209:
210: protected Publication getPublication(Session session, String pubId)
211: throws PublicationException {
212: return getFactory(session).getPublication(pubId);
213: }
214:
215: protected Publication getPublication(String id)
216: throws PublicationException {
217: return getFactory().getPublication(id);
218: }
219:
220: protected Identity getIdentity() {
221: return getSession().getIdentity();
222: }
223: }
|