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: package org.apache.cocoon.portal.profile.impl;
018:
019: import java.util.HashMap;
020: import java.util.Map;
021:
022: import org.apache.avalon.framework.configuration.Configuration;
023: import org.apache.avalon.framework.service.ServiceException;
024: import org.apache.avalon.framework.service.ServiceManager;
025: import org.apache.avalon.framework.service.Serviceable;
026: import org.apache.cocoon.ProcessingException;
027: import org.apache.cocoon.webapps.authentication.AuthenticationManager;
028: import org.apache.cocoon.webapps.authentication.configuration.ApplicationConfiguration;
029: import org.apache.cocoon.webapps.authentication.user.RequestState;
030: import org.apache.cocoon.webapps.authentication.user.UserHandler;
031:
032: /**
033: * Get the information about the current user.
034: * This implementation uses the authentication-fw block
035: *
036: * @author <a href="mailto:cziegeler@apache.org">Carsten Ziegeler</a>
037: * @version CVS $Id: MapProfileLS.java 30941 2004-07-29 19:56:58Z vgritsenko $
038: */
039: public class AuthenticationFWUserInfoProvider implements
040: UserInfoProvider, Serviceable {
041:
042: protected ServiceManager manager;
043:
044: /* (non-Javadoc)
045: * @see org.apache.avalon.framework.service.Serviceable#service(org.apache.avalon.framework.service.ServiceManager)
046: */
047: public void service(ServiceManager manager) throws ServiceException {
048: this .manager = manager;
049: }
050:
051: /* (non-Javadoc)
052: * @see org.apache.cocoon.portal.profile.impl.UserInfoProvider#getUserInfo(java.lang.String, java.lang.String)
053: */
054: public UserInfo getUserInfo(String portalName, String layoutKey)
055: throws Exception {
056: AuthenticationManager authManager = null;
057: try {
058: authManager = (AuthenticationManager) this .manager
059: .lookup(AuthenticationManager.ROLE);
060: final RequestState state = authManager.getState();
061: final UserHandler handler = state.getHandler();
062:
063: final UserInfo info = new AFWUserInfo(portalName,
064: layoutKey, handler);
065:
066: info.setUserName(handler.getUserId());
067: try {
068: info.setGroup((String) handler.getContext()
069: .getContextInfo().get("group"));
070: } catch (ProcessingException pe) {
071: // ignore this
072: }
073:
074: final ApplicationConfiguration ac = state
075: .getApplicationConfiguration();
076: if (ac == null) {
077: throw new ProcessingException(
078: "Configuration for portal not found in application configuration.");
079: }
080: final Configuration appConf = ac.getConfiguration("portal");
081: if (appConf == null) {
082: throw new ProcessingException(
083: "Configuration for portal not found in application configuration.");
084: }
085: final Configuration config = appConf.getChild("profiles");
086: final Configuration[] children = config.getChildren();
087: final Map configs = new HashMap();
088: if (children != null) {
089: for (int i = 0; i < children.length; i++) {
090: configs.put(children[i].getName(), children[i]
091: .getAttribute("uri"));
092: }
093: }
094: info.setConfigurations(configs);
095: return info;
096: } finally {
097: this .manager.release(authManager);
098: }
099: }
100:
101: public static final class AFWUserInfo extends UserInfo {
102:
103: protected final UserHandler handler;
104:
105: /**
106: * @param portalName
107: * @param layoutKey
108: */
109: public AFWUserInfo(String portalName, String layoutKey,
110: UserHandler handler) {
111: super (portalName, layoutKey);
112: this .handler = handler;
113: }
114:
115: /* (non-Javadoc)
116: * @see org.apache.cocoon.portal.profile.PortalUser#isUserInRole(java.lang.String)
117: */
118: public boolean isUserInRole(String role) {
119: return this.isUserInRole(role);
120: }
121: }
122: }
|