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