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