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 * Contributors:
016 *     Nuxeo - initial API and implementation
017 */
018
019package org.nuxeo.ecm.platform.shibboleth.web;
020
021import java.io.Serializable;
022import java.util.ArrayList;
023import java.util.Collections;
024import java.util.HashMap;
025import java.util.HashSet;
026import java.util.List;
027import java.util.Map;
028
029import org.jboss.seam.annotations.In;
030import org.jboss.seam.annotations.Install;
031import org.jboss.seam.annotations.Name;
032import org.jboss.seam.annotations.Scope;
033import org.nuxeo.ecm.core.api.DocumentModel;
034import org.nuxeo.ecm.directory.SizeLimitExceededException;
035import org.nuxeo.ecm.platform.shibboleth.ShibbolethGroupHelper;
036import org.nuxeo.ecm.platform.shibboleth.web.tree.UserTreeNode;
037import org.nuxeo.ecm.platform.shibboleth.web.tree.UserTreeNodeHelper;
038import org.nuxeo.ecm.platform.ui.web.util.SuggestionActionsBean;
039import org.nuxeo.ecm.platform.usermanager.UserManagerImpl;
040import org.nuxeo.ecm.webapp.security.UserSuggestionActionsBean;
041import org.richfaces.component.UITree;
042
043import static org.jboss.seam.ScopeType.CONVERSATION;
044import static org.jboss.seam.annotations.Install.FRAMEWORK;
045
046/**
047 * Action bean handling tree's nodes generation and filling them with groups
048 *
049 * @author <a href="mailto:akervern@nuxeo.com">Arnaud Kervern</a>
050 * @see org.nuxeo.ecm.webapp.security.UserSuggestionActionsBean
051 */
052@Name("shibbUserSuggestionWithGroupTree")
053@Scope(CONVERSATION)
054@Install(precedence = FRAMEWORK)
055public class UserSuggestionWithGroupTreeActionsBean extends UserSuggestionActionsBean {
056
057    @In(create = true)
058    SuggestionActionsBean suggestionActions;
059
060    private static final long serialVersionUID = -1L;
061
062    private List<UserTreeNode> treeRoot;
063
064    protected String shibbUserName = "";
065
066    /**
067     * Build the tree with all groups (not virtual) / shibbGroup and merge them into a List of UserTreeNode to be
068     * displayed
069     */
070    protected void buildTree() {
071        List<DocumentModel> groups = getGroups();
072        List<DocumentModel> shibbGroups = getShibbGroups();
073
074        treeRoot = new ArrayList<UserTreeNode>();
075        treeRoot.addAll(UserTreeNodeHelper.getHierarcicalNodes(groups));
076        treeRoot.addAll(UserTreeNodeHelper.buildBranch(UserTreeNodeHelper.getShibbGroupBasePath(), shibbGroups));
077    }
078
079    /**
080     * Get all groups (without virtual) and shibbGroups with the userManager bean
081     *
082     * @return list of group which match the pattern
083     * @see UserManagerImpl
084     */
085    protected List<DocumentModel> getGroups() {
086        try {
087            Map<String, Serializable> filter = new HashMap<String, Serializable>();
088            filter.put("__virtualGroup", false);
089
090            // parameters must be serializable so copy keySet to HashSet
091            return userManager.searchGroups(filter, new HashSet<String>(filter.keySet()));
092        } catch (SizeLimitExceededException e) {
093            return Collections.emptyList();
094        }
095    }
096
097    /**
098     * Get all shibboleth group with the Shibboleth Helper
099     *
100     * @return All Shibboleth groups, or an empty list if SizeLimitExceededException is reached.
101     * @see ShibbolethGroupHelper
102     */
103    protected List<DocumentModel> getShibbGroups() {
104        try {
105            return ShibbolethGroupHelper.getGroups();
106        } catch (SizeLimitExceededException e) {
107            return Collections.emptyList();
108        }
109    }
110
111    @Override
112    public Map<String, Object> getUserInfo(String id) {
113        Map<String, Object> userInfo = super.getUserInfo(id);
114        if (userInfo.get(ENTRY_KEY_NAME) == null) {
115            DocumentModel doc = userManager.getBareUserModel();
116            doc.setProperty(userManager.getUserSchemaName(), userManager.getUserIdField(), id);
117            userInfo.put(ENTRY_KEY_NAME, doc);
118        }
119        return userInfo;
120    }
121
122    public void forceShibUserValue(String newValue) {
123        setShibbUserName(newValue);
124    }
125
126    /**
127     * Check if the node is open or not
128     */
129    public Boolean adviseNodeOpened(UITree treeComponent) {
130        return null;
131    }
132
133    public void refreshRoot() {
134        treeRoot = null;
135    }
136
137    public List<UserTreeNode> getTreeRoot() {
138        if (treeRoot == null) {
139            buildTree();
140        }
141        return treeRoot;
142    }
143
144    public String getShibbUserName() {
145        return shibbUserName;
146    }
147
148    public void setShibbUserName(String shibbUserName) {
149        this.shibbUserName = shibbUserName;
150    }
151}