1 16 17 package org.apache.jetspeed.modules.actions.portlets.security; 18 19 import org.apache.velocity.context.Context; 21 import org.apache.velocity.app.FieldMethodizer; 22 23 import org.apache.turbine.util.RunData; 25 import org.apache.turbine.util.StringUtils; 26 27 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 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 import org.apache.regexp.RE; 46 import org.apache.regexp.RECompiler; 47 48 54 public class UserBrowserAction extends VelocityPortletAction 55 { 56 57 public static final String NUMBER_PER_PAGE = "number-per-page"; 58 59 60 public static final String DISPLAY_PAGE = "ubpage"; 61 62 63 public static final String FILTER_VALUE = "filter_value"; 64 65 66 public static final String FILTER_REGEXP = "filter_regexp"; 67 68 69 public static final String FILTER_TYPE = "filter_type"; 70 71 72 public static final String FILTER_TYPE_USERNAME = "filter_type_username"; 73 74 75 public static final String FILTER_TYPE_LASTNAME = "filter_type_lastname"; 76 77 80 private static final JetspeedLogger logger = JetspeedLogFactoryService.getLogger(UserBrowserAction.class.getName()); 81 82 89 protected void buildMaximizedContext( VelocityPortlet portlet, 90 Context context, 91 RunData rundata ) 92 { 93 buildNormalContext( portlet, context, rundata); 94 } 95 96 104 protected void buildConfigureContext( VelocityPortlet portlet, 105 Context context, 106 RunData rundata ) 107 { 108 109 buildNormalContext( portlet, context, rundata); 110 } 111 112 119 protected void buildNormalContext( VelocityPortlet portlet, 120 Context context, 121 RunData rundata ) 122 { 123 try 124 { 125 context.put("s_config", new FieldMethodizer( context.get("config") ) ); 127 128 Iterator users = JetspeedSecurity.getUsers(); 130 131 List userList = new ArrayList (); 132 133 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 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 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 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 } | Popular Tags |