1/*2 * Copyright 2000-2001,2004 The Apache Software Foundation.3 * 4 * Licensed under the Apache License, Version 2.0 (the "License");5 * you may not use this file except in compliance with the License.6 * You may obtain a copy of the License at7 * 8 * http://www.apache.org/licenses/LICENSE-2.09 * 10 * Unless required by applicable law or agreed to in writing, software11 * distributed under the License is distributed on an "AS IS" BASIS,12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.13 * See the License for the specific language governing permissions and14 * limitations under the License.15 */1617packageorg.apache.jetspeed.modules.actions.portlets.security;
1819// velocity20import org.apache.velocity.context.Context;
21import org.apache.velocity.app.FieldMethodizer;
2223// turbine util24import org.apache.turbine.util.RunData;
25import org.apache.turbine.util.StringUtils;
2627// jetspeed services28import org.apache.jetspeed.services.JetspeedSecurity;
29import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30import org.apache.jetspeed.services.logging.JetspeedLogger;
31import org.apache.jetspeed.services.security.JetspeedSecurityException;
32import org.apache.jetspeed.services.resources.JetspeedResources;
33import org.apache.jetspeed.util.template.JetspeedLink;
34import org.apache.jetspeed.om.security.JetspeedUser;
3536// jetspeed velocity37import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
38import org.apache.jetspeed.portal.portlets.VelocityPortlet;
3940import java.util.List;
41import java.util.Iterator;
42import java.util.ArrayList;
4344// regexp stuff45import org.apache.regexp.RE;
46import org.apache.regexp.RECompiler;
4748/***49 * This action sets up the template context for browsing of users in the Turbine database.50 *51 * @author <a href="mailto:taylor@apache.org">David Sean Taylor</a>52 * @version $Id: UserBrowserAction.java,v 1.14 2004/02/23 02:53:08 jford Exp $53 */54publicclassUserBrowserActionextendsVelocityPortletAction55 {
56/*** name of the parameter to this portlet that tells us how many rows to show per page */57publicstaticfinal String NUMBER_PER_PAGE = "number-per-page";
5859/*** name of the parameter that holds the page number to display */60publicstaticfinal String DISPLAY_PAGE = "ubpage";
6162/*** name of the parameter that holds the filter value */63publicstaticfinal String FILTER_VALUE = "filter_value";
6465/*** name of the parameter that holds the regexp flag */66publicstaticfinal String FILTER_REGEXP = "filter_regexp";
6768/*** name of the parameter that holds the filter type */69publicstaticfinal String FILTER_TYPE = "filter_type";
7071/*** value of the filter type parameter for searching by username */72publicstaticfinal String FILTER_TYPE_USERNAME = "filter_type_username";
7374/*** value of the filter type parameter for searching by last name */75publicstaticfinal String FILTER_TYPE_LASTNAME = "filter_type_lastname";
7677/***78 * Static initialization of the logger for this class79 */80privatestaticfinalJetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserBrowserAction.class.getName());
8182/***83 * Build the maximized state content for this portlet. (Same as normal state).84 *85 * @param portlet The velocity-based portlet that is being built.86 * @param context The velocity context for this request.87 * @param rundata The turbine rundata context for this request.88 */89protectedvoid buildMaximizedContext( VelocityPortlet portlet,
90 Context context,
91 RunData rundata )
92 {
93 buildNormalContext( portlet, context, rundata);
94 }
9596/***97 * Build the configure state content for this portlet.98 * TODO: we could configure this portlet with configurable skins, etc..99 *100 * @param portlet The velocity-based portlet that is being built.101 * @param context The velocity context for this request.102 * @param rundata The turbine rundata context for this request.103 */104protectedvoid buildConfigureContext( VelocityPortlet portlet,
105 Context context,
106 RunData rundata )
107 {
108109 buildNormalContext( portlet, context, rundata);
110 }
111112/***113 * Build the normal state content for this portlet.114 *115 * @param portlet The velocity-based portlet that is being built.116 * @param context The velocity context for this request.117 * @param rundata The turbine rundata context for this request.118 */119protectedvoid buildNormalContext( VelocityPortlet portlet,
120 Context context,
121 RunData rundata )
122 {
123try124 {
125//hack to make the static variables visible in template126 context.put("s_config", new FieldMethodizer( context.get("config") ) );
127128// Currently, the getUsers(filter) is not implemented - need to do local filtering129 Iterator users = JetspeedSecurity.getUsers();
130131 List userList = new ArrayList();
132133// Is filtering requested?134 String filterValue = rundata.getParameters().getString(FILTER_VALUE);
135if (filterValue != null)
136 {
137 String filterType = rundata.getParameters().getString(FILTER_TYPE, FILTER_TYPE_USERNAME);
138boolean useRE = rundata.getParameters().getBoolean(FILTER_REGEXP);
139 RE r = null;
140 RECompiler rc = null;
141if (useRE)
142 {
143try144 {
145 rc = new RECompiler();
146 r = new RE();
147 r.setProgram(rc.compile(filterValue));
148 }
149catch (org.apache.regexp.RESyntaxException rex)
150 {
151 logger.warn("UserBrowserAction: error processing regular expression [" + filterValue + "]: " +
152 rex.toString());
153 }
154 }
155while (users.hasNext())
156 {
157JetspeedUser user = (JetspeedUser) users.next();
158 String compareValue = null;
159if (filterType.equals(FILTER_TYPE_USERNAME))
160 {
161 compareValue = user.getUserName();
162 }
163elseif (filterType.equals(FILTER_TYPE_LASTNAME))
164 {
165 compareValue = user.getLastName();
166 }
167168if (compareValue != null)
169 {
170if (useRE && r.match(compareValue))
171 {
172 userList.add(user);
173 }
174elseif (compareValue.startsWith(filterValue))
175 {
176 userList.add(user);
177 }
178 }
179 }
180 } else {
181while (users.hasNext())
182 {
183 userList.add(users.next());
184 }
185 }
186187188int currentPage = rundata.getParameters().getInt(DISPLAY_PAGE, 1);
189190int numberPerPage;
191192try193 {
194 numberPerPage = Integer.parseInt(portlet.getPortletConfig().getInitParameter(NUMBER_PER_PAGE,"50"));
195 }
196catch (NumberFormatException e)
197 {
198 numberPerPage = 50;
199 }
200201if (userList.size() > numberPerPage)
202 {
203int numberOfPages = (int) ((userList.size() - 1 + numberPerPage) / numberPerPage);
204int from = (currentPage - 1) * numberPerPage;
205int to = Math.min(currentPage * numberPerPage,userList.size());
206 context.put(SecurityConstants.CONTEXT_USERS, userList.subList(from, to));
207208//now build a set of links to access each page (assumed we will show all links)209 StringBuffer pageLinks = new StringBuffer("page: ");
210for (int i = 1; i <= numberOfPages; i++)
211 {
212if (i == currentPage)
213 {
214 pageLinks.append("( " + i + " ) ");
215 }
216else217 {
218// make sure the page navigation always points to 219// the right pane220 Object jslink = context.get("jslink");
221if (jslink instanceof JetspeedLink) {
222 pageLinks.append("[ <a href=\"" + ((JetspeedLink)jslink).getPaneByName("UserBrowser").addQueryData(DISPLAY_PAGE, new Integer(i)).toString() + "\">" + i + "</a> ] ");
223 } else {
224 pageLinks.append("[ <a href=\"./portal/" + DISPLAY_PAGE + "/" + i + "\">" + i + "</a> ] ");
225 }
226 }
227 }
228 context.put("pagelinks", pageLinks);
229 }
230else231 {
232 context.put(SecurityConstants.CONTEXT_USERS, userList);
233 }
234 context.put(DISPLAY_PAGE,Integer.toString(currentPage));
235236237 }
238catch (JetspeedSecurityException e)
239 {
240// log the error msg241 logger.error("Exception", e);
242243 rundata.setMessage("Error in Jetspeed User Security: " + e.toString());
244 rundata.setStackTrace(StringUtils.stackTrace(e), e);
245 rundata.setScreenTemplate(JetspeedResources.getString("template.error","Error"));
246 }
247 }
248249 }