001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (https://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: https://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: https://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.main.CmsException;
033import org.opencms.main.OpenCms;
034import org.opencms.security.CmsAccessControlEntry;
035import org.opencms.security.CmsPrincipal;
036import org.opencms.security.CmsRole;
037import org.opencms.security.I_CmsPrincipal;
038import org.opencms.ui.A_CmsUI;
039import org.opencms.ui.CmsVaadinUtils;
040import org.opencms.ui.FontOpenCms;
041import org.opencms.ui.apps.user.CmsAccountsApp;
042import org.opencms.ui.components.CmsBasicDialog;
043import org.opencms.ui.components.CmsBasicDialog.DialogWidth;
044import org.opencms.ui.components.CmsResourceInfo;
045import org.opencms.ui.components.OpenCmsTheme;
046import org.opencms.util.CmsStringUtil;
047import org.opencms.util.CmsUUID;
048import org.opencms.workplace.commons.Messages;
049
050import java.util.Iterator;
051import java.util.List;
052import java.util.Map;
053
054import com.vaadin.ui.Button;
055import com.vaadin.ui.Button.ClickEvent;
056import com.vaadin.ui.Button.ClickListener;
057import com.vaadin.ui.CssLayout;
058import com.vaadin.ui.Window;
059import com.vaadin.v7.data.Item;
060import com.vaadin.v7.data.util.IndexedContainer;
061import com.vaadin.v7.data.util.filter.Or;
062import com.vaadin.v7.data.util.filter.SimpleStringFilter;
063import com.vaadin.v7.ui.Label;
064import com.vaadin.v7.ui.Table;
065import com.vaadin.v7.ui.VerticalLayout;
066
067/**
068 * Table for the ACE Entries.<p>
069 */
070public class CmsPermissionViewTable extends Table {
071
072    /**
073     * Column with FavIcon.<p>
074     */
075    class ViewColumn implements Table.ColumnGenerator {
076
077        /**vaadin serial id.*/
078        private static final long serialVersionUID = -3772456970393398685L;
079
080        /**
081         * @see com.vaadin.ui.Table.ColumnGenerator#generateCell(com.vaadin.ui.Table, java.lang.Object, java.lang.Object)
082         */
083        public Object generateCell(Table source, Object itemId, Object columnId) {
084
085            return m_dialog.buildPermissionEntryForm((CmsAccessControlEntry)itemId, m_editable, false, null);
086        }
087    }
088
089    /**vaadin serial id. */
090    private static final long serialVersionUID = -2759899760528588890L;
091
092    /**View column.*/
093    private static final String PROP_VIEW = "view";
094
095    /**Name column (hidden, only used for filter). */
096    private static final String PROP_NAME = "name";
097
098    /**editable. */
099    boolean m_editable;
100
101    /**Data container. */
102    IndexedContainer m_container;
103
104    /**Calling dialog. */
105    CmsPermissionDialog m_dialog;
106
107    /**
108     * public constructor.<p>
109     *
110     * @param cms CmsObject
111     * @param entries to be shown
112     * @param editable boolean
113     * @param showRes with resources?
114     * @param parents parents
115     * @param dialog calling dialog
116     */
117    public CmsPermissionViewTable(
118        CmsObject cms,
119        List<CmsAccessControlEntry> entries,
120        boolean editable,
121        boolean showRes,
122        Map<CmsUUID, String> parents,
123        CmsPermissionDialog dialog) {
124
125        m_editable = editable;
126        m_dialog = dialog;
127        setHeight("450px");
128        setWidth("100%");
129        setPageLength(4);
130        setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
131        m_container = new IndexedContainer();
132        m_container.addContainerProperty(PROP_VIEW, VerticalLayout.class, null);
133        m_container.addContainerProperty(PROP_NAME, String.class, "");
134        setCellStyleGenerator(new CellStyleGenerator() {
135
136            private static final long serialVersionUID = 3943163625035784161L;
137
138            public String getStyle(Table source, Object itemId, Object propertyId) {
139
140                return " " + OpenCmsTheme.TABLE_CONST_COLOR;
141            }
142
143        });
144
145        Iterator<CmsAccessControlEntry> i = entries.iterator();
146        boolean hasEntries = i.hasNext();
147
148        if (hasEntries) {
149            // list all entries
150            while (i.hasNext()) {
151                CmsAccessControlEntry curEntry = i.next();
152
153                CmsPermissionView view = m_dialog.buildPermissionEntryForm(
154                    curEntry,
155                    m_editable,
156                    false,
157                    showRes ? curEntry.getResource() : null);
158                Item item = m_container.addItem(view);
159                item.getItemProperty(PROP_VIEW).setValue(
160                    getLayoutFromEntry(cms, curEntry, view, showRes ? parents.get(curEntry.getResource()) : null));
161                item.getItemProperty(PROP_NAME).setValue(view.getPrincipalName());
162            }
163        }
164
165        setContainerDataSource(m_container);
166        setVisibleColumns(PROP_VIEW);
167    }
168
169    /**
170     * Filter the table according to string.<p>
171     *
172     * @param search string
173     */
174    public void filterTable(String search) {
175
176        m_container.removeAllContainerFilters();
177        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) {
178            m_container.addContainerFilter(new Or(new SimpleStringFilter(PROP_NAME, search, true, false)));
179        }
180    }
181
182    /**
183     * Makes item for table.<p>
184     *
185     * @param cms CmsObject
186     * @param entry ACE
187     * @param view permission table
188     * @param resPath parentResource (or null)
189     * @return VerticalLayout
190     */
191    private VerticalLayout getLayoutFromEntry(
192        CmsObject cms,
193        CmsAccessControlEntry entry,
194        final CmsPermissionView view,
195        String resPath) {
196
197        VerticalLayout res = new VerticalLayout();
198        res.setSpacing(false);
199        I_CmsPrincipal principal = null;
200        boolean canShowMembers = false;
201        try {
202            principal = CmsPrincipal.readPrincipalIncludingHistory(cms, entry.getPrincipal());
203            canShowMembers = principal instanceof CmsGroup;
204        } catch (CmsException e) {
205            principal = new CmsGroup(entry.getPrincipal(), null, "", "", 0);
206
207        }
208        if (principal != null) {
209            CmsResourceInfo info = CmsAccountsApp.getPrincipalInfo(principal);
210
211            CssLayout cssl = new CssLayout();
212            cssl.addStyleName("o-permission-view-toolbar");
213            info.setButtonWidget(cssl);
214
215            if (canShowMembers) {
216                final CmsGroup group = (CmsGroup)principal;
217                if (OpenCms.getRoleManager().hasRole(cms, CmsRole.ADMINISTRATOR.forOrgUnit(group.getOuFqn()))) {
218                    Button manageButton = new Button(FontOpenCms.SEARCH_SMALL);
219                    manageButton.addStyleName(
220                        "borderless o-toolbar-button o-resourceinfo-toolbar o-toolbar-icon-visible");
221                    manageButton.addClickListener(new ClickListener() {
222
223                        private static final long serialVersionUID = -6112693137800596485L;
224
225                        public void buttonClick(ClickEvent event) {
226
227                            Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
228                            window.setContent(new CmsPermissionUserListDialog(cms, group));
229                            window.setCaption(
230                                CmsVaadinUtils.getMessageText(
231                                    org.opencms.ui.apps.Messages.GUI_USERMANAGEMENT_TOOL_NAME_0));
232                            window.setModal(true);
233                            window.setResizable(false);
234                            A_CmsUI.get().addWindow(window);
235                        }
236                    });
237                    cssl.addComponent(manageButton);
238                }
239
240            } else {
241                CmsRole role = CmsRole.valueOfId(principal.getId());
242                if (role != null) {
243                    if (OpenCms.getRoleManager().hasRole(cms, CmsRole.ADMINISTRATOR.forOrgUnit(""))) {
244                        Button manageButton = new Button(FontOpenCms.SEARCH_SMALL);
245                        manageButton.addStyleName(
246                            "borderless o-toolbar-button o-resourceinfo-toolbar o-toolbar-icon-visible");
247                        manageButton.addClickListener(new ClickListener() {
248
249                            private static final long serialVersionUID = -6112693137800596485L;
250
251                            public void buttonClick(ClickEvent event) {
252
253                                Window window = CmsBasicDialog.prepareWindow(DialogWidth.max);
254                                window.setContent(new CmsPermissionUserListDialog(cms, role));
255                                window.setCaption(
256                                    CmsVaadinUtils.getMessageText(
257                                        org.opencms.ui.apps.Messages.GUI_USERMANAGEMENT_TOOL_NAME_0));
258                                window.setModal(true);
259                                window.setResizable(false);
260                                A_CmsUI.get().addWindow(window);
261                            }
262                        });
263                        cssl.addComponent(manageButton);
264                    }
265                }
266
267            }
268            if (view.isEditable()) {
269                Button removeButton = new Button(FontOpenCms.TRASH_SMALL);
270                removeButton.addStyleName("borderless o-toolbar-button o-resourceinfo-toolbar o-toolbar-icon-visible");
271                removeButton.addClickListener(new ClickListener() {
272
273                    private static final long serialVersionUID = -6112693137800596485L;
274
275                    public void buttonClick(ClickEvent event) {
276
277                        view.deletePermissionSet();
278
279                    }
280
281                });
282                cssl.addComponent(removeButton);
283            }
284            res.addComponent(info);
285            if (resPath != null) {
286                Label resLabel = new Label(
287                    CmsVaadinUtils.getMessageText(Messages.GUI_PERMISSION_INHERITED_FROM_1, resPath));
288                resLabel.addStyleName("o-report");
289                res.addComponent(resLabel);
290            }
291        }
292        res.addComponent(view);
293        return res;
294    }
295}