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.main.CmsException;
033import org.opencms.security.CmsAccessControlEntry;
034import org.opencms.security.CmsPrincipal;
035import org.opencms.security.I_CmsPrincipal;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.FontOpenCms;
038import org.opencms.ui.apps.user.CmsAccountsApp;
039import org.opencms.ui.components.CmsResourceInfo;
040import org.opencms.ui.components.OpenCmsTheme;
041import org.opencms.util.CmsStringUtil;
042import org.opencms.util.CmsUUID;
043import org.opencms.workplace.commons.Messages;
044
045import java.util.Iterator;
046import java.util.List;
047import java.util.Map;
048
049import com.vaadin.v7.data.Item;
050import com.vaadin.v7.data.util.IndexedContainer;
051import com.vaadin.v7.data.util.filter.Or;
052import com.vaadin.v7.data.util.filter.SimpleStringFilter;
053import com.vaadin.ui.Button;
054import com.vaadin.ui.Button.ClickEvent;
055import com.vaadin.ui.Button.ClickListener;
056import com.vaadin.ui.CssLayout;
057import com.vaadin.v7.ui.Label;
058import com.vaadin.v7.ui.Table;
059import com.vaadin.v7.ui.VerticalLayout;
060
061/**
062 * Table for the ACE Entries.<p>
063 */
064public class CmsPermissionViewTable extends Table {
065
066    /**
067     * Column with FavIcon.<p>
068     */
069    class ViewColumn implements Table.ColumnGenerator {
070
071        /**vaadin serial id.*/
072        private static final long serialVersionUID = -3772456970393398685L;
073
074        /**
075         * @see com.vaadin.ui.Table.ColumnGenerator#generateCell(com.vaadin.ui.Table, java.lang.Object, java.lang.Object)
076         */
077        public Object generateCell(Table source, Object itemId, Object columnId) {
078
079            return m_dialog.buildPermissionEntryForm((CmsAccessControlEntry)itemId, m_editable, false, null);
080        }
081    }
082
083    /**vaadin serial id. */
084    private static final long serialVersionUID = -2759899760528588890L;
085
086    /**View column.*/
087    private static final String PROP_VIEW = "view";
088
089    /**Name column (hidden, only used for filter). */
090    private static final String PROP_NAME = "name";
091
092    /**editable. */
093    boolean m_editable;
094
095    /**Data container. */
096    IndexedContainer m_container;
097
098    /**Calling dialog. */
099    CmsPermissionDialog m_dialog;
100
101    /**
102     * public constructor.<p>
103     *
104     * @param cms CmsObject
105     * @param entries to be shown
106     * @param editable boolean
107     * @param showRes with resources?
108     * @param parents parents
109     * @param dialog calling dialog
110     */
111    public CmsPermissionViewTable(
112        CmsObject cms,
113        List<CmsAccessControlEntry> entries,
114        boolean editable,
115        boolean showRes,
116        Map<CmsUUID, String> parents,
117        CmsPermissionDialog dialog) {
118
119        m_editable = editable;
120        m_dialog = dialog;
121        setHeight("450px");
122        setWidth("100%");
123        setPageLength(4);
124        setColumnHeaderMode(ColumnHeaderMode.HIDDEN);
125        m_container = new IndexedContainer();
126        m_container.addContainerProperty(PROP_VIEW, VerticalLayout.class, null);
127        m_container.addContainerProperty(PROP_NAME, String.class, "");
128        setCellStyleGenerator(new CellStyleGenerator() {
129
130            private static final long serialVersionUID = 3943163625035784161L;
131
132            public String getStyle(Table source, Object itemId, Object propertyId) {
133
134                return " " + OpenCmsTheme.TABLE_CONST_COLOR;
135            }
136
137        });
138
139        Iterator<CmsAccessControlEntry> i = entries.iterator();
140        boolean hasEntries = i.hasNext();
141
142        if (hasEntries) {
143            // list all entries
144            while (i.hasNext()) {
145                CmsAccessControlEntry curEntry = i.next();
146
147                CmsPermissionView view = m_dialog.buildPermissionEntryForm(
148                    curEntry,
149                    m_editable,
150                    false,
151                    showRes ? curEntry.getResource() : null);
152                Item item = m_container.addItem(view);
153                item.getItemProperty(PROP_VIEW).setValue(
154                    getLayoutFromEntry(cms, curEntry, view, showRes ? parents.get(curEntry.getResource()) : null));
155                item.getItemProperty(PROP_NAME).setValue(view.getPrincipalName());
156            }
157        }
158
159        setContainerDataSource(m_container);
160        setVisibleColumns(PROP_VIEW);
161    }
162
163    /**
164     * Filter the table according to string.<p>
165     *
166     * @param search string
167     */
168    public void filterTable(String search) {
169
170        m_container.removeAllContainerFilters();
171        if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(search)) {
172            m_container.addContainerFilter(new Or(new SimpleStringFilter(PROP_NAME, search, true, false)));
173        }
174    }
175
176    /**
177     * Makes item for table.<p>
178     *
179     * @param cms CmsObject
180     * @param entry ACE
181     * @param view permission table
182     * @param resPath parentResource (or null)
183     * @return VerticalLayout
184     */
185    private VerticalLayout getLayoutFromEntry(
186        CmsObject cms,
187        CmsAccessControlEntry entry,
188        final CmsPermissionView view,
189        String resPath) {
190
191        VerticalLayout res = new VerticalLayout();
192        res.setSpacing(false);
193        I_CmsPrincipal principal = null;
194        try {
195            principal = CmsPrincipal.readPrincipalIncludingHistory(cms, entry.getPrincipal());
196
197        } catch (CmsException e) {
198            principal = new CmsGroup(entry.getPrincipal(), null, "", "", 0);
199
200        }
201        if (principal != null) {
202            CmsResourceInfo info = CmsAccountsApp.getPrincipalInfo(principal);
203            if (view.isEditable()) {
204                CssLayout cssl = new CssLayout();
205                Button removeButton = new Button(FontOpenCms.TRASH_SMALL);
206                removeButton.addStyleName("borderless o-toolbar-button o-resourceinfo-toolbar o-toolbar-icon-visible");
207                removeButton.addClickListener(new ClickListener() {
208
209                    private static final long serialVersionUID = -6112693137800596485L;
210
211                    public void buttonClick(ClickEvent event) {
212
213                        view.deletePermissionSet();
214
215                    }
216
217                });
218                cssl.addComponent(removeButton);
219                info.setButtonWidget(cssl);
220            }
221            res.addComponent(info);
222            if (resPath != null) {
223                Label resLabel = new Label(
224                    CmsVaadinUtils.getMessageText(Messages.GUI_PERMISSION_INHERITED_FROM_1, resPath));
225                resLabel.addStyleName("o-report");
226                res.addComponent(resLabel);
227            }
228        }
229        res.addComponent(view);
230        return res;
231    }
232}