1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.portlets.security.groups;
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.GroupManager;
41 import org.apache.jetspeed.security.Role;
42 import org.apache.jetspeed.security.RoleManager;
43 import org.apache.jetspeed.security.SecurityException;
44 import org.apache.jetspeed.security.User;
45 import org.apache.jetspeed.security.UserManager;
46 import org.apache.jetspeed.security.UserPrincipal;
47 import org.apache.portals.gems.browser.BrowserIterator;
48 import org.apache.portals.gems.browser.DatabaseBrowserIterator;
49 import org.apache.portals.gems.browser.BrowserPortlet;
50 import org.apache.portals.gems.util.StatusMessage;
51 import org.apache.portals.messaging.PortletMessaging;
52 import org.apache.velocity.context.Context;
53
54 /***
55 * Group Details
56 *
57 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
58 * @version $Id: GroupDetails.java 348264 2005-11-22 22:06:45Z taylor $
59 */
60 public class GroupDetails extends BrowserPortlet
61 {
62 private UserManager userManager;
63 private RoleManager roleManager;
64 private GroupManager groupManager;
65
66 public void init(PortletConfig config)
67 throws PortletException
68 {
69 super.init(config);
70 userManager = (UserManager) getPortletContext().getAttribute(CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
71 if (null == userManager)
72 {
73 throw new PortletException("Failed to find the User Manager on portlet initialization");
74 }
75 roleManager = (RoleManager) getPortletContext().getAttribute(CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
76 if (null == roleManager)
77 {
78 throw new PortletException("Failed to find the Role Manager on portlet initialization");
79 }
80 groupManager = (GroupManager) getPortletContext().getAttribute(CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
81 if (null == groupManager)
82 {
83 throw new PortletException("Failed to find the Group Manager on portlet initialization");
84 }
85 }
86
87 public void getRows(RenderRequest request, String sql, int windowSize)
88 {
89 getRows(request, sql, windowSize, null);
90 }
91
92 public void getRows(RenderRequest request, String sql, int windowSize, String filter)
93 {
94 List resultSetTitleList = new ArrayList();
95 List resultSetTypeList = new ArrayList();
96 if ( filter != null )
97 {
98 if ( filter.length() == 0 )
99 {
100 filter = null;
101 }
102 else
103 {
104 filter = filter.toLowerCase();
105 }
106 }
107
108 List list = new ArrayList();
109 resultSetTypeList.add(String.valueOf(Types.VARCHAR));
110
111 String groupTab = request.getParameter("groupTab");
112 if ( groupTab == null )
113 {
114 groupTab = "users";
115 }
116 if ("users".equals(groupTab))
117 {
118 resultSetTitleList.add("usersingroup");
119
120 String selectedGroup = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
121 if (selectedGroup != null)
122 {
123 try
124 {
125 Iterator users = userManager.getUsersInGroup(selectedGroup).iterator();
126 while (users.hasNext())
127 {
128 User user = (User)users.next();
129 Principal principal = SecurityUtil.getPrincipal(user.getSubject(),
130 UserPrincipal.class);
131 if ( filter == null || principal.getName().toLowerCase().startsWith(filter))
132 {
133 list.add(principal.getName());
134 }
135 }
136 }
137 catch (SecurityException sex)
138 {
139 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
140 }
141
142 }
143 BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList, windowSize);
144 setBrowserIterator(request, iterator);
145 iterator.sort("usersingroup");
146 }
147 else
148 {
149 resultSetTitleList.add("rolesingroup");
150
151 String selectedGroup = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
152 if (selectedGroup != null)
153 {
154 try
155 {
156 Iterator roles = roleManager.getRolesInGroup(selectedGroup).iterator();
157 while (roles.hasNext())
158 {
159 String roleName = ((Role)roles.next()).getPrincipal().getName();
160 if ( filter == null || roleName.toLowerCase().startsWith(filter))
161 {
162 list.add(roleName);
163 }
164 }
165 }
166 catch (SecurityException sex)
167 {
168 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
169 }
170 }
171 BrowserIterator iterator = new DatabaseBrowserIterator(list, resultSetTitleList, resultSetTypeList, windowSize);
172 setBrowserIterator(request, iterator);
173 iterator.sort("usersingroup");
174 }
175 }
176
177 public void doView(RenderRequest request, RenderResponse response)
178 throws PortletException, IOException
179 {
180 String change = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_CHANGED);
181 if (change != null)
182 {
183 this.clearBrowserIterator(request);
184 PortletMessaging.cancel(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_FILTERED);
185 }
186
187 Context context = this.getContext(request);
188
189 String selectedGroup = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
190 if (selectedGroup != null)
191 {
192 context.put("group", selectedGroup);
193 }
194 String groupTab = request.getParameter("groupTab");
195 if ( groupTab == null )
196 {
197 groupTab = "users";
198 }
199 context.put("groupTab",groupTab);
200
201 if ("users".equals(groupTab))
202 {
203 String popupChooser = SecurityUtil.getAbsoluteUrl(request, "/Administrative/choosers/multiusers.psml");
204 context.put("popupChooser", popupChooser);
205 }
206 else
207 {
208 String popupChooser = SecurityUtil.getAbsoluteUrl(request, "/Administrative/choosers/multiroles.psml");
209 context.put("popupChooser", popupChooser);
210 }
211
212 StatusMessage msg = (StatusMessage)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_STATUS);
213 if (msg != null)
214 {
215 this.getContext(request).put("statusMsg", msg);
216 }
217
218 String filtered = (String)PortletMessaging.receive(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_FILTERED);
219 if (filtered != null)
220 {
221 this.getContext(request).put(FILTERED, "on");
222 }
223
224 String refresh = (String)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH);
225 if (refresh != null)
226 {
227 this.clearBrowserIterator(request);
228 }
229
230 ArrayList errorMessages = (ArrayList)PortletMessaging.consume(request, SecurityResources.TOPIC_GROUP, SecurityResources.ERROR_MESSAGES);
231 if (errorMessages != null )
232 {
233 this.getContext(request).put(SecurityResources.ERROR_MESSAGES, errorMessages);
234 }
235
236 super.doView(request, response);
237 }
238
239
240 public void processAction(ActionRequest request, ActionResponse response)
241 throws PortletException, IOException
242 {
243 if (request.getPortletMode() == PortletMode.VIEW)
244 {
245 String groupTab = request.getParameter("groupTab");
246 if ( groupTab == null )
247 {
248 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH, "true");
249 PortletMessaging.cancel(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_FILTERED);
250 groupTab = request.getParameter("switchTab");
251 if ( groupTab == null )
252 {
253
254 groupTab = "users";
255 }
256 }
257 response.setRenderParameter("groupTab",groupTab);
258
259
260 if (request.getParameter("group.action.Save") != null)
261 {
262 addGroup(request);
263 }
264 else if (request.getParameter("group.action.Add_New_Group") != null)
265 {
266 PortletMessaging.cancel(request, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
267 }
268 else if (request.getParameter("group.action.Remove_Group") != null)
269 {
270 removeGroup(request);
271 }
272 else if ("users".equals(groupTab))
273 {
274 String users = request.getParameter("users");
275
276 if (users != null && users.length() > 0)
277 {
278 addUsersToGroup(request, users);
279 }
280 else if (request.getParameter("group.action.Remove_Checked_Users") != null)
281 {
282 removeUsersFromGroup(request);
283 }
284 }
285 else
286 {
287 String roles = request.getParameter("roles");
288
289 if (roles != null && roles.length() > 0)
290 {
291 addRolesToGroup(request, roles);
292 }
293 else if (request.getParameter("group.action.Remove_Checked_Roles") != null)
294 {
295 removeRolesFromGroup(request);
296 }
297 }
298
299 if (request.getParameter(FILTERED) != null )
300 {
301 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_FILTERED, "on");
302 }
303 else
304 {
305 PortletMessaging.cancel(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_FILTERED);
306 }
307 }
308
309
310 super.processAction(request, response);
311 }
312
313 protected void addGroup(ActionRequest actionRequest)
314 {
315 String group = actionRequest.getParameter("group");
316 if (!SecurityUtil.isEmpty(group))
317 {
318 try
319 {
320 groupManager.addGroup(group);
321 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_REFRESH, "true");
322 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED, group);
323 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_CHANGED, group);
324 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_REFRESH_GROUPS, "true");
325 }
326 catch (NotSerializableException e)
327 {
328 e.printStackTrace();
329 }
330 catch (SecurityException sex)
331 {
332 SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_GROUP, sex.getMessage());
333 }
334 }
335 }
336
337 protected void removeGroup(ActionRequest actionRequest)
338 {
339 String group = actionRequest.getParameter("group");
340 if (!SecurityUtil.isEmpty(group))
341 {
342 try
343 {
344 groupManager.removeGroup(group);
345 try
346 {
347 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_REFRESH, "true");
348 PortletMessaging.publish(actionRequest, SecurityResources.TOPIC_USERS, SecurityResources.MESSAGE_REFRESH_GROUPS, "true");
349 }
350 catch (NotSerializableException e)
351 {
352 e.printStackTrace();
353 }
354 PortletMessaging.cancel(actionRequest, SecurityResources.TOPIC_GROUPS, SecurityResources.MESSAGE_SELECTED);
355 }
356 catch (SecurityException sex)
357 {
358 SecurityUtil.publishErrorMessage(actionRequest, SecurityResources.TOPIC_GROUP, sex.getMessage());
359 }
360 }
361 }
362
363 protected void addUsersToGroup(ActionRequest request, String users)
364 {
365 String group = request.getParameter("group");
366 if (group != null)
367 {
368 int count = 0;
369 StringTokenizer tokenizer = new StringTokenizer(users, ",");
370 while (tokenizer.hasMoreTokens())
371 {
372 String user = tokenizer.nextToken();
373 try
374 {
375 if (user.startsWith("box_"))
376 {
377 user = user.substring("box_".length());
378 groupManager.addUserToGroup(user, group);
379 count++;
380 }
381 }
382 catch (SecurityException sex)
383 {
384 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
385 }
386 }
387 if (count > 0)
388 {
389 try
390 {
391 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH, "true");
392 }
393 catch (Exception e)
394 {
395 e.printStackTrace();
396 }
397 }
398 }
399 }
400
401 protected void removeUsersFromGroup(ActionRequest request)
402 {
403 String group = request.getParameter("group");
404 if (group != null)
405 {
406 int count = 0;
407 Enumeration e = request.getParameterNames();
408 while (e.hasMoreElements())
409 {
410 String name = (String)e.nextElement();
411 if (name.startsWith("box_"))
412 {
413 String user = name.substring("box_".length());
414 try
415 {
416 groupManager.removeUserFromGroup(user, group);
417 count++;
418 }
419 catch (SecurityException sex)
420 {
421 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
422 }
423
424 }
425 }
426 if (count > 0)
427 {
428 try
429 {
430 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH, "true");
431 }
432 catch (Exception ex)
433 {
434 ex.printStackTrace();
435 }
436 }
437 }
438 }
439
440 protected void addRolesToGroup(ActionRequest request, String roles)
441 {
442 String group = request.getParameter("group");
443 if (group != null)
444 {
445 int count = 0;
446 StringTokenizer tokenizer = new StringTokenizer(roles, ",");
447 while (tokenizer.hasMoreTokens())
448 {
449 String role = tokenizer.nextToken();
450 try
451 {
452 if (role.startsWith("box_"))
453 {
454 role = role.substring("box_".length());
455 roleManager.addRoleToGroup(role, group);
456 count++;
457 }
458 }
459 catch (SecurityException sex)
460 {
461 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
462 }
463 }
464 if (count > 0)
465 {
466 try
467 {
468 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH, "true");
469 }
470 catch (Exception e)
471 {
472 e.printStackTrace();
473 }
474 }
475 }
476 }
477
478 protected void removeRolesFromGroup(ActionRequest request)
479 {
480 String group = request.getParameter("group");
481 if (group != null)
482 {
483 int count = 0;
484 Enumeration e = request.getParameterNames();
485 while (e.hasMoreElements())
486 {
487 String name = (String)e.nextElement();
488 if (name.startsWith("box_"))
489 {
490 String role = name.substring("box_".length());
491 try
492 {
493 roleManager.removeRoleFromGroup(role, group);
494 count++;
495 }
496 catch (SecurityException sex)
497 {
498 SecurityUtil.publishErrorMessage(request, SecurityResources.TOPIC_GROUP, sex.getMessage());
499 }
500
501 }
502 }
503 if (count > 0)
504 {
505 try
506 {
507 PortletMessaging.publish(request, SecurityResources.TOPIC_GROUP, SecurityResources.MESSAGE_REFRESH, "true");
508 }
509 catch (Exception ex)
510 {
511 ex.printStackTrace();
512 }
513 }
514 }
515 }
516 }