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.file.CmsGroup;
031import org.opencms.file.CmsObject;
032import org.opencms.main.CmsException;
033import org.opencms.main.CmsLog;
034import org.opencms.main.OpenCms;
035import org.opencms.security.CmsPrincipal;
036import org.opencms.security.I_CmsPrincipal;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.FontOpenCms;
039import org.opencms.ui.apps.Messages;
040import org.opencms.ui.components.CmsBasicDialog;
041import org.opencms.ui.components.CmsResourceInfo;
042import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect;
043import org.opencms.ui.dialogs.permissions.CmsPrincipalSelect.WidgetType;
044import org.opencms.util.CmsStringUtil;
045import org.opencms.util.CmsUUID;
046
047import java.util.ArrayList;
048import java.util.HashSet;
049import java.util.List;
050import java.util.Set;
051
052import org.apache.commons.logging.Log;
053
054import com.vaadin.ui.Button;
055import com.vaadin.ui.Button.ClickEvent;
056import com.vaadin.ui.Button.ClickListener;
057import com.vaadin.ui.Panel;
058import com.vaadin.ui.Window;
059import com.vaadin.v7.shared.ui.label.ContentMode;
060import com.vaadin.v7.ui.HorizontalLayout;
061import com.vaadin.v7.ui.Label;
062import com.vaadin.v7.ui.VerticalLayout;
063
064/**
065 * Dialog for delete multiple principal.<p>
066 */
067public class CmsDeleteMultiplePrincipalDialog extends CmsBasicDialog {
068
069    /** The log object for this class. */
070    private static final Log LOG = CmsLog.getLog(CmsDeleteMultiplePrincipalDialog.class);
071
072    /**vaadin serial id. */
073    private static final long serialVersionUID = -1191281655158071555L;
074
075    /**The icon. */
076    private Label m_icon;
077
078    /**The icon. */
079    private Label m_icon2;
080
081    /**Vaadin component. */
082    Button m_okButton;
083
084    /**Vaadin component. */
085    CmsObject m_cms;
086
087    /**Vaadin component. */
088    private Label m_label;
089
090    /**Vaadin component. */
091    Button m_cancelButton;
092
093    /**The ids to delete. */
094    private Set<String> m_ids;
095
096    /**Ids of the user to delete. */
097    private Set<CmsUUID> m_userIDs;
098
099    /**Ids of the group to delete.*/
100    private Set<CmsUUID> m_groupIDs;
101
102    /**vaadin component. */
103    private Panel m_dependencyPanel;
104
105    /**The label shown in case of trying to delete a default user or group.*/
106    private HorizontalLayout m_label_deleteDefault;
107
108    /**Confirmation layout. */
109    private HorizontalLayout m_deleteConfirm;
110
111    /**vaadin component. */
112    private CmsPrincipalSelect m_principalSelect;
113
114    /**vaadin component. */
115    private VerticalLayout m_principalSelectLayout;
116
117    /**
118     * Public constructor.<p>
119     *
120     * @param cms CmsObeject
121     * @param context ids of principal to delete
122     * @param window window
123     * @param app
124     */
125    public CmsDeleteMultiplePrincipalDialog(CmsObject cms, Set<String> context, Window window, CmsAccountsApp app) {
126
127        init(cms, window, app);
128        boolean defaultUser = false;
129        m_ids = context;
130        m_groupIDs = new HashSet<CmsUUID>();
131        m_userIDs = new HashSet<CmsUUID>();
132        List<CmsResourceInfo> infos = new ArrayList<CmsResourceInfo>();
133        for (String id : m_ids) {
134            try {
135                CmsPrincipal principal = (CmsPrincipal)CmsPrincipal.readPrincipal(cms, new CmsUUID(id));
136                if (OpenCms.getDefaultUsers().isDefaultUser(principal.getName())
137                    || OpenCms.getDefaultUsers().isDefaultGroup(principal.getName())) {
138                    defaultUser = true;
139                    continue;
140                }
141                infos.add(CmsAccountsApp.getPrincipalInfo(CmsPrincipal.readPrincipal(cms, new CmsUUID(id))));
142                if (principal instanceof CmsGroup) {
143                    m_groupIDs.add(new CmsUUID(id));
144                } else {
145                    m_userIDs.add(new CmsUUID(id));
146                }
147            } catch (CmsException e) {
148                LOG.error("Unable to read Principal.", e);
149            }
150        }
151
152        displayResourceInfoDirectly(infos);
153        String labelMessage = m_userIDs.isEmpty()
154        ? Messages.GUI_USERMANAGEMENT_GROUP_DELETE_MULTIPLE_0
155        : Messages.GUI_USERMANAGEMENT_USER_DELETE_MULTIPLE_0;
156        m_label_deleteDefault.setVisible(defaultUser && m_userIDs.isEmpty() && m_groupIDs.isEmpty());
157        m_label.setValue(CmsVaadinUtils.getMessageText(labelMessage));
158        CmsResourceInfoTable table = new CmsResourceInfoTable(m_cms, m_userIDs, m_groupIDs);
159        table.setHeight("300px");
160        table.setWidth("100%");
161        m_dependencyPanel.setVisible(table.size() > 0);
162        m_principalSelectLayout.setVisible(table.size() > 0);
163        m_okButton.setVisible(!m_userIDs.isEmpty() || !m_groupIDs.isEmpty());
164        m_deleteConfirm.setVisible(!m_userIDs.isEmpty() || !m_groupIDs.isEmpty());
165        if (m_dependencyPanel.isVisible()) {
166            m_dependencyPanel.setContent(table);
167            m_principalSelect.setRealPrincipalsOnly(true);
168
169            if ((m_userIDs.size() == 0) | (m_groupIDs.size() == 0)) {
170                m_principalSelect.setWidgetType(m_userIDs.size() > 0 ? WidgetType.userwidget : WidgetType.groupwidget);
171                m_principalSelect.setPrincipalType(
172                    m_userIDs.size() > 0 ? I_CmsPrincipal.PRINCIPAL_USER : I_CmsPrincipal.PRINCIPAL_GROUP);
173            } else {
174                m_principalSelect.setWidgetType(WidgetType.principalwidget);
175            }
176
177        }
178    }
179
180    /**
181     * Deletes the given user.<p>
182     */
183    protected void deletePrincipal() {
184
185        try {
186            String principalNameToCopyTo = m_principalSelect.getValue();
187            I_CmsPrincipal principalTarget = null;
188            if (!CmsStringUtil.isEmptyOrWhitespaceOnly(principalNameToCopyTo)) {
189                principalTarget = CmsPrincipal.readPrincipal(m_cms, principalNameToCopyTo);
190            }
191
192            for (CmsUUID id : m_groupIDs) {
193                m_cms.deleteGroup(id, principalTarget != null ? principalTarget.getId() : null);
194            }
195            for (CmsUUID id : m_userIDs) {
196                m_cms.deleteUser(id, principalTarget != null ? principalTarget.getId() : null);
197            }
198        } catch (CmsException e) {
199            LOG.error("Unable to delete principal", e);
200        }
201    }
202
203    /**
204     * Initialized the dialog.<p>
205     *
206     * @param cms CmsObject
207     * @param window window
208     * @param app
209     */
210    private void init(CmsObject cms, final Window window, final CmsAccountsApp app) {
211
212        CmsVaadinUtils.readAndLocalizeDesign(this, CmsVaadinUtils.getWpMessagesForCurrentLocale(), null);
213        m_icon.setContentMode(ContentMode.HTML);
214        m_icon.setValue(FontOpenCms.WARNING.getHtml());
215        m_icon2.setContentMode(ContentMode.HTML);
216        m_icon2.setValue(FontOpenCms.WARNING.getHtml());
217        m_label_deleteDefault.setVisible(false);
218        m_cms = cms;
219        m_okButton.addClickListener(new ClickListener() {
220
221            private static final long serialVersionUID = -7845894751587879028L;
222
223            public void buttonClick(ClickEvent event) {
224
225                deletePrincipal();
226                window.close();
227                app.reload();
228
229            }
230
231        });
232
233        m_cancelButton.addClickListener(new ClickListener() {
234
235            private static final long serialVersionUID = 6649262870116199591L;
236
237            public void buttonClick(ClickEvent event) {
238
239                window.close();
240
241            }
242
243        });
244    }
245
246}