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.jsp.CmsJspActionElement;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsRuntimeException;
034import org.opencms.workplace.list.A_CmsListDialog;
035import org.opencms.workplace.list.CmsListColumnAlignEnum;
036import org.opencms.workplace.list.CmsListColumnDefinition;
037import org.opencms.workplace.list.CmsListDirectAction;
038import org.opencms.workplace.list.CmsListItem;
039import org.opencms.workplace.list.CmsListItemActionIconComparator;
040import org.opencms.workplace.list.CmsListMetadata;
041import org.opencms.workplace.list.CmsListMultiAction;
042
043import java.util.HashSet;
044import java.util.Iterator;
045import java.util.List;
046import java.util.Set;
047
048import javax.servlet.http.HttpServletRequest;
049import javax.servlet.http.HttpServletResponse;
050import javax.servlet.jsp.PageContext;
051
052/**
053 * User groups view.<p>
054 *
055 * @since 6.0.0
056 */
057public class CmsUserGroupsList extends A_CmsUserGroupsList {
058
059    /** list action id constant. */
060    public static final String LIST_ACTION_REMOVE = "ar";
061
062    /** list action id constant. */
063    public static final String LIST_DEFACTION_REMOVE = "dr";
064
065    /** list id constant. */
066    public static final String LIST_ID = "lug";
067
068    /** list action id constant. */
069    public static final String LIST_MACTION_REMOVE = "mr";
070
071    /** a set of action id's to use for removing. */
072    protected static Set m_removeActionIds = new HashSet();
073
074    /**
075     * Public constructor.<p>
076     *
077     * @param jsp an initialized JSP action element
078     */
079    public CmsUserGroupsList(CmsJspActionElement jsp) {
080
081        this(jsp, LIST_ID);
082    }
083
084    /**
085     * Public constructor with JSP variables.<p>
086     *
087     * @param context the JSP page context
088     * @param req the JSP request
089     * @param res the JSP response
090     */
091    public CmsUserGroupsList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
092
093        this(new CmsJspActionElement(context, req, res));
094    }
095
096    /**
097     * Public constructor.<p>
098     *
099     * @param jsp an initialized JSP action element
100     * @param listId the id of the list
101     */
102    protected CmsUserGroupsList(CmsJspActionElement jsp, String listId) {
103
104        super(jsp, listId, Messages.get().container(Messages.GUI_USERGROUPS_LIST_NAME_0), true);
105    }
106
107    /**
108     * @see org.opencms.workplace.list.A_CmsListDialog#executeListMultiActions()
109     */
110    @Override
111    public void executeListMultiActions() throws CmsRuntimeException {
112
113        if (getParamListAction().equals(LIST_MACTION_REMOVE)) {
114            // execute the remove multiaction
115            Iterator itItems = getSelectedItems().iterator();
116            while (itItems.hasNext()) {
117                CmsListItem listItem = (CmsListItem)itItems.next();
118                String groupName = (String)listItem.get(LIST_COLUMN_NAME);
119                boolean directGroup = false;
120                try {
121                    Iterator it = getCms().getGroupsOfUser(getParamUsername(), true).iterator();
122                    while (it.hasNext()) {
123                        CmsGroup group = (CmsGroup)it.next();
124                        if (group.getName().equals(groupName)) {
125                            directGroup = true;
126                            break;
127                        }
128                    }
129                    if (directGroup) {
130                        getCms().removeUserFromGroup(getParamUsername(), groupName);
131                    }
132                } catch (CmsException e) {
133                    // could be an indirectly assigned group
134                }
135            }
136        } else {
137            throwListUnsupportedActionException();
138        }
139        listSave();
140    }
141
142    /**
143     * @see org.opencms.workplace.list.A_CmsListDialog#executeListSingleActions()
144     */
145    @Override
146    public void executeListSingleActions() throws CmsRuntimeException {
147
148        if (m_removeActionIds.contains(getParamListAction())) {
149            CmsListItem listItem = getSelectedItem();
150            try {
151                getCms().removeUserFromGroup(getParamUsername(), (String)listItem.get(LIST_COLUMN_NAME));
152            } catch (CmsException e) {
153                // should never happen
154                throw new CmsRuntimeException(Messages.get().container(Messages.ERR_REMOVE_SELECTED_GROUP_0), e);
155            }
156        } else {
157            throwListUnsupportedActionException();
158        }
159        listSave();
160    }
161
162    /**
163     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#getGroups(boolean)
164     */
165    @Override
166    protected List getGroups(boolean withOtherOus) throws CmsException {
167
168        return getCms().getGroupsOfUser(getParamUsername(), false, withOtherOus);
169    }
170
171    /**
172     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setDefaultAction(org.opencms.workplace.list.CmsListColumnDefinition)
173     */
174    @Override
175    protected void setDefaultAction(CmsListColumnDefinition nameCol) {
176
177        // add default remove action for direct groups
178        CmsGroupRemoveAction removeAction = new CmsGroupRemoveAction(LIST_DEFACTION_REMOVE, true);
179        removeAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_REMOVE_NAME_0));
180        removeAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_REMOVE_HELP_0));
181        nameCol.addDefaultAction(removeAction);
182
183        // add default remove action for indirect groups
184        CmsGroupRemoveAction indirRemoveAction = new CmsGroupRemoveAction(LIST_DEFACTION_REMOVE + "i", false);
185        indirRemoveAction.setName(Messages.get().container(Messages.GUI_USERGROUPS_LIST_ACTION_STATE_DISABLED_NAME_0));
186        indirRemoveAction.setHelpText(
187            Messages.get().container(Messages.GUI_USERGROUPS_LIST_ACTION_STATE_DISABLED_HELP_0));
188        indirRemoveAction.setEnabled(false);
189        nameCol.addDefaultAction(indirRemoveAction);
190
191        // keep the ids
192        m_removeActionIds.add(removeAction.getId());
193        m_removeActionIds.add(indirRemoveAction.getId());
194    }
195
196    /**
197     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setIconAction(org.opencms.workplace.list.CmsListColumnDefinition)
198     */
199    @Override
200    protected void setIconAction(CmsListColumnDefinition iconCol) {
201
202        // adds a direct group icon
203        CmsListDirectAction dirAction = new CmsGroupStateAction(LIST_ACTION_ICON_DIRECT, true);
204        dirAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_DIRECT_NAME_0));
205        dirAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_DIRECT_HELP_0));
206        dirAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "group.png");
207        dirAction.setEnabled(false);
208        iconCol.addDirectAction(dirAction);
209
210        // adds an indirect group icon
211        CmsListDirectAction indirAction = new CmsGroupStateAction(LIST_ACTION_ICON_INDIRECT, false);
212        indirAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_INDIRECT_NAME_0));
213        indirAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_INDIRECT_HELP_0));
214        indirAction.setIconPath(A_CmsUsersList.PATH_BUTTONS + "group_indirect.png");
215        indirAction.setEnabled(false);
216        iconCol.addDirectAction(indirAction);
217
218        iconCol.setListItemComparator(new CmsListItemActionIconComparator());
219    }
220
221    /**
222     * @see org.opencms.workplace.list.A_CmsListDialog#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
223     */
224    @Override
225    protected void setMultiActions(CmsListMetadata metadata) {
226
227        // add remove multi action
228        CmsListMultiAction removeMultiAction = new CmsListMultiAction(LIST_MACTION_REMOVE);
229        removeMultiAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_REMOVE_NAME_0));
230        removeMultiAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_REMOVE_HELP_0));
231        removeMultiAction.setConfirmationMessage(
232            Messages.get().container(Messages.GUI_GROUPS_LIST_MACTION_REMOVE_CONF_0));
233        removeMultiAction.setIconPath(ICON_MULTI_MINUS);
234        metadata.addMultiAction(removeMultiAction);
235    }
236
237    /**
238     * @see org.opencms.workplace.tools.accounts.A_CmsUserGroupsList#setStateActionCol(org.opencms.workplace.list.CmsListMetadata)
239     */
240    @Override
241    protected void setStateActionCol(CmsListMetadata metadata) {
242
243        // create column for state change
244        CmsListColumnDefinition stateCol = new CmsListColumnDefinition(LIST_COLUMN_STATE);
245        stateCol.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_STATE_0));
246        stateCol.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_COLS_STATE_HELP_0));
247        stateCol.setWidth("20");
248        stateCol.setAlign(CmsListColumnAlignEnum.ALIGN_CENTER);
249        stateCol.setSorteable(false);
250
251        // add remove action for direct groups
252        CmsGroupRemoveAction dirStateAction = new CmsGroupRemoveAction(LIST_ACTION_REMOVE, true);
253        dirStateAction.setName(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_REMOVE_NAME_0));
254        dirStateAction.setHelpText(Messages.get().container(Messages.GUI_GROUPS_LIST_DEFACTION_REMOVE_HELP_0));
255        dirStateAction.setIconPath(ICON_MINUS);
256        stateCol.addDirectAction(dirStateAction);
257
258        // add remove action for indirect groups
259        CmsGroupRemoveAction indirStateAction = new CmsGroupRemoveAction(LIST_ACTION_REMOVE + "i", false);
260        indirStateAction.setName(Messages.get().container(Messages.GUI_USERGROUPS_LIST_ACTION_STATE_DISABLED_NAME_0));
261        indirStateAction.setHelpText(
262            Messages.get().container(Messages.GUI_USERGROUPS_LIST_ACTION_STATE_DISABLED_HELP_0));
263        indirStateAction.setIconPath(A_CmsListDialog.ICON_DISABLED);
264        indirStateAction.setEnabled(false);
265        stateCol.addDirectAction(indirStateAction);
266
267        stateCol.setListItemComparator(new CmsListItemActionIconComparator());
268
269        // add it to the list definition
270        metadata.addColumn(stateCol);
271        // keep the ids
272        m_removeActionIds.add(dirStateAction.getId());
273        m_removeActionIds.add(indirStateAction.getId());
274    }
275}