001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com) 006 * 007 * This library is free software; you can redistribute it and/or 008 * modify it under the terms of the GNU Lesser General Public 009 * License as published by the Free Software Foundation; either 010 * version 2.1 of the License, or (at your option) any later version. 011 * 012 * This library is distributed in the hope that it will be useful, 013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 015 * Lesser General Public License for more details. 016 * 017 * For further information about Alkacon Software GmbH & Co. KG, please see the 018 * company website: http://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: http://www.opencms.org 022 * 023 * You should have received a copy of the GNU Lesser General Public 024 * License along with this library; if not, write to the Free Software 025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 026 */ 027 028package org.opencms.workplace.commons; 029 030import org.opencms.file.CmsGroup; 031import org.opencms.file.CmsUser; 032import org.opencms.file.CmsUserSearchParameters; 033import org.opencms.file.CmsUserSearchParameters.SortKey; 034import org.opencms.jsp.CmsJspActionElement; 035import org.opencms.main.CmsException; 036import org.opencms.main.CmsRuntimeException; 037import org.opencms.main.OpenCms; 038import org.opencms.security.CmsOrganizationalUnit; 039import org.opencms.security.CmsPrincipal; 040import org.opencms.util.CmsStringUtil; 041import org.opencms.workplace.list.A_CmsListDefaultJsAction; 042import org.opencms.workplace.list.A_CmsListDialog; 043import org.opencms.workplace.list.CmsListColumnAlignEnum; 044import org.opencms.workplace.list.CmsListColumnDefinition; 045import org.opencms.workplace.list.CmsListDefaultAction; 046import org.opencms.workplace.list.CmsListDirectAction; 047import org.opencms.workplace.list.CmsListItem; 048import org.opencms.workplace.list.CmsListMetadata; 049import org.opencms.workplace.list.CmsListOrderEnum; 050import org.opencms.workplace.list.CmsListSearchAction; 051import org.opencms.workplace.list.CmsListState; 052import org.opencms.workplace.tools.CmsToolMacroResolver; 053 054import java.util.ArrayList; 055import java.util.Iterator; 056import java.util.List; 057 058import javax.servlet.http.HttpServletRequest; 059import javax.servlet.http.HttpServletResponse; 060import javax.servlet.jsp.PageContext; 061 062import com.google.common.collect.Lists; 063 064/** 065 * User selection dialog.<p> 066 * 067 * @since 6.0.0 068 */ 069public class CmsUserSelectionList extends A_CmsListDialog { 070 071 /** list action id constant. */ 072 public static final String LIST_ACTION_ICON = "ai"; 073 074 /** list action id constant. */ 075 public static final String LIST_ACTION_SELECT = "js"; 076 077 /** list column id constant. */ 078 public static final String LIST_COLUMN_FULLNAME = "cf"; 079 080 /** list column id constant. */ 081 public static final String LIST_COLUMN_ICON = "ci"; 082 083 /** list column id constant. */ 084 public static final String LIST_COLUMN_LOGIN = "cn"; 085 086 /** list id constant. */ 087 public static final String LIST_ID = "lus"; 088 089 /** Stores the value of the request parameter for the flags. */ 090 private String m_paramFlags; 091 092 /** Stores the value of the request parameter for the group name. */ 093 private String m_paramGroup; 094 095 /** 096 * Public constructor.<p> 097 * 098 * @param jsp an initialized JSP action element 099 */ 100 public CmsUserSelectionList(CmsJspActionElement jsp) { 101 102 super( 103 jsp, 104 LIST_ID, 105 Messages.get().container(Messages.GUI_USERSELECTION_LIST_NAME_0), 106 LIST_COLUMN_LOGIN, 107 CmsListOrderEnum.ORDER_ASCENDING, 108 null); 109 } 110 111 /** 112 * Public constructor.<p> 113 * 114 * @param jsp an initialized JSP action element 115 * @param lazy signals whether lazy initialization should be used or not 116 */ 117 public CmsUserSelectionList(CmsJspActionElement jsp, boolean lazy) { 118 119 super( 120 jsp, 121 LIST_ID, 122 Messages.get().container(Messages.GUI_USERSELECTION_LIST_NAME_0), 123 LIST_COLUMN_LOGIN, 124 CmsListOrderEnum.ORDER_ASCENDING, 125 LIST_COLUMN_LOGIN, 126 lazy); 127 } 128 129 /** 130 * Public constructor with JSP variables.<p> 131 * 132 * @param context the JSP page context 133 * @param req the JSP request 134 * @param res the JSP response 135 */ 136 public CmsUserSelectionList(PageContext context, HttpServletRequest req, HttpServletResponse res) { 137 138 this(new CmsJspActionElement(context, req, res)); 139 } 140 141 /** 142 * Public constructor with JSP variables.<p> 143 * 144 * @param context the JSP page context 145 * @param req the JSP request 146 * @param res the JSP response 147 * @param lazy signals whether lazy initialization should be used or not 148 */ 149 public CmsUserSelectionList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) { 150 151 this(new CmsJspActionElement(context, req, res), lazy); 152 } 153 154 /** 155 * @see org.opencms.workplace.tools.CmsToolDialog#dialogTitle() 156 */ 157 @Override 158 public String dialogTitle() { 159 160 // build title 161 StringBuffer html = new StringBuffer(512); 162 html.append("<div class='screenTitle'>\n"); 163 html.append("\t<table width='100%' cellspacing='0'>\n"); 164 html.append("\t\t<tr>\n"); 165 html.append("\t\t\t<td>\n"); 166 String param = ""; 167 if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(getParamGroup())) { 168 param = Messages.get().getBundle(getLocale()).key( 169 Messages.GUI_USERSELECTION_GROUP_BLOCK_1, 170 getParamGroup()); 171 } 172 html.append(key(Messages.GUI_USERSELECTION_INTRO_TITLE_1, new Object[] {param})); 173 html.append("\n\t\t\t</td>"); 174 html.append("\t\t</tr>\n"); 175 html.append("\t</table>\n"); 176 html.append("</div>\n"); 177 return CmsToolMacroResolver.resolveMacros(html.toString(), this); 178 } 179 180 /** 181 * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions() 182 */ 183 @Override 184 public void executeListMultiActions() throws CmsRuntimeException { 185 186 throwListUnsupportedActionException(); 187 } 188 189 /** 190 * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions() 191 */ 192 @Override 193 public void executeListSingleActions() throws CmsRuntimeException { 194 195 throwListUnsupportedActionException(); 196 } 197 198 /** 199 * Returns the flags parameter value.<p> 200 * 201 * @return the flags parameter value 202 */ 203 public String getParamFlags() { 204 205 return m_paramFlags; 206 } 207 208 /** 209 * Returns the Group name parameter value.<p> 210 * 211 * @return the Group name parameter value 212 */ 213 public String getParamGroup() { 214 215 return m_paramGroup; 216 } 217 218 /** 219 * Sets the flags parameter value.<p> 220 * 221 * @param flags the flags parameter value to set 222 */ 223 public void setParamFlags(String flags) { 224 225 m_paramFlags = flags; 226 } 227 228 /** 229 * Sets the group name parameter value.<p> 230 * 231 * @param groupName the group name parameter value to set 232 */ 233 public void setParamGroup(String groupName) { 234 235 m_paramGroup = groupName; 236 } 237 238 /** 239 * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String) 240 */ 241 @Override 242 protected void fillDetails(String detailId) { 243 244 // noop 245 } 246 247 /** 248 * @see org.opencms.workplace.list.A_CmsListDialog#getListItems() 249 */ 250 @Override 251 protected List<CmsListItem> getListItems() throws CmsException { 252 253 if (!m_lazy) { 254 255 List<CmsListItem> ret = new ArrayList<CmsListItem>(); 256 257 // get content 258 List<CmsPrincipal> users = getUsers(); 259 Iterator<CmsPrincipal> itUsers = users.iterator(); 260 while (itUsers.hasNext()) { 261 CmsPrincipal prin = itUsers.next(); 262 if (prin instanceof CmsUser) { 263 CmsUser user = (CmsUser)prin; 264 CmsListItem item = makeListItem(user); 265 ret.add(item); 266 } 267 } 268 269 return ret; 270 } else { 271 CmsUserSearchParameters params = getSearchParams(); 272 List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params); 273 int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params); 274 getList().setSize(count); 275 List<CmsListItem> result = Lists.newArrayList(); 276 for (CmsUser user : users) { 277 CmsListItem item = makeListItem(user); 278 result.add(item); 279 } 280 return result; 281 } 282 } 283 284 /** 285 * Gets the user search parameters.<p> 286 * 287 * @return the user search parameters 288 * 289 * @throws CmsException if something goes wrong 290 */ 291 protected CmsUserSearchParameters getSearchParams() throws CmsException { 292 293 CmsListState state = getListState(); 294 CmsUserSearchParameters params = new CmsUserSearchParameters(); 295 String searchFilter = state.getFilter(); 296 params.setSearchFilter(searchFilter); 297 params.setPaging(getList().getMaxItemsPerPage(), state.getPage()); 298 params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING)); 299 String groupStr = getParamGroup(); 300 if (!CmsStringUtil.isEmpty(groupStr)) { 301 CmsGroup group = getCms().readGroup(getParamGroup()); 302 params.setGroup(group); 303 } else { 304 List<CmsOrganizationalUnit> ous = OpenCms.getRoleManager().getManageableOrgUnits(getCms(), "", true, false); 305 params.setAllowedOus(ous); 306 } 307 if (getParamFlags() != null) { 308 int flags = Integer.parseInt(getParamFlags()); 309 params.setFlags(flags); 310 } 311 params.setCaseSensitive(false); 312 return params; 313 } 314 315 /** 316 * Gets the sort key to use.<p> 317 * 318 * @param column the list column id 319 * @return the sort key 320 */ 321 protected SortKey getSortKey(String column) { 322 323 if (column == null) { 324 return null; 325 } 326 if (column.equals(LIST_COLUMN_FULLNAME)) { 327 return SortKey.fullName; 328 } else if (column.equals(LIST_COLUMN_LOGIN)) { 329 return SortKey.loginName; 330 } 331 return null; 332 } 333 334 /** 335 * Returns the list of users for selection.<p> 336 * 337 * @return a list of users 338 * 339 * @throws CmsException if womething goes wrong 340 */ 341 protected List<CmsPrincipal> getUsers() throws CmsException { 342 343 List<CmsPrincipal> ret = new ArrayList<CmsPrincipal>(); 344 if (getParamGroup() != null) { 345 ret.addAll(getCms().getUsersOfGroup(getParamGroup())); 346 } else { 347 ret.addAll(OpenCms.getRoleManager().getManageableUsers(getCms(), "", true)); 348 } 349 if (getParamFlags() != null) { 350 int flags = Integer.parseInt(getParamFlags()); 351 return new ArrayList<CmsPrincipal>(CmsPrincipal.filterFlag(ret, flags)); 352 } 353 return ret; 354 } 355 356 /** 357 * Makes a list item from a user.<p> 358 * 359 * @param user the user 360 * 361 * @return the list item 362 */ 363 protected CmsListItem makeListItem(CmsUser user) { 364 365 CmsListItem item = getList().newItem(user.getId().toString()); 366 item.set(LIST_COLUMN_LOGIN, user.getName()); 367 item.set(LIST_COLUMN_FULLNAME, user.getFullName()); 368 return item; 369 370 } 371 372 /** 373 * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata) 374 */ 375 @Override 376 protected void setColumns(CmsListMetadata metadata) { 377 378 // create column for icon display 379 CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON); 380 iconCol.setName(Messages.get().container(Messages.GUI_USERSELECTION_LIST_COLS_ICON_0)); 381 iconCol.setHelpText(Messages.get().container(Messages.GUI_USERSELECTION_LIST_COLS_ICON_HELP_0)); 382 iconCol.setWidth("20"); 383 iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 384 iconCol.setSorteable(false); 385 // set icon action 386 CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON); 387 iconAction.setName(Messages.get().container(Messages.GUI_USERSELECTION_LIST_ICON_NAME_0)); 388 iconAction.setHelpText(Messages.get().container(Messages.GUI_USERSELECTION_LIST_ICON_HELP_0)); 389 iconAction.setIconPath("buttons/user.png"); 390 iconAction.setEnabled(false); 391 iconCol.addDirectAction(iconAction); 392 // add it to the list definition 393 metadata.addColumn(iconCol); 394 395 // create column for login 396 CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN); 397 loginCol.setName(Messages.get().container(Messages.GUI_USERSELECTION_LIST_COLS_LOGIN_0)); 398 loginCol.setWidth("60%"); 399 CmsListDefaultAction selectAction = new A_CmsListDefaultJsAction(LIST_ACTION_SELECT) { 400 401 /** 402 * @see org.opencms.workplace.list.A_CmsListDirectJsAction#jsCode() 403 */ 404 @Override 405 public String jsCode() { 406 407 return "window.opener.setUserFormValue('" 408 + getItem().get(LIST_COLUMN_LOGIN) 409 + "'); window.opener.focus(); window.close();"; 410 } 411 }; 412 selectAction.setName(Messages.get().container(Messages.GUI_USERSELECTION_LIST_ACTION_SELECT_NAME_0)); 413 selectAction.setHelpText(Messages.get().container(Messages.GUI_USERSELECTION_LIST_ACTION_SELECT_HELP_0)); 414 loginCol.addDefaultAction(selectAction); 415 // add it to the list definition 416 metadata.addColumn(loginCol); 417 418 // create column for fullname 419 CmsListColumnDefinition fullnameCol = new CmsListColumnDefinition(LIST_COLUMN_FULLNAME); 420 fullnameCol.setName(Messages.get().container(Messages.GUI_USERSELECTION_LIST_COLS_FULLNAME_0)); 421 fullnameCol.setWidth("40%"); 422 fullnameCol.setTextWrapping(true); 423 // add it to the list definition 424 metadata.addColumn(fullnameCol); 425 } 426 427 /** 428 * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata) 429 */ 430 @Override 431 protected void setIndependentActions(CmsListMetadata metadata) { 432 433 CmsListSearchAction searchAction = new CmsListSearchAction(metadata.getColumnDefinition(LIST_COLUMN_LOGIN)); 434 searchAction.addColumn(metadata.getColumnDefinition(LIST_COLUMN_FULLNAME)); 435 searchAction.setCaseInSensitive(true); 436 metadata.setSearchAction(searchAction); 437 } 438 439 /** 440 * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata) 441 */ 442 @Override 443 protected void setMultiActions(CmsListMetadata metadata) { 444 445 // no-op 446 } 447 448 /** 449 * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters() 450 */ 451 @Override 452 protected void validateParamaters() throws Exception { 453 454 try { 455 getCms().readGroup(getParamGroup()).getName(); 456 } catch (Exception e) { 457 setParamGroup(null); 458 } 459 try { 460 Integer.valueOf(getParamFlags()); 461 } catch (Throwable e) { 462 setParamFlags(null); 463 } 464 } 465 466}