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.CmsUser;
031import org.opencms.i18n.CmsMessageContainer;
032import org.opencms.jsp.CmsJspActionElement;
033import org.opencms.main.CmsException;
034import org.opencms.security.CmsOrganizationalUnit;
035import org.opencms.util.CmsUUID;
036import org.opencms.workplace.list.A_CmsListDialog;
037import org.opencms.workplace.list.CmsListColumnAlignEnum;
038import org.opencms.workplace.list.CmsListColumnDefinition;
039import org.opencms.workplace.list.CmsListIndependentAction;
040import org.opencms.workplace.list.CmsListItem;
041import org.opencms.workplace.list.CmsListItemDetails;
042import org.opencms.workplace.list.CmsListItemDetailsFormatter;
043import org.opencms.workplace.list.CmsListMetadata;
044import org.opencms.workplace.list.CmsListOrderEnum;
045
046import java.io.IOException;
047import java.util.ArrayList;
048import java.util.Iterator;
049import java.util.List;
050import java.util.Map;
051
052import jakarta.servlet.ServletException;
053import jakarta.servlet.jsp.JspException;
054
055/**
056 * Generalized user groups view.<p>
057 *
058 * @since 6.0.0
059 */
060public abstract class A_CmsGroupUsersList extends A_CmsListDialog {
061
062    /** list action id constant. */
063    public static final String LIST_ACTION_ICON = "ai";
064
065    /** list action id constant. */
066    public static final String LIST_ACTION_STATE = "as";
067
068    /** list column id constant. */
069    public static final String LIST_COLUMN_FULLNAME = "cf";
070
071    /** list column id constant. */
072    public static final String LIST_COLUMN_ICON = "ci";
073
074    /** list column id constant. */
075    public static final String LIST_COLUMN_LOGIN = "cl";
076
077    /** list column id constant. */
078    public static final String LIST_COLUMN_NAME = "cn";
079
080    /** list column id constant. */
081    public static final String LIST_COLUMN_ORGUNIT = "co";
082
083    /** list column id constant. */
084    public static final String LIST_COLUMN_STATE = "cs";
085
086    /** list item detail id constant. */
087    public static final String LIST_DETAIL_OTHEROU = "doo";
088
089    /** Cached value. */
090    private Boolean m_hasUsersInOtherOus;
091
092    /** Stores the value of the request parameter for the user id. */
093    private String m_paramGroupid;
094
095    /** Stores the value of the request parameter for the user name. */
096    private String m_paramGroupname;
097
098    /** Stores the value of the request parameter for the organizational unit fqn. */
099    private String m_paramOufqn;
100
101    /**
102     * Public constructor.<p>
103     *
104     * @param jsp an initialized JSP action element
105     * @param listId the id of the list
106     * @param listName the name of the list
107     * @param searchable searchable flag
108     */
109    protected A_CmsGroupUsersList(
110        CmsJspActionElement jsp,
111        String listId,
112        CmsMessageContainer listName,
113        boolean searchable) {
114
115        this(jsp, listId, listName, searchable, false);
116    }
117
118    /**
119     * Public constructor.<p>
120     *
121     * @param jsp an initialized JSP action element
122     * @param listId the id of the list
123     * @param listName the name of the list
124     * @param searchable searchable flag
125     * @param lazy the lazy flag
126     */
127    protected A_CmsGroupUsersList(
128        CmsJspActionElement jsp,
129        String listId,
130        CmsMessageContainer listName,
131        boolean searchable,
132        boolean lazy) {
133
134        super(
135            jsp,
136            listId,
137            listName,
138            LIST_COLUMN_LOGIN,
139            CmsListOrderEnum.ORDER_ASCENDING,
140            searchable ? LIST_COLUMN_NAME : null,
141            lazy);
142    }
143
144    /**
145     * @see org.opencms.workplace.list.A_CmsListDialog#actionDialog()
146     */
147    @Override
148    public void actionDialog() throws JspException, ServletException, IOException {
149
150        updateGroupList();
151        super.actionDialog();
152    }
153
154    /**
155     * Returns the right icon path for the given list item.<p>
156     *
157     * @param item the list item to get the icon path for
158     *
159     * @return the icon path for the given role
160     */
161    public String getIconPath(CmsListItem item) {
162
163        try {
164            CmsUser user = getCms().readUser((String)item.get(LIST_COLUMN_LOGIN));
165            if (user.getOuFqn().equals(getParamOufqn())) {
166                return A_CmsUsersList.PATH_BUTTONS + "user.png";
167            } else {
168                return A_CmsUsersList.PATH_BUTTONS + "user_other_ou.png";
169            }
170        } catch (CmsException e) {
171            return A_CmsUsersList.PATH_BUTTONS + "user.png";
172        }
173    }
174
175    /**
176     * Returns the user id parameter value.<p>
177     *
178     * @return the user id parameter value
179     */
180    public String getParamGroupid() {
181
182        return m_paramGroupid;
183    }
184
185    /**
186     * Returns the Group name parameter.<p>
187     *
188     * @return the Group name parameter
189     */
190    public String getParamGroupname() {
191
192        return m_paramGroupname;
193    }
194
195    /**
196     * Returns the organizational unit fqn parameter value.<p>
197     *
198     * @return the organizational unit fqn parameter value
199     */
200    public String getParamOufqn() {
201
202        return m_paramOufqn;
203    }
204
205    /**
206     * Returns true if the list of users has users of other organizational units.<p>
207     *
208     * @return <code>true</code> if the list of users has users of other organizational units
209     */
210    public boolean hasUsersInOtherOus() {
211
212        if (m_lazy) {
213            // if we use database-side paging, we have to assume that there may be users from other OUs
214            return true;
215        }
216        if (m_hasUsersInOtherOus == null) {
217            // lazy initialization
218            m_hasUsersInOtherOus = Boolean.FALSE;
219            try {
220                Iterator<CmsUser> itUsers = getUsers(true).iterator();
221                while (itUsers.hasNext()) {
222                    CmsUser user = itUsers.next();
223                    if (!user.getOuFqn().equals(getParamOufqn())) {
224                        m_hasUsersInOtherOus = Boolean.TRUE;
225                        break;
226                    }
227                }
228            } catch (Exception e) {
229                // ignore
230            }
231        }
232        return m_hasUsersInOtherOus.booleanValue();
233    }
234
235    /**
236     * Sets the user id parameter value.<p>
237     *
238     * @param userId the user id parameter value
239     */
240    public void setParamGroupid(String userId) {
241
242        m_paramGroupid = userId;
243    }
244
245    /**
246     * Sets the organizational unit fqn parameter value.<p>
247     *
248     * @param ouFqn the organizational unit fqn parameter value
249     */
250    public void setParamOufqn(String ouFqn) {
251
252        if (ouFqn == null) {
253            ouFqn = "";
254        }
255        m_paramOufqn = ouFqn;
256    }
257
258    /**
259     * Updates the main user list.<p>
260     */
261    public void updateGroupList() {
262
263        Map<?, ?> objects = (Map<?, ?>)getSettings().getListObject();
264        if (objects != null) {
265            objects.remove(CmsGroupsList.class.getName());
266            objects.remove(A_CmsUsersList.class.getName());
267        }
268    }
269
270    /**
271     * @see org.opencms.workplace.list.A_CmsListDialog#fillDetails(java.lang.String)
272     */
273    @Override
274    protected void fillDetails(String detailId) {
275
276        // noop
277    }
278
279    /**
280     * @see org.opencms.workplace.list.A_CmsListDialog#getListItems()
281     */
282    @Override
283    protected List<CmsListItem> getListItems() throws CmsException {
284
285        List<CmsListItem> ret = new ArrayList<CmsListItem>();
286
287        boolean withOtherOus = hasOuDetail() && hasUsersInOtherOus();
288        // get content
289        Iterator<CmsUser> itUsers = getUsers(withOtherOus).iterator();
290        while (itUsers.hasNext()) {
291            CmsUser user = itUsers.next();
292            CmsListItem item = makeListItem(user);
293            ret.add(item);
294        }
295        return ret;
296    }
297
298    /**
299     * Returns a list of users to display.<p>
300     *
301     * @param withOtherOus if not set only users of the current ou should be returned
302     *
303     * @return a list of {@link CmsUser}s
304     *
305     * @throws CmsException if something goes wrong
306     */
307    protected abstract List<CmsUser> getUsers(boolean withOtherOus) throws CmsException;
308
309    /**
310     * Checks whether users of other OUs should be shown.<p>
311     *
312     * @return true if users of other OUs should be shown
313     */
314    protected boolean hasOuDetail() {
315
316        CmsListMetadata meta = getList().getMetadata();
317        CmsListItemDetails detail = meta.getItemDetailDefinition(LIST_DETAIL_OTHEROU);
318        return (detail != null) && detail.isVisible();
319    }
320
321    /**
322     * @see org.opencms.workplace.list.A_CmsListDialog#initializeDetail(java.lang.String)
323     */
324    @Override
325    protected void initializeDetail(String detailId) {
326
327        super.initializeDetail(detailId);
328        if (detailId.equals(LIST_DETAIL_OTHEROU)) {
329            boolean visible = hasUsersInOtherOus()
330                && getList().getMetadata().getItemDetailDefinition(LIST_DETAIL_OTHEROU).isVisible();
331            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setVisible(visible);
332            getList().getMetadata().getColumnDefinition(LIST_COLUMN_ORGUNIT).setPrintable(visible);
333        }
334    }
335
336    /**
337     * @see org.opencms.workplace.CmsWorkplace#initMessages()
338     */
339    @Override
340    protected void initMessages() {
341
342        // add specific dialog resource bundle
343        addMessages(Messages.get().getBundleName());
344        // add default resource bundles
345        super.initMessages();
346    }
347
348    /**
349     * Makes a list item from a user.<p>
350     *
351     * @param user a user
352     *
353     * @return a list item
354     */
355    protected CmsListItem makeListItem(CmsUser user) {
356
357        CmsListItem item = getList().newItem(user.getId().toString());
358        setUserData(user, item);
359        return item;
360    }
361
362    /**
363     * Makes a list item for a given user.<p>
364     *
365     * @param user the user
366     *
367     * @return the list item
368     */
369    protected CmsListItem makeListItemForUser(CmsUser user) {
370
371        CmsListItem item = getList().newItem(user.getId().toString());
372        setUserData(user, item);
373        return item;
374    }
375
376    /**
377     * @see org.opencms.workplace.list.A_CmsListDialog#setColumns(org.opencms.workplace.list.CmsListMetadata)
378     */
379    @Override
380    protected void setColumns(CmsListMetadata metadata) {
381
382        // create column for icon display
383        CmsListColumnDefinition iconCol = new CmsListColumnDefinition(LIST_COLUMN_ICON);
384        iconCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ICON_0));
385        iconCol.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ICON_HELP_0));
386        iconCol.setWidth("20");
387        iconCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
388        iconCol.setSorteable(false);
389        // set icon action
390        setIconAction(iconCol);
391        // add it to the list definition
392        metadata.addColumn(iconCol);
393
394        setStateActionCol(metadata);
395
396        // create column for login
397        CmsListColumnDefinition loginCol = new CmsListColumnDefinition(LIST_COLUMN_LOGIN);
398        loginCol.setVisible(false);
399        // add it to the list definition
400        metadata.addColumn(loginCol);
401
402        // create column for name
403        CmsListColumnDefinition nameCol = new CmsListColumnDefinition(LIST_COLUMN_NAME);
404        nameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_LOGIN_0));
405        nameCol.setWidth("35%");
406        setDefaultAction(nameCol);
407        // add it to the list definition
408        metadata.addColumn(nameCol);
409
410        // create column for orgunit
411        CmsListColumnDefinition orgunitCol = new CmsListColumnDefinition(LIST_COLUMN_ORGUNIT);
412        orgunitCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ORGUNIT_0));
413        orgunitCol.setVisible(false);
414        // add it to the list definition
415        metadata.addColumn(orgunitCol);
416
417        // create column for fullname
418        CmsListColumnDefinition fullnameCol = new CmsListColumnDefinition(LIST_COLUMN_FULLNAME);
419        fullnameCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_FULLNAME_0));
420        fullnameCol.setWidth("65%");
421        fullnameCol.setTextWrapping(true);
422        // add it to the list definition
423        metadata.addColumn(fullnameCol);
424    }
425
426    /**
427     * Sets the optional login default action.<p>
428     *
429     * @param loginCol the login column
430     */
431    protected abstract void setDefaultAction(CmsListColumnDefinition loginCol);
432
433    /**
434     * Sets the needed icon action(s).<p>
435     *
436     * @param iconCol the list column for edition.
437     */
438    protected abstract void setIconAction(CmsListColumnDefinition iconCol);
439
440    /**
441     * @see org.opencms.workplace.list.A_CmsListDialog#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
442     */
443    @Override
444    protected void setIndependentActions(CmsListMetadata metadata) {
445
446        // add other ou button
447        CmsListItemDetails otherOuDetails = new CmsListItemDetails(LIST_DETAIL_OTHEROU);
448        otherOuDetails.setVisible(false);
449        otherOuDetails.setHideAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
450
451            /**
452             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
453             */
454            @Override
455            public String getIconPath() {
456
457                return A_CmsListDialog.ICON_DETAILS_HIDE;
458            }
459
460            /**
461             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
462             */
463            @Override
464            public boolean isVisible() {
465
466                return ((A_CmsGroupUsersList)getWp()).hasUsersInOtherOus();
467            }
468        });
469        otherOuDetails.setShowAction(new CmsListIndependentAction(LIST_DETAIL_OTHEROU) {
470
471            /**
472             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
473             */
474            @Override
475            public String getIconPath() {
476
477                return A_CmsListDialog.ICON_DETAILS_SHOW;
478            }
479
480            /**
481             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
482             */
483            @Override
484            public boolean isVisible() {
485
486                return ((A_CmsGroupUsersList)getWp()).hasUsersInOtherOus();
487            }
488        });
489        otherOuDetails.setShowActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_OTHEROU_NAME_0));
490        otherOuDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_OTHEROU_HELP_0));
491        otherOuDetails.setHideActionName(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_OTHEROU_NAME_0));
492        otherOuDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_OTHEROU_HELP_0));
493        otherOuDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_OTHEROU_NAME_0));
494        otherOuDetails.setFormatter(
495            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_USERS_DETAIL_OTHEROU_NAME_0)));
496        metadata.addItemDetails(otherOuDetails);
497    }
498
499    /**
500     * Sets the optional state change action column.<p>
501     *
502     * @param metadata the list metadata object
503     */
504    protected abstract void setStateActionCol(CmsListMetadata metadata);
505
506    /**
507     * Sets all needed data of the user into the list item object.<p>
508     *
509     * @param user the user to set the data for
510     * @param item the list item object to set the data into
511     */
512    protected void setUserData(CmsUser user, CmsListItem item) {
513
514        item.set(LIST_COLUMN_LOGIN, user.getName());
515        item.set(LIST_COLUMN_NAME, user.getSimpleName());
516        item.set(LIST_COLUMN_ORGUNIT, CmsOrganizationalUnit.SEPARATOR + user.getOuFqn());
517        item.set(LIST_COLUMN_FULLNAME, user.getFullName());
518    }
519
520    /**
521     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
522     */
523    @Override
524    protected void validateParamaters() throws Exception {
525
526        // test the needed parameters
527        m_paramGroupname = getCms().readGroup(new CmsUUID(getParamGroupid())).getName();
528    }
529}