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.CmsListDirectAction;
043import org.opencms.workplace.list.CmsListItem;
044import org.opencms.workplace.list.CmsListMetadata;
045import org.opencms.workplace.list.CmsListMultiAction;
046import org.opencms.workplace.list.CmsListOrderEnum;
047import org.opencms.workplace.list.CmsListState;
048
049import java.util.HashSet;
050import java.util.Iterator;
051import java.util.List;
052import java.util.Set;
053
054import com.google.common.collect.Lists;
055
056import jakarta.servlet.http.HttpServletRequest;
057import jakarta.servlet.http.HttpServletResponse;
058import jakarta.servlet.jsp.PageContext;
059
060/**
061 * Not role users view.<p>
062 *
063 * @since 6.5.6
064 */
065public class CmsNotRoleUsersList extends A_CmsRoleUsersList {
066
067    /** list action id constant. */
068    public static final String LIST_ACTION_ADD = "aa";
069
070    /** list action id constant. */
071    public static final String LIST_DEFACTION_ADD = "da";
072
073    /** list id constant. */
074    public static final String LIST_ID = "lnru";
075
076    /** list action id constant. */
077    public static final String LIST_MACTION_ADD = "ma";
078
079    /** a set of action id's to use for adding. */
080    protected static Set<String> m_addActionIds = new HashSet<String>();
081
082    /**
083     * Public constructor.<p>
084     *
085     * @param jsp an initialized JSP action element
086     */
087    public CmsNotRoleUsersList(CmsJspActionElement jsp) {
088
089        this(jsp, LIST_ID);
090    }
091
092    /**
093     * Public constructor.<p>
094     *
095     * @param jsp an initialized JSP action element
096     * @param lazy the lazy flag
097     */
098    public CmsNotRoleUsersList(CmsJspActionElement jsp, boolean lazy) {
099
100        this(jsp, LIST_ID, lazy);
101    }
102
103    /**
104     * Public constructor with JSP variables.<p>
105     *
106     * @param context the JSP page context
107     * @param req the JSP request
108     * @param res the JSP response
109     */
110    public CmsNotRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
111
112        this(new CmsJspActionElement(context, req, res));
113    }
114
115    /**
116     * Public constructor with JSP variables.<p>
117     *
118     * @param context the JSP page context
119     * @param req the JSP request
120     * @param res the JSP response
121     * @param lazy the lazy flag
122     */
123    public CmsNotRoleUsersList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
124
125        this(new CmsJspActionElement(context, req, res), lazy);
126    }
127
128    /**
129     * Protected constructor.<p>
130     * @param jsp an initialized JSP action element
131     * @param listId the id of the specialized list
132     */
133    protected CmsNotRoleUsersList(CmsJspActionElement jsp, String listId) {
134
135        super(jsp, listId, Messages.get().container(Messages.GUI_NOTROLEUSERS_LIST_NAME_0), true);
136    }
137
138    /**
139     * Protected constructor.<p>
140     * @param jsp an initialized JSP action element
141     * @param listId the id of the specialized list
142     * @param lazy the lazy flag
143     */
144    protected CmsNotRoleUsersList(CmsJspActionElement jsp, String listId, boolean lazy) {
145
146        super(jsp, listId, Messages.get().container(Messages.GUI_NOTROLEUSERS_LIST_NAME_0), true, lazy);
147    }
148
149    /**
150     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
151     */
152    @Override
153    public void executeListMultiActions() throws CmsRuntimeException {
154
155        if (getParamListAction().equals(LIST_MACTION_ADD)) {
156            // execute the remove multiaction
157            try {
158                Iterator<CmsListItem> itItems = getSelectedItems().iterator();
159                while (itItems.hasNext()) {
160                    CmsListItem listItem = itItems.next();
161                    OpenCms.getRoleManager().addUserToRole(
162                        getCms(),
163                        CmsRole.valueOf(getCms().readGroup(getParamRole())),
164                        (String)listItem.get(LIST_COLUMN_LOGIN));
165                }
166            } catch (CmsException e) {
167                // refresh the list
168                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ADD_SELECTED_USERS_0), e);
169            }
170        } else {
171            throwListUnsupportedActionException();
172        }
173        listSave();
174    }
175
176    /**
177     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
178     */
179    @Override
180    public void executeListSingleActions() throws CmsRuntimeException {
181
182        if (m_addActionIds.contains(getParamListAction())) {
183            CmsListItem listItem = getSelectedItem();
184            try {
185                OpenCms.getRoleManager().addUserToRole(
186                    getCms(),
187                    CmsRole.valueOf(getCms().readGroup(getParamRole())),
188                    (String)listItem.get(LIST_COLUMN_LOGIN));
189            } catch (CmsException e) {
190                // should never happen
191                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_ADD_SELECTED_USER_0), e);
192            }
193        } else {
194            throwListUnsupportedActionException();
195        }
196        listSave();
197    }
198
199    /**
200     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getListItems()
201     */
202    @Override
203    protected List<CmsListItem> getListItems() throws CmsException {
204
205        if (!m_lazy) {
206            return super.getListItems();
207        } else {
208            CmsUserSearchParameters params = getSearchParams();
209            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
210            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
211            getList().setSize(count);
212            List<CmsListItem> result = Lists.newArrayList();
213            for (CmsUser user : users) {
214                CmsListItem item = makeUserItem(user);
215                result.add(item);
216            }
217            return result;
218        }
219    }
220
221    /**
222     * Gets the search parameters.<p>
223     *
224     * @return the search parameters
225     *
226     * @throws CmsException if something goes wrong
227     */
228    protected CmsUserSearchParameters getSearchParams() throws CmsException {
229
230        CmsListState state = getListState();
231        CmsUserSearchParameters params = new CmsUserSearchParameters();
232        String searchFilter = state.getFilter();
233        params.setSearchFilter(searchFilter);
234        if (!otherOrgUnitsVisible()) {
235            CmsOrganizationalUnit ou = OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), getParamOufqn());
236            params.setOrganizationalUnit(ou);
237        }
238        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
239        CmsRole role = CmsRole.valueOf(getCms().readGroup(getParamRole()));
240        Set<CmsGroup> roleGroups = OpenCms.getRoleManager().getRoleGroups(getCms(), role, false);
241        params.setNotAnyGroups(roleGroups);
242        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
243        params.setFilterByGroupOu(false);
244        return params;
245    }
246
247    /**
248     * Gets the sort key for a column.<p>
249     *
250     * @param column a column
251     * @return the sort key
252     */
253    protected SortKey getSortKey(String column) {
254
255        if (column == null) {
256            return null;
257        }
258        if (column.equals(LIST_COLUMN_FULLNAME)) {
259            return SortKey.fullName;
260        } else if (column.equals(LIST_COLUMN_NAME)) {
261            return SortKey.loginName;
262        } else if (column.equals(LIST_COLUMN_ORGUNIT)) {
263            return SortKey.orgUnit;
264        }
265        return null;
266    }
267
268    /**
269     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#getUsers(boolean)
270     */
271    @Override
272    protected List<CmsUser> getUsers(boolean withOtherOus) throws CmsException {
273
274        List<CmsUser> roleUsers = OpenCms.getRoleManager().getUsersOfRole(
275            getCms(),
276            CmsRole.valueOf(getCms().readGroup(getParamRole())),
277            withOtherOus,
278            false);
279        List<CmsUser> users;
280        if (withOtherOus) {
281            users = OpenCms.getRoleManager().getManageableUsers(getCms(), "", true);
282        } else {
283            users = OpenCms.getRoleManager().getManageableUsers(getCms(), getParamOufqn(), false);
284        }
285        users.removeAll(roleUsers);
286        return users;
287    }
288
289    /**
290     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
291     */
292    @Override
293    protected void setColumns(CmsListMetadata metadata) {
294
295        if (m_lazy) {
296            metadata.setSelfManaged(true);
297        }
298        super.setColumns(metadata);
299
300        // create column for state change
301        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
302        stateCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_0));
303        stateCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_STATE_HELP_0));
304        stateCol.setWidth("20");
305        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
306        stateCol.setSorteable(false);
307        // add add action
308        CmsListDirectAction stateAction = new CmsListDirectAction(LIST_ACTION_ADD);
309        stateAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_ADD_NAME_0));
310        stateAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_ADD_HELP_0));
311        stateAction.setIconPath(ICON_ADD);
312        stateCol.addDirectAction(stateAction);
313        // add it to the list definition
314        metadata.addColumn(stateCol, 1);
315        // keep the id
316        m_addActionIds.add(stateAction.getId());
317
318        CmsListDirectAction iconAction = new CmsListDirectAction(LIST_ACTION_ICON) {
319
320            /**
321             * @see org.opencms.workplace.tools.I_CmsHtmlIconButton#getIconPath()
322             */
323            @Override
324            public String getIconPath() {
325
326                try {
327                    CmsUser user = getCms().readUser((String)getItem().get(LIST_COLUMN_LOGIN));
328                    if (user.getOuFqn().equals(((A_CmsRoleUsersList)getWp()).getParamOufqn())) {
329                        return A_CmsUsersList.PATH_BUTTONS + "user.png";
330
331                    } else {
332                        return A_CmsUsersList.PATH_BUTTONS + "user_other_ou.png";
333                    }
334                } catch (CmsException e) {
335                    return A_CmsUsersList.PATH_BUTTONS + "user.png";
336                }
337            }
338        };
339        iconAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_INROLE_NAME_0));
340        iconAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_INROLE_HELP_0));
341        iconAction.setIconPath(PATH_BUTTONS + "user.png");
342        iconAction.setEnabled(false);
343        metadata.getColumnDefinition(LIST_COLUMN_ICON).removeDirectAction(LIST_ACTION_ICON);
344        metadata.getColumnDefinition(LIST_COLUMN_ICON).addDirectAction(iconAction);
345    }
346
347    /**
348     * @see org.opencms.workplace.tools.accounts.A_CmsRoleUsersList#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
349     */
350    @Override
351    protected void setIndependentActions(CmsListMetadata metadata) {
352
353        super.setIndependentActions(metadata);
354
355        metadata.getItemDetailDefinition(LIST_DETAIL_ORGUNIT).setVisible(false);
356    }
357
358    /**
359     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
360     */
361    @Override
362    protected void setMultiActions(CmsListMetadata metadata) {
363
364        // add add multi action
365        CmsListMultiAction addMultiAction = new CmsListMultiAction(LIST_MACTION_ADD);
366        addMultiAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ADD_NAME_0));
367        addMultiAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ADD_HELP_0));
368        addMultiAction.setConfirmationMessage(Messages.get().container(Messages.GUI_USERS_LIST_MACTION_ADD_CONF_0));
369        addMultiAction.setIconPath(ICON_MULTI_ADD);
370        metadata.addMultiAction(addMultiAction);
371    }
372
373}