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.roles;
18  
19  import java.io.IOException;
20  import java.io.NotSerializableException;
21  import java.security.Principal;
22  import java.sql.Types;
23  import java.util.ArrayList;
24  import java.util.Enumeration;
25  import java.util.Iterator;
26  import java.util.List;
27  import java.util.StringTokenizer;
28  
29  import javax.portlet.ActionRequest;
30  import javax.portlet.ActionResponse;
31  import javax.portlet.PortletConfig;
32  import javax.portlet.PortletException;
33  import javax.portlet.PortletMode;
34  import javax.portlet.RenderRequest;
35  import javax.portlet.RenderResponse;
36  
37  import org.apache.jetspeed.CommonPortletServices;
38  import org.apache.jetspeed.portlets.security.SecurityResources;
39  import org.apache.jetspeed.portlets.security.SecurityUtil;
40  import org.apache.jetspeed.security.RoleManager;
41  import org.apache.jetspeed.security.SecurityException;
42  import org.apache.jetspeed.security.User;
43  import org.apache.jetspeed.security.UserManager;
44  import org.apache.jetspeed.security.UserPrincipal;
45  import org.apache.portals.gems.browser.BrowserIterator;
46  import org.apache.portals.gems.browser.DatabaseBrowserIterator;
47  import org.apache.portals.gems.browser.BrowserPortlet;
48  import org.apache.portals.gems.util.StatusMessage;
49  import org.apache.portals.messaging.PortletMessaging;
50  import org.apache.velocity.context.Context;
51  
52  /***
53   * Role Details
54   * 
55   * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
56   * @version $Id: RoleDetails.java 348264 2005-11-22 22:06:45Z taylor $
57   */
58  public class RoleDetails extends BrowserPortlet
59  {
60      private UserManager userManager;
61      private RoleManager roleManager;
62          
63      public void init(PortletConfig config)
64      throws PortletException 
65      {
66          super.init(config);
67          userManager = (UserManager) getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
68          if (null == userManager)
69          {
70              throw new PortletException("Failed to find the User Manager on portlet initialization");
71          }
72          roleManager = (RoleManager) getPortletContext().getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
73          if (null == roleManager)
74          {
75              throw new PortletException("Failed to find the Role Manager on portlet initialization");
76          }        
77      }
78      
79      public void getRows(RenderRequest request, String sql, int windowSize)
80      {
81          getRows(request, sql, windowSize, null);        
82      }
83  
84      public void getRows(RenderRequest request, String sql, int windowSize, String filter)
85      {
86          List resultSetTitleList = new ArrayList();
87          List resultSetTypeList = new ArrayList();
88          if ( filter != null )
89          {
90              if ( filter.length() == 0 )
91              {
92                  filter = null;
93              }
94              else
95              {
96                  filter = filter.toLowerCase();
97              }
98          }
99          
100         List list = new ArrayList();
101         resultSetTypeList.add(String.valueOf(Types.VARCHAR));
102         resultSetTitleList.add("usersinrole"); // resource bundle key
103         
104         String selectedRole = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED);
105         if (selectedRole != null)
106         {
107             try
108             {
109                 Iterator users = userManager.getUsersInRole(selectedRole).iterator();                                    
110                 while (users.hasNext())
111                 {
112                     User user = (User)users.next();
113                     Principal principal = SecurityUtil.getPrincipal(user.getSubject(),
114                             UserPrincipal.class);
115                     if ( filter == null || principal.getName().toLowerCase().startsWith(filter))
116                     {
117                         list.add(principal.getName());
118                     }
119                 }
120             } 
121             catch (SecurityException sex)
122             {
123                 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_ROLE, sex.getMessage());
124             }                                    
125         }
126         BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList, windowSize);
127         setBrowserIterator(request, iterator);
128         iterator.sort("usersinrole"); // resource bundle key
129     }
130            
131     public void doView(RenderRequest request, RenderResponse response)
132     throws PortletException, IOException
133     {
134         String change = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_CHANGED);
135         if (change != null)
136         { 
137             this.clearBrowserIterator(request);
138         }
139         Context context = this.getContext(request);
140                 
141         String selectedRole = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED);
142         if (selectedRole != null)
143         {        
144             context.put("role", selectedRole);
145         }        
146         
147         String userChooser = SecurityUtil.getAbsoluteUrl(request, "/Administrative/choosers/multiusers.psml");        
148         context.put("userChooser", userChooser);
149         
150         StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_STATUS);
151         if (msg != null)
152         {
153             this.getContext(request).put("statusMsg", msg);            
154         }
155           
156         String filtered = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_FILTERED);
157         if (filtered != null)
158         {
159             this.getContext(request).put(FILTERED, "on");            
160         }
161 
162         String refresh = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_REFRESH); 
163         if (refresh != null)
164         {        
165             this.clearBrowserIterator(request);
166         }                
167         
168         ArrayList errorMessages = (ArrayList)PortletMessaging.consume(request, SecurityResources.TOPIC_ROLE, SecurityResources.ERROR_MESSAGES);
169         if (errorMessages != null )
170         {
171             this.getContext(request).put(SecurityResources.ERROR_MESSAGES, errorMessages);
172         }
173 
174         super.doView(request, response);
175     }
176         
177     
178     public void processAction(ActionRequest request, ActionResponse response)
179     throws PortletException, IOException
180     {
181         if (request.getPortletMode() == PortletMode.VIEW)
182         {
183             String users = request.getParameter("users");
184             
185             if (users != null && users.length() > 0)
186             {
187                 addUsersToRole(request, users);
188             }
189             else if (request.getParameter("role.action.Add_New_Role") != null)
190             {
191                 PortletMessaging.cancel(request, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED);                
192             }
193             else if (request.getParameter("role.action.Remove_Checked_Users") != null)
194             {
195                 removeUsersFromRole(request);
196             }
197             else if (request.getParameter("role.action.Remove_Role") != null)
198             {
199                 removeRole(request);
200             }
201             else if (request.getParameter("role.action.Save") != null)
202             {
203                 addRole(request);
204             }
205 
206             if (request.getParameter(FILTERED) != null )
207             {
208                 PortletMessaging.publish(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_FILTERED, "on");            
209             }
210             else
211             {
212                 PortletMessaging.cancel(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_FILTERED);                    
213             }            
214         }
215         super.processAction(request, response);            
216     }
217 
218     protected void addRole(ActionRequest actionRequest)
219     {
220         String role = actionRequest.getParameter("role");
221         if (!SecurityUtil.isEmpty(role)) 
222         {
223             try
224             {            
225                 roleManager.addRole(role);
226                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_REFRESH, "true");
227                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED, role);
228                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_CHANGED, role);
229                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_REFRESH_ROLES, "true");
230                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_REFRESH_ROLES, "true");
231             }            
232             catch (SecurityException sex)
233             {
234                 SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_ROLE, sex.getMessage());
235             }
236             catch (NotSerializableException e)
237             {
238                 e.printStackTrace();
239             }
240         }
241     }
242 
243     protected void removeRole(ActionRequest actionRequest)
244     {
245         String role = actionRequest.getParameter("role");
246         if (!SecurityUtil.isEmpty(role)) 
247         {
248             try
249             {            
250                 roleManager.removeRole(role);
251                 try
252                 {
253                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_REFRESH, "true");
254                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_REFRESH_ROLES, "true");
255                 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_REFRESH_ROLES, "true");
256                 }
257                 catch (NotSerializableException e)
258                 {
259                     e.printStackTrace();
260                 }
261                 PortletMessaging.cancel(actionRequest, SecurityResources.TOPIC_ROLES, SecurityResources.MESSAGE_SELECTED);                                                
262             }
263             catch (SecurityException sex)
264             {
265                 SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_ROLE, sex.getMessage());
266             } 
267         }
268     }
269     
270     protected void addUsersToRole(ActionRequest request, String users)
271     {
272         String role = request.getParameter("role");
273         if (role != null)
274         {
275             int count = 0;
276             StringTokenizer tokenizer = new StringTokenizer(users, ",");
277             while (tokenizer.hasMoreTokens())
278             {
279                 String user = tokenizer.nextToken();
280                 try
281                 {
282                     if (user.startsWith("box_"))
283                     {
284                         user = user.substring("box_".length());
285                         roleManager.addRoleToUser(user, role);
286                         count++;
287                     }
288                 }
289                 catch (SecurityException sex)
290                 {
291                     SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_ROLE, sex.getMessage());
292                 }
293             }
294             if (count > 0)
295             {
296                 try
297                 {
298                     PortletMessaging.publish(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_REFRESH, "true");
299                 }
300                 catch (Exception e)
301                 {
302                     e.printStackTrace();
303                 }
304             }
305         }
306     }
307 
308     protected void removeUsersFromRole(ActionRequest request)
309     {
310         String role = request.getParameter("role");
311         if (role != null)
312         {
313             int count = 0;
314             Enumeration e = request.getParameterNames();
315             while (e.hasMoreElements())
316             {
317                 String name = (String)e.nextElement();
318                 if (name.startsWith("box_"))
319                 {
320                     String user = name.substring("box_".length());
321                     try
322                     {
323                         roleManager.removeRoleFromUser(user, role);
324                         count++;
325                     }
326                     catch (SecurityException sex)
327                     {
328                         SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_ROLE, sex.getMessage());
329                     }
330                 }
331             }
332             if (count > 0)
333             {
334                 try
335                 {
336                     PortletMessaging.publish(request, SecurityResources.TOPIC_ROLE, SecurityResources.MESSAGE_REFRESH, "true");
337                 }
338                 catch (Exception ex)
339                 {
340                     ex.printStackTrace();
341                 }
342             }
343         }
344     }
345     
346 }