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, 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.ui.apps.user;
029
030import org.opencms.db.CmsUserSettings;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.CmsSessionInfo;
036import org.opencms.main.OpenCms;
037import org.opencms.security.CmsRole;
038import org.opencms.security.CmsRoleViolationException;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsCssIcon;
041import org.opencms.ui.CmsVaadinUtils;
042import org.opencms.ui.apps.Messages;
043import org.opencms.ui.apps.sessions.CmsKillSessionDialog;
044import org.opencms.ui.apps.sessions.CmsUserInfoDialog;
045import org.opencms.ui.components.CmsBasicDialog;
046import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
047import org.opencms.ui.components.CmsConfirmationDialog;
048import org.opencms.ui.components.OpenCmsTheme;
049import org.opencms.ui.contextmenu.CmsContextMenu;
050import org.opencms.ui.contextmenu.CmsMenuItemVisibilityMode;
051import org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry;
052import org.opencms.util.CmsDateUtil;
053import org.opencms.util.CmsStringUtil;
054import org.opencms.util.CmsUUID;
055
056import java.text.DateFormat;
057import java.util.ArrayList;
058import java.util.Date;
059import java.util.HashMap;
060import java.util.HashSet;
061import java.util.Iterator;
062import java.util.List;
063import java.util.Locale;
064import java.util.Map;
065import java.util.Set;
066
067import org.apache.commons.logging.Log;
068
069import com.vaadin.server.Resource;
070import com.vaadin.shared.MouseEventDetails.MouseButton;
071import com.vaadin.ui.Component;
072import com.vaadin.ui.Window;
073import com.vaadin.ui.themes.ValoTheme;
074import com.vaadin.v7.data.Item;
075import com.vaadin.v7.data.util.IndexedContainer;
076import com.vaadin.v7.data.util.filter.Or;
077import com.vaadin.v7.data.util.filter.SimpleStringFilter;
078import com.vaadin.v7.event.ItemClickEvent;
079import com.vaadin.v7.event.ItemClickEvent.ItemClickListener;
080import com.vaadin.v7.ui.Table;
081import com.vaadin.v7.ui.VerticalLayout;
082
083/**
084 * Table for user.<p>
085 *
086 */
087public class CmsUserTable extends Table implements I_CmsFilterableTable, I_CmsToggleTable {
088
089    /**Table properties. */
090    public enum TableProperty {
091        /**Last login. */
092        Created(Messages.GUI_USERMANAGEMENT_USER_DATE_CREATED_0, Long.class, new Long(0L)),
093        /**Is the user disabled? */
094        DISABLED("", Boolean.class, new Boolean(false)),
095        /**From Other ou?. */
096        FROMOTHEROU("", Boolean.class, new Boolean(false)),
097        /**Description. */
098        FullName(Messages.GUI_USERMANAGEMENT_USER_NAME_0, String.class, ""),
099        /**Icon. */
100        Icon(null, Resource.class, new CmsCssIcon("oc-icon-24-user")),
101        /**IsIndirect?. */
102        INDIRECT("", Boolean.class, new Boolean(false)),
103        /**Last login. */
104        LastLogin(Messages.GUI_USERMANAGEMENT_USER_LAST_LOGIN_0, Long.class, new Long(0L)),
105        /**Name. */
106        Name(Messages.GUI_USERMANAGEMENT_USER_USER_0, String.class, ""),
107        /**Is the user new? */
108        NEWUSER("", Boolean.class, new Boolean(false)),
109        /**OU. */
110        OU(Messages.GUI_USERMANAGEMENT_USER_OU_0, String.class, ""),
111        /**Status. */
112        STATUS("", Integer.class, new Integer(0)),
113        /**Full system Name. */
114        SystemName("", String.class, "");
115
116        /**Default value for column.*/
117        private Object m_defaultValue;
118
119        /**Header Message key.*/
120        private String m_headerMessage;
121
122        /**Type of column property.*/
123        private Class<?> m_type;
124
125        /**
126         * constructor.<p>
127         *
128         * @param name Name
129         * @param type type
130         * @param defaultValue value
131         */
132        TableProperty(String name, Class<?> type, Object defaultValue) {
133
134            m_headerMessage = name;
135            m_type = type;
136            m_defaultValue = defaultValue;
137        }
138
139        /**
140         * The default value.<p>
141         *
142         * @return the default value object
143         */
144        Object getDefault() {
145
146            return m_defaultValue;
147        }
148
149        /**
150         * Gets the name of the property.<p>
151         *
152         * @return a name
153         */
154        String getName() {
155
156            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(m_headerMessage)) {
157                return CmsVaadinUtils.getMessageText(m_headerMessage);
158            }
159            return "";
160        }
161
162        /**
163         * Gets the type of property.<p>
164         *
165         * @return the type
166         */
167        Class<?> getType() {
168
169            return m_type;
170        }
171
172    }
173
174    /**
175     *Entry to addition info dialog.<p>
176     */
177    class EntryAddInfos implements I_CmsSimpleContextMenuEntry<Set<String>> {
178
179        /**
180         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
181         */
182        public void executeAction(Set<String> context) {
183
184            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
185            CmsAdditionalInfosDialog dialog = new CmsAdditionalInfosDialog(
186                m_cms,
187                new CmsUUID(context.iterator().next()),
188                window,
189                m_app);
190            window.setContent(dialog);
191            A_CmsUI.get().addWindow(window);
192        }
193
194        /**
195         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
196         */
197        public String getTitle(Locale locale) {
198
199            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_ADDITIONAL_INFOS_MENU_0);
200        }
201
202        /**
203         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
204         */
205        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
206
207            if (context.size() > 1) {
208                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
209            }
210            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
211        }
212
213    }
214
215    /**
216     * Menu entry to delete user.<p>
217     */
218    class EntryDelete implements I_CmsSimpleContextMenuEntry<Set<String>> {
219
220        /**
221         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
222         */
223        public void executeAction(final Set<String> context) {
224
225            Window window = CmsBasicDialog.prepareWindow();
226            CmsBasicDialog dialog = null;
227
228            dialog = new CmsDeleteMultiplePrincipalDialog(m_cms, context, window, m_app);
229
230            window.setContent(dialog);
231            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DELETE_0));
232            A_CmsUI.get().addWindow(window);
233        }
234
235        /**
236         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
237         */
238        public String getTitle(Locale locale) {
239
240            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DELETE_0);
241        }
242
243        /**
244         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
245         */
246        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
247
248            return onlyVisibleForOU(context);
249        }
250
251    }
252
253    /**
254     * Menu entry to edit user.<p>
255     */
256    class EntryEdit implements I_CmsSimpleContextMenuEntry<Set<String>> {
257
258        /**
259         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
260         */
261        public void executeAction(Set<String> context) {
262
263            Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
264            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_USER_0));
265            window.setContent(new CmsUserEditDialog(m_cms, new CmsUUID(context.iterator().next()), window, m_app));
266
267            A_CmsUI.get().addWindow(window);
268        }
269
270        /**
271         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
272         */
273        public String getTitle(Locale locale) {
274
275            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_USER_0);
276        }
277
278        /**
279         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
280         */
281        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
282
283            CmsUUID id = new CmsUUID(context.iterator().next());
284            return CmsMenuItemVisibilityMode.activeInactive(
285                m_app.canEditUser(id) && onlyVisibleForOU(id) && (context.size() <= 1));
286        }
287
288    }
289
290    /**
291     * Menu entry to edit groups.<p>
292     */
293    class EntryEditGroup implements I_CmsSimpleContextMenuEntry<Set<String>> {
294
295        /**
296         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
297         */
298        public void executeAction(Set<String> context) {
299
300            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
301
302            window.setContent(
303                new CmsUserEditGroupsDialog(m_cms, new CmsUUID(context.iterator().next()), window, m_app));
304            A_CmsUI.get().addWindow(window);
305        }
306
307        /**
308         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
309         */
310        public String getTitle(Locale locale) {
311
312            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_USERGROUP_0);
313        }
314
315        /**
316         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
317         */
318        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
319
320            return CmsMenuItemVisibilityMode.activeInactive(
321                onlyVisibleForOU(new CmsUUID(context.iterator().next())) && (context.size() <= 1));
322        }
323
324    }
325
326    /**
327     * Menu entry for editing roles of user.<p>
328     */
329    class EntryEditRole implements I_CmsSimpleContextMenuEntry<Set<String>> {
330
331        /**
332         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
333         */
334        public void executeAction(Set<String> context) {
335
336            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
337
338            window.setContent(new CmsUserEditRoleDialog(m_cms, new CmsUUID(context.iterator().next()), window, m_app));
339
340            A_CmsUI.get().addWindow(window);
341        }
342
343        /**
344         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
345         */
346        public String getTitle(Locale locale) {
347
348            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_USERROLES_0);
349        }
350
351        /**
352         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
353         */
354        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
355
356            return CmsMenuItemVisibilityMode.activeInactive(
357                onlyVisibleForOU(new CmsUUID(context.iterator().next())) && (context.size() <= 1));
358        }
359
360    }
361
362    /**
363     *Entry to enable after password was entered wrong.<p>
364     */
365    class EntryEnable implements I_CmsSimpleContextMenuEntry<Set<String>> {
366
367        /**
368         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
369         */
370        public void executeAction(Set<String> context) {
371
372            final Window window = CmsBasicDialog.prepareWindow();
373            try {
374                final CmsUser user = m_cms.readUser(new CmsUUID(context.iterator().next()));
375
376                CmsConfirmationDialog dialog = new CmsConfirmationDialog(
377                    CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_UNLOCK_USER_LABEL_0),
378                    new Runnable() {
379
380                        public void run() {
381
382                            OpenCms.getLoginManager().resetUserTempDisable(user.getName());
383                            window.close();
384                            m_app.reload();
385                        }
386
387                    },
388                    new Runnable() {
389
390                        public void run() {
391
392                            window.close();
393
394                        }
395
396                    });
397                window.setCaption(
398                    CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_UNLOCK_USER_CAPTION_1, user.getName()));
399                window.setContent(dialog);
400                A_CmsUI.get().addWindow(window);
401            } catch (CmsException e) {
402                LOG.error("Unable to read user", e);
403            }
404
405        }
406
407        /**
408         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
409         */
410        public String getTitle(Locale locale) {
411
412            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_UNLOCK_USER_CONTEXT_MENU_TITLE_0);
413        }
414
415        /**
416         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
417         */
418        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
419
420            try {
421                CmsUser user = m_cms.readUser(new CmsUUID(context.iterator().next()));
422                if (OpenCms.getLoginManager().isUserTempDisabled(user.getName())) {
423                    return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
424                } else {
425                    return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
426                }
427
428            } catch (CmsException e) {
429                LOG.error("Can not read user.", e);
430            }
431            return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
432        }
433
434    }
435
436    /**
437     *Entry to info dialog.<p>
438     */
439    class EntryInfo implements I_CmsSimpleContextMenuEntry<Set<String>>, I_CmsSimpleContextMenuEntry.I_HasCssStyles {
440
441        /**
442         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
443         */
444        public void executeAction(Set<String> context) {
445
446            try {
447                openInfoDialog(new CmsUUID(context.iterator().next()));
448            } catch (CmsException e) {
449                LOG.error("Error on opening user info dialog", e);
450            }
451        }
452
453        /**
454         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry.I_HasCssStyles#getStyles()
455         */
456        public String getStyles() {
457
458            return ValoTheme.LABEL_BOLD;
459        }
460
461        /**
462         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
463         */
464        public String getTitle(Locale locale) {
465
466            return CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_SHOW_USER_0);
467        }
468
469        /**
470         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
471         */
472        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
473
474            if (context.size() > 1) {
475                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
476            }
477            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
478        }
479
480    }
481
482    /**
483     *Entry to kill Session.<p>
484     */
485    class EntryKillSession implements I_CmsSimpleContextMenuEntry<Set<String>> {
486
487        /**
488         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
489         */
490        public void executeAction(Set<String> context) {
491
492            final Window window = CmsBasicDialog.prepareWindow();
493            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_DESTROY_SESSION_0));
494            Set<String> sessionIds = new HashSet<String>();
495            for (CmsSessionInfo info : OpenCms.getSessionManager().getSessionInfos(
496                new CmsUUID(context.iterator().next()))) {
497                sessionIds.add(info.getSessionId().getStringValue());
498            }
499            CmsKillSessionDialog dialog = new CmsKillSessionDialog(sessionIds, new Runnable() {
500
501                public void run() {
502
503                    window.close();
504                }
505            });
506            window.setContent(dialog);
507            A_CmsUI.get().addWindow(window);
508        }
509
510        /**
511         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
512         */
513        public String getTitle(Locale locale) {
514
515            return CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_DESTROY_SESSION_0);
516        }
517
518        /**
519         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
520         */
521        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
522
523            if (context.size() > 1) {
524                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
525            }
526            if (!OpenCms.getSessionManager().getSessionInfos(new CmsUUID(context.iterator().next())).isEmpty()) {
527                return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
528            }
529            return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
530        }
531
532    }
533
534    /**
535     * Menu entry to move user to another ou.<p>
536     */
537    class EntryMoveOU implements I_CmsSimpleContextMenuEntry<Set<String>> {
538
539        /**
540         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
541         */
542        public void executeAction(final Set<String> context) {
543
544            Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
545            CmsBasicDialog dialog = null;
546
547            dialog = new CmsMoveUserToOU(m_cms, new CmsUUID(context.iterator().next()), window, m_app);
548
549            window.setContent(dialog);
550            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_MOVE_OU_0));
551            A_CmsUI.get().addWindow(window);
552        }
553
554        /**
555         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
556         */
557        public String getTitle(Locale locale) {
558
559            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_MOVE_OU_0);
560        }
561
562        /**
563         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
564         */
565        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
566
567            try {
568                if (OpenCms.getOrgUnitManager().readOrganizationalUnit(m_cms, m_ou).hasFlagWebuser()) {
569                    return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
570                }
571            } catch (CmsException e) {
572                LOG.error("Unable to read OU", e);
573            }
574
575            return onlyVisibleForOU(context);
576        }
577
578    }
579
580    /**
581     * Menu entry to remove user from group.<p>
582     */
583    class EntryRemoveFromGroup implements I_CmsSimpleContextMenuEntry<Set<String>> {
584
585        /**
586         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
587         */
588        public void executeAction(final Set<String> context) {
589
590            try {
591                final Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
592                final String user = m_cms.readUser(new CmsUUID(context.iterator().next())).getName();
593                String confirmText = "";
594                String caption = "";
595                Runnable okRunnable = null;
596                if (m_type.isGroup()) {
597                    caption = CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_GROUP_0);
598                    confirmText = CmsVaadinUtils.getMessageText(
599                        Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_GROUP_CONFIRM_2,
600                        user,
601                        m_group);
602                    okRunnable = new Runnable() {
603
604                        public void run() {
605
606                            try {
607                                m_cms.removeUserFromGroup(user, m_group);
608                            } catch (CmsException e) {
609                                //
610                            }
611                            window.close();
612                            m_app.reload();
613                        }
614                    };
615                }
616                if (m_type.equals(CmsOuTreeType.ROLE)) {
617                    caption = CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_ROLE_0);
618                    confirmText = CmsVaadinUtils.getMessageText(
619                        Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_ROLE_CONFIRM_2,
620                        user,
621                        m_group);
622                    okRunnable = new Runnable() {
623
624                        public void run() {
625
626                            try {
627                                OpenCms.getRoleManager().removeUserFromRole(
628                                    m_cms,
629                                    CmsRole.valueOfRoleName(m_group),
630                                    user);
631
632                            } catch (CmsException e) {
633                                //
634                            }
635                            window.close();
636                            m_app.reload();
637                        }
638                    };
639                }
640
641                window.setCaption(caption);
642                window.setContent(new CmsConfirmationDialog(confirmText, okRunnable, new Runnable() {
643
644                    public void run() {
645
646                        window.close();
647                    }
648                }));
649
650                A_CmsUI.get().addWindow(window);
651            } catch (CmsException e) {
652                //
653            }
654        }
655
656        /**
657         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
658         */
659        public String getTitle(Locale locale) {
660
661            if (m_type.isGroup()) {
662                return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_GROUP_0);
663            }
664            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_REMOVE_USER_FROM_ROLE_0);
665        }
666
667        /**
668         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
669         */
670        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
671
672            if (!m_app.canRemoveGroupMemebers(m_group)) {
673                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
674            }
675
676            if (context.size() > 1) {
677                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
678            }
679            if (m_group == null) {
680                return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
681            }
682
683            try {
684                CmsUser user = m_cms.readUser(context.iterator().next());
685                return ((Boolean)(getItem(user).getItemProperty(TableProperty.INDIRECT).getValue())).booleanValue()
686                ? CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE
687                : CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
688            } catch (CmsException e) {
689                //
690            }
691            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
692        }
693
694    }
695
696    /**
697     * Menu entry for show resources of user.<p>
698     */
699    class EntryShowResources implements I_CmsSimpleContextMenuEntry<Set<String>> {
700
701        /**
702         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
703         */
704        public void executeAction(Set<String> context) {
705
706            Window window = CmsBasicDialog.prepareWindow(DialogWidth.wide);
707            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_SHOW_RESOURCES_0));
708            window.setContent(new CmsShowResourcesDialog(context.iterator().next(), window));
709
710            A_CmsUI.get().addWindow(window);
711        }
712
713        /**
714         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
715         */
716        public String getTitle(Locale locale) {
717
718            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_SHOW_RESOURCES_0);
719        }
720
721        /**
722         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
723         */
724        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
725
726            if (context.size() > 1) {
727                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
728            }
729            return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
730        }
731
732    }
733
734    /**
735     *Entry to switch user.<p>
736     */
737    class EntrySwitchUser implements I_CmsSimpleContextMenuEntry<Set<String>> {
738
739        /**
740         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#executeAction(java.lang.Object)
741         */
742        public void executeAction(Set<String> context) {
743
744            final Window window = CmsBasicDialog.prepareWindow();
745            final CmsUUID userID = new CmsUUID(context.iterator().next());
746            window.setCaption(CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_SWITCH_USER_0));
747            CmsConfirmationDialog dialog = new CmsConfirmationDialog(
748                CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_SWITCH_USER_CONFIRM_0),
749                new Runnable() {
750
751                    public void run() {
752
753                        try {
754                            String path = OpenCms.getSessionManager().switchUser(
755                                A_CmsUI.getCmsObject(),
756                                CmsVaadinUtils.getRequest(),
757                                m_cms.readUser(userID));
758                            if (path == null) {
759                                path = CmsVaadinUtils.getWorkplaceLink() + "?_lrid=" + (new Date()).getTime();
760                            }
761
762                            A_CmsUI.get().getPage().setLocation(path);
763                            if (path.contains("workplace#")) {
764                                A_CmsUI.get().getPage().reload();
765                            }
766                            window.close();
767                        } catch (CmsException e) {
768                            LOG.error("Unable to swith user", e);
769                        }
770                    }
771                },
772                new Runnable() {
773
774                    public void run() {
775
776                        window.close();
777                    }
778                });
779            window.setContent(dialog);
780            A_CmsUI.get().addWindow(window);
781        }
782
783        /**
784         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getTitle(java.util.Locale)
785         */
786        public String getTitle(Locale locale) {
787
788            return CmsVaadinUtils.getMessageText(Messages.GUI_MESSAGES_SWITCH_USER_0);
789        }
790
791        /**
792         * @see org.opencms.ui.contextmenu.I_CmsSimpleContextMenuEntry#getVisibility(java.lang.Object)
793         */
794        public CmsMenuItemVisibilityMode getVisibility(Set<String> context) {
795
796            try {
797                OpenCms.getRoleManager().checkRole(m_cms, CmsRole.ADMINISTRATOR);
798                return CmsMenuItemVisibilityMode.activeInactive(
799                    onlyVisibleForOU(new CmsUUID(context.iterator().next())) && (context.size() <= 1));
800            } catch (CmsRoleViolationException e) {
801                return CmsMenuItemVisibilityMode.VISIBILITY_INVISIBLE;
802            }
803
804        }
805
806    }
807
808    /**Map for password status of user. */
809    protected static final Map<CmsUUID, Boolean> USER_PASSWORD_STATUS = new HashMap<CmsUUID, Boolean>();
810
811    /** Log instance for this class. */
812    static final Log LOG = CmsLog.getLog(CmsUserTable.class);
813
814    /**vaadin serial id.*/
815    private static final long serialVersionUID = 7863356514060544048L;
816
817    /**Name of group to show user for, or null. */
818    protected String m_group;
819
820    /**Type to be shown. */
821    protected I_CmsOuTreeType m_type;
822
823    /**The app.*/
824    CmsAccountsApp m_app;
825
826    /**CmsObject.*/
827    CmsObject m_cms;
828
829    /**List of indirect user. */
830    List<CmsUser> m_indirects;
831
832    /** The context menu. */
833    CmsContextMenu m_menu;
834
835    /**OU. */
836    String m_ou;
837
838    /**Black list of user from higher OU than current user.*/
839    private HashSet<CmsUser> m_blackList = new HashSet<CmsUser>();
840
841    /**Set of user with Flag to reset password. */
842    private Set<CmsUUID> m_checkedUserPasswordReset;
843
844    /**Indexed container. */
845    private IndexedContainer m_container;
846
847    /**vaadin component.*/
848    private VerticalLayout m_emptyLayout;
849
850    /**flag indicates if all indirect items (for roles or sub OUs) are loaded. */
851    private boolean m_fullyLoaded;
852
853    /** The available menu entries. */
854    private List<I_CmsSimpleContextMenuEntry<Set<String>>> m_menuEntries;
855
856    /**List of user. */
857    private List<CmsUser> m_users;
858
859    /**
860     * puiblic constructor.<p>
861     *
862     * @param ou ou name
863     * @param groupID id of group
864     * @param cmsOuTreeType type to be shown
865     * @param showAll show all user including inherited?
866     * @param app the app
867     */
868    public CmsUserTable(
869        String ou,
870        CmsUUID groupID,
871        I_CmsOuTreeType cmsOuTreeType,
872        boolean showAll,
873        CmsAccountsApp app) {
874
875        m_ou = ou.equals("/") ? "" : ou;
876        m_app = app;
877
878        try {
879            m_cms = getCmsObject();
880            m_type = cmsOuTreeType;
881
882            m_indirects = new ArrayList<CmsUser>();
883            if (m_type.isGroup()) {
884                m_group = m_cms.readGroup(groupID).getName();
885                m_users = m_cms.getUsersOfGroup(m_group, true);
886                m_fullyLoaded = true;
887            }
888            if (m_type.isRole()) {
889                m_group = CmsRole.valueOfId(groupID).forOrgUnit(ou).getFqn();
890                List<CmsUser> directs = OpenCms.getRoleManager().getUsersOfRole(
891                    m_cms,
892                    CmsRole.valueOfId(groupID).forOrgUnit(ou),
893                    true,
894                    true);
895                if (showAll) {
896                    setAllUsers(directs);
897                } else {
898                    m_users = directs;
899                }
900            }
901
902            init(showAll);
903        } catch (CmsException e) {
904            LOG.error("Unable to read user", e);
905        }
906    }
907
908    /**
909     * public constructor.<p>
910     *
911     * @param ou name
912     * @param type the tree type
913     * @param app the app
914     * @param showAll boolean
915     */
916    public CmsUserTable(String ou, I_CmsOuTreeType type, CmsAccountsApp app, boolean showAll) {
917
918        m_ou = ou.equals("/") ? "" : ou;
919        m_app = app;
920        try {
921            m_cms = getCmsObject();
922            m_type = type;
923            List<CmsUser> directs = m_app.getUsersWithoutAdditionalInfo(m_cms, type, ou, false);
924            m_indirects = new ArrayList<CmsUser>();
925            if (showAll) {
926                setAllUsers(directs);
927            } else {
928                m_users = directs;
929            }
930            init(showAll);
931        } catch (CmsException e) {
932            LOG.error("Unable to read user.", e);
933        }
934    }
935
936    /**
937     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#filter(java.lang.String)
938     */
939    public void filter(String data) {
940
941        m_container.removeAllContainerFilters();
942        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(data)) {
943            m_container.addContainerFilter(
944                new Or(
945                    new SimpleStringFilter(TableProperty.SystemName, data, true, false),
946                    new SimpleStringFilter(TableProperty.FullName, data, true, false)));
947        }
948
949    }
950
951    /**
952     * @see org.opencms.ui.apps.user.I_CmsToggleTable#getCurrentSize()
953     */
954    public int getCurrentSize() {
955
956        return this.size();
957    }
958
959    /**
960     * Layout which gets displayed if table is empty.
961     *
962     * @see org.opencms.ui.apps.user.I_CmsFilterableTable#getEmptyLayout()
963     */
964    public VerticalLayout getEmptyLayout() {
965
966        m_emptyLayout = CmsVaadinUtils.getInfoLayout(CmsOuTreeType.USER.getEmptyMessageKey());
967        setVisible(size() > 0);
968        m_emptyLayout.setVisible(size() == 0);
969        return m_emptyLayout;
970    }
971
972    /**
973     * Gets currently visible user.
974     *
975     * @return List of user
976     */
977    public List<CmsUser> getVisibleUser() {
978
979        if (!m_fullyLoaded) {
980            return m_users;
981        }
982        if (size() == m_users.size()) {
983            return m_users;
984        }
985        List<CmsUser> directs = new ArrayList<CmsUser>();
986        for (CmsUser user : m_users) {
987            if (!m_indirects.contains(user)) {
988                directs.add(user);
989            }
990        }
991        return directs;
992    }
993
994    /**
995     * @see org.opencms.ui.apps.user.I_CmsToggleTable#toggle(boolean)
996     */
997    public void toggle(boolean pressed) {
998
999        try {
1000
1001            if (pressed && !m_fullyLoaded) {
1002                setAllUsers(m_users);
1003            }
1004        } catch (CmsException e) {
1005            m_fullyLoaded = false;
1006            LOG.error("Error loading user", e);
1007        }
1008        fillContainer(pressed);
1009    }
1010
1011    /**
1012     * Adds given user to given IndexedContainer.<p>
1013     *
1014     * @param container to add the user to
1015     * @param user to add
1016     */
1017    protected void addUserToContainer(IndexedContainer container, CmsUser user) {
1018
1019        if (m_blackList.contains(user)) {
1020            return;
1021        }
1022        Item item = container.addItem(user);
1023        fillItem(item, user);
1024    }
1025
1026    /**
1027     * Fills the container.<p>
1028     *
1029     * @param showIndirect true-> show all user, false -> only direct user
1030     */
1031    protected void fillContainer(boolean showIndirect) {
1032
1033        m_container.removeAllContainerFilters();
1034        m_container.removeAllItems();
1035        m_checkedUserPasswordReset = new HashSet<CmsUUID>();
1036        for (CmsUser user : m_users) {
1037            if (showIndirect || !m_indirects.contains(user)) {
1038                addUserToContainer(m_container, user);
1039            }
1040        }
1041        setVisibilities();
1042    }
1043
1044    /**
1045     * Fills the container item for a user.<p>
1046     *
1047     * @param item the item
1048     * @param user the user
1049     */
1050    protected void fillItem(Item item, CmsUser user) {
1051
1052        item.getItemProperty(TableProperty.Name).setValue(user.getSimpleName());
1053        item.getItemProperty(TableProperty.FullName).setValue(user.getFullName());
1054        item.getItemProperty(TableProperty.SystemName).setValue(user.getName());
1055        boolean disabled = !user.isEnabled();
1056        item.getItemProperty(TableProperty.DISABLED).setValue(new Boolean(disabled));
1057        boolean newUser = user.getLastlogin() == 0L;
1058        item.getItemProperty(TableProperty.NEWUSER).setValue(new Boolean(newUser));
1059        try {
1060            item.getItemProperty(TableProperty.OU).setValue(
1061                OpenCms.getOrgUnitManager().readOrganizationalUnit(m_cms, user.getOuFqn()).getDisplayName(
1062                    A_CmsUI.get().getLocale()));
1063        } catch (CmsException e) {
1064            LOG.error("Can't read OU", e);
1065        }
1066        item.getItemProperty(TableProperty.LastLogin).setValue(new Long(user.getLastlogin()));
1067        item.getItemProperty(TableProperty.Created).setValue(new Long(user.getDateCreated()));
1068        item.getItemProperty(TableProperty.INDIRECT).setValue(new Boolean(m_indirects.contains(user)));
1069        item.getItemProperty(TableProperty.FROMOTHEROU).setValue(new Boolean(!user.getOuFqn().equals(m_ou)));
1070        item.getItemProperty(TableProperty.STATUS).setValue(getStatusInt(disabled, newUser));
1071    }
1072
1073    /**
1074     * initializes table.
1075     * @param showAll boolean
1076     */
1077    protected void init(boolean showAll) {
1078
1079        m_menu = new CmsContextMenu();
1080        m_menu.setAsTableContextMenu(this);
1081
1082        m_container = new IndexedContainer();
1083
1084        for (TableProperty prop : TableProperty.values()) {
1085            m_container.addContainerProperty(prop, prop.getType(), prop.getDefault());
1086            setColumnHeader(prop, prop.getName());
1087        }
1088        m_app.addUserContainerProperties(m_container);
1089        setContainerDataSource(m_container);
1090        setItemIconPropertyId(TableProperty.Icon);
1091        setRowHeaderMode(RowHeaderMode.ICON_ONLY);
1092
1093        setColumnWidth(null, 40);
1094        setColumnWidth(TableProperty.STATUS, 100);
1095        setSelectable(true);
1096        setMultiSelect(true);
1097
1098        setVisibleColumns(TableProperty.Name, TableProperty.OU);
1099
1100        fillContainer(showAll);
1101
1102        addItemClickListener(new ItemClickListener() {
1103
1104            private static final long serialVersionUID = 4807195510202231174L;
1105
1106            @SuppressWarnings("unchecked")
1107            public void itemClick(ItemClickEvent event) {
1108
1109                changeValueIfNotMultiSelect(event.getItemId());
1110
1111                if (event.getButton().equals(MouseButton.RIGHT) || (event.getPropertyId() == null)) {
1112                    Set<String> userIds = new HashSet<String>();
1113                    for (CmsUser user : (Set<CmsUser>)getValue()) {
1114                        userIds.add(user.getId().getStringValue());
1115                    }
1116
1117                    m_menu.setEntries(getMenuEntries(), userIds);
1118                    m_menu.openForTable(event, event.getItemId(), event.getPropertyId(), CmsUserTable.this);
1119                } else if (event.getButton().equals(MouseButton.LEFT)
1120                    && TableProperty.Name.equals(event.getPropertyId())) {
1121                    CmsUser user = ((Set<CmsUser>)getValue()).iterator().next();
1122                    try {
1123                        openInfoDialog(user.getId());
1124                    } catch (CmsException e) {
1125                        LOG.error("Error on opening user info dialog", e);
1126                    }
1127                }
1128
1129            }
1130
1131        });
1132        setCellStyleGenerator(new CellStyleGenerator() {
1133
1134            private static final long serialVersionUID = 4685652851810828147L;
1135
1136            public String getStyle(Table source, Object itemId, Object propertyId) {
1137
1138                if (TableProperty.STATUS.equals(propertyId)) {
1139                    return getStatusStyleForItem(source.getItem(itemId), (CmsUser)itemId);
1140
1141                }
1142                String css = " ";
1143
1144                if (((Boolean)(source.getItem(itemId).getItemProperty(
1145                    TableProperty.FROMOTHEROU).getValue())).booleanValue()) {
1146                    css += OpenCmsTheme.EXPIRED;
1147                }
1148
1149                if (TableProperty.Name.equals(propertyId)) {
1150                    css += " " + OpenCmsTheme.HOVER_COLUMN;
1151                }
1152
1153                if (((Boolean)source.getItem(itemId).getItemProperty(
1154                    TableProperty.INDIRECT).getValue()).booleanValue()) {
1155                    return css + " " + OpenCmsTheme.TABLE_CELL_DISABLED;
1156                }
1157                return css.length() == 1 ? null : css;
1158            }
1159
1160            private String getStatusStyleForItem(Item item, CmsUser user) {
1161
1162                if (((Boolean)item.getItemProperty(TableProperty.DISABLED).getValue()).booleanValue()) {
1163                    return OpenCmsTheme.TABLE_COLUMN_BOX_GRAY;
1164                }
1165
1166                if (((Boolean)item.getItemProperty(TableProperty.NEWUSER).getValue()).booleanValue()) {
1167                    return OpenCmsTheme.TABLE_COLUMN_BOX_BLUE;
1168                }
1169
1170                if (OpenCms.getLoginManager().isUserTempDisabled(user.getName())) {
1171                    return OpenCmsTheme.TABLE_COLUMN_BOX_RED;
1172                }
1173
1174                if (isUserPasswordReset(user)) {
1175                    return OpenCmsTheme.TABLE_COLUMN_BOX_ORANGE;
1176                }
1177
1178                return OpenCmsTheme.TABLE_COLUMN_BOX_GREEN;
1179            }
1180        });
1181        addGeneratedColumn(TableProperty.STATUS, new ColumnGenerator() {
1182
1183            private static final long serialVersionUID = -2144476865774782965L;
1184
1185            public Object generateCell(Table source, Object itemId, Object columnId) {
1186
1187                return getStatus(
1188                    (CmsUser)itemId,
1189                    ((Boolean)source.getItem(itemId).getItemProperty(TableProperty.DISABLED).getValue()).booleanValue(),
1190                    ((Boolean)source.getItem(itemId).getItemProperty(TableProperty.NEWUSER).getValue()).booleanValue());
1191
1192            }
1193
1194        });
1195        addGeneratedColumn(TableProperty.LastLogin, new ColumnGenerator() {
1196
1197            private static final long serialVersionUID = -6781906011584975559L;
1198
1199            public Object generateCell(Table source, Object itemId, Object columnId) {
1200
1201                long lastLogin = ((Long)source.getItem(itemId).getItemProperty(
1202                    TableProperty.LastLogin).getValue()).longValue();
1203                return lastLogin == 0L
1204                ? CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_NEVER_LOGGED_IN_0)
1205                : CmsDateUtil.getDateTime(new Date(lastLogin), DateFormat.SHORT, A_CmsUI.get().getLocale());
1206
1207            }
1208
1209        });
1210
1211        addGeneratedColumn(TableProperty.Created, new ColumnGenerator() {
1212
1213            private static final long serialVersionUID = -6781906011584975559L;
1214
1215            public Object generateCell(Table source, Object itemId, Object columnId) {
1216
1217                long created = ((Long)source.getItem(itemId).getItemProperty(
1218                    TableProperty.Created).getValue()).longValue();
1219                return created == 0L
1220                ? ""
1221                : CmsDateUtil.getDateTime(new Date(created), DateFormat.SHORT, A_CmsUI.get().getLocale());
1222
1223            }
1224
1225        });
1226
1227        setItemDescriptionGenerator(new ItemDescriptionGenerator() {
1228
1229            private static final long serialVersionUID = 7367011213487089661L;
1230
1231            public String generateDescription(Component source, Object itemId, Object propertyId) {
1232
1233                if (TableProperty.STATUS.equals(propertyId)) {
1234
1235                    return getStatusHelp(
1236                        (CmsUser)itemId,
1237                        ((Boolean)((Table)source).getItem(itemId).getItemProperty(
1238                            TableProperty.DISABLED).getValue()).booleanValue(),
1239                        ((Boolean)((Table)source).getItem(itemId).getItemProperty(
1240                            TableProperty.NEWUSER).getValue()).booleanValue());
1241                }
1242                return null;
1243            }
1244        });
1245        setVisibleColumns(
1246            TableProperty.STATUS,
1247            TableProperty.Name,
1248            TableProperty.FullName,
1249            TableProperty.OU,
1250            TableProperty.LastLogin,
1251            TableProperty.Created);
1252    }
1253
1254    /**
1255     * Visibility which is only active if user is in right ou.<p>
1256     *
1257     * @param userId to be checked
1258     * @return CmsMenuItemVisibilityMode
1259     */
1260    protected boolean onlyVisibleForOU(CmsUUID userId) {
1261
1262        try {
1263            if (m_app.isOUManagable(m_cms.readUser(userId).getOuFqn())) {
1264                return true;
1265            }
1266        } catch (CmsException e) {
1267            //
1268        }
1269        return false;
1270    }
1271
1272    /**
1273     * Checks if all selected items are editable.<p>
1274     *
1275     * @param context items
1276     * @return CmsMenuItemVisibilityMode
1277     */
1278    protected CmsMenuItemVisibilityMode onlyVisibleForOU(Set<String> context) {
1279
1280        for (String id : context) {
1281            if (!onlyVisibleForOU(new CmsUUID(id))) {
1282                return CmsMenuItemVisibilityMode.VISIBILITY_INACTIVE;
1283            }
1284        }
1285        return CmsMenuItemVisibilityMode.VISIBILITY_ACTIVE;
1286    }
1287
1288    /**
1289     * Opens the user info dialog.<p>
1290     *
1291     * @param id of user
1292     * @throws CmsException exception
1293     */
1294    protected void openInfoDialog(CmsUUID id) throws CmsException {
1295
1296        CmsUserInfoDialog.showUserInfo(m_cms.readUser(id));
1297    }
1298
1299    /**
1300     * Checks value of table and sets it new if needed:<p>
1301     * if multiselect: new itemId is in current Value? -> no change of value<p>
1302     * no multiselect and multiselect, but new item not selected before: set value to new item<p>
1303     *
1304     * @param itemId if of clicked item
1305     */
1306    void changeValueIfNotMultiSelect(Object itemId) {
1307
1308        @SuppressWarnings("unchecked")
1309        Set<String> value = (Set<String>)getValue();
1310        if (value == null) {
1311            select(itemId);
1312        } else if (!value.contains(itemId)) {
1313            setValue(null);
1314            select(itemId);
1315        }
1316    }
1317
1318    /**
1319     * Returns the available menu entries.<p>
1320     *
1321     * @return the menu entries
1322     */
1323    List<I_CmsSimpleContextMenuEntry<Set<String>>> getMenuEntries() {
1324
1325        if (m_menuEntries == null) {
1326            m_menuEntries = new ArrayList<I_CmsSimpleContextMenuEntry<Set<String>>>();
1327            m_menuEntries.add(new EntryInfo());
1328            m_menuEntries.add(new EntryEdit());
1329            m_menuEntries.add(new EntryEditRole());
1330            m_menuEntries.add(new EntryEditGroup());
1331            m_menuEntries.add(new EntryAddInfos());
1332            m_menuEntries.add(new EntryShowResources());
1333            m_menuEntries.add(new EntrySwitchUser());
1334            m_menuEntries.add(new EntryRemoveFromGroup());
1335            m_menuEntries.add(new EntryMoveOU());
1336            m_menuEntries.add(new EntryDelete());
1337            m_menuEntries.add(new EntryKillSession());
1338            m_menuEntries.add(new EntryEnable());
1339        }
1340        return m_menuEntries;
1341    }
1342
1343    /**
1344     * Returns status message.
1345     *
1346     * @param user CmsUser
1347     * @param disabled boolean
1348     * @param newUser boolean
1349     * @return String
1350     */
1351    String getStatus(CmsUser user, boolean disabled, boolean newUser) {
1352
1353        if (disabled) {
1354            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DISABLED_0);
1355        }
1356        if (newUser) {
1357            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_INACTIVE_0);
1358        }
1359        if (OpenCms.getLoginManager().isUserTempDisabled(user.getName())) {
1360            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_UNLOCK_USER_STATUS_0);
1361        }
1362        if (isUserPasswordReset(user)) {
1363            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_PASSWORT_RESET_0);
1364        }
1365        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_ACTIVE_0);
1366    }
1367
1368    /**
1369     * Returns status help message.
1370     *
1371     * @param user CmsUser
1372     * @param disabled boolean
1373     * @param newUser boolean
1374     * @return String
1375     */
1376    String getStatusHelp(CmsUser user, boolean disabled, boolean newUser) {
1377
1378        if (disabled) {
1379            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_DISABLED_HELP_0);
1380        }
1381        if (newUser) {
1382            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_INACTIVE_HELP_0);
1383        }
1384        if (isUserPasswordReset(user)) {
1385            return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_PASSWORT_RESET_HELP_0);
1386        }
1387        long lastLogin = user.getLastlogin();
1388        return CmsVaadinUtils.getMessageText(
1389            Messages.GUI_USERMANAGEMENT_USER_ACTIVE_HELP_1,
1390            CmsDateUtil.getDateTime(new Date(lastLogin), DateFormat.SHORT, A_CmsUI.get().getLocale()));
1391    }
1392
1393    /**
1394     * Integer value for status (allows to sort column).
1395     *
1396     * @param disabled boolean
1397     * @param newUser boolean
1398     * @return Integer
1399     */
1400    Integer getStatusInt(boolean disabled, boolean newUser) {
1401
1402        if (disabled) {
1403            return new Integer(2);
1404        }
1405        if (newUser) {
1406            return new Integer(1);
1407        }
1408        return new Integer(0);
1409    }
1410
1411    /**
1412     * Is the user password reset?
1413     *
1414     * @param user User to check
1415     * @return boolean
1416     */
1417    boolean isUserPasswordReset(CmsUser user) {
1418
1419        if (USER_PASSWORD_STATUS.containsKey(user.getId())) { //Check if user was checked before
1420            if (!USER_PASSWORD_STATUS.get(user.getId()).booleanValue()) { // was false before, false->true is never done without changing map
1421                return false;
1422            }
1423            if (m_checkedUserPasswordReset.contains(user.getId())) { //was true before, true->false happens when user resets password. Only check one time per table load.
1424                return true; //Set gets flushed on reloading table
1425            }
1426        }
1427        CmsUser currentUser = user;
1428        if (user.getAdditionalInfo().size() < 3) {
1429
1430            try {
1431                currentUser = m_cms.readUser(user.getId());
1432            } catch (CmsException e) {
1433                LOG.error("Can not read user", e);
1434            }
1435        }
1436        if (currentUser.getAdditionalInfo(CmsUserSettings.ADDITIONAL_INFO_PASSWORD_RESET) != null) {
1437            USER_PASSWORD_STATUS.put(currentUser.getId(), new Boolean(true));
1438            m_checkedUserPasswordReset.add(currentUser.getId());
1439            return true;
1440        }
1441        USER_PASSWORD_STATUS.put(currentUser.getId(), new Boolean(false));
1442        return false;
1443    }
1444
1445    /**
1446     * Gets list of indirect users to show.<p>
1447     *
1448     * @param allUsers all users
1449     * @param directUsers direct user
1450     * @return indirect user
1451     */
1452    private List<CmsUser> getAllowedIndirects(List<CmsUser> allUsers, List<CmsUser> directUsers) {
1453
1454        List<CmsUser> res = new ArrayList<CmsUser>();
1455        for (CmsUser u : allUsers) {
1456            if (!directUsers.contains(u)) {
1457                res.add(u);
1458            }
1459        }
1460
1461        return res;
1462    }
1463
1464    /**
1465     * Gets CmsObject.<p>
1466     *
1467     * @return cmsobject
1468     */
1469    private CmsObject getCmsObject() {
1470
1471        CmsObject cms;
1472        try {
1473            cms = OpenCms.initCmsObject(A_CmsUI.getCmsObject());
1474            //m_cms.getRequestContext().setSiteRoot("");
1475        } catch (CmsException e) {
1476            cms = A_CmsUI.getCmsObject();
1477        }
1478        return cms;
1479    }
1480
1481    /**
1482     * Checks is it is allown for the current user to see given user.<p>
1483     *
1484     * @param user to be checked
1485     * @return boolean
1486     * @throws CmsException exception
1487     */
1488    private boolean isAllowedUser(CmsUser user) throws CmsException {
1489
1490        if (user.getOuFqn().startsWith(m_cms.getRequestContext().getOuFqn())) {
1491            return true;
1492        }
1493        return OpenCms.getRoleManager().getRolesOfUser(m_cms, user.getName(), m_ou, true, true, false).size() > 0;
1494    }
1495
1496    /**
1497     * Sets all user, including indirect user for roles.<p>
1498     *
1499     * @param directs direct user
1500     * @throws CmsException exception
1501     */
1502    private void setAllUsers(List<CmsUser> directs) throws CmsException {
1503
1504        m_fullyLoaded = true;
1505        if (m_type.equals(CmsOuTreeType.ROLE)) {
1506            m_users = OpenCms.getRoleManager().getUsersOfRole(
1507                m_cms,
1508                CmsRole.valueOfRoleName(m_group).forOrgUnit(m_ou),
1509                true,
1510                false);
1511        } else if (m_type.equals(CmsOuTreeType.USER)) {
1512            m_users = m_app.getUsersWithoutAdditionalInfo(m_cms, m_type, m_ou, true);
1513
1514        }
1515        Iterator<CmsUser> it = m_users.iterator();
1516        while (it.hasNext()) {
1517            CmsUser u = it.next();
1518            if (!isAllowedUser(u)) {
1519                m_blackList.add(u);
1520            }
1521        }
1522        m_indirects.addAll(getAllowedIndirects(m_users, directs));
1523    }
1524
1525    /**
1526     * Sets the visibility of table and empty info panel.<p>     *
1527     */
1528    private void setVisibilities() {
1529
1530        setVisible(size() > 0);
1531        if (m_emptyLayout != null) {
1532            m_emptyLayout.setVisible(size() == 0);
1533        }
1534    }
1535}