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.file.CmsUser;
033import org.opencms.main.CmsException;
034import org.opencms.main.CmsIllegalArgumentException;
035import org.opencms.main.CmsLog;
036import org.opencms.ui.CmsVaadinUtils;
037import org.opencms.ui.apps.Messages;
038import org.opencms.util.CmsUUID;
039
040import java.util.HashSet;
041import java.util.Iterator;
042import java.util.Set;
043
044import org.apache.commons.logging.Log;
045
046import com.vaadin.ui.Button;
047import com.vaadin.ui.Window;
048import com.vaadin.v7.data.util.IndexedContainer;
049import com.vaadin.v7.ui.HorizontalLayout;
050import com.vaadin.v7.ui.VerticalLayout;
051
052/**
053 * Class for the group edit dialog for users.<p>
054 */
055public class CmsUserEditGroupsDialog extends A_CmsEditUserGroupRoleDialog {
056
057    /**vaadin serial id.*/
058    private static final long serialVersionUID = 7548706839526481814L;
059
060    /** The log object for this class. */
061    private static final Log LOG = CmsLog.getLog(CmsUserEditGroupsDialog.class);
062
063    /**ID.*/
064    public static final String ID_OU = "ou";
065
066    /**vaadin component.*/
067    Button m_close;
068
069    /**vaadin component.*/
070    VerticalLayout m_leftTableHolder;
071
072    /**vaadin component.*/
073    VerticalLayout m_rightTableHolder;
074
075    /**vaadin component.*/
076    VerticalLayout m_vlayout;
077
078    /**vaadin component.*/
079    HorizontalLayout m_hlayout;
080
081    /**
082     * public constructor.<p>
083     *
084     * @param cms CmsObject
085     * @param userId id of user
086     * @param window window
087     * @param app the app instance
088     */
089    public CmsUserEditGroupsDialog(CmsObject cms, CmsUUID userId, final Window window, CmsAccountsApp app) {
090
091        super(cms, userId, window, app);
092    }
093
094    /**
095     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#addItem(java.util.Set)
096     */
097    @Override
098    public void addItem(Set<String> data) {
099
100        if (m_app.checkAddGroup((CmsUser)m_principal, data)) {
101            Iterator<String> it = data.iterator();
102            while (it.hasNext()) {
103                String groupName = it.next();
104                try {
105                    m_cms.addUserToGroup(m_principal.getName(), groupName);
106                } catch (CmsException e) {
107                    LOG.error("Unable to add user to group", e);
108                }
109            }
110        }
111    }
112
113    /**
114     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAddActionCaption()
115     */
116    @Override
117    public String getAddActionCaption() {
118
119        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_ADD_GROUP_0);
120    }
121
122    /**
123     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAddCaptionText()
124     */
125    @Override
126    public String getAddCaptionText() {
127
128        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_CHOOSE_GROUP_0);
129    }
130
131    /**
132     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAvailableItemsIndexedContainer(java.lang.String, java.lang.String)
133     */
134    @Override
135    public IndexedContainer getAvailableItemsIndexedContainer(String caption, String propIcon) {
136
137        try {
138            return m_app.getAvailableGroupsContainerWithout(
139                m_cms,
140                m_principal.getOuFqn(),
141                caption,
142                propIcon,
143                ID_OU,
144                m_cms.getGroupsOfUser(m_principal.getName(), true),
145                m_app::getGroupIcon);
146        } catch (CmsException e) {
147            LOG.error("Can't read groups of user", e);
148            return null;
149        }
150    }
151
152    /**
153     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getCloseButton()
154     */
155    @Override
156    public Button getCloseButton() {
157
158        return m_close;
159    }
160
161    /**
162     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getCurrentTableCaption()
163     */
164    @Override
165    public String getCurrentTableCaption() {
166
167        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_CURRENTLY_SET_GROUPS_0);
168    }
169
170    /**
171     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getDescriptionForItemId(java.lang.Object)
172     */
173    @Override
174    public String getDescriptionForItemId(Object itemId) {
175
176        return null;
177
178    }
179
180    /**
181     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getEmptyMessage()
182     */
183    @Override
184    public String getEmptyMessage() {
185
186        return Messages.GUI_USERMANAGEMENT_EDIT_EMPTY_GROUPS_0;
187    }
188
189    /**
190     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getFurtherColumnId()
191     */
192    @Override
193    public String getFurtherColumnId() {
194
195        return ID_OU;
196    }
197
198    /**
199     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getHLayout()
200     */
201    @Override
202    public HorizontalLayout getHLayout() {
203
204        return m_hlayout;
205    }
206
207    /**
208     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getItemName()
209     */
210    @Override
211    public String getItemName() {
212
213        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_NAME_0);
214    }
215
216    /**
217     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getItemsOfUserIndexedContainer(java.lang.String, java.lang.String, java.lang.String)
218     */
219    @Override
220    public IndexedContainer getItemsOfUserIndexedContainer(String propName, String propIcon, String propStatus) {
221
222        CmsUser user = (CmsUser)m_principal;
223        IndexedContainer container = m_app.getUserGroupsEditorContainer(user, propName, propIcon, propStatus);
224        return container;
225    }
226
227    /**
228     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getLeftTableLayout()
229     */
230    @Override
231    public VerticalLayout getLeftTableLayout() {
232
233        return m_leftTableHolder;
234    }
235
236    /**
237     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getParentLayout()
238     */
239    @Override
240    public VerticalLayout getParentLayout() {
241
242        return m_vlayout;
243    }
244
245    /**
246     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getRightTableLayout()
247     */
248    @Override
249    public VerticalLayout getRightTableLayout() {
250
251        return m_rightTableHolder;
252    }
253
254    /**
255     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getStringSetValue(java.util.Set)
256     */
257    @Override
258    public Set<String> getStringSetValue(Set<Object> value) {
259
260        Set<String> res = new HashSet<String>();
261        for (Object o : value) {
262            res.add(((CmsGroup)o).getName());
263        }
264        return res;
265    }
266
267    /**
268     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getWindowCaptionMessageKey()
269     */
270    @Override
271    public String getWindowCaptionMessageKey() {
272
273        return Messages.GUI_USERMANAGEMENT_EDIT_USERGROUP_1;
274    }
275
276    /**
277     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#removeItem(java.util.Set)
278     */
279    @Override
280    public void removeItem(Set<String> items) {
281
282        if (m_app.checkRemoveGroups((CmsUser)m_principal, items)) {
283            Iterator<String> iterator = items.iterator();
284            while (iterator.hasNext()) {
285                try {
286
287                    m_cms.removeUserFromGroup(m_principal.getName(), iterator.next());
288
289                } catch (CmsIllegalArgumentException | CmsException e) {
290                    //happens if admin group was deleted( = user is deleted at the same time)
291                }
292            }
293        }
294    }
295
296}