View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    * 
9    *      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.jetspeed.layout.impl;
18  
19  import java.security.Principal;
20  import java.util.HashMap;
21  import java.util.Iterator;
22  import java.util.Map;
23  import java.util.List;
24  import java.util.ArrayList;
25  import java.util.prefs.Preferences;
26  
27  import javax.security.auth.Subject;
28  
29  import org.apache.commons.logging.Log;
30  import org.apache.commons.logging.LogFactory;
31  import org.apache.jetspeed.Jetspeed;
32  import org.apache.jetspeed.administration.PortalConfiguration;
33  import org.apache.jetspeed.ajax.AJAXException;
34  import org.apache.jetspeed.ajax.AjaxAction;
35  import org.apache.jetspeed.ajax.AjaxBuilder;
36  import org.apache.jetspeed.layout.PortletActionSecurityBehavior;
37  import org.apache.jetspeed.request.RequestContext;
38  import org.apache.jetspeed.security.RolePrincipal;
39  import org.apache.jetspeed.security.User;
40  import org.apache.jetspeed.security.UserManager;
41  import org.apache.jetspeed.security.impl.RolePrincipalImpl;
42  
43  /***
44   * Retrieve user information of the current user
45   * 
46   * AJAX action:
47   * 		action = getuserinfo
48   * 
49   * AJAX Parameters:
50   * 		none
51   *     
52   * @author <a href="mailto:mikko.wuokko@evtek.fi">Mikko Wuokko</a>
53   * @version $Id: $
54   */
55  public class GetUserInformationAction 
56      extends BaseUserAction 
57      implements AjaxAction, AjaxBuilder, Constants
58  {
59      protected Log log = LogFactory.getLog(GetUserInformationAction.class);
60  
61      public GetUserInformationAction(String template, 
62                              String errorTemplate, 
63                              UserManager um,
64                              RolesSecurityBehavior rolesSecurityBehavior)                            
65      {
66          super(template, errorTemplate, um, rolesSecurityBehavior);
67      }
68      
69      public boolean run(RequestContext requestContext, Map resultMap)
70              throws AJAXException
71      {
72          boolean success = true;
73          String status = "success";
74          try
75          {
76              resultMap.put(ACTION, "userinformation");
77              // Get the necessary parameters off of the request
78          	if(!requestContext.getUserPrincipal().getName().equals(userManager.getAnonymousUser()))
79          	{        		
80          		Principal principal = requestContext.getUserPrincipal();        		
81                  resultMap.put(USERNAME, principal.getName());
82                  resultMap.put(TYPE, principal.getClass().getName());
83                  
84                  // Loading the userinfo
85                  User user = userManager.getUser(principal.getName());
86                  if(user != null)
87                  {
88                  	Preferences prefs = user.getUserAttributes();
89                  	String[] prefKeys = prefs.keys();
90                  	Map prefsSet = new HashMap();
91                  	for(int i = 0; i<prefKeys.length; i++)
92                  	{
93                  		prefsSet.put(prefKeys[i], prefs.get(prefKeys[i], "No value"));                		
94                  	}
95                  	resultMap.put(USERINFO, prefsSet);
96  
97                  	List roles = new ArrayList();
98                  	Subject userSubject = user.getSubject();
99                  	if ( userSubject != null )
100                 	{
101                 		Iterator rolesIter = userSubject.getPrincipals( RolePrincipalImpl.class ).iterator();
102                 		while ( rolesIter.hasNext() )
103                         {
104                 			RolePrincipal role = (RolePrincipal)rolesIter.next();
105                             roles.add( role.getName() );
106                         }
107                 	}
108                 	resultMap.put( ROLES, roles);
109                 }
110         	}
111         	else
112         	{
113         		status = "failure";
114         		resultMap.put(REASON, "Not logged in");
115         		return false;
116         	}
117             resultMap.put(STATUS, status);
118         } 
119         catch (Exception e)
120         {
121             log.error("exception with user account access", e);
122             resultMap.put(REASON, e.toString());
123             success = false;
124         }
125         return success;
126     }    
127 
128 }