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.jetspeed.layout.impl;
018:
019: import java.security.Principal;
020: import java.util.HashMap;
021: import java.util.Iterator;
022: import java.util.Map;
023: import java.util.List;
024: import java.util.ArrayList;
025: import java.util.prefs.Preferences;
026:
027: import javax.security.auth.Subject;
028:
029: import org.apache.commons.logging.Log;
030: import org.apache.commons.logging.LogFactory;
031: import org.apache.jetspeed.Jetspeed;
032: import org.apache.jetspeed.administration.PortalConfiguration;
033: import org.apache.jetspeed.ajax.AJAXException;
034: import org.apache.jetspeed.ajax.AjaxAction;
035: import org.apache.jetspeed.ajax.AjaxBuilder;
036: import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
037: import org.apache.jetspeed.request.RequestContext;
038: import org.apache.jetspeed.security.RolePrincipal;
039: import org.apache.jetspeed.security.User;
040: import org.apache.jetspeed.security.UserManager;
041: import org.apache.jetspeed.security.impl.RolePrincipalImpl;
042:
043: /**
044: * Retrieve user information of the current user
045: *
046: * AJAX action:
047: * action = getuserinfo
048: *
049: * AJAX Parameters:
050: * none
051: *
052: * @author <a href="mailto:mikko.wuokko@evtek.fi">Mikko Wuokko</a>
053: * @version $Id: $
054: */
055: public class GetUserInformationAction extends BaseUserAction implements
056: AjaxAction, AjaxBuilder, Constants {
057: protected Log log = LogFactory
058: .getLog(GetUserInformationAction.class);
059:
060: public GetUserInformationAction(String template,
061: String errorTemplate, UserManager um,
062: RolesSecurityBehavior rolesSecurityBehavior) {
063: super (template, errorTemplate, um, rolesSecurityBehavior);
064: }
065:
066: public boolean run(RequestContext requestContext, Map resultMap)
067: throws AJAXException {
068: boolean success = true;
069: String status = "success";
070: try {
071: resultMap.put(ACTION, "userinformation");
072: // Get the necessary parameters off of the request
073: if (!requestContext.getUserPrincipal().getName().equals(
074: userManager.getAnonymousUser())) {
075: Principal principal = requestContext.getUserPrincipal();
076: resultMap.put(USERNAME, principal.getName());
077: resultMap.put(TYPE, principal.getClass().getName());
078:
079: // Loading the userinfo
080: User user = userManager.getUser(principal.getName());
081: if (user != null) {
082: Preferences prefs = user.getUserAttributes();
083: String[] prefKeys = prefs.keys();
084: Map prefsSet = new HashMap();
085: for (int i = 0; i < prefKeys.length; i++) {
086: prefsSet.put(prefKeys[i], prefs.get(
087: prefKeys[i], "No value"));
088: }
089: resultMap.put(USERINFO, prefsSet);
090:
091: List roles = new ArrayList();
092: Subject userSubject = user.getSubject();
093: if (userSubject != null) {
094: Iterator rolesIter = userSubject.getPrincipals(
095: RolePrincipalImpl.class).iterator();
096: while (rolesIter.hasNext()) {
097: RolePrincipal role = (RolePrincipal) rolesIter
098: .next();
099: roles.add(role.getName());
100: }
101: }
102: resultMap.put(ROLES, roles);
103: }
104: } else {
105: status = "failure";
106: resultMap.put(REASON, "Not logged in");
107: return false;
108: }
109: resultMap.put(STATUS, status);
110: } catch (Exception e) {
111: log.error("exception with user account access", e);
112: resultMap.put(REASON, e.toString());
113: success = false;
114: }
115: return success;
116: }
117:
118: }
|