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.CmsListItem;
041import org.opencms.workplace.list.CmsListMetadata;
042import org.opencms.workplace.list.CmsListOrderEnum;
043import org.opencms.workplace.list.CmsListState;
044
045import java.util.List;
046import java.util.Set;
047
048import com.google.common.collect.Lists;
049
050import jakarta.servlet.http.HttpServletRequest;
051import jakarta.servlet.http.HttpServletResponse;
052import jakarta.servlet.jsp.PageContext;
053
054/**
055 * Role users list view.<p>
056 *
057 * @since 6.5.6
058 */
059public class CmsShowRoleUsersList extends A_CmsRoleUsersList {
060
061    /** list id constant. */
062    public static final String LIST_ID = "lsru";
063
064    /**
065     * Public constructor.<p>
066     *
067     * @param jsp an initialized JSP action element
068     */
069    public CmsShowRoleUsersList(CmsJspActionElement jsp) {
070
071        this(jsp, LIST_ID);
072    }
073
074    /**
075     * Public constructor.<p>
076     *
077     * @param jsp an initialized JSP action element
078     * @param lazy the lazy flag
079     */
080    public CmsShowRoleUsersList(CmsJspActionElement jsp, boolean lazy) {
081
082        this(jsp, LIST_ID, lazy);
083    }
084
085    /**
086     * Public constructor with JSP variables.<p>
087     *
088     * @param context the JSP page context
089     * @param req the JSP request
090     * @param res the JSP response
091     */
092    public CmsShowRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
093
094        this(new CmsJspActionElement(context, req, res));
095    }
096
097    /**
098     * Public constructor with JSP variables.<p>
099     *
100     * @param context the JSP page context
101     * @param req the JSP request
102     * @param res the JSP response
103     * @param lazy the lazy flag
104     */
105    public CmsShowRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
106
107        this(new CmsJspActionElement(context, req, res), lazy);
108    }
109
110    /**
111     * Protected constructor.<p>
112     * @param jsp an initialized JSP action element
113     * @param listId the id of the specialized list
114     */
115    protected CmsShowRoleUsersList(CmsJspActionElement jsp, String listId) {
116
117        super(jsp, listId, Messages.get().container(Messages.GUI_ROLEUSERS_LIST_NAME_0), true);
118    }
119
120    /**
121     * Protected constructor.<p>
122     * @param jsp an initialized JSP action element
123     * @param listId the id of the specialized list
124     * @param lazy the lazy flag
125     */
126    protected CmsShowRoleUsersList(CmsJspActionElement jsp, String listId, boolean lazy) {
127
128        super(jsp, listId, Messages.get().container(Messages.GUI_ROLEUSERS_LIST_NAME_0), true, lazy);
129    }
130
131    /**
132     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
133     */
134    @Override
135    public String defaultActionHtmlStart() {
136
137        return getList().listJs() + dialogContentStart(getParamTitle());
138    }
139
140    /**
141     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
142     */
143    @Override
144    public void executeListMultiActions() throws CmsRuntimeException {
145
146        throwListUnsupportedActionException();
147    }
148
149    /**
150     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
151     */
152    @Override
153    public void executeListSingleActions() {
154
155        throwListUnsupportedActionException();
156    }
157
158    /**
159     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata)
160     */
161    @Override
162    public void setColumns(CmsListMetadata meta) {
163
164        if (m_lazy) {
165            meta.setSelfManaged(true);
166        }
167        super.setColumns(meta);
168    }
169
170    /**
171     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getListItems()
172     */
173    @Override
174    protected List<CmsListItem> getListItems() throws CmsException {
175
176        if (!m_lazy) {
177            return super.getListItems();
178        } else {
179            CmsUserSearchParameters params = getSearchParams();
180            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
181            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
182            getList().setSize(count);
183            List<CmsListItem> result = Lists.newArrayList();
184            for (CmsUser user : users) {
185                CmsListItem item = makeUserItem(user);
186                result.add(item);
187            }
188            return result;
189        }
190    }
191
192    /**
193     * Gets the search parameters.<p>
194     *
195     * @return the search parameters
196     *
197     * @throws CmsException if something goes wrong
198     */
199    protected CmsUserSearchParameters getSearchParams() throws CmsException {
200
201        CmsListState state = getListState();
202        CmsUserSearchParameters params = new CmsUserSearchParameters();
203        String searchFilter = state.getFilter();
204        params.setSearchFilter(searchFilter);
205        if (!otherOrgUnitsVisible()) {
206            CmsOrganizationalUnit ou = OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn());
207            params.setOrganizationalUnit(ou);
208        }
209        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
210        CmsRole role = CmsRole.valueOf(getCms().readGroup(getParamRole()));
211        Set<CmsGroup> roleGroups = OpenCms.getRoleManager().getRoleGroups(getCms(), role, false);
212        params.setAnyGroups(roleGroups);
213        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
214        params.setFilterByGroupOu(false);
215        return params;
216    }
217
218    /**
219     * Gets the sort key for a column.<p>
220     *
221     * @param column a column
222     * @return the sort key
223     */
224    protected SortKey getSortKey(String column) {
225
226        if (column == null) {
227            return null;
228        }
229        if (column.equals(LIST_COLUMN_FULLNAME)) {
230            return SortKey.fullName;
231        } else if (column.equals(LIST_COLUMN_NAME)) {
232            return SortKey.loginName;
233        } else if (column.equals(LIST_COLUMN_ORGUNIT)) {
234            return SortKey.orgUnit;
235        }
236        return null;
237    }
238
239    /**
240     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getUsers(boolean)
241     */
242    @Override
243    protected List<CmsUser> getUsers(boolean withOtherOus) throws CmsException {
244
245        return OpenCms.getRoleManager().getUsersOfRole(
246            getCms(),
247            CmsRole.valueOf(getCms().readGroup(getParamRole())),
248            withOtherOus,
249            false);
250    }
251
252    /**
253     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
254     */
255    @Override
256    protected void setMultiActions(CmsListMetadata metadata) {
257
258        // noop
259    }
260
261}