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