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.portlets.security.users;
18  
19  import java.io.IOException;
20  import java.security.Principal;
21  import java.sql.Types;
22  import java.util.ArrayList;
23  import java.util.Iterator;
24  import java.util.List;
25  
26  import javax.portlet.ActionRequest;
27  import javax.portlet.ActionResponse;
28  import javax.portlet.PortletConfig;
29  import javax.portlet.PortletException;
30  import javax.portlet.PortletMode;
31  import javax.portlet.RenderRequest;
32  import javax.portlet.RenderResponse;
33  import javax.security.auth.Subject;
34  
35  import org.apache.jetspeed.CommonPortletServices;
36  import org.apache.jetspeed.portlets.security.SecurityResources;
37  import org.apache.jetspeed.portlets.security.SecurityUtil;
38  import org.apache.jetspeed.security.SecurityException;
39  import org.apache.jetspeed.security.User;
40  import org.apache.jetspeed.security.UserManager;
41  import org.apache.jetspeed.security.UserPrincipal;
42  import org.apache.portals.gems.browser.BrowserIterator;
43  import org.apache.portals.gems.browser.BrowserPortlet;
44  import org.apache.portals.gems.browser.DatabaseBrowserIterator;
45  import org.apache.portals.gems.util.StatusMessage;
46  import org.apache.portals.messaging.PortletMessaging;
47  import org.apache.velocity.context.Context;
48  
49  /***
50   * Role Browser - flat non-hierarchical view
51   * 
52   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
53   * @version $Id: UserBrowser.java 348264 2005-11-22 22:06:45Z taylor $
54   */
55  public class UserBrowser extends BrowserPortlet
56  {
57      protected UserManager userManager;
58  
59      // view context
60      public static final String STATUS = "statusMsg";
61      public static final String SELECTED = "selected";
62  
63      public void init(PortletConfig config)
64      throws PortletException 
65      {
66          super.init(config);
67          userManager = (UserManager) 
68              getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
69          if (null == userManager)
70          {
71              throw new PortletException("Failed to find the User Manager on portlet initialization");
72          }
73      }
74  
75      public void doView(RenderRequest request, RenderResponse response)
76      throws PortletException, IOException
77      {
78          String selected = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_SELECTED);
79          if (selected != null)
80          {        
81              Context context = this.getContext(request);
82              context.put(SELECTED, selected);
83          }
84          StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_STATUS);
85          if (msg != null)
86          {
87              this.getContext(request).put(STATUS, msg);            
88          }
89          String refresh = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_REFRESH); 
90          if (refresh != null)
91          {        
92              this.clearBrowserIterator(request);
93          }                
94          
95          String filtered = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_FILTERED);
96          if (filtered != null)
97          {
98              this.getContext(request).put(FILTERED, "on");            
99          }
100         
101         ArrayList errorMessages = (ArrayList)PortletMessaging.consume(request, SecurityResources.TOPIC_USERS, SecurityResources.ERROR_MESSAGES);
102         if (errorMessages != null )
103         {
104             this.getContext(request).put(SecurityResources.ERROR_MESSAGES, errorMessages);
105         }
106         
107         super.doView(request, response);
108     }
109         
110     public void processAction(ActionRequest request, ActionResponse response)
111     throws PortletException, IOException
112     {
113         if (request.getPortletMode() == PortletMode.VIEW)
114         {
115             String selected = request.getParameter("user");
116             if (selected != null)
117             {
118                 PortletMessaging.publish(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_SELECTED, selected);
119             }
120         }
121         
122         // TODO: if request parameters were working correctly we could replace this with render parameters
123         String filtered = request.getParameter(FILTERED);
124         if (filtered != null)
125         {
126             PortletMessaging.publish(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_FILTERED, "on");            
127         }
128         else
129         {
130             PortletMessaging.cancel(request, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_FILTERED);
131         }
132         
133         super.processAction(request, response);            
134     }
135       
136     public void getRows(RenderRequest request, String sql, int windowSize)
137     {
138         getRows(request, sql, windowSize, "");
139     }
140 
141     public void getRows(RenderRequest request, String sql, int windowSize, String filter)
142     {
143         String roleFilter = request.getPreferences().getValue("FilterByRole", "");
144         if (roleFilter == null)
145             roleFilter = "";
146         boolean filterByRole = !roleFilter.equals("") || roleFilter.equalsIgnoreCase("false");
147         List resultSetTitleList = new ArrayList();
148         List resultSetTypeList = new ArrayList();
149         resultSetTypeList.add(String.valueOf(Types.VARCHAR));
150         resultSetTitleList.add("user"); // resource bundle key
151 
152         List list = new ArrayList();
153         try
154         {
155             if (filterByRole)
156             {
157                 Iterator users = userManager.getUsersInRole(roleFilter).iterator();
158                 while (users.hasNext())
159                 {
160                     // NOTE: this can be a bit costly if you have a lot of users in a role
161                     User user = (User)users.next();
162                     Principal pr = getBestPrincipal(user.getSubject(), UserPrincipal.class);
163                     list.add(pr.getName());
164                 }                            
165             }
166             else
167             {
168                 Iterator users = userManager.getUserNames(filter);
169                 while (users.hasNext())
170                 {
171                     list.add(users.next());
172                 }            
173                 
174             }                            
175         }
176         catch (SecurityException sex)
177         {
178             SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_USERS, sex.getMessage());
179         }                                    
180         BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList, windowSize);
181         setBrowserIterator(request, iterator);
182         iterator.sort("user"); // resource bundle key        
183     }    
184     
185     public static Principal getBestPrincipal(Subject subject, Class classe)
186     {
187 
188         Principal principal = null;
189         Iterator principals = subject.getPrincipals().iterator();
190         while (principals.hasNext())
191         {
192             Principal p = (Principal) principals.next();
193             if (classe.isInstance(p))
194             {
195                 principal = p;
196                 break;
197             }
198             else
199             {
200                 if (principal == null)
201                 {
202                     principal = p;
203                 }
204             }
205         }
206         return principal;
207     }    
208 }