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.users;
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: import javax.security.auth.Subject;
034:
035: import org.apache.jetspeed.CommonPortletServices;
036: import org.apache.jetspeed.portlets.security.SecurityResources;
037: import org.apache.jetspeed.portlets.security.SecurityUtil;
038: import org.apache.jetspeed.security.SecurityException;
039: import org.apache.jetspeed.security.User;
040: import org.apache.jetspeed.security.UserManager;
041: import org.apache.jetspeed.security.UserPrincipal;
042: import org.apache.portals.gems.browser.BrowserIterator;
043: import org.apache.portals.gems.browser.BrowserPortlet;
044: import org.apache.portals.gems.browser.DatabaseBrowserIterator;
045: import org.apache.portals.gems.util.StatusMessage;
046: import org.apache.portals.messaging.PortletMessaging;
047: import org.apache.velocity.context.Context;
048:
049: /**
050: * Role Browser - flat non-hierarchical view
051: *
052: * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>
053: * @version $Id: UserBrowser.java 348264 2005-11-22 22:06:45Z taylor $
054: */
055: public class UserBrowser extends BrowserPortlet {
056: protected UserManager userManager;
057:
058: // view context
059: public static final String STATUS = "statusMsg";
060: public static final String SELECTED = "selected";
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: }
071:
072: public void doView(RenderRequest request, RenderResponse response)
073: throws PortletException, IOException {
074: String selected = (String) PortletMessaging.receive(request,
075: SecurityResources.TOPIC_USERS,
076: SecurityResources.MESSAGE_SELECTED);
077: if (selected != null) {
078: Context context = this .getContext(request);
079: context.put(SELECTED, selected);
080: }
081: StatusMessage msg = (StatusMessage) PortletMessaging.consume(
082: request, SecurityResources.TOPIC_USERS,
083: SecurityResources.MESSAGE_STATUS);
084: if (msg != null) {
085: this .getContext(request).put(STATUS, msg);
086: }
087: String refresh = (String) PortletMessaging.consume(request,
088: SecurityResources.TOPIC_USERS,
089: SecurityResources.MESSAGE_REFRESH);
090: if (refresh != null) {
091: this .clearBrowserIterator(request);
092: }
093:
094: String filtered = (String) PortletMessaging.receive(request,
095: SecurityResources.TOPIC_USERS,
096: SecurityResources.MESSAGE_FILTERED);
097: if (filtered != null) {
098: this .getContext(request).put(FILTERED, "on");
099: }
100:
101: ArrayList errorMessages = (ArrayList) PortletMessaging.consume(
102: request, SecurityResources.TOPIC_USERS,
103: SecurityResources.ERROR_MESSAGES);
104: if (errorMessages != null) {
105: this .getContext(request).put(
106: SecurityResources.ERROR_MESSAGES, errorMessages);
107: }
108:
109: super .doView(request, response);
110: }
111:
112: public void processAction(ActionRequest request,
113: ActionResponse response) throws PortletException,
114: IOException {
115: if (request.getPortletMode() == PortletMode.VIEW) {
116: String selected = request.getParameter("user");
117: if (selected != null) {
118: PortletMessaging.publish(request,
119: SecurityResources.TOPIC_USERS,
120: SecurityResources.MESSAGE_SELECTED, selected);
121: }
122: }
123:
124: // TODO: if request parameters were working correctly we could replace this with render parameters
125: String filtered = request.getParameter(FILTERED);
126: if (filtered != null) {
127: PortletMessaging.publish(request,
128: SecurityResources.TOPIC_USERS,
129: SecurityResources.MESSAGE_FILTERED, "on");
130: } else {
131: PortletMessaging.cancel(request,
132: SecurityResources.TOPIC_USERS,
133: SecurityResources.MESSAGE_FILTERED);
134: }
135:
136: super .processAction(request, response);
137: }
138:
139: public void getRows(RenderRequest request, String sql,
140: int windowSize) {
141: getRows(request, sql, windowSize, "");
142: }
143:
144: public void getRows(RenderRequest request, String sql,
145: int windowSize, String filter) {
146: String roleFilter = request.getPreferences().getValue(
147: "FilterByRole", "");
148: if (roleFilter == null)
149: roleFilter = "";
150: boolean filterByRole = !roleFilter.equals("")
151: || roleFilter.equalsIgnoreCase("false");
152: List resultSetTitleList = new ArrayList();
153: List resultSetTypeList = new ArrayList();
154: resultSetTypeList.add(String.valueOf(Types.VARCHAR));
155: resultSetTitleList.add("user"); // resource bundle key
156:
157: List list = new ArrayList();
158: try {
159: if (filterByRole) {
160: Iterator users = userManager.getUsersInRole(roleFilter)
161: .iterator();
162: while (users.hasNext()) {
163: // NOTE: this can be a bit costly if you have a lot of users in a role
164: User user = (User) users.next();
165: Principal pr = getBestPrincipal(user.getSubject(),
166: UserPrincipal.class);
167: list.add(pr.getName());
168: }
169: } else {
170: Iterator users = userManager.getUserNames(filter);
171: while (users.hasNext()) {
172: list.add(users.next());
173: }
174:
175: }
176: } catch (SecurityException sex) {
177: SecurityUtil.publishErrorMessage(request,
178: SecurityResources.TOPIC_USERS, sex.getMessage());
179: }
180: BrowserIterator iterator = new DatabaseBrowserIterator(list,
181: resultSetTitleList, resultSetTypeList, windowSize);
182: setBrowserIterator(request, iterator);
183: iterator.sort("user"); // resource bundle key
184: }
185:
186: public static Principal getBestPrincipal(Subject subject,
187: Class classe) {
188:
189: Principal principal = null;
190: Iterator principals = subject.getPrincipals().iterator();
191: while (principals.hasNext()) {
192: Principal p = (Principal) principals.next();
193: if (classe.isInstance(p)) {
194: principal = p;
195: break;
196: } else {
197: if (principal == null) {
198: principal = p;
199: }
200: }
201: }
202: return principal;
203: }
204: }
|