001/* 002 * This library is part of OpenCms - 003 * the Open Source Content Management System 004 * 005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com 019 * 020 * For further information about OpenCms, please see the 021 * project website: https://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.tools.accounts; 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.CmsRole; 040import org.opencms.workplace.list.CmsListColumnAlignEnum; 041import org.opencms.workplace.list.CmsListColumnDefinition; 042import org.opencms.workplace.list.CmsListDefaultAction; 043import org.opencms.workplace.list.CmsListDirectAction; 044import org.opencms.workplace.list.CmsListItem; 045import org.opencms.workplace.list.CmsListMetadata; 046import org.opencms.workplace.list.CmsListMultiAction; 047import org.opencms.workplace.list.CmsListOrderEnum; 048import org.opencms.workplace.list.CmsListState; 049 050import java.util.HashSet; 051import java.util.Iterator; 052import java.util.List; 053import java.util.Set; 054 055import com.google.common.collect.Lists; 056 057import jakarta.servlet.http.HttpServletRequest; 058import jakarta.servlet.http.HttpServletResponse; 059import jakarta.servlet.jsp.PageContext; 060 061/** 062 * Role users view.<p> 063 * 064 * @since 6.5.6 065 */ 066public class CmsRoleUsersList extends A_CmsRoleUsersList { 067 068 /** list action id constant. */ 069 public static final String LIST_ACTION_REMOVE = "ar"; 070 071 /** list action id constant. */ 072 public static final String LIST_DEFACTION_REMOVE = "dr"; 073 074 /** list id constant. */ 075 public static final String LIST_ID = "lru"; 076 077 /** list action id constant. */ 078 public static final String LIST_MACTION_REMOVE = "mr"; 079 080 /** a set of action id's to use for removing. */ 081 protected static Set<String> m_removeActionIds = new HashSet<String>(); 082 083 /** 084 * Public constructor.<p> 085 * 086 * @param jsp an initialized JSP action element 087 */ 088 public CmsRoleUsersList(CmsJspActionElement jsp) { 089 090 this(jsp, LIST_ID); 091 } 092 093 /** 094 * Public constructor.<p> 095 * 096 * @param jsp an initialized JSP action element 097 * @param lazy the lazy flag 098 */ 099 public CmsRoleUsersList(CmsJspActionElement jsp, boolean lazy) { 100 101 this(jsp, LIST_ID, lazy); 102 } 103 104 /** 105 * Public constructor with JSP variables.<p> 106 * 107 * @param context the JSP page context 108 * @param req the JSP request 109 * @param res the JSP response 110 */ 111 public CmsRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) { 112 113 this(new CmsJspActionElement(context, req, res)); 114 } 115 116 /** 117 * Public constructor with JSP variables.<p> 118 * 119 * @param context the JSP page context 120 * @param req the JSP request 121 * @param res the JSP response 122 * @param lazy the lazy flag 123 */ 124 public CmsRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) { 125 126 this(new CmsJspActionElement(context, req, res), lazy); 127 } 128 129 /** 130 * Protected constructor.<p> 131 * @param jsp an initialized JSP action element 132 * @param listId the id of the specialized list 133 */ 134 protected CmsRoleUsersList(CmsJspActionElement jsp, String listId) { 135 136 super(jsp, listId, Messages.get().container(Messages.GUI_ROLEUSERS_LIST_NAME_0), true); 137 } 138 139 /** 140 * Protected constructor.<p> 141 * @param jsp an initialized JSP action element 142 * @param listId the id of the specialized list 143 * @param lazy the lazy flag 144 */ 145 protected CmsRoleUsersList(CmsJspActionElement jsp, String listId, boolean lazy) { 146 147 super(jsp, listId, Messages.get().container(Messages.GUI_ROLEUSERS_LIST_NAME_0), true, lazy); 148 } 149 150 /** 151 * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions() 152 */ 153 @Override 154 public void executeListMultiActions() throws CmsRuntimeException { 155 156 if (getParamListAction().equals(LIST_MACTION_REMOVE)) { 157 // execute the remove multiaction 158 Iterator<CmsListItem> itItems = getSelectedItems().iterator(); 159 while (itItems.hasNext()) { 160 CmsListItem listItem = itItems.next(); 161 String userName = (String)listItem.get(LIST_COLUMN_LOGIN); 162 try { 163 if (getCms().readUser(userName).getOuFqn().equals(getParamOufqn())) { 164 OpenCms.getRoleManager().removeUserFromRole( 165 getCms(), 166 CmsRole.valueOf(getCms().readGroup(getParamRole())), 167 userName); 168 } 169 } catch (CmsException e) { 170 // noop 171 } 172 } 173 } else { 174 throwListUnsupportedActionException(); 175 } 176 listSave(); 177 } 178 179 /** 180 * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions() 181 */ 182 @Override 183 public void executeListSingleActions() throws CmsRuntimeException { 184 185 if (m_removeActionIds.contains(getParamListAction())) { 186 CmsListItem listItem = getSelectedItem(); 187 try { 188 OpenCms.getRoleManager().removeUserFromRole( 189 getCms(), 190 CmsRole.valueOf(getCms().readGroup(getParamRole())), 191 (String)listItem.get(LIST_COLUMN_LOGIN)); 192 } catch (CmsException e) { 193 // should never happen 194 throw new CmsRuntimeException(Messages.get().container(Messages.ERR_REMOVE_SELECTED_GROUP_0), e); 195 } 196 } else { 197 throwListUnsupportedActionException(); 198 } 199 listSave(); 200 } 201 202 /** 203 * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getListItems() 204 */ 205 @Override 206 protected List<CmsListItem> getListItems() throws CmsException { 207 208 if (!m_lazy) { 209 return super.getListItems(); 210 } else { 211 CmsUserSearchParameters params = getSearchParams(); 212 List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params); 213 int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params); 214 getList().setSize(count); 215 List<CmsListItem> result = Lists.newArrayList(); 216 for (CmsUser user : users) { 217 CmsListItem item = makeUserItem(user); 218 result.add(item); 219 } 220 return result; 221 } 222 } 223 224 /** 225 * Gets the search parameters.<p> 226 * 227 * @return the search parameters 228 * 229 * @throws CmsException if something goes wrong 230 */ 231 protected CmsUserSearchParameters getSearchParams() throws CmsException { 232 233 CmsListState state = getListState(); 234 CmsUserSearchParameters params = new CmsUserSearchParameters(); 235 String searchFilter = state.getFilter(); 236 params.setSearchFilter(searchFilter); 237 if (!otherOrgUnitsVisible()) { 238 CmsOrganizationalUnit ou = OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn()); 239 params.setOrganizationalUnit(ou); 240 } 241 getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_ORGUNIT).isVisible(); 242 params.setPaging(getList().getMaxItemsPerPage(), state.getPage()); 243 CmsRole role = CmsRole.valueOf(getCms().readGroup(getParamRole())); 244 Set<CmsGroup> roleGroups = OpenCms.getRoleManager().getRoleGroups(getCms(), role, false); 245 params.setAnyGroups(roleGroups); 246 params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING)); 247 CmsGroup group = getCms().readGroup(getParamRole()); 248 params.setGroup(group); 249 params.setFilterByGroupOu(false); 250 return params; 251 } 252 253 /** 254 * Gets the sort key for a column.<p> 255 * 256 * @param column a column 257 * @return the sort key 258 */ 259 protected SortKey getSortKey(String column) { 260 261 if (column == null) { 262 return null; 263 } 264 if (column.equals(LIST_COLUMN_FULLNAME)) { 265 return SortKey.fullName; 266 } else if (column.equals(LIST_COLUMN_NAME)) { 267 return SortKey.loginName; 268 } else if (column.equals(LIST_COLUMN_ORGUNIT)) { 269 return SortKey.orgUnit; 270 } 271 return null; 272 } 273 274 /** 275 * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getUsers(boolean) 276 */ 277 @Override 278 protected List<CmsUser> getUsers(boolean withOtherOus) throws CmsException { 279 280 return OpenCms.getRoleManager().getUsersOfRole( 281 getCms(), 282 CmsRole.valueOf(getCms().readGroup(getParamRole())), 283 withOtherOus, 284 true); 285 } 286 287 /** 288 * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata) 289 */ 290 @Override 291 protected void setColumns(CmsListMetadata metadata) { 292 293 if (m_lazy) { 294 metadata.setSelfManaged(true); 295 } 296 super.setColumns(metadata); 297 // create column for state change 298 CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE); 299 stateCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_0)); 300 stateCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_HELP_0)); 301 stateCol.setWidth("20"); 302 stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER); 303 stateCol.setSorteable(false); 304 // add remove action 305 CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_REMOVE); 306 stateAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_NAME_0)); 307 stateAction.setHelpText(Messages.get().container(Messages.GUI_ROLEUSERS_LIST_DEFACTION_REMOVE_HELP_0)); 308 stateAction.setIconPath(ICON_MINUS); 309 stateCol.addDirectAction(stateAction); 310 // add it to the list definition 311 metadata.addColumn(stateCol, 1); 312 // keep the id 313 m_removeActionIds.add(stateAction.getId()); 314 } 315 316 /** 317 * Sets the optional login default action.<p> 318 * 319 * @param loginCol the user login column 320 */ 321 protected void setDefaultAction(CmsListColumnDefinition loginCol) { 322 323 // add default remove action 324 CmsListDefaultAction removeAction = new CmsListDefaultAction(LIST_DEFACTION_REMOVE); 325 removeAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_NAME_0)); 326 removeAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_REMOVE_HELP_0)); 327 loginCol.addDefaultAction(removeAction); 328 // keep the id 329 m_removeActionIds.add(removeAction.getId()); 330 } 331 332 /** 333 * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata) 334 */ 335 @Override 336 protected void setMultiActions(CmsListMetadata metadata) { 337 338 // add remove multi action 339 CmsListMultiAction removeMultiAction = new CmsListMultiAction(LIST_MACTION_REMOVE); 340 removeMultiAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_NAME_0)); 341 removeMultiAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_HELP_0)); 342 removeMultiAction.setConfirmationMessage( 343 Messages.get().container(Messages.GUI_USERS_LIST_MACTION_REMOVE_CONF_0)); 344 removeMultiAction.setIconPath(ICON_MULTI_MINUS); 345 metadata.addMultiAction(removeMultiAction); 346 } 347 348 /** 349 * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#validateParamaters() 350 */ 351 @Override 352 protected void validateParamaters() throws Exception { 353 354 super.validateParamaters(); 355 OpenCms.getRoleManager().checkRole(getCms(), CmsRole.valueOf(getCms().readGroup(getParamRole()))); 356 } 357}