1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.jetspeed.modules.actions.portlets.security;
18
19
20 import org.apache.velocity.context.Context;
21 import org.apache.velocity.app.FieldMethodizer;
22
23
24 import org.apache.turbine.util.RunData;
25 import org.apache.turbine.util.StringUtils;
26
27
28 import org.apache.jetspeed.services.JetspeedSecurity;
29 import org.apache.jetspeed.services.logging.JetspeedLogFactoryService;
30 import org.apache.jetspeed.services.logging.JetspeedLogger;
31 import org.apache.jetspeed.services.security.JetspeedSecurityException;
32 import org.apache.jetspeed.services.resources.JetspeedResources;
33 import org.apache.jetspeed.util.template.JetspeedLink;
34 import org.apache.jetspeed.om.security.JetspeedUser;
35
36
37 import org.apache.jetspeed.modules.actions.portlets.VelocityPortletAction;
38 import org.apache.jetspeed.portal.portlets.VelocityPortlet;
39
40 import java.util.List;
41 import java.util.Iterator;
42 import java.util.ArrayList;
43
44
45 import org.apache.regexp.RE;
46 import org.apache.regexp.RECompiler;
47
48 /***
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 */
54 public class UserBrowserAction extends VelocityPortletAction
55 {
56 /*** name of the parameter to this portlet that tells us how many rows to show per page */
57 public static final String NUMBER_PER_PAGE = "number-per-page";
58
59 /*** name of the parameter that holds the page number to display */
60 public static final String DISPLAY_PAGE = "ubpage";
61
62 /*** name of the parameter that holds the filter value */
63 public static final String FILTER_VALUE = "filter_value";
64
65 /*** name of the parameter that holds the regexp flag */
66 public static final String FILTER_REGEXP = "filter_regexp";
67
68 /*** name of the parameter that holds the filter type */
69 public static final String FILTER_TYPE = "filter_type";
70
71 /*** value of the filter type parameter for searching by username */
72 public static final String FILTER_TYPE_USERNAME = "filter_type_username";
73
74 /*** value of the filter type parameter for searching by last name */
75 public static final String FILTER_TYPE_LASTNAME = "filter_type_lastname";
76
77 /***
78 * Static initialization of the logger for this class
79 */
80 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserBrowserAction.class.getName());
81
82 /***
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 */
89 protected void buildMaximizedContext( VelocityPortlet portlet,
90 Context context,
91 RunData rundata )
92 {
93 buildNormalContext( portlet, context, rundata);
94 }
95
96 /***
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 */
104 protected void buildConfigureContext( VelocityPortlet portlet,
105 Context context,
106 RunData rundata )
107 {
108
109 buildNormalContext( portlet, context, rundata);
110 }
111
112 /***
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 */
119 protected void buildNormalContext( VelocityPortlet portlet,
120 Context context,
121 RunData rundata )
122 {
123 try
124 {
125
126 context.put("s_config", new FieldMethodizer( context.get("config") ) );
127
128
129 Iterator users = JetspeedSecurity.getUsers();
130
131 List userList = new ArrayList();
132
133
134 String filterValue = rundata.getParameters().getString(FILTER_VALUE);
135 if (filterValue != null)
136 {
137 String filterType = rundata.getParameters().getString(FILTER_TYPE, FILTER_TYPE_USERNAME);
138 boolean useRE = rundata.getParameters().getBoolean(FILTER_REGEXP);
139 RE r = null;
140 RECompiler rc = null;
141 if (useRE)
142 {
143 try
144 {
145 rc = new RECompiler();
146 r = new RE();
147 r.setProgram(rc.compile(filterValue));
148 }
149 catch (org.apache.regexp.RESyntaxException rex)
150 {
151 logger.warn("UserBrowserAction: error processing regular expression [" + filterValue + "]: " +
152 rex.toString());
153 }
154 }
155 while (users.hasNext())
156 {
157 JetspeedUser user = (JetspeedUser) users.next();
158 String compareValue = null;
159 if (filterType.equals(FILTER_TYPE_USERNAME))
160 {
161 compareValue = user.getUserName();
162 }
163 else if (filterType.equals(FILTER_TYPE_LASTNAME))
164 {
165 compareValue = user.getLastName();
166 }
167
168 if (compareValue != null)
169 {
170 if (useRE && r.match(compareValue))
171 {
172 userList.add(user);
173 }
174 else if (compareValue.startsWith(filterValue))
175 {
176 userList.add(user);
177 }
178 }
179 }
180 } else {
181 while (users.hasNext())
182 {
183 userList.add(users.next());
184 }
185 }
186
187
188 int currentPage = rundata.getParameters().getInt(DISPLAY_PAGE, 1);
189
190 int numberPerPage;
191
192 try
193 {
194 numberPerPage = Integer.parseInt(portlet.getPortletConfig().getInitParameter(NUMBER_PER_PAGE,"50"));
195 }
196 catch (NumberFormatException e)
197 {
198 numberPerPage = 50;
199 }
200
201 if (userList.size() > numberPerPage)
202 {
203 int numberOfPages = (int) ((userList.size() - 1 + numberPerPage) / numberPerPage);
204 int from = (currentPage - 1) * numberPerPage;
205 int to = Math.min(currentPage * numberPerPage,userList.size());
206 context.put(SecurityConstants.CONTEXT_USERS, userList.subList(from, to));
207
208
209 StringBuffer pageLinks = new StringBuffer("page: ");
210 for (int i = 1; i <= numberOfPages; i++)
211 {
212 if (i == currentPage)
213 {
214 pageLinks.append("( " + i + " ) ");
215 }
216 else
217 {
218
219
220 Object jslink = context.get("jslink");
221 if (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 }
230 else
231 {
232 context.put(SecurityConstants.CONTEXT_USERS, userList);
233 }
234 context.put(DISPLAY_PAGE,Integer.toString(currentPage));
235
236
237 }
238 catch (JetspeedSecurityException e)
239 {
240
241 logger.error("Exception", e);
242
243 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 }
248
249 }