001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.jetspeed.portlets.security.groups;
018:
019: import java.io.IOException;
020: import java.io.NotSerializableException;
021: import java.security.Principal;
022: import java.sql.Types;
023: import java.util.ArrayList;
024: import java.util.Enumeration;
025: import java.util.Iterator;
026: import java.util.List;
027: import java.util.StringTokenizer;
028:
029: import javax.portlet.ActionRequest;
030: import javax.portlet.ActionResponse;
031: import javax.portlet.PortletConfig;
032: import javax.portlet.PortletException;
033: import javax.portlet.PortletMode;
034: import javax.portlet.RenderRequest;
035: import javax.portlet.RenderResponse;
036:
037: import org.apache.jetspeed.CommonPortletServices;
038: import org.apache.jetspeed.portlets.security.SecurityResources;
039: import org.apache.jetspeed.portlets.security.SecurityUtil;
040: import org.apache.jetspeed.security.GroupManager;
041: import org.apache.jetspeed.security.Role;
042: import org.apache.jetspeed.security.RoleManager;
043: import org.apache.jetspeed.security.SecurityException;
044: import org.apache.jetspeed.security.User;
045: import org.apache.jetspeed.security.UserManager;
046: import org.apache.jetspeed.security.UserPrincipal;
047: import org.apache.portals.gems.browser.BrowserIterator;
048: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
049: import org.apache.portals.gems.browser.BrowserPortlet;
050: import org.apache.portals.gems.util.StatusMessage;
051: import org.apache.portals.messaging.PortletMessaging;
052: import org.apache.velocity.context.Context;
053:
054: /**
055: * Group Details
056: *
057: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
058: * @version $Id: GroupDetails.java 348264 2005-11-22 22:06:45Z taylor $
059: */
060: public class GroupDetails extends BrowserPortlet {
061: private UserManager userManager;
062: private RoleManager roleManager;
063: private GroupManager groupManager;
064:
065: public void init(PortletConfig config) throws PortletException {
066: super .init(config);
067: userManager = (UserManager) getPortletContext().getAttribute(
068: CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
069: if (null == userManager) {
070: throw new PortletException(
071: "Failed to find the User Manager on portlet initialization");
072: }
073: roleManager = (RoleManager) getPortletContext().getAttribute(
074: CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
075: if (null == roleManager) {
076: throw new PortletException(
077: "Failed to find the Role Manager on portlet initialization");
078: }
079: groupManager = (GroupManager) getPortletContext().getAttribute(
080: CommonPortletServices.CPS_GROUP_MANAGER_COMPONENT);
081: if (null == groupManager) {
082: throw new PortletException(
083: "Failed to find the Group Manager on portlet initialization");
084: }
085: }
086:
087: public void getRows(RenderRequest request, String sql,
088: int windowSize) {
089: getRows(request, sql, windowSize, null);
090: }
091:
092: public void getRows(RenderRequest request, String sql,
093: int windowSize, String filter) {
094: List resultSetTitleList = new ArrayList();
095: List resultSetTypeList = new ArrayList();
096: if (filter != null) {
097: if (filter.length() == 0) {
098: filter = null;
099: } else {
100: filter = filter.toLowerCase();
101: }
102: }
103:
104: List list = new ArrayList();
105: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
106:
107: String groupTab = request.getParameter("groupTab");
108: if (groupTab == null) {
109: groupTab = "users";
110: }
111: if ("users".equals(groupTab)) {
112: resultSetTitleList.add("usersingroup"); // resource bundle key
113:
114: String selectedGroup = (String) PortletMessaging.receive(
115: request, SecurityResources.TOPIC_GROUPS,
116: SecurityResources.MESSAGE_SELECTED);
117: if (selectedGroup != null) {
118: try {
119: Iterator users = userManager.getUsersInGroup(
120: selectedGroup).iterator();
121: while (users.hasNext()) {
122: User user = (User) users.next();
123: Principal principal = SecurityUtil
124: .getPrincipal(user.getSubject(),
125: UserPrincipal.class);
126: if (filter == null
127: || principal.getName().toLowerCase()
128: .startsWith(filter)) {
129: list.add(principal.getName());
130: }
131: }
132: } catch (SecurityException sex) {
133: SecurityUtil.publishErrorMessage(request,
134: SecurityResources.TOPIC_GROUP, sex
135: .getMessage());
136: }
137:
138: }
139: BrowserIterator iterator = new DatabaseBrowserIterator(
140: list, resultSetTitleList, resultSetTypeList,
141: windowSize);
142: setBrowserIterator(request, iterator);
143: iterator.sort("usersingroup"); // resource bundle key
144: } else {
145: resultSetTitleList.add("rolesingroup"); // resource bundle key
146:
147: String selectedGroup = (String) PortletMessaging.receive(
148: request, SecurityResources.TOPIC_GROUPS,
149: SecurityResources.MESSAGE_SELECTED);
150: if (selectedGroup != null) {
151: try {
152: Iterator roles = roleManager.getRolesInGroup(
153: selectedGroup).iterator();
154: while (roles.hasNext()) {
155: String roleName = ((Role) roles.next())
156: .getPrincipal().getName();
157: if (filter == null
158: || roleName.toLowerCase().startsWith(
159: filter)) {
160: list.add(roleName);
161: }
162: }
163: } catch (SecurityException sex) {
164: SecurityUtil.publishErrorMessage(request,
165: SecurityResources.TOPIC_GROUP, sex
166: .getMessage());
167: }
168: }
169: BrowserIterator iterator = new DatabaseBrowserIterator(
170: list, resultSetTitleList, resultSetTypeList,
171: windowSize);
172: setBrowserIterator(request, iterator);
173: iterator.sort("usersingroup"); // resource bundle key
174: }
175: }
176:
177: public void doView(RenderRequest request, RenderResponse response)
178: throws PortletException, IOException {
179: String change = (String) PortletMessaging.consume(request,
180: SecurityResources.TOPIC_GROUPS,
181: SecurityResources.MESSAGE_CHANGED);
182: if (change != null) {
183: this .clearBrowserIterator(request);
184: PortletMessaging.cancel(request,
185: SecurityResources.TOPIC_GROUP,
186: SecurityResources.MESSAGE_FILTERED);
187: }
188:
189: Context context = this .getContext(request);
190:
191: String selectedGroup = (String) PortletMessaging.receive(
192: request, SecurityResources.TOPIC_GROUPS,
193: SecurityResources.MESSAGE_SELECTED);
194: if (selectedGroup != null) {
195: context.put("group", selectedGroup);
196: }
197: String groupTab = request.getParameter("groupTab");
198: if (groupTab == null) {
199: groupTab = "users";
200: }
201: context.put("groupTab", groupTab);
202:
203: if ("users".equals(groupTab)) {
204: String popupChooser = SecurityUtil.getAbsoluteUrl(request,
205: "/Administrative/choosers/multiusers.psml");
206: context.put("popupChooser", popupChooser);
207: } else {
208: String popupChooser = SecurityUtil.getAbsoluteUrl(request,
209: "/Administrative/choosers/multiroles.psml");
210: context.put("popupChooser", popupChooser);
211: }
212:
213: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
214: request, SecurityResources.TOPIC_GROUP,
215: SecurityResources.MESSAGE_STATUS);
216: if (msg != null) {
217: this .getContext(request).put("statusMsg", msg);
218: }
219:
220: String filtered = (String) PortletMessaging.receive(request,
221: SecurityResources.TOPIC_GROUP,
222: SecurityResources.MESSAGE_FILTERED);
223: if (filtered != null) {
224: this .getContext(request).put(FILTERED, "on");
225: }
226:
227: String refresh = (String) PortletMessaging.consume(request,
228: SecurityResources.TOPIC_GROUP,
229: SecurityResources.MESSAGE_REFRESH);
230: if (refresh != null) {
231: this .clearBrowserIterator(request);
232: }
233:
234: ArrayList errorMessages = (ArrayList) PortletMessaging.consume(
235: request, SecurityResources.TOPIC_GROUP,
236: SecurityResources.ERROR_MESSAGES);
237: if (errorMessages != null) {
238: this .getContext(request).put(
239: SecurityResources.ERROR_MESSAGES, errorMessages);
240: }
241:
242: super .doView(request, response);
243: }
244:
245: public void processAction(ActionRequest request,
246: ActionResponse response) throws PortletException,
247: IOException {
248: if (request.getPortletMode() == PortletMode.VIEW) {
249: String groupTab = request.getParameter("groupTab");
250: if (groupTab == null) {
251: PortletMessaging.publish(request,
252: SecurityResources.TOPIC_GROUP,
253: SecurityResources.MESSAGE_REFRESH, "true");
254: PortletMessaging.cancel(request,
255: SecurityResources.TOPIC_GROUP,
256: SecurityResources.MESSAGE_FILTERED);
257: groupTab = request.getParameter("switchTab");
258: if (groupTab == null) {
259: // should never happen
260: groupTab = "users";
261: }
262: }
263: response.setRenderParameter("groupTab", groupTab);
264:
265: if (request.getParameter("group.action.Save") != null) {
266: addGroup(request);
267: } else if (request
268: .getParameter("group.action.Add_New_Group") != null) {
269: PortletMessaging.cancel(request,
270: SecurityResources.TOPIC_GROUPS,
271: SecurityResources.MESSAGE_SELECTED);
272: } else if (request
273: .getParameter("group.action.Remove_Group") != null) {
274: removeGroup(request);
275: } else if ("users".equals(groupTab)) {
276: String users = request.getParameter("users");
277:
278: if (users != null && users.length() > 0) {
279: addUsersToGroup(request, users);
280: } else if (request
281: .getParameter("group.action.Remove_Checked_Users") != null) {
282: removeUsersFromGroup(request);
283: }
284: } else {
285: String roles = request.getParameter("roles");
286:
287: if (roles != null && roles.length() > 0) {
288: addRolesToGroup(request, roles);
289: } else if (request
290: .getParameter("group.action.Remove_Checked_Roles") != null) {
291: removeRolesFromGroup(request);
292: }
293: }
294:
295: if (request.getParameter(FILTERED) != null) {
296: PortletMessaging.publish(request,
297: SecurityResources.TOPIC_GROUP,
298: SecurityResources.MESSAGE_FILTERED, "on");
299: } else {
300: PortletMessaging.cancel(request,
301: SecurityResources.TOPIC_GROUP,
302: SecurityResources.MESSAGE_FILTERED);
303: }
304: }
305:
306: super .processAction(request, response);
307: }
308:
309: protected void addGroup(ActionRequest actionRequest) {
310: String group = actionRequest.getParameter("group");
311: if (!SecurityUtil.isEmpty(group)) {
312: try {
313: groupManager.addGroup(group);
314: PortletMessaging.publish(actionRequest,
315: SecurityResources.TOPIC_GROUPS,
316: SecurityResources.MESSAGE_REFRESH, "true");
317: PortletMessaging.publish(actionRequest,
318: SecurityResources.TOPIC_GROUPS,
319: SecurityResources.MESSAGE_SELECTED, group);
320: PortletMessaging.publish(actionRequest,
321: SecurityResources.TOPIC_GROUPS,
322: SecurityResources.MESSAGE_CHANGED, group);
323: PortletMessaging.publish(actionRequest,
324: SecurityResources.TOPIC_USERS,
325: SecurityResources.MESSAGE_REFRESH_GROUPS,
326: "true");
327: } catch (NotSerializableException e) {
328: e.printStackTrace();
329: } catch (SecurityException sex) {
330: SecurityUtil
331: .publishErrorMessage(actionRequest,
332: SecurityResources.TOPIC_GROUP, sex
333: .getMessage());
334: }
335: }
336: }
337:
338: protected void removeGroup(ActionRequest actionRequest) {
339: String group = actionRequest.getParameter("group");
340: if (!SecurityUtil.isEmpty(group)) {
341: try {
342: groupManager.removeGroup(group);
343: try {
344: PortletMessaging.publish(actionRequest,
345: SecurityResources.TOPIC_GROUPS,
346: SecurityResources.MESSAGE_REFRESH, "true");
347: PortletMessaging.publish(actionRequest,
348: SecurityResources.TOPIC_USERS,
349: SecurityResources.MESSAGE_REFRESH_GROUPS,
350: "true");
351: } catch (NotSerializableException e) {
352: e.printStackTrace();
353: }
354: PortletMessaging.cancel(actionRequest,
355: SecurityResources.TOPIC_GROUPS,
356: SecurityResources.MESSAGE_SELECTED);
357: } catch (SecurityException sex) {
358: SecurityUtil
359: .publishErrorMessage(actionRequest,
360: SecurityResources.TOPIC_GROUP, sex
361: .getMessage());
362: }
363: }
364: }
365:
366: protected void addUsersToGroup(ActionRequest request, String users) {
367: String group = request.getParameter("group");
368: if (group != null) {
369: int count = 0;
370: StringTokenizer tokenizer = new StringTokenizer(users, ",");
371: while (tokenizer.hasMoreTokens()) {
372: String user = tokenizer.nextToken();
373: try {
374: if (user.startsWith("box_")) {
375: user = user.substring("box_".length());
376: groupManager.addUserToGroup(user, group);
377: count++;
378: }
379: } catch (SecurityException sex) {
380: SecurityUtil.publishErrorMessage(request,
381: SecurityResources.TOPIC_GROUP, sex
382: .getMessage());
383: }
384: }
385: if (count > 0) {
386: try {
387: PortletMessaging.publish(request,
388: SecurityResources.TOPIC_GROUP,
389: SecurityResources.MESSAGE_REFRESH, "true");
390: } catch (Exception e) {
391: e.printStackTrace();
392: }
393: }
394: }
395: }
396:
397: protected void removeUsersFromGroup(ActionRequest request) {
398: String group = request.getParameter("group");
399: if (group != null) {
400: int count = 0;
401: Enumeration e = request.getParameterNames();
402: while (e.hasMoreElements()) {
403: String name = (String) e.nextElement();
404: if (name.startsWith("box_")) {
405: String user = name.substring("box_".length());
406: try {
407: groupManager.removeUserFromGroup(user, group);
408: count++;
409: } catch (SecurityException sex) {
410: SecurityUtil.publishErrorMessage(request,
411: SecurityResources.TOPIC_GROUP, sex
412: .getMessage());
413: }
414:
415: }
416: }
417: if (count > 0) {
418: try {
419: PortletMessaging.publish(request,
420: SecurityResources.TOPIC_GROUP,
421: SecurityResources.MESSAGE_REFRESH, "true");
422: } catch (Exception ex) {
423: ex.printStackTrace();
424: }
425: }
426: }
427: }
428:
429: protected void addRolesToGroup(ActionRequest request, String roles) {
430: String group = request.getParameter("group");
431: if (group != null) {
432: int count = 0;
433: StringTokenizer tokenizer = new StringTokenizer(roles, ",");
434: while (tokenizer.hasMoreTokens()) {
435: String role = tokenizer.nextToken();
436: try {
437: if (role.startsWith("box_")) {
438: role = role.substring("box_".length());
439: roleManager.addRoleToGroup(role, group);
440: count++;
441: }
442: } catch (SecurityException sex) {
443: SecurityUtil.publishErrorMessage(request,
444: SecurityResources.TOPIC_GROUP, sex
445: .getMessage());
446: }
447: }
448: if (count > 0) {
449: try {
450: PortletMessaging.publish(request,
451: SecurityResources.TOPIC_GROUP,
452: SecurityResources.MESSAGE_REFRESH, "true");
453: } catch (Exception e) {
454: e.printStackTrace();
455: }
456: }
457: }
458: }
459:
460: protected void removeRolesFromGroup(ActionRequest request) {
461: String group = request.getParameter("group");
462: if (group != null) {
463: int count = 0;
464: Enumeration e = request.getParameterNames();
465: while (e.hasMoreElements()) {
466: String name = (String) e.nextElement();
467: if (name.startsWith("box_")) {
468: String role = name.substring("box_".length());
469: try {
470: roleManager.removeRoleFromGroup(role, group);
471: count++;
472: } catch (SecurityException sex) {
473: SecurityUtil.publishErrorMessage(request,
474: SecurityResources.TOPIC_GROUP, sex
475: .getMessage());
476: }
477:
478: }
479: }
480: if (count > 0) {
481: try {
482: PortletMessaging.publish(request,
483: SecurityResources.TOPIC_GROUP,
484: SecurityResources.MESSAGE_REFRESH, "true");
485: } catch (Exception ex) {
486: ex.printStackTrace();
487: }
488: }
489: }
490: }
491: }
|