001/*
002 *  Copyright 2016 Anyware Services
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 */
016package org.ametys.core.group;
017
018import java.util.Collections;
019import java.util.HashSet;
020import java.util.LinkedHashSet;
021import java.util.Set;
022
023import org.apache.avalon.framework.component.Component;
024import org.apache.avalon.framework.service.ServiceException;
025import org.apache.avalon.framework.service.ServiceManager;
026import org.apache.avalon.framework.service.Serviceable;
027
028import org.ametys.core.group.directory.GroupDirectory;
029import org.ametys.core.user.UserIdentity;
030import org.ametys.runtime.plugin.component.AbstractLogEnabled;
031
032/**
033 * Component for getting group list.
034 */
035public class GroupManager extends AbstractLogEnabled implements Component, Serviceable
036{
037    /** Avalon Role */
038    public static final String ROLE = GroupManager.class.getName();
039    
040    /** The DAO for group directories */
041    protected GroupDirectoryDAO _groupDirectoryDAO;
042    /** The helper for the associations group directory/context */
043    protected GroupDirectoryContextHelper _directoryContextHelper;
044    
045    @Override
046    public void service(ServiceManager manager) throws ServiceException
047    {
048        _groupDirectoryDAO = (GroupDirectoryDAO) manager.lookup(GroupDirectoryDAO.ROLE);
049        _directoryContextHelper = (GroupDirectoryContextHelper) manager.lookup(GroupDirectoryContextHelper.ROLE);
050    }
051    
052    // ------------------------------
053    //    GET A PARTICULAR GROUP
054    // ------------------------------
055    
056    /**
057     * Get a particular group of the given {@link GroupDirectory} by its id
058     * @param groupIdentity The group identity
059     * @return The group or null if not found
060     */
061    public Group getGroup(GroupIdentity groupIdentity)
062    {
063        return getGroup(groupIdentity.getDirectoryId(), groupIdentity.getId());
064    }
065    
066    /**
067     * Get a particular group of the given {@link GroupDirectory} by its id
068     * @param groupDirectory The group directory
069     * @param groupId The id of the group
070     * @return The group or null if not found
071     */
072    public Group getGroup(GroupDirectory groupDirectory, String groupId)
073    {
074        return groupDirectory.getGroup(groupId);
075    }
076    
077    /**
078     * Get a particular group of the given {@link GroupDirectory} by its id
079     * @param groupDirectoryId The id of the group directory
080     * @param groupId The id of the group
081     * @return The group or null if not found
082     */
083    public Group getGroup(String groupDirectoryId, String groupId)
084    {
085        GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId);
086        if (groupDirectory != null)
087        {
088            return getGroup(groupDirectory, groupId);
089        }
090        else
091        {
092            return null;
093        }
094    }
095    
096    /**
097     * Get a particular group on a given context
098     * @param context The context
099     * @param groupId The id of the group
100     * @return The group or null if not found
101     */
102    public Group getGroupByContext(String context, String groupId)
103    {
104        for (String directoryId : _directoryContextHelper.getGroupDirectoriesOnContext(context))
105        {
106            Group group = getGroup(directoryId, groupId);
107            if (group != null)
108            {
109                return group;
110            }
111        }
112        return null;
113    }
114    
115    // ------------------------------
116    //    GET A SET OF GROUPS
117    // ------------------------------
118    
119    /**
120     * Get all the groups of a given {@link GroupDirectory}
121     * @param groupDirectory The group directory
122     * @return The set of groups of the group directory
123     */
124    public Set<Group> getGroups(GroupDirectory groupDirectory)
125    {
126        return groupDirectory.getGroups();
127    }
128    
129    /**
130     * Get all the groups of a given {@link GroupDirectory}
131     * @param groupDirectoryId The id of the group directory
132     * @return The set of groups of the group directory
133     */
134    public Set<Group> getGroups(String groupDirectoryId)
135    {
136        GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId);
137        if (groupDirectory != null)
138        {
139            return getGroups(groupDirectory);
140        }
141        else
142        {
143            return Collections.emptySet();
144        }
145    }
146    
147    /**
148     * Get all the groups on a given context
149     * @param context The context
150     * @return The set of groups
151     */
152    public Set<Group> getGroupsByContext(String context)
153    {
154        Set<Group> groups = new LinkedHashSet<>();
155        
156        for (String directoryId : _directoryContextHelper.getGroupDirectoriesOnContext(context))
157        {
158            groups.addAll(getGroups(directoryId));
159        }
160        
161        return groups;
162    }
163    
164    // ------------------------------
165    //    GET GROUPS A USER IS IN
166    // ------------------------------
167    
168    /**
169     * Get all the groups the given user is in
170     * @param userIdentity The user identity.
171     * @return The set of groups the user is in
172     */
173    public Set<GroupIdentity> getUserGroups(UserIdentity userIdentity)
174    {
175        Set<GroupIdentity> result = new HashSet<>();
176        for (GroupDirectory groupDirectory : _groupDirectoryDAO.getGroupDirectories())
177        {
178            for (String groupId : getUserGroups(groupDirectory, userIdentity))
179            {
180                result.add(new GroupIdentity(groupId, groupDirectory.getId()));
181            }
182        }
183        
184        return result;
185    }
186    
187    /**
188     * Get all the groups of a {@link GroupDirectory} the given user is in
189     * @param groupDirectory The group directory
190     * @param userIdentity The user identity.
191     * @return A set of groups the user is in
192     */
193    public Set<String> getUserGroups(GroupDirectory groupDirectory, UserIdentity userIdentity)
194    {
195        return groupDirectory.getUserGroups(userIdentity);
196    }
197    
198    /**
199     * Get all the groups of a {@link GroupDirectory} the given user is in
200     * @param groupDirectoryId The id of the group directory
201     * @param userIdentity The user identity.
202     * @return A set of groups the user is in
203     */
204    public Set<String> getUserGroups(String groupDirectoryId, UserIdentity userIdentity)
205    {
206        GroupDirectory groupDirectory = _groupDirectoryDAO.getGroupDirectory(groupDirectoryId);
207        if (groupDirectory != null)
208        {
209            return getUserGroups(groupDirectory, userIdentity);
210        }
211        else
212        {
213            return Collections.emptySet();
214        }
215    }
216
217}