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.Map;
22  import java.util.prefs.Preferences;
23  
24  import org.apache.commons.logging.Log;
25  import org.apache.commons.logging.LogFactory;
26  import org.apache.jetspeed.ajax.AJAXException;
27  import org.apache.jetspeed.ajax.AjaxAction;
28  import org.apache.jetspeed.ajax.AjaxBuilder;
29  import org.apache.jetspeed.request.RequestContext;
30  import org.apache.jetspeed.security.User;
31  import org.apache.jetspeed.security.UserManager;
32  
33  /***
34   * Retrieve user information of the current user
35   * 
36   * AJAX action:
37   * 		action = getuserinfo
38   * 
39   * AJAX Parameters:
40   * 		none
41   *     
42   * @author <a href="mailto:mikko.wuokko@evtek.fi">Mikko Wuokko</a>
43   * @version $Id: $
44   */
45  public class GetUserInformationAction 
46      extends BaseUserAction 
47      implements AjaxAction, AjaxBuilder, Constants
48  {
49      protected Log log = LogFactory.getLog(GetUserInformationAction.class);
50  
51      public GetUserInformationAction(String template, 
52                              String errorTemplate, 
53                              UserManager um,
54                              RolesSecurityBehavior securityBehavior)                            
55      {
56          super(template, errorTemplate, um, securityBehavior); 
57      }
58      
59      public boolean run(RequestContext requestContext, Map resultMap)
60              throws AJAXException
61      {
62          boolean success = true;
63          String status = "success";
64          try
65          {
66              resultMap.put(ACTION, "userinformation");
67              // Get the necessary parameters off of the request
68          	if(!requestContext.getUserPrincipal().getName().equals(userManager.getAnonymousUser()))
69          	{        		
70          		Principal principal = requestContext.getUserPrincipal();        		
71                  resultMap.put(USERNAME, principal.getName());
72                  resultMap.put(TYPE, principal.getClass().getName());
73                  
74                  // Loading the userinfo
75                  User user = userManager.getUser(principal.getName());
76                  if(user != null)
77                  {
78                  	Preferences prefs = user.getUserAttributes();
79                  	String[] prefKeys = prefs.keys();
80                  	Map prefsSet = new HashMap();
81                  	for(int i = 0; i<prefKeys.length; i++)
82                  	{
83                  		prefsSet.put(prefKeys[i], prefs.get(prefKeys[i], "No value"));                		
84                  	}
85                  	resultMap.put(USERINFO, prefsSet);
86  
87                  }
88                  	
89          	}
90          	else
91          	{
92          		status = "failure";
93          		resultMap.put(REASON, "Not logged in");
94          		return false;
95          	}
96              resultMap.put(STATUS, status);
97          } 
98          catch (Exception e)
99          {
100             log.error("exception with user account access", e);
101             resultMap.put(REASON, e.toString());
102             success = false;
103         }
104         return success;
105     }    
106 
107 }