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.roles;
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.RoleManager;
041: import org.apache.jetspeed.security.SecurityException;
042: import org.apache.jetspeed.security.User;
043: import org.apache.jetspeed.security.UserManager;
044: import org.apache.jetspeed.security.UserPrincipal;
045: import org.apache.portals.gems.browser.BrowserIterator;
046: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
047: import org.apache.portals.gems.browser.BrowserPortlet;
048: import org.apache.portals.gems.util.StatusMessage;
049: import org.apache.portals.messaging.PortletMessaging;
050: import org.apache.velocity.context.Context;
051:
052: /**
053: * Role Details
054: *
055: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
056: * @version $Id: RoleDetails.java 348264 2005-11-22 22:06:45Z taylor $
057: */
058: public class RoleDetails extends BrowserPortlet {
059: private UserManager userManager;
060: private RoleManager roleManager;
061:
062: public void init(PortletConfig config) throws PortletException {
063: super .init(config);
064: userManager = (UserManager) getPortletContext().getAttribute(
065: CommonPortletServices.CPS_USER_MANAGER_COMPONENT);
066: if (null == userManager) {
067: throw new PortletException(
068: "Failed to find the User Manager on portlet initialization");
069: }
070: roleManager = (RoleManager) getPortletContext().getAttribute(
071: CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
072: if (null == roleManager) {
073: throw new PortletException(
074: "Failed to find the Role Manager on portlet initialization");
075: }
076: }
077:
078: public void getRows(RenderRequest request, String sql,
079: int windowSize) {
080: getRows(request, sql, windowSize, null);
081: }
082:
083: public void getRows(RenderRequest request, String sql,
084: int windowSize, String filter) {
085: List resultSetTitleList = new ArrayList();
086: List resultSetTypeList = new ArrayList();
087: if (filter != null) {
088: if (filter.length() == 0) {
089: filter = null;
090: } else {
091: filter = filter.toLowerCase();
092: }
093: }
094:
095: List list = new ArrayList();
096: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
097: resultSetTitleList.add("usersinrole"); // resource bundle key
098:
099: String selectedRole = (String) PortletMessaging.receive(
100: request, SecurityResources.TOPIC_ROLES,
101: SecurityResources.MESSAGE_SELECTED);
102: if (selectedRole != null) {
103: try {
104: Iterator users = userManager.getUsersInRole(
105: selectedRole).iterator();
106: while (users.hasNext()) {
107: User user = (User) users.next();
108: Principal principal = SecurityUtil.getPrincipal(
109: user.getSubject(), UserPrincipal.class);
110: if (filter == null
111: || principal.getName().toLowerCase()
112: .startsWith(filter)) {
113: list.add(principal.getName());
114: }
115: }
116: } catch (SecurityException sex) {
117: SecurityUtil.publishErrorMessage(request,
118: SecurityResources.TOPIC_ROLE, sex.getMessage());
119: }
120: }
121: BrowserIterator iterator = new DatabaseBrowserIterator(list,
122: resultSetTitleList, resultSetTypeList, windowSize);
123: setBrowserIterator(request, iterator);
124: iterator.sort("usersinrole"); // resource bundle key
125: }
126:
127: public void doView(RenderRequest request, RenderResponse response)
128: throws PortletException, IOException {
129: String change = (String) PortletMessaging.consume(request,
130: SecurityResources.TOPIC_ROLES,
131: SecurityResources.MESSAGE_CHANGED);
132: if (change != null) {
133: this .clearBrowserIterator(request);
134: }
135: Context context = this .getContext(request);
136:
137: String selectedRole = (String) PortletMessaging.receive(
138: request, SecurityResources.TOPIC_ROLES,
139: SecurityResources.MESSAGE_SELECTED);
140: if (selectedRole != null) {
141: context.put("role", selectedRole);
142: }
143:
144: String userChooser = SecurityUtil.getAbsoluteUrl(request,
145: "/Administrative/choosers/multiusers.psml");
146: context.put("userChooser", userChooser);
147:
148: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
149: request, SecurityResources.TOPIC_ROLE,
150: SecurityResources.MESSAGE_STATUS);
151: if (msg != null) {
152: this .getContext(request).put("statusMsg", msg);
153: }
154:
155: String filtered = (String) PortletMessaging.receive(request,
156: SecurityResources.TOPIC_ROLE,
157: SecurityResources.MESSAGE_FILTERED);
158: if (filtered != null) {
159: this .getContext(request).put(FILTERED, "on");
160: }
161:
162: String refresh = (String) PortletMessaging.consume(request,
163: SecurityResources.TOPIC_ROLE,
164: SecurityResources.MESSAGE_REFRESH);
165: if (refresh != null) {
166: this .clearBrowserIterator(request);
167: }
168:
169: ArrayList errorMessages = (ArrayList) PortletMessaging.consume(
170: request, SecurityResources.TOPIC_ROLE,
171: SecurityResources.ERROR_MESSAGES);
172: if (errorMessages != null) {
173: this .getContext(request).put(
174: SecurityResources.ERROR_MESSAGES, errorMessages);
175: }
176:
177: super .doView(request, response);
178: }
179:
180: public void processAction(ActionRequest request,
181: ActionResponse response) throws PortletException,
182: IOException {
183: if (request.getPortletMode() == PortletMode.VIEW) {
184: String users = request.getParameter("users");
185:
186: if (users != null && users.length() > 0) {
187: addUsersToRole(request, users);
188: } else if (request.getParameter("role.action.Add_New_Role") != null) {
189: PortletMessaging.cancel(request,
190: SecurityResources.TOPIC_ROLES,
191: SecurityResources.MESSAGE_SELECTED);
192: } else if (request
193: .getParameter("role.action.Remove_Checked_Users") != null) {
194: removeUsersFromRole(request);
195: } else if (request.getParameter("role.action.Remove_Role") != null) {
196: removeRole(request);
197: } else if (request.getParameter("role.action.Save") != null) {
198: addRole(request);
199: }
200:
201: if (request.getParameter(FILTERED) != null) {
202: PortletMessaging.publish(request,
203: SecurityResources.TOPIC_ROLE,
204: SecurityResources.MESSAGE_FILTERED, "on");
205: } else {
206: PortletMessaging.cancel(request,
207: SecurityResources.TOPIC_ROLE,
208: SecurityResources.MESSAGE_FILTERED);
209: }
210: }
211: super .processAction(request, response);
212: }
213:
214: protected void addRole(ActionRequest actionRequest) {
215: String role = actionRequest.getParameter("role");
216: if (!SecurityUtil.isEmpty(role)) {
217: try {
218: roleManager.addRole(role);
219: PortletMessaging.publish(actionRequest,
220: SecurityResources.TOPIC_ROLES,
221: SecurityResources.MESSAGE_REFRESH, "true");
222: PortletMessaging.publish(actionRequest,
223: SecurityResources.TOPIC_ROLES,
224: SecurityResources.MESSAGE_SELECTED, role);
225: PortletMessaging.publish(actionRequest,
226: SecurityResources.TOPIC_ROLES,
227: SecurityResources.MESSAGE_CHANGED, role);
228: PortletMessaging
229: .publish(
230: actionRequest,
231: SecurityResources.TOPIC_USERS,
232: SecurityResources.MESSAGE_REFRESH_ROLES,
233: "true");
234: PortletMessaging
235: .publish(
236: actionRequest,
237: SecurityResources.TOPIC_GROUPS,
238: SecurityResources.MESSAGE_REFRESH_ROLES,
239: "true");
240: } catch (SecurityException sex) {
241: SecurityUtil.publishErrorMessage(actionRequest,
242: SecurityResources.TOPIC_ROLE, sex.getMessage());
243: } catch (NotSerializableException e) {
244: e.printStackTrace();
245: }
246: }
247: }
248:
249: protected void removeRole(ActionRequest actionRequest) {
250: String role = actionRequest.getParameter("role");
251: if (!SecurityUtil.isEmpty(role)) {
252: try {
253: roleManager.removeRole(role);
254: try {
255: PortletMessaging.publish(actionRequest,
256: SecurityResources.TOPIC_ROLES,
257: SecurityResources.MESSAGE_REFRESH, "true");
258: PortletMessaging.publish(actionRequest,
259: SecurityResources.TOPIC_USERS,
260: SecurityResources.MESSAGE_REFRESH_ROLES,
261: "true");
262: PortletMessaging.publish(actionRequest,
263: SecurityResources.TOPIC_GROUPS,
264: SecurityResources.MESSAGE_REFRESH_ROLES,
265: "true");
266: } catch (NotSerializableException e) {
267: e.printStackTrace();
268: }
269: PortletMessaging.cancel(actionRequest,
270: SecurityResources.TOPIC_ROLES,
271: SecurityResources.MESSAGE_SELECTED);
272: } catch (SecurityException sex) {
273: SecurityUtil.publishErrorMessage(actionRequest,
274: SecurityResources.TOPIC_ROLE, sex.getMessage());
275: }
276: }
277: }
278:
279: protected void addUsersToRole(ActionRequest request, String users) {
280: String role = request.getParameter("role");
281: if (role != null) {
282: int count = 0;
283: StringTokenizer tokenizer = new StringTokenizer(users, ",");
284: while (tokenizer.hasMoreTokens()) {
285: String user = tokenizer.nextToken();
286: try {
287: if (user.startsWith("box_")) {
288: user = user.substring("box_".length());
289: roleManager.addRoleToUser(user, role);
290: count++;
291: }
292: } catch (SecurityException sex) {
293: SecurityUtil.publishErrorMessage(request,
294: SecurityResources.TOPIC_ROLE, sex
295: .getMessage());
296: }
297: }
298: if (count > 0) {
299: try {
300: PortletMessaging.publish(request,
301: SecurityResources.TOPIC_ROLE,
302: SecurityResources.MESSAGE_REFRESH, "true");
303: } catch (Exception e) {
304: e.printStackTrace();
305: }
306: }
307: }
308: }
309:
310: protected void removeUsersFromRole(ActionRequest request) {
311: String role = request.getParameter("role");
312: if (role != null) {
313: int count = 0;
314: Enumeration e = request.getParameterNames();
315: while (e.hasMoreElements()) {
316: String name = (String) e.nextElement();
317: if (name.startsWith("box_")) {
318: String user = name.substring("box_".length());
319: try {
320: roleManager.removeRoleFromUser(user, role);
321: count++;
322: } catch (SecurityException sex) {
323: SecurityUtil.publishErrorMessage(request,
324: SecurityResources.TOPIC_ROLE, sex
325: .getMessage());
326: }
327: }
328: }
329: if (count > 0) {
330: try {
331: PortletMessaging.publish(request,
332: SecurityResources.TOPIC_ROLE,
333: SecurityResources.MESSAGE_REFRESH, "true");
334: } catch (Exception ex) {
335: ex.printStackTrace();
336: }
337: }
338: }
339: }
340:
341: }
|