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.dialogs.permissions;
029
030import org.opencms.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsLog;
035import org.opencms.main.OpenCms;
036import org.opencms.security.CmsAccessControlEntry;
037import org.opencms.security.CmsRole;
038import org.opencms.security.I_CmsPrincipal;
039import org.opencms.ui.A_CmsUI;
040import org.opencms.ui.CmsCssIcon;
041import org.opencms.ui.CmsVaadinUtils;
042import org.opencms.ui.FontOpenCms;
043import org.opencms.ui.apps.Messages;
044import org.opencms.ui.components.CmsBasicDialog;
045import org.opencms.ui.components.OpenCmsTheme;
046import org.opencms.ui.dialogs.CmsEmbeddedDialogContext;
047import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect.I_PrincipalSelectHandler;
048import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect.WidgetType;
049
050import java.util.ArrayList;
051import java.util.List;
052import java.util.Map;
053
054import org.apache.commons.logging.Log;
055
056import com.vaadin.server.FontIcon;
057import com.vaadin.ui.Button;
058import com.vaadin.ui.Button.ClickEvent;
059import com.vaadin.ui.Button.ClickListener;
060import com.vaadin.ui.UI;
061import com.vaadin.ui.Window;
062import com.vaadin.ui.themes.ValoTheme;
063import com.vaadin.v7.data.Item;
064import com.vaadin.v7.data.Property.ValueChangeEvent;
065import com.vaadin.v7.data.Property.ValueChangeListener;
066import com.vaadin.v7.data.util.IndexedContainer;
067import com.vaadin.v7.event.FieldEvents.TextChangeEvent;
068import com.vaadin.v7.event.FieldEvents.TextChangeListener;
069import com.vaadin.v7.ui.ComboBox;
070import com.vaadin.v7.ui.HorizontalLayout;
071import com.vaadin.v7.ui.Label;
072import com.vaadin.v7.ui.TextField;
073import com.vaadin.v7.ui.VerticalLayout;
074
075/**
076 * Class for the dialog to show the principal table.<p>
077 */
078public class CmsPrincipalSelectDialog extends CmsBasicDialog {
079
080    /** Parameter key OU. */
081    public static String PARAM_OU = "ou";
082
083    /** Parameter key widget type. */
084    public static String PARAM_TYPE = "type";
085
086    /** Parameter key start view type. */
087    public static String PARAM_START_TYPE = "starttype";
088
089    /** Parameter key real groups only. */
090    public static String PARAM_REAL_ONLY = "realonly";
091
092    /** The dialog id. */
093    public static final String DIALOG_ID = "principalselect";
094
095    /**vaadin serial id.*/
096    private static final long serialVersionUID = 4650407086145654695L;
097
098    /** The log object for this class. */
099    private static final Log LOG = CmsLog.getLog(CmsPrincipalSelectDialog.class);
100
101    /**Icon property. */
102    private static String ID_ICON = "icon";
103
104    /**Caption property. */
105    private static String ID_CAPTION = "caption";
106
107    /**Descrpition property. */
108    private static String ID_DESC = "desc";
109
110    /**Organizational unit property. */
111    private static String ID_OU = "ou";
112
113    /**CmsObject.*/
114    CmsObject m_cms;
115
116    /**Calling vaadin component.*/
117    I_CmsPrincipalSelect m_selectField;
118
119    /**Vaadin component.*/
120    private Button m_closeButton;
121
122    /**Vaadin component.*/
123    ComboBox m_typeCombo;
124
125    /**Vaadin component.*/
126    private TextField m_tableFilter;
127
128    /**Vaadin component.*/
129    CmsPrincipalTable m_table;
130
131    /**Ou name. */
132    private String m_ou;
133
134    /**Type to display. */
135    WidgetType m_type;
136
137    /**Only show real principals? */
138    private boolean m_realOnly;
139
140    /**Vaadin component.*/
141    private ComboBox m_ouCombo;
142
143    /** The principal select handler. */
144    private I_PrincipalSelectHandler m_selectHandler;
145
146    /**
147     * public constructor.<p>
148     *
149     * @param cmsPrincipalSelect calling vaadin component
150     * @param ou the current OU
151     * @param window window to be closed after finishing
152     * @param widgetType type of principal to be shown
153     * @param realOnly true, only show real principals
154     * @param defaultView default mode to open
155     */
156    public CmsPrincipalSelectDialog(
157        I_CmsPrincipalSelect cmsPrincipalSelect,
158        String ou,
159        final Window window,
160        WidgetType widgetType,
161        boolean realOnly,
162        WidgetType defaultView) {
163
164        this(cmsPrincipalSelect, ou, window, widgetType, realOnly, defaultView, true);
165
166    }
167
168    /**
169     * public constructor.<p>
170     *
171     * @param cmsPrincipalSelect calling vaadin component
172     * @param ou the current OU
173     * @param window window to be closed after finishing
174     * @param widgetType type of principal to be shown
175     * @param realOnly true, only show real principals
176     * @param defaultView default mode to open
177     * @param includeWebOus boolean
178     */
179    public CmsPrincipalSelectDialog(
180        I_CmsPrincipalSelect cmsPrincipalSelect,
181        String ou,
182        final Window window,
183        WidgetType widgetType,
184        boolean realOnly,
185        WidgetType defaultView,
186        boolean includeWebOus) {
187
188        m_ou = ou;
189        m_type = widgetType;
190        m_realOnly = realOnly;
191        try {
192            m_cms = A_CmsUI.getCmsObject();
193
194            m_selectField = cmsPrincipalSelect;
195
196            m_ouCombo = CmsVaadinUtils.getOUComboBox(m_cms, m_cms.getRequestContext().getOuFqn(), null, includeWebOus);
197            m_ouCombo.select(m_ou);
198
199            IndexedContainer data;
200
201            data = getContainerForType(defaultView, m_realOnly, (String)m_ouCombo.getValue());
202            m_table = new CmsPrincipalTable(this, data, ID_ICON, ID_CAPTION, ID_DESC, ID_OU);
203            m_table.setColumnHeader(
204                ID_CAPTION,
205                CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GROUP_NAME_0));
206            m_table.setColumnHeader(
207                ID_DESC,
208                CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GROUP_DESCRIPTION_0));
209            m_table.setColumnHeader(ID_OU, CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GROUP_OU_0));
210
211            m_tableFilter = new TextField();
212            m_tableFilter.setIcon(FontOpenCms.FILTER);
213            m_tableFilter.setInputPrompt(
214                Messages.get().getBundle(UI.getCurrent().getLocale()).key(Messages.GUI_EXPLORER_FILTER_0));
215            m_tableFilter.addStyleName(ValoTheme.TEXTFIELD_INLINE_ICON);
216            m_tableFilter.setWidth("200px");
217            m_tableFilter.addTextChangeListener(new TextChangeListener() {
218
219                private static final long serialVersionUID = 1L;
220
221                public void textChange(TextChangeEvent event) {
222
223                    m_table.filterTable(event.getText());
224                }
225            });
226            VerticalLayout vl = new VerticalLayout();
227            vl.setSpacing(true);
228            HorizontalLayout hl = new HorizontalLayout();
229            hl.setSpacing(true);
230            hl.setWidth("100%");
231            m_typeCombo = new ComboBox();
232            Label space = new Label();
233
234            hl.addComponent(m_typeCombo);
235            hl.addComponent(m_ouCombo);
236            hl.addComponent(space);
237            hl.addComponent(m_tableFilter);
238            hl.setExpandRatio(space, 1);
239            hl.setComponentAlignment(m_ouCombo, com.vaadin.ui.Alignment.MIDDLE_CENTER);
240            vl.addComponent(hl);
241            vl.addComponent(m_table);
242
243            if (!OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ACCOUNT_MANAGER)) {
244                m_ouCombo.setValue(m_cms.getRequestContext().getOuFqn());
245                m_ouCombo.setEnabled(false);
246                m_typeCombo.setValue(WidgetType.groupwidget);
247                m_typeCombo.setEnabled(false);
248            }
249
250            setContent(vl);
251        } catch (CmsException e) {
252            LOG.error("Can't read principals", e);
253        }
254        m_closeButton = new Button(CmsVaadinUtils.messageClose());
255        addButton(m_closeButton);
256
257        m_closeButton.addClickListener(new ClickListener() {
258
259            private static final long serialVersionUID = -3702402223196220788L;
260
261            public void buttonClick(ClickEvent event) {
262
263                window.close();
264
265            }
266        });
267        m_ouCombo.addValueChangeListener(new ValueChangeListener() {
268
269            private static final long serialVersionUID = -5035831853626955191L;
270
271            public void valueChange(ValueChangeEvent event) {
272
273                initTable((WidgetType)m_typeCombo.getValue());
274
275            }
276        });
277        m_typeCombo.addValueChangeListener(new ValueChangeListener() {
278
279            private static final long serialVersionUID = 645817336220889132L;
280
281            public void valueChange(ValueChangeEvent event) {
282
283                initTable((WidgetType)m_typeCombo.getValue());
284                if (m_selectField != null) {
285                    if (WidgetType.groupwidget.equals(m_typeCombo.getValue())) {
286                        m_selectField.setType(I_CmsPrincipal.PRINCIPAL_GROUP);
287                    } else if (WidgetType.userwidget.equals(m_typeCombo.getValue())) {
288                        m_selectField.setType(I_CmsPrincipal.PRINCIPAL_USER);
289                    }
290                }
291            }
292
293        });
294        initTypeCombo(defaultView);
295    }
296
297    /**
298     * Opens the principal select dialog within an embedded dialog context.<p>
299     *
300     * @param dialogContext the dialog context
301     * @param params the request parameters
302     */
303    public static void openEmbeddedDialog(final CmsEmbeddedDialogContext dialogContext, Map<String, String[]> params) {
304
305        openEmbeddedDialog(dialogContext, params, true);
306    }
307
308    /**
309     * Opens the principal select dialog within an embedded dialog context.<p>
310     *
311     * @param dialogContext the dialog context
312     * @param params the request parameters
313     * @param includeWebOus include WebOu?
314     */
315    public static void openEmbeddedDialog(
316        final CmsEmbeddedDialogContext dialogContext,
317        Map<String, String[]> params,
318        boolean includeWebOus) {
319
320        String[] param = params.get(PARAM_OU);
321        String ou;
322        if ((param != null) && (param.length >= 1)) {
323            ou = param[0];
324        } else {
325            ou = dialogContext.getCms().getRequestContext().getCurrentUser().getOuFqn();
326        }
327        boolean realOnly;
328        param = params.get(PARAM_REAL_ONLY);
329        if ((param != null) && (param.length >= 1)) {
330            realOnly = Boolean.parseBoolean(param[0]);
331        } else {
332            realOnly = true;
333        }
334        WidgetType type = WidgetType.groupwidget;
335        param = params.get(PARAM_TYPE);
336        if ((param != null) && (param.length >= 1)) {
337            try {
338                type = WidgetType.valueOf(param[0]);
339            } catch (Exception e) {
340                // ignore
341            }
342        }
343        WidgetType startType = null;
344        param = params.get(PARAM_START_TYPE);
345        if ((param != null) && (param.length >= 1)) {
346            try {
347                startType = WidgetType.valueOf(param[0]);
348            } catch (Exception e) {
349                // ignore
350            }
351        }
352        if (startType == null) {
353            startType = type != WidgetType.principalwidget ? type : WidgetType.groupwidget;
354        }
355        Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
356        dialogContext.setWindow(window);
357        CmsPrincipalSelectDialog dialog = new CmsPrincipalSelectDialog(
358            null,
359            ou,
360            window,
361            type,
362            realOnly,
363            startType,
364            includeWebOus);
365        dialog.setSelectHandler(new I_PrincipalSelectHandler() {
366
367            public void onPrincipalSelect(String principalType, String principalName) {
368
369                dialogContext.setPrincipal(principalName);
370            }
371        });
372        window.setCaption(
373            CmsVaadinUtils.getMessageText(
374                org.opencms.workplace.commons.Messages.GUI_PRINCIPALSELECTION_LIST_ACTION_SELECT_NAME_0));
375        window.setContent(dialog);
376        A_CmsUI.get().addWindow(window);
377    }
378
379    /**
380     * Selects a principal and closes the dialog.<p>
381     *
382     * @param value the principal which was clicked
383     */
384    public void select(I_CmsPrincipal value) {
385
386        if (m_selectField != null) {
387            m_selectField.handlePrincipal(value);
388        }
389        if (m_selectHandler != null) {
390            m_selectHandler.onPrincipalSelect(
391                value.isGroup() ? I_CmsPrincipal.PRINCIPAL_GROUP : I_CmsPrincipal.PRINCIPAL_USER,
392                value.getName());
393        }
394        m_closeButton.click();
395    }
396
397    /**
398     * En/Disables the ou combo box.<p>
399     *
400     * @param enabled enabled boolean
401     */
402    public void setOuComboBoxEnabled(boolean enabled) {
403
404        m_ouCombo.setEnabled(enabled);
405    }
406
407    /**
408     * Sets the principal select handler.<p>
409     *
410     * @param selectHandler the principal select handler
411     */
412    public void setSelectHandler(I_PrincipalSelectHandler selectHandler) {
413
414        m_selectHandler = selectHandler;
415    }
416
417    /**
418     * Init table.<p>
419     * @param type WidgetType to initialize
420     */
421    void initTable(WidgetType type) {
422
423        IndexedContainer data;
424        try {
425            data = getContainerForType(type, m_realOnly, (String)m_ouCombo.getValue());
426            m_table.updateContainer(data);
427            m_tableFilter.setValue("");
428        } catch (CmsException e) {
429            LOG.error("Can't read principals", e);
430        }
431    }
432
433    /**
434     * Returns the container for the currently selected Principal group.<p>
435     *
436     * @param type to be shown
437     * @param realOnly true->get only real principals
438     * @param ou ou
439     * @return indexed container
440     * @throws CmsException exception
441     */
442    private IndexedContainer getContainerForType(WidgetType type, boolean realOnly, String ou) throws CmsException {
443
444        IndexedContainer res = null;
445        List<FontIcon> icon = new ArrayList<FontIcon>();
446        if (!OpenCms.getRoleManager().hasRole(m_cms, CmsRole.ACCOUNT_MANAGER)) {
447            List<CmsGroup> groups = OpenCms.getOrgUnitManager().getGroups(
448                m_cms,
449                m_cms.getRequestContext().getOuFqn(),
450                false);
451            return CmsVaadinUtils.getPrincipalContainer(
452                A_CmsUI.getCmsObject(),
453                groups,
454                ID_CAPTION,
455                ID_DESC,
456                ID_ICON,
457                ID_OU,
458                OpenCmsTheme.ICON_GROUP,
459                icon);
460        }
461
462        if (type.equals(WidgetType.groupwidget) | type.equals(WidgetType.principalwidget)) {
463            List<CmsGroup> groups = OpenCms.getRoleManager().getManageableGroups(m_cms, ou, false);
464            if (!realOnly) {
465                groups.add(
466                    0,
467                    new CmsGroup(
468                        CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID,
469                        null,
470                        CmsVaadinUtils.getMessageText(org.opencms.workplace.commons.Messages.GUI_LABEL_ALLOTHERS_0),
471                        CmsVaadinUtils.getMessageText(
472                            org.opencms.workplace.commons.Messages.GUI_DESCRIPTION_ALLOTHERS_0),
473                        0));
474                icon.add(new CmsCssIcon(OpenCmsTheme.ICON_PRINCIPAL_ALL));
475                if (OpenCms.getRoleManager().hasRole(m_cms, CmsRole.VFS_MANAGER)) {
476                    groups.add(
477                        0,
478                        new CmsGroup(
479                            CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID,
480                            null,
481                            CmsVaadinUtils.getMessageText(
482                                org.opencms.workplace.commons.Messages.GUI_LABEL_OVERWRITEALL_0),
483                            CmsVaadinUtils.getMessageText(
484                                org.opencms.workplace.commons.Messages.GUI_DESCRIPTION_OVERWRITEALL_0),
485                            0));
486                    icon.add(0, new CmsCssIcon(OpenCmsTheme.ICON_PRINCIPAL_OVERWRITE));
487                }
488            }
489
490            res = CmsVaadinUtils.getPrincipalContainer(
491                A_CmsUI.getCmsObject(),
492                groups,
493                ID_CAPTION,
494                ID_DESC,
495                ID_ICON,
496                ID_OU,
497                OpenCmsTheme.ICON_GROUP,
498                icon);
499        }
500        if (type.equals(WidgetType.userwidget)) {
501            List<CmsUser> users = OpenCms.getRoleManager().getManageableUsers(m_cms, ou, false, true);
502            if (!realOnly) {
503                CmsUser user = new CmsUser(
504                    CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID,
505                    CmsVaadinUtils.getMessageText(org.opencms.workplace.commons.Messages.GUI_LABEL_ALLOTHERS_0),
506                    "",
507                    "",
508                    "",
509                    "",
510                    0,
511                    0,
512                    0,
513                    null);
514                user.setDescription(
515                    CmsVaadinUtils.getMessageText(org.opencms.workplace.commons.Messages.GUI_DESCRIPTION_ALLOTHERS_0));
516                users.add(0, user);
517                icon.add(new CmsCssIcon(OpenCmsTheme.ICON_PRINCIPAL_ALL));
518                if (OpenCms.getRoleManager().hasRole(m_cms, CmsRole.VFS_MANAGER)) {
519                    user = new CmsUser(
520                        CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID,
521                        CmsVaadinUtils.getMessageText(org.opencms.workplace.commons.Messages.GUI_LABEL_OVERWRITEALL_0),
522                        "",
523                        "",
524                        "",
525                        "",
526                        0,
527                        0,
528                        0,
529                        null);
530                    user.setDescription(
531                        CmsVaadinUtils.getMessageText(
532                            org.opencms.workplace.commons.Messages.GUI_DESCRIPTION_OVERWRITEALL_0));
533                    users.add(0, user);
534                    icon.add(0, new CmsCssIcon(OpenCmsTheme.ICON_PRINCIPAL_OVERWRITE));
535                }
536            }
537            res = CmsVaadinUtils.getPrincipalContainer(
538                A_CmsUI.getCmsObject(),
539                users,
540                ID_CAPTION,
541                ID_DESC,
542                ID_ICON,
543                ID_OU,
544                OpenCmsTheme.ICON_USER,
545                icon);
546        }
547
548        return res;
549    }
550
551    /**
552     * Init ComboBox for choosing type of principal.<p>
553     * @param defaultType Default mode to open
554     */
555    private void initTypeCombo(WidgetType defaultType) {
556
557        IndexedContainer container = new IndexedContainer();
558
559        container.addContainerProperty("caption", String.class, "");
560
561        Item item = container.addItem(WidgetType.groupwidget);
562        item.getItemProperty("caption").setValue(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_GROUP_0));
563
564        item = container.addItem(WidgetType.userwidget);
565        item.getItemProperty("caption").setValue(CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_0));
566
567        m_typeCombo.setContainerDataSource(container);
568        m_typeCombo.select(defaultType);
569        m_typeCombo.setEnabled(m_type.equals(WidgetType.principalwidget) | (m_type == null));
570        m_typeCombo.setItemCaptionPropertyId("caption");
571        m_typeCombo.setNullSelectionAllowed(false);
572        m_typeCombo.setNewItemsAllowed(false);
573
574    }
575
576}