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.CmsObject;
031import org.opencms.main.CmsException;
032import org.opencms.main.CmsLog;
033import org.opencms.main.OpenCms;
034import org.opencms.security.CmsRole;
035import org.opencms.ui.A_CmsUI;
036import org.opencms.ui.CmsCssIcon;
037import org.opencms.ui.CmsVaadinUtils;
038import org.opencms.ui.apps.Messages;
039import org.opencms.ui.components.OpenCmsTheme;
040import org.opencms.util.CmsUUID;
041
042import java.util.ArrayList;
043import java.util.HashSet;
044import java.util.Iterator;
045import java.util.List;
046import java.util.Set;
047
048import org.apache.commons.logging.Log;
049
050import com.vaadin.ui.Button;
051import com.vaadin.ui.Window;
052import com.vaadin.v7.data.Item;
053import com.vaadin.v7.data.util.IndexedContainer;
054import com.vaadin.v7.ui.HorizontalLayout;
055import com.vaadin.v7.ui.VerticalLayout;
056
057/**
058 * Class for the dialog to edit and view roles of user.<p>
059 */
060public class CmsUserEditRoleDialog extends A_CmsEditUserGroupRoleDialog {
061
062    /** The log object for this class. */
063    private static final Log LOG = CmsLog.getLog(CmsUserEditRoleDialog.class);
064
065    /**vaadin serial id.*/
066    private static final long serialVersionUID = -5734296145021453705L;
067
068    /**vaadin component.*/
069    Button m_close;
070
071    /**vaadin component. */
072    HorizontalLayout m_hlayout;
073
074    /**vaadin component. */
075    VerticalLayout m_leftTableHolder;
076
077    /**vaadin component. */
078    VerticalLayout m_rightTableHolder;
079
080    /**vaadin component. */
081    VerticalLayout m_vlayout;
082
083    /**
084     * public constructor.<p>
085     *
086     * @param cms CmsObject
087     * @param userId id of user
088     * @param window window
089     * @param app
090     */
091    public CmsUserEditRoleDialog(CmsObject cms, CmsUUID userId, final Window window, CmsAccountsApp app) {
092
093        super(cms, userId, window, app);
094    }
095
096    /**
097     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#addItem(java.util.Set)
098     */
099    @Override
100    public void addItem(Set<String> data) {
101
102        Iterator<String> it = data.iterator();
103        while (it.hasNext()) {
104            String roleName = it.next();
105            try {
106                OpenCms.getRoleManager().addUserToRole(m_cms, CmsRole.valueOfRoleName(roleName), m_principal.getName());
107            } catch (CmsException e) {
108                LOG.error("Unable to add user to role", e);
109            }
110        }
111
112    }
113
114    /**
115     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAddActionCaption()
116     */
117    @Override
118    public String getAddActionCaption() {
119
120        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_ADD_ROLE_0);
121    }
122
123    /**
124     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAddCaptionText()
125     */
126    @Override
127    public String getAddCaptionText() {
128
129        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_CHOOSE_ROLE_0);
130    }
131
132    /**
133     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getAvailableItemsIndexedContainer(java.lang.String, java.lang.String)
134     */
135    @Override
136    public IndexedContainer getAvailableItemsIndexedContainer(String caption, String icon) {
137
138        try {
139            List<CmsRole> roles = OpenCms.getRoleManager().getRoles(m_cms, m_principal.getOuFqn(), false);
140            List<CmsRole> invisibleRoles = new ArrayList<CmsRole>();
141            for (CmsRole role : roles) {
142                if (!OpenCms.getRoleManager().hasRole(
143                    m_cms,
144                    m_cms.getRequestContext().getCurrentUser().getName(),
145                    role)) {
146                    invisibleRoles.add(role);
147                }
148            }
149            roles.removeAll(invisibleRoles);
150            CmsRole.applySystemRoleOrder(roles);
151            List<CmsRole> userRoles = OpenCms.getRoleManager().getRolesOfUser(
152                m_cms,
153                m_principal.getName(),
154                m_principal.getOuFqn(),
155                false,
156                false,
157                false);
158            IndexedContainer container = new IndexedContainer();
159            container.addContainerProperty(caption, String.class, "");
160            container.addContainerProperty(icon, CmsCssIcon.class, new CmsCssIcon(OpenCmsTheme.ICON_ROLE));
161            for (CmsRole role : roles) {
162
163                if (!userRoles.contains(role)) {
164                    Item item = container.addItem(role);
165                    item.getItemProperty(caption).setValue(role.getDisplayName(m_cms, A_CmsUI.get().getLocale()));
166                }
167            }
168            return container;
169        } catch (CmsException e) {
170            LOG.error("unable to get roles", e);
171            return null;
172        }
173    }
174
175    /**
176     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getCloseButton()
177     */
178    @Override
179    public Button getCloseButton() {
180
181        return m_close;
182    }
183
184    /**
185     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getCurrentTableCaption()
186     */
187    @Override
188    public String getCurrentTableCaption() {
189
190        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_EDIT_CURRENTLY_SET_ROLES_0);
191    }
192
193    /**
194     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getDescriptionForItemId(java.lang.Object)
195     */
196    @Override
197    public String getDescriptionForItemId(Object itemId) {
198
199        CmsRole role = (CmsRole)itemId;
200        return role.getDescription(m_cms.getRequestContext().getLocale());
201    }
202
203    /**
204     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getEmptyMessage()
205     */
206    @Override
207    public String getEmptyMessage() {
208
209        return Messages.GUI_USERMANAGEMENT_EDIT_EMPTY_ROLES_0;
210    }
211
212    /**
213     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getFurtherColumnId()
214     */
215    @Override
216    public String getFurtherColumnId() {
217
218        return null;
219    }
220
221    /**
222     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getHLayout()
223     */
224    @Override
225    public HorizontalLayout getHLayout() {
226
227        return m_hlayout;
228    }
229
230    /**
231     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getItemName()
232     */
233    @Override
234    public String getItemName() {
235
236        return CmsVaadinUtils.getMessageText(Messages.GUI_USERMANAGEMENT_USER_NAME_0);
237    }
238
239    /**
240     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getItemsOfUserIndexedContainer(java.lang.String, java.lang.String, java.lang.String)
241     */
242    @Override
243    public IndexedContainer getItemsOfUserIndexedContainer(String propName, String propIcon, String propStatus) {
244
245        try {
246            List<CmsRole> userRoles = OpenCms.getRoleManager().getRolesOfUser(
247                m_cms,
248                m_principal.getName(),
249                m_principal.getOuFqn(),
250                false,
251                false,
252                false);
253            CmsRole.applySystemRoleOrder(userRoles);
254            List<CmsRole> directRoles = OpenCms.getRoleManager().getRolesOfUser(
255                m_cms,
256                m_principal.getName(),
257                "",
258                true,
259                true,
260                true);
261            //            CmsRole.applySystemRoleOrder(directRoles);
262            for (CmsRole directRole : directRoles) {
263                if (!userRoles.contains(directRole)) {
264                    //Role is from other OU directly set to user.
265                    userRoles.add(directRole);
266                }
267            }
268
269            IndexedContainer container = new IndexedContainer();
270            container.addContainerProperty(propName, String.class, "");
271            container.addContainerProperty(propIcon, CmsCssIcon.class, new CmsCssIcon(OpenCmsTheme.ICON_ROLE));
272            container.addContainerProperty(propStatus, Boolean.class, Boolean.valueOf(true));
273            for (CmsRole role : userRoles) {
274                Item item = container.addItem(role);
275                item.getItemProperty(propName).setValue(role.getDisplayName(m_cms, A_CmsUI.get().getLocale()));
276                item.getItemProperty(propStatus).setValue(Boolean.valueOf(directRoles.contains(role)));
277            }
278            return container;
279        } catch (CmsException e) {
280            LOG.error("unable to get roles", e);
281            return null;
282        }
283    }
284
285    /**
286     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getLeftTableLayout()
287     */
288    @Override
289    public VerticalLayout getLeftTableLayout() {
290
291        return m_leftTableHolder;
292    }
293
294    /**
295     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getParentLayout()
296     */
297    @Override
298    public VerticalLayout getParentLayout() {
299
300        return m_vlayout;
301    }
302
303    /**
304     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getRightTableLayout()
305     */
306    @Override
307    public VerticalLayout getRightTableLayout() {
308
309        return m_rightTableHolder;
310    }
311
312    /**
313     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getStringSetValue(java.util.Set)
314     */
315    @Override
316    public Set<String> getStringSetValue(Set<Object> value) {
317
318        Set<String> res = new HashSet<String>();
319        for (Object o : value) {
320            CmsRole role = (CmsRole)o;
321            res.add(role.getFqn());
322        }
323        return res;
324    }
325
326    /**
327     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#getWindowCaptionMessageKey()
328     */
329    @Override
330    public String getWindowCaptionMessageKey() {
331
332        return Messages.GUI_USERMANAGEMENT_EDIT_USERROLES_1;
333    }
334
335    /**
336     * @see org.opencms.ui.apps.user.A_CmsEditUserGroupRoleDialog#removeItem(java.util.Set)
337     */
338    @Override
339    public void removeItem(Set<String> items) {
340
341        try {
342            Iterator<String> iterator = items.iterator();
343            while (iterator.hasNext()) {
344                CmsRole role = CmsRole.valueOfRoleName(iterator.next());
345                OpenCms.getRoleManager().removeUserFromRole(m_cms, role, m_principal.getName());
346            }
347        } catch (CmsException e) {
348            LOG.error("Unable to remove user from role", e);
349        }
350    }
351}