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.security.Principal;
021: import java.sql.Types;
022: import java.util.ArrayList;
023: import java.util.Iterator;
024: import java.util.List;
025:
026: import javax.portlet.ActionRequest;
027: import javax.portlet.ActionResponse;
028: import javax.portlet.PortletConfig;
029: import javax.portlet.PortletException;
030: import javax.portlet.PortletMode;
031: import javax.portlet.RenderRequest;
032: import javax.portlet.RenderResponse;
033:
034: import org.apache.jetspeed.CommonPortletServices;
035: import org.apache.jetspeed.portlets.security.SecurityResources;
036: import org.apache.jetspeed.portlets.security.SecurityUtil;
037: import org.apache.jetspeed.security.Role;
038: import org.apache.jetspeed.security.RoleManager;
039: import org.apache.jetspeed.security.SecurityException;
040: import org.apache.portals.gems.browser.BrowserIterator;
041: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
042: import org.apache.portals.gems.browser.BrowserPortlet;
043: import org.apache.portals.gems.util.StatusMessage;
044: import org.apache.portals.messaging.PortletMessaging;
045: import org.apache.velocity.context.Context;
046:
047: /**
048: * Role Browser - flat non-hierarchical view
049: *
050: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
051: * @version $Id: RoleBrowser.java 348264 2005-11-22 22:06:45Z taylor $
052: */
053: public class RoleBrowser extends BrowserPortlet {
054: private RoleManager roleManager;
055:
056: public void init(PortletConfig config) throws PortletException {
057: super .init(config);
058: roleManager = (RoleManager) getPortletContext().getAttribute(
059: CommonPortletServices.CPS_ROLE_MANAGER_COMPONENT);
060: if (null == roleManager) {
061: throw new PortletException(
062: "Failed to find the Role Manager on portlet initialization");
063: }
064: }
065:
066: public void getRows(RenderRequest request, String sql,
067: int windowSize) {
068: getRows(request, sql, windowSize, "");
069: }
070:
071: public void getRows(RenderRequest request, String sql,
072: int windowSize, String filter) {
073: List resultSetTitleList = new ArrayList();
074: List resultSetTypeList = new ArrayList();
075: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
076: resultSetTitleList.add("role"); // resource bundle key
077:
078: List list = new ArrayList();
079: try {
080: Iterator roles = roleManager.getRoles(filter);
081:
082: while (roles.hasNext()) {
083: Role role = (Role) roles.next();
084:
085: Principal principal = role.getPrincipal();
086: list.add(principal.getName());
087: }
088: } catch (SecurityException sex) {
089: SecurityUtil.publishErrorMessage(request,
090: SecurityResources.TOPIC_ROLES, sex.getMessage());
091: }
092: BrowserIterator iterator = new DatabaseBrowserIterator(list,
093: resultSetTitleList, resultSetTypeList, windowSize);
094: setBrowserIterator(request, iterator);
095: iterator.sort("role"); // resource bundle key
096: }
097:
098: public void doView(RenderRequest request, RenderResponse response)
099: throws PortletException, IOException {
100: String selected = (String) PortletMessaging.receive(request,
101: SecurityResources.TOPIC_ROLES,
102: SecurityResources.MESSAGE_SELECTED);
103: if (selected != null) {
104: Context context = this .getContext(request);
105: context.put("selected", selected);
106: }
107: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
108: request, SecurityResources.TOPIC_ROLES,
109: SecurityResources.MESSAGE_STATUS);
110: if (msg != null) {
111: this .getContext(request).put("statusMsg", msg);
112: }
113:
114: String filtered = (String) PortletMessaging.receive(request,
115: SecurityResources.TOPIC_ROLES,
116: SecurityResources.MESSAGE_FILTERED);
117: if (filtered != null) {
118: this .getContext(request).put(FILTERED, "on");
119: }
120:
121: String refresh = (String) PortletMessaging.consume(request,
122: SecurityResources.TOPIC_ROLES,
123: SecurityResources.MESSAGE_REFRESH);
124: if (refresh != null) {
125: this .clearBrowserIterator(request);
126: }
127:
128: ArrayList errorMessages = (ArrayList) PortletMessaging.consume(
129: request, SecurityResources.TOPIC_ROLES,
130: SecurityResources.ERROR_MESSAGES);
131: if (errorMessages != null) {
132: this .getContext(request).put(
133: SecurityResources.ERROR_MESSAGES, errorMessages);
134: }
135:
136: super .doView(request, response);
137: }
138:
139: public void processAction(ActionRequest request,
140: ActionResponse response) throws PortletException,
141: IOException {
142: if (request.getPortletMode() == PortletMode.VIEW) {
143: String selected = request.getParameter("role");
144: if (selected != null) {
145: Role role = lookupRole(request, selected);
146: if (role != null) {
147: PortletMessaging.publish(request,
148: SecurityResources.TOPIC_ROLES,
149: SecurityResources.MESSAGE_SELECTED,
150: selected);
151: PortletMessaging
152: .publish(request,
153: SecurityResources.TOPIC_ROLES,
154: SecurityResources.MESSAGE_CHANGED,
155: selected);
156: }
157: }
158: }
159:
160: String filtered = request.getParameter(FILTERED);
161: if (filtered != null) {
162: PortletMessaging.publish(request,
163: SecurityResources.TOPIC_ROLES,
164: SecurityResources.MESSAGE_FILTERED, "on");
165: } else {
166: PortletMessaging.cancel(request,
167: SecurityResources.TOPIC_ROLES,
168: SecurityResources.MESSAGE_FILTERED);
169: }
170:
171: super .processAction(request, response);
172:
173: }
174:
175: private Role lookupRole(ActionRequest actionRequest, String roleName) {
176: try {
177: return roleManager.getRole(roleName);
178: } catch (SecurityException sex) {
179: SecurityUtil.publishErrorMessage(actionRequest,
180: SecurityResources.TOPIC_ROLES, sex.getMessage());
181: return null;
182: }
183: }
184: }
|