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