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.i18n.CmsMessageContainer;
031import org.opencms.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsRole;
036import org.opencms.util.CmsUUID;
037import org.opencms.workplace.list.A_CmsListDialog;
038import org.opencms.workplace.list.CmsListIndependentAction;
039import org.opencms.workplace.list.CmsListItem;
040import org.opencms.workplace.list.CmsListItemDetails;
041import org.opencms.workplace.list.CmsListItemDetailsFormatter;
042import org.opencms.workplace.list.CmsListMetadata;
043
044import java.io.IOException;
045import java.util.ArrayList;
046import java.util.Iterator;
047import java.util.List;
048
049import javax.servlet.http.HttpServletRequest;
050import javax.servlet.http.HttpServletResponse;
051import javax.servlet.jsp.PageContext;
052
053/**
054 * User roles overview view.<p>
055 *
056 * @since 6.5.6
057 */
058public class CmsShowUserRolesList extends A_CmsRolesList {
059
060    /** list id constant. */
061    public static final String LIST_ID = "lsur";
062
063    /** Cached value. */
064    private Boolean m_hasRolesInOtherOus;
065
066    /** Stores the value of the request parameter for the user id. */
067    private String m_paramUserid;
068
069    /**
070     * Public constructor.<p>
071     *
072     * @param jsp an initialized JSP action element
073     */
074    public CmsShowUserRolesList(CmsJspActionElement jsp) {
075
076        this(jsp, LIST_ID);
077    }
078
079    /**
080     * Public constructor.<p>
081     *
082     * @param jsp an initialized JSP action element
083     * @param listId the id of the list
084     */
085    public CmsShowUserRolesList(CmsJspActionElement jsp, String listId) {
086
087        this(jsp, listId, Messages.get().container(Messages.GUI_USERROLES_LIST_NAME_0));
088    }
089
090    /**
091     * Public constructor with JSP variables.<p>
092     *
093     * @param context the JSP page context
094     * @param req the JSP request
095     * @param res the JSP response
096     */
097    public CmsShowUserRolesList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
098
099        this(new CmsJspActionElement(context, req, res));
100    }
101
102    /**
103     * Public constructor.<p>
104     *
105     * @param jsp an initialized JSP action element
106     * @param listId the id of the list
107     * @param listName the name of the list
108     */
109    protected CmsShowUserRolesList(CmsJspActionElement jsp, String listId, CmsMessageContainer listName) {
110
111        super(jsp, listId, listName);
112    }
113
114    /**
115     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
116     */
117    @Override
118    public void executeListMultiActions() throws CmsRuntimeException {
119
120        throwListUnsupportedActionException();
121    }
122
123    /**
124     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
125     */
126    @Override
127    public void executeListSingleActions() throws CmsRuntimeException {
128
129        throwListUnsupportedActionException();
130    }
131
132    /**
133     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getIconPath(CmsListItem)
134     */
135    @Override
136    public String getIconPath(CmsListItem item) {
137
138        List<CmsListItem> roles = getList().getAllContent();
139        Iterator<CmsListItem> itRoles = roles.iterator();
140        List<CmsRole> roleObjects = new ArrayList<CmsRole>();
141        try {
142            while (itRoles.hasNext()) {
143                CmsListItem listItem = itRoles.next();
144                roleObjects.add(CmsRole.valueOf(getCms().readGroup((String)listItem.get(LIST_COLUMN_GROUP_NAME))));
145            }
146
147            CmsRole role = CmsRole.valueOf(getCms().readGroup((String)item.get(LIST_COLUMN_GROUP_NAME)));
148            if ((role.getParentRole() != null) && roleObjects.contains(role.getParentRole())) {
149                if (role.getOuFqn().equals(getParamOufqn())) {
150                    return PATH_BUTTONS + "role_child.png";
151                } else {
152                    return PATH_BUTTONS + "role_other_ou_inherit.png";
153                }
154            }
155            if (role.getOuFqn().equals(getParamOufqn())) {
156                return PATH_BUTTONS + "role.png";
157            } else {
158                return PATH_BUTTONS + "role_other_ou.png";
159            }
160        } catch (CmsException e) {
161            return PATH_BUTTONS + "role.png";
162        }
163    }
164
165    /**
166     * Returns the User id parameter value.<p>
167     *
168     * @return the User id parameter value
169     */
170    public String getParamUserid() {
171
172        return m_paramUserid;
173    }
174
175    /**
176     * Sets the User id parameter value.<p>
177     *
178     * @param userid the userid to set
179     */
180    public void setParamUserid(String userid) {
181
182        m_paramUserid = userid;
183    }
184
185    /**
186     * @see org.opencms.workplace.list.A_CmsListDialog#writeDialog()
187     */
188    @Override
189    public void writeDialog() throws IOException {
190
191        try {
192            if (!getCms().readUser(new CmsUUID(getParamUserid())).isWebuser()) {
193                super.writeDialog();
194            }
195        } catch (CmsException e) {
196            // ignore
197        }
198    }
199
200    /**
201     * @see org.opencms.workplace.list.A_CmsListDialog#defaultActionHtmlStart()
202     */
203    @Override
204    protected String defaultActionHtmlStart() {
205
206        return dialogContentStart(getParamTitle());
207    }
208
209    /**
210     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#getRoles()
211     */
212    @Override
213    protected List<CmsRole> getRoles() throws CmsException {
214
215        List<CmsRole> allRoles = OpenCms.getRoleManager().getRolesOfUser(
216            getCms(),
217            getCms().readUser(new CmsUUID(getParamUserid())).getName(),
218            "",
219            true,
220            true,
221            false);
222
223        List<CmsRole> childRoles = OpenCms.getRoleManager().getRolesOfUser(
224            getCms(),
225            getCms().readUser(new CmsUUID(getParamUserid())).getName(),
226            getParamOufqn(),
227            false,
228            false,
229            false);
230
231        Iterator<CmsRole> itChildRoles = childRoles.iterator();
232        while (itChildRoles.hasNext()) {
233            CmsRole role = itChildRoles.next();
234            if (!allRoles.contains(role)) {
235                allRoles.add(role);
236            }
237        }
238        return allRoles;
239    }
240
241    /**
242     * Returns true if the list of users has users of other organizational units.<p>
243     *
244     * @return <code>true</code> if the list of users has users of other organizational units
245     */
246    protected boolean hasRolesInOtherOus() {
247
248        if (m_hasRolesInOtherOus == null) {
249            m_hasRolesInOtherOus = Boolean.FALSE;
250            try {
251                Iterator<CmsRole> itRoles = getRoles().iterator();
252                while (itRoles.hasNext()) {
253                    CmsRole role = itRoles.next();
254                    if (!role.getOuFqn().equals(getParamOufqn())) {
255                        m_hasRolesInOtherOus = Boolean.TRUE;
256                        break;
257                    }
258                }
259            } catch (Exception e) {
260                // ignore
261            }
262        }
263        return m_hasRolesInOtherOus.booleanValue();
264    }
265
266    /**
267     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#includeOuDetails()
268     */
269    @Override
270    protected boolean includeOuDetails() {
271
272        return false;
273    }
274
275    /**
276     * @see org.opencms.workplace.tools.accounts.A_CmsRolesList#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
277     */
278    @Override
279    protected void setIndependentActions(CmsListMetadata metadata) {
280
281        super.setIndependentActions(metadata);
282
283        // add other ou button
284        CmsListItemDetails pathDetails = new CmsListItemDetails(LIST_DETAIL_PATH);
285        pathDetails.setAtColumn(LIST_COLUMN_NAME);
286        pathDetails.setVisible(false);
287        pathDetails.setHideAction(new CmsListIndependentAction(LIST_DETAIL_PATH) {
288
289            /**
290             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
291             */
292            @Override
293            public String getIconPath() {
294
295                return A_CmsListDialog.ICON_DETAILS_HIDE;
296            }
297
298            /**
299             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
300             */
301            @Override
302            public boolean isVisible() {
303
304                return ((CmsShowUserRolesList)getWp()).hasRolesInOtherOus();
305            }
306        });
307        pathDetails.setShowAction(new CmsListIndependentAction(LIST_DETAIL_PATH) {
308
309            /**
310             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#getIconPath()
311             */
312            @Override
313            public String getIconPath() {
314
315                return A_CmsListDialog.ICON_DETAILS_SHOW;
316            }
317
318            /**
319             * @see org.opencms.workplace.tools.A_CmsHtmlIconButton#isVisible()
320             */
321            @Override
322            public boolean isVisible() {
323
324                return ((CmsShowUserRolesList)getWp()).hasRolesInOtherOus();
325            }
326        });
327        pathDetails.setShowActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_PATH_NAME_0));
328        pathDetails.setShowActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_SHOW_PATH_HELP_0));
329        pathDetails.setHideActionName(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_PATH_NAME_0));
330        pathDetails.setHideActionHelpText(Messages.get().container(Messages.GUI_ROLES_DETAIL_HIDE_PATH_HELP_0));
331        pathDetails.setName(Messages.get().container(Messages.GUI_ROLES_DETAIL_PATH_NAME_0));
332        pathDetails.setFormatter(
333            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_ROLES_DETAIL_PATH_NAME_0)));
334        metadata.addItemDetails(pathDetails);
335    }
336
337    /**
338     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
339     */
340    @Override
341    protected void setMultiActions(CmsListMetadata metadata) {
342
343        // noop
344    }
345}