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.file.CmsUserSearchParameters;
032import org.opencms.file.CmsUserSearchParameters.SearchKey;
033import org.opencms.file.CmsUserSearchParameters.SortKey;
034import org.opencms.jsp.CmsJspActionElement;
035import org.opencms.main.CmsException;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsOrganizationalUnit;
038import org.opencms.security.CmsPrincipal;
039import org.opencms.workplace.CmsDialog;
040import org.opencms.workplace.list.CmsListColumnDefinition;
041import org.opencms.workplace.list.CmsListDirectAction;
042import org.opencms.workplace.list.CmsListItem;
043import org.opencms.workplace.list.CmsListItemDetails;
044import org.opencms.workplace.list.CmsListItemDetailsFormatter;
045import org.opencms.workplace.list.CmsListMetadata;
046import org.opencms.workplace.list.CmsListOrderEnum;
047import org.opencms.workplace.list.CmsListState;
048
049import java.io.IOException;
050import java.util.HashMap;
051import java.util.Iterator;
052import java.util.List;
053import java.util.Map;
054
055import javax.servlet.ServletException;
056import javax.servlet.http.HttpServletRequest;
057import javax.servlet.http.HttpServletResponse;
058import javax.servlet.jsp.PageContext;
059
060import com.google.common.collect.Lists;
061
062/**
063 * User account view over all manageable organizational units.<p>
064 *
065 * @since 6.5.6
066 */
067public class CmsUsersAllOrgUnitsList extends A_CmsUsersList {
068
069    /** list action id constant. */
070    public static final String LIST_ACTION_OVERVIEW = "ao";
071
072    /** list column id constant. */
073    public static final String LIST_COLUMN_ORGUNIT = "co";
074
075    /** list item detail id constant. */
076    public static final String LIST_DETAIL_ORGUNIT_DESC = "dd";
077
078    /** list id constant. */
079    public static final String LIST_ID = "lsuaou";
080
081    /**
082     * Public constructor.<p>
083     *
084     * @param jsp an initialized JSP action element
085     */
086    public CmsUsersAllOrgUnitsList(CmsJspActionElement jsp) {
087
088        this(jsp, false);
089    }
090
091    /**
092     * Public constructor.<p>
093     *
094     * @param jsp an initialized JSP action element
095     */
096    public CmsUsersAllOrgUnitsList(CmsJspActionElement jsp, boolean lazy) {
097
098        super(jsp, LIST_ID, Messages.get().container(Messages.GUI_USERS_LIST_NAME_0), lazy);
099    }
100
101    /**
102     * Public constructor.<p>
103     *
104     * @param context a page context
105     * @param req a request
106     * @param res a response
107     */
108    public CmsUsersAllOrgUnitsList(PageContext context, HttpServletRequest req, HttpServletResponse res) {
109
110        this(context, req, res, false);
111    }
112
113    /**
114     * Public constructor with JSP variables.<p>
115     *
116     * @param context the JSP page context
117     * @param req the JSP request
118     * @param res the JSP response
119     * @param lazy the lazy flag
120     */
121    public CmsUsersAllOrgUnitsList(PageContext context, HttpServletRequest req, HttpServletResponse res, boolean lazy) {
122
123        this(new CmsJspActionElement(context, req, res), lazy);
124    }
125
126    /**
127     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#executeListSingleActions()
128     */
129    @Override
130    public void executeListSingleActions() throws IOException, ServletException {
131
132        String userId = getSelectedItem().getId();
133
134        Map params = new HashMap();
135        params.put(A_CmsEditUserDialog.PARAM_USERID, userId);
136        params.put(A_CmsOrgUnitDialog.PARAM_OUFQN, getSelectedItem().get(LIST_COLUMN_ORGUNIT).toString().substring(1));
137        // set action parameter to initial dialog call
138        params.put(CmsDialog.PARAM_ACTION, CmsDialog.DIALOG_INITIAL);
139
140        if (getParamListAction().equals(LIST_ACTION_OVERVIEW)) {
141            // forward
142            getToolManager().jspForwardTool(this, "/accounts/orgunit/users/edit", params);
143        } else if (getParamListAction().equals(LIST_DEFACTION_EDIT)) {
144            // forward to the edit user screen
145            getToolManager().jspForwardTool(this, "/accounts/orgunit/users/edit", params);
146        } else {
147            super.executeListSingleActions();
148        }
149        listSave();
150    }
151
152    /**
153     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#fillDetails(java.lang.String)
154     */
155    @Override
156    protected void fillDetails(String detailId) {
157
158        super.fillDetails(detailId);
159
160        List users = getList().getAllContent();
161        Iterator itUsers = users.iterator();
162        while (itUsers.hasNext()) {
163            CmsListItem item = (CmsListItem)itUsers.next();
164            String userName = item.get(LIST_COLUMN_LOGIN).toString();
165            StringBuffer html = new StringBuffer(512);
166            try {
167                if (detailId.equals(LIST_DETAIL_ORGUNIT_DESC)) {
168                    CmsUser user = readUser(userName);
169                    html.append(
170                        OpenCms.getOrgUnitManager().readOrganizationalUnit(getCms(), user.getOuFqn()).getDescription(
171                            getLocale()));
172                } else {
173                    continue;
174                }
175            } catch (Exception e) {
176                // noop
177            }
178            item.set(detailId, html.toString());
179        }
180    }
181
182    /**
183     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getGroupIcon()
184     */
185    @Override
186    protected String getGroupIcon() {
187
188        return null;
189    }
190
191    /**
192     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getListItems()
193     */
194    @Override
195    protected List<CmsListItem> getListItems() throws CmsException {
196
197        if (!m_lazy) {
198            return super.getListItems();
199        } else {
200
201            CmsUserSearchParameters params = getSearchParams();
202
203            List<CmsUser> users = OpenCms.getOrgUnitManager().searchUsers(getCms(), params);
204            int count = (int)OpenCms.getOrgUnitManager().countUsers(getCms(), params);
205            getList().setSize(count);
206            List<CmsListItem> result = Lists.newArrayList();
207            for (CmsUser user : users) {
208                CmsListItem item = makeListItemForUser(user);
209                result.add(item);
210            }
211            return result;
212        }
213    }
214
215    /**
216     * Gets the search parameters.<p>
217     *
218     * @return the search parameters
219     *
220     * @throws CmsException if something goes wrong
221     */
222    protected CmsUserSearchParameters getSearchParams() throws CmsException {
223
224        CmsListState state = getListState();
225        List<CmsOrganizationalUnit> ous = OpenCms.getRoleManager().getManageableOrgUnits(getCms(), "", true, false);
226        CmsUserSearchParameters params = new CmsUserSearchParameters();
227        params.setAllowedOus(ous);
228        String searchFilter = state.getFilter();
229        params.addSearch(SearchKey.email);
230        params.addSearch(SearchKey.orgUnit);
231        params.setSearchFilter(searchFilter);
232        params.setFilterCore(true);
233        params.setPaging(getList().getMaxItemsPerPage(), state.getPage());
234        params.setSorting(getSortKey(state.getColumn()), state.getOrder().equals(CmsListOrderEnum.ORDER_ASCENDING));
235        return params;
236    }
237
238    /**
239     * Returns the sort key for the column.<p>
240     *
241     * @param column the list column
242     * @return the sort key
243     */
244    protected SortKey getSortKey(String column) {
245
246        if (column == null) {
247            return null;
248        }
249        if (column.equals(LIST_COLUMN_ENABLED)) {
250            return SortKey.activated;
251        } else if (column.equals(LIST_COLUMN_LASTLOGIN)) {
252            return SortKey.lastLogin;
253        } else if (column.equals(LIST_COLUMN_DISPLAY)) {
254            return SortKey.loginName;
255        } else if (column.equals(LIST_COLUMN_NAME)) {
256            return SortKey.fullName;
257        } else if (column.equals(LIST_COLUMN_EMAIL)) {
258            return SortKey.email;
259        } else if (column.equals(LIST_COLUMN_ORGUNIT)) {
260            return SortKey.orgUnit;
261        }
262        return null;
263    }
264
265    /**
266     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#getUsers()
267     */
268    @Override
269    protected List<CmsUser> getUsers() throws CmsException {
270
271        return CmsPrincipal.filterCoreUsers(OpenCms.getRoleManager().getManageableUsers(getCms(), "", true));
272    }
273
274    /**
275     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#readUser(java.lang.String)
276     */
277    @Override
278    protected CmsUser readUser(String name) throws CmsException {
279
280        return getCms().readUser(name);
281    }
282
283    /**
284     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setColumns(org.opencms.workplace.list.CmsListMetadata)
285     */
286    @Override
287    protected void setColumns(CmsListMetadata metadata) {
288
289        if (m_lazy) {
290            metadata.setSelfManaged(true);
291        }
292        super.setColumns(metadata);
293        metadata.getColumnDefinition(LIST_COLUMN_GROUPS).setVisible(false);
294        metadata.getColumnDefinition(LIST_COLUMN_ROLE).setVisible(false);
295        metadata.getColumnDefinition(LIST_COLUMN_ACTIVATE).setVisible(false);
296        metadata.getColumnDefinition(LIST_COLUMN_DELETE).setVisible(false);
297        metadata.getColumnDefinition(LIST_COLUMN_LASTLOGIN).setVisible(false);
298
299        // add column for orgunit
300        CmsListColumnDefinition orgUnitCol = new CmsListColumnDefinition(LIST_COLUMN_ORGUNIT);
301        orgUnitCol.setName(Messages.get().container(Messages.GUI_USERS_LIST_COLS_ORGUNIT_0));
302        orgUnitCol.setWidth("30%");
303        metadata.addColumn(
304            orgUnitCol,
305            metadata.getColumnDefinitions().indexOf(metadata.getColumnDefinition(LIST_COLUMN_NAME)));
306    }
307
308    /**
309     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setDeleteAction(org.opencms.workplace.list.CmsListColumnDefinition)
310     */
311    @Override
312    protected void setDeleteAction(CmsListColumnDefinition deleteCol) {
313
314        // noop
315    }
316
317    /**
318     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setEditAction(org.opencms.workplace.list.CmsListColumnDefinition)
319     */
320    @Override
321    protected void setEditAction(CmsListColumnDefinition editCol) {
322
323        CmsListDirectAction editAction = new CmsListDirectAction(LIST_ACTION_OVERVIEW);
324        editAction.setName(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_NAME_0));
325        editAction.setHelpText(Messages.get().container(Messages.GUI_USERS_LIST_DEFACTION_EDIT_HELP_0));
326        editAction.setIconPath(PATH_BUTTONS + "user.png");
327        editCol.addDirectAction(editAction);
328    }
329
330    /**
331     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setIndependentActions(org.opencms.workplace.list.CmsListMetadata)
332     */
333    @Override
334    protected void setIndependentActions(CmsListMetadata metadata) {
335
336        super.setIndependentActions(metadata);
337
338        // add orgunit description details
339        CmsListItemDetails orgUnitDescDetails = new CmsListItemDetails(LIST_DETAIL_ORGUNIT_DESC);
340        orgUnitDescDetails.setAtColumn(LIST_COLUMN_DISPLAY);
341        orgUnitDescDetails.setVisible(false);
342        orgUnitDescDetails.setShowActionName(
343            Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_ORGUNIT_DESC_NAME_0));
344        orgUnitDescDetails.setShowActionHelpText(
345            Messages.get().container(Messages.GUI_USERS_DETAIL_SHOW_ORGUNIT_DESC_HELP_0));
346        orgUnitDescDetails.setHideActionName(
347            Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_ORGUNIT_DESC_NAME_0));
348        orgUnitDescDetails.setHideActionHelpText(
349            Messages.get().container(Messages.GUI_USERS_DETAIL_HIDE_ORGUNIT_DESC_HELP_0));
350        orgUnitDescDetails.setName(Messages.get().container(Messages.GUI_USERS_DETAIL_ORGUNIT_DESC_NAME_0));
351        orgUnitDescDetails.setFormatter(
352            new CmsListItemDetailsFormatter(Messages.get().container(Messages.GUI_USERS_DETAIL_ORGUNIT_DESC_NAME_0)));
353        metadata.addItemDetails(orgUnitDescDetails);
354        metadata.getSearchAction().addColumn(metadata.getColumnDefinition(LIST_COLUMN_EMAIL));
355        metadata.getSearchAction().addColumn(metadata.getColumnDefinition(LIST_COLUMN_ORGUNIT));
356    }
357
358    /**
359     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setMultiActions(org.opencms.workplace.list.CmsListMetadata)
360     */
361    @Override
362    protected void setMultiActions(CmsListMetadata metadata) {
363
364        // noop
365    }
366
367    /**
368     * @see org.opencms.workplace.tools.accounts.A_CmsUsersList#setUserData(org.opencms.file.CmsUser, org.opencms.workplace.list.CmsListItem)
369     */
370    @Override
371    protected void setUserData(CmsUser user, CmsListItem item) {
372
373        super.setUserData(user, item);
374        item.set(LIST_COLUMN_ORGUNIT, CmsOrganizationalUnit.SEPARATOR + user.getOuFqn());
375    }
376
377    /**
378     * @see org.opencms.workplace.list.A_CmsListDialog#validateParamaters()
379     */
380    @Override
381    protected void validateParamaters() throws Exception {
382
383        // no param check needed
384    }
385
386}