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