001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Arnaud Kervern
016 */
017
018package org.nuxeo.ecm.platform.shibboleth.web;
019
020import static org.jboss.seam.ScopeType.CONVERSATION;
021import static org.jboss.seam.annotations.Install.FRAMEWORK;
022
023import org.jboss.seam.annotations.In;
024import org.jboss.seam.annotations.Install;
025import org.jboss.seam.annotations.Name;
026import org.jboss.seam.annotations.Observer;
027import org.jboss.seam.annotations.Scope;
028import org.jboss.seam.core.Events;
029import org.jboss.seam.international.StatusMessage;
030import org.nuxeo.ecm.core.api.CoreSession;
031import org.nuxeo.ecm.core.api.DocumentModel;
032import org.nuxeo.ecm.core.api.NuxeoPrincipal;
033import org.nuxeo.ecm.core.api.PropertyException;
034import org.nuxeo.ecm.core.api.model.InvalidPropertyValueException;
035import org.nuxeo.ecm.directory.BaseSession;
036import org.nuxeo.ecm.platform.shibboleth.ShibbolethGroupHelper;
037import org.nuxeo.ecm.platform.usermanager.exceptions.GroupAlreadyExistsException;
038import org.nuxeo.ecm.webapp.security.AbstractUserGroupManagement;
039
040@Name("shibbGroupManagerActions")
041@Scope(CONVERSATION)
042@Install(precedence = FRAMEWORK)
043public class ShibbolethGroupManagerActionsBean extends AbstractUserGroupManagement {
044
045    protected static final String EVENT_SHIBB_GROUP_LISTING = "shibbGroupsListingChanged";
046
047    protected static final String VIEW_SHIBB_GROUP = "view_shibbGroup";
048
049    protected static final String VIEW_SHIBB_GROUPS = "view_shibbGroups";
050
051    protected static final String EDIT_SHIBB_GROUP = "edit_shibbGroup";
052
053    private static final long serialVersionUID = -2103588024105680788L;
054
055    protected DocumentModel selectedGroup;
056
057    protected DocumentModel newGroup;
058
059    protected Boolean canEditGroups;
060
061    @In(create = true)
062    protected transient CoreSession documentManager;
063
064    public void createGroup() {
065        createGroup(false);
066    }
067
068    public void createGroup(boolean createAnotherGroup) {
069        try {
070            selectedGroup = ShibbolethGroupHelper.createGroup(newGroup);
071            newGroup = null;
072            facesMessages.add(StatusMessage.Severity.INFO,
073                    resourcesAccessor.getMessages().get("info.groupManager.groupCreated"));
074            if (createAnotherGroup) {
075                showCreateForm = true;
076            } else {
077                showCreateForm = false;
078                showUserOrGroup = true;
079            }
080            fireSeamEvent(EVENT_SHIBB_GROUP_LISTING);
081        } catch (GroupAlreadyExistsException e) {
082            String message = resourcesAccessor.getMessages().get("error.groupManager.groupAlreadyExists");
083            facesMessages.addToControl("groupName", StatusMessage.Severity.ERROR, message);
084        } catch (PropertyException e) {
085            String message = resourcesAccessor.getMessages().get("error.shibboleth.groupManager.wrongEl");
086            facesMessages.addToControl("expressionLanguage", StatusMessage.Severity.ERROR, message);
087        }
088    }
089
090    public void deleteGroup() {
091        ShibbolethGroupHelper.deleteGroup(selectedGroup);
092        selectedGroup = null;
093        showUserOrGroup = false;
094        fireSeamEvent(EVENT_SHIBB_GROUP_LISTING);
095    }
096
097    public String editGroup() {
098        selectedGroup = refreshGroup(selectedGroup.getId());
099        return EDIT_SHIBB_GROUP;
100    }
101
102    protected String getTrimmedSearchString() {
103        if (searchString == null) {
104            return null;
105        }
106        return searchString.trim();
107    }
108
109    public DocumentModel getNewGroup() {
110        if (newGroup == null) {
111            newGroup = ShibbolethGroupHelper.getBareGroupModel(documentManager);
112        }
113        return newGroup;
114    }
115
116    public boolean isSelectedGroupReadOnly() {
117        return false;
118    }
119
120    public void updateGroup() {
121        try {
122            ShibbolethGroupHelper.updateGroup(selectedGroup);
123            detailsMode = DETAILS_VIEW_MODE;
124            fireSeamEvent(EVENT_SHIBB_GROUP_LISTING);
125        } catch (PropertyException e) {
126            String message = resourcesAccessor.getMessages().get("error.shibboleth.groupManager.wrongEl");
127            facesMessages.addToControl("expressionLanguage", StatusMessage.Severity.ERROR, message);
128        }
129    }
130
131    public String viewGroup() {
132        if (selectedGroup == null) {
133            return null;
134        } else {
135            return viewGroup(selectedGroup, false);
136        }
137    }
138
139    public String viewGroup(String groupName) {
140        setSelectedGroup(groupName);
141        showUserOrGroup = true;
142        return viewGroup(ShibbolethGroupHelper.getGroup(groupName), false);
143    }
144
145    // refresh to get references
146    protected DocumentModel refreshGroup(String groupName) {
147        return ShibbolethGroupHelper.getGroup(groupName);
148    }
149
150    protected String viewGroup(DocumentModel group, boolean refresh) {
151        if (group != null) {
152            selectedGroup = group;
153            if (refresh) {
154                selectedGroup = refreshGroup(group.getId());
155            }
156            if (selectedGroup != null) {
157                return VIEW_SHIBB_GROUP;
158            }
159        }
160        return null;
161    }
162
163    public void clearSearch() {
164        searchString = null;
165        fireSeamEvent(EVENT_SHIBB_GROUP_LISTING);
166    }
167
168    protected boolean getCanEditGroups() {
169        if (canEditGroups == null) {
170            canEditGroups = false;
171            if (!userManager.areGroupsReadOnly() && currentUser instanceof NuxeoPrincipal) {
172                NuxeoPrincipal pal = (NuxeoPrincipal) currentUser;
173                if (pal.isAdministrator()) {
174                    canEditGroups = true;
175                }
176            }
177        }
178        return canEditGroups;
179    }
180
181    public boolean getAllowCreateGroup() {
182        return getCanEditGroups();
183    }
184
185    public boolean getAllowDeleteGroup() {
186        return getCanEditGroups() && !BaseSession.isReadOnlyEntry(selectedGroup);
187    }
188
189    public boolean getAllowEditGroup() {
190        return getCanEditGroups() && !BaseSession.isReadOnlyEntry(selectedGroup);
191    }
192
193    @Override
194    protected String computeListingMode() {
195        return userManager.getGroupListingMode();
196    }
197
198    public DocumentModel getSelectedGroup() {
199        return selectedGroup;
200    }
201
202    public void setSelectedGroup(String group) {
203        selectedGroup = refreshGroup(group);
204    }
205
206    protected void fireSeamEvent(String eventName) {
207        Events.instance().raiseEvent(eventName);
208    }
209
210    @Observer(value = { EVENT_SHIBB_GROUP_LISTING })
211    public void onUsersListingChanged() {
212        contentViewActions.refreshOnSeamEvent(EVENT_SHIBB_GROUP_LISTING);
213        contentViewActions.resetPageProviderOnSeamEvent(EVENT_SHIBB_GROUP_LISTING);
214    }
215}