Search in sources :

Example 16 with GroupsException

use of org.apereo.portal.groups.GroupsException in project uPortal by Jasig.

the class PersonDirectorySearcher method searchForEntities.

@Override
public EntityIdentifier[] searchForEntities(String query, int method) throws GroupsException {
    switch(method) {
        case IS:
            {
                break;
            }
        case STARTS_WITH:
            {
                query = query + IPersonAttributeDao.WILDCARD;
                break;
            }
        case ENDS_WITH:
            {
                query = IPersonAttributeDao.WILDCARD + query;
                break;
            }
        case CONTAINS:
            {
                query = IPersonAttributeDao.WILDCARD + query + IPersonAttributeDao.WILDCARD;
                break;
            }
        default:
            {
                throw new GroupsException("Unknown search type");
            }
    }
    log.debug("Searching for a person directory account matching query string " + query);
    final String usernameAttribute = this.usernameAttributeProvider.getUsernameAttribute();
    final Map<String, Object> queryMap = Collections.<String, Object>singletonMap(usernameAttribute, query);
    final Set<IPersonAttributes> results = this.personAttributeDao.getPeople(queryMap);
    // create an array of EntityIdentifiers from the search results
    final List<EntityIdentifier> entityIdentifiers = new ArrayList<EntityIdentifier>(results.size());
    for (final IPersonAttributes personAttributes : results) {
        entityIdentifiers.add(new EntityIdentifier(personAttributes.getName(), this.personEntityType));
    }
    return entityIdentifiers.toArray(new EntityIdentifier[entityIdentifiers.size()]);
}
Also used : IPersonAttributes(org.jasig.services.persondir.IPersonAttributes) GroupsException(org.apereo.portal.groups.GroupsException) ArrayList(java.util.ArrayList) EntityIdentifier(org.apereo.portal.EntityIdentifier)

Example 17 with GroupsException

use of org.apereo.portal.groups.GroupsException in project uPortal by Jasig.

the class SmartLdapGroupStore method searchForGroups.

public EntityIdentifier[] searchForGroups(String query, int method, Class leaftype) throws GroupsException {
    if (isTreeRefreshRequired()) {
        refreshTree();
    }
    log.debug("Invoking searchForGroups():  query={}, method={}, leaftype=", query, method, leaftype.getClass().getName());
    // We only match the IPerson leaf type...
    final IEntityGroup root = getRootGroup();
    if (!leaftype.equals(root.getLeafType())) {
        return new EntityIdentifier[0];
    }
    // We need to escape regex special characters that appear in the query string...
    final String[][] specials = new String[][] { /* backslash must come first! */
    new String[] { "\\", "\\\\" }, new String[] { "[", "\\[" }, /* closing ']' isn't needed b/c it's a normal character w/o a preceding '[' */
    new String[] { "{", "\\{" }, /* closing '}' isn't needed b/c it's a normal character w/o a preceding '{' */
    new String[] { "^", "\\^" }, new String[] { "$", "\\$" }, new String[] { ".", "\\." }, new String[] { "|", "\\|" }, new String[] { "?", "\\?" }, new String[] { "*", "\\*" }, new String[] { "+", "\\+" }, new String[] { "(", "\\(" }, new String[] { ")", "\\)" } };
    for (String[] s : specials) {
        query = query.replace(s[0], s[1]);
    }
    // Establish the regex pattern to match on...
    String regex;
    switch(method) {
        case IGroupConstants.IS:
            regex = query.toUpperCase();
            break;
        case IGroupConstants.STARTS_WITH:
            regex = query.toUpperCase() + ".*";
            break;
        case IGroupConstants.ENDS_WITH:
            regex = ".*" + query.toUpperCase();
            break;
        case IGroupConstants.CONTAINS:
            regex = ".*" + query.toUpperCase() + ".*";
            break;
        default:
            String msg = "Unsupported search method:  " + method;
            throw new GroupsException(msg);
    }
    List<EntityIdentifier> rslt = new LinkedList<>();
    for (Map.Entry<String, List<String>> y : groupsTree.getKeysByUpperCaseName().entrySet()) {
        if (y.getKey().matches(regex)) {
            List<String> keys = y.getValue();
            for (String k : keys) {
                rslt.add(new EntityIdentifier(k, IEntityGroup.class));
            }
        }
    }
    return rslt.toArray(new EntityIdentifier[rslt.size()]);
}
Also used : IEntityGroup(org.apereo.portal.groups.IEntityGroup) GroupsException(org.apereo.portal.groups.GroupsException) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) EntityIdentifier(org.apereo.portal.EntityIdentifier) HashMap(java.util.HashMap) Map(java.util.Map) LinkedList(java.util.LinkedList)

Example 18 with GroupsException

use of org.apereo.portal.groups.GroupsException in project uPortal by Jasig.

the class GroupService method ifinishedSession.

/**
 * Receives notice that the UserInstance has been unbound from the HttpSession. In response, we
 * remove the corresponding group member from the cache. We use the roundabout route of creating
 * a group member and then getting its EntityIdentifier because we need the EntityIdentifier for
 * the group member, which is cached, not the EntityIdentifier for the IPerson, which is not.
 *
 * @param person org.apereo.portal.security.IPerson
 */
private void ifinishedSession(IPerson person) throws GroupsException {
    IGroupMember gm = getGroupMember(person.getEntityIdentifier());
    try {
        final EntityIdentifier entityIdentifier = gm.getEntityIdentifier();
        EntityCachingService.getEntityCachingService().remove(entityIdentifier.getType(), entityIdentifier.getKey());
    } catch (CachingException ce) {
        throw new GroupsException("Problem removing group member " + gm.getKey() + " from cache", ce);
    }
}
Also used : CachingException(org.apereo.portal.concurrency.CachingException) IGroupMember(org.apereo.portal.groups.IGroupMember) GroupsException(org.apereo.portal.groups.GroupsException) EntityIdentifier(org.apereo.portal.EntityIdentifier)

Example 19 with GroupsException

use of org.apereo.portal.groups.GroupsException in project uPortal by Jasig.

the class FileSystemGroupStore method searchForGroups.

/**
 * Returns an EntityIdentifier[] of groups of the given leaf type whose names match the query
 * string according to the search method.
 *
 * <p>Treats case sensitive and case insensitive searches the same.
 *
 * @param query String the string used to match group names.
 * @param searchMethod see org.apereo.portal.groups.IGroupConstants.
 * @param leafType the leaf type of the groups we are searching for.
 * @return EntityIdentifier[]
 */
public EntityIdentifier[] searchForGroups(String query, SearchMethod searchMethod, Class leafType) throws GroupsException {
    List ids = new ArrayList();
    File baseDir = getFileRoot(leafType);
    if (log.isDebugEnabled())
        log.debug(DEBUG_CLASS_NAME + "searchForGroups(): " + query + " method: " + searchMethod + " type: " + leafType);
    if (baseDir != null) {
        String nameFilter = null;
        switch(searchMethod) {
            case DISCRETE:
            case DISCRETE_CI:
                nameFilter = query;
                break;
            case STARTS_WITH:
            case STARTS_WITH_CI:
                nameFilter = query + ".*";
                break;
            case ENDS_WITH:
            case ENDS_WITH_CI:
                nameFilter = ".*" + query;
                break;
            case CONTAINS:
            case CONTAINS_CI:
                nameFilter = ".*" + query + ".*";
                break;
            default:
                throw new GroupsException(DEBUG_CLASS_NAME + ".searchForGroups(): Unknown search method: " + searchMethod);
        }
        final Pattern namePattern = Pattern.compile(nameFilter);
        final FilenameFilter filter = new FilenameFilter() {

            @Override
            public boolean accept(File dir, String name) {
                return namePattern.matcher(name).matches();
            }
        };
        Set allDirs = getAllDirectoriesBelow(baseDir);
        allDirs.add(baseDir);
        for (Iterator itr = allDirs.iterator(); itr.hasNext(); ) {
            File[] files = ((File) itr.next()).listFiles(filter);
            for (int filesIdx = 0; filesIdx < files.length; filesIdx++) {
                String key = getKeyFromFile(files[filesIdx]);
                EntityIdentifier ei = new EntityIdentifier(key, ICompositeGroupService.GROUP_ENTITY_TYPE);
                ids.add(ei);
            }
        }
    }
    if (log.isDebugEnabled())
        log.debug(DEBUG_CLASS_NAME + ".searchForGroups(): found " + ids.size() + " files.");
    return (EntityIdentifier[]) ids.toArray(new EntityIdentifier[ids.size()]);
}
Also used : Pattern(java.util.regex.Pattern) FilenameFilter(java.io.FilenameFilter) HashSet(java.util.HashSet) Set(java.util.Set) GroupsException(org.apereo.portal.groups.GroupsException) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) EntityIdentifier(org.apereo.portal.EntityIdentifier) File(java.io.File)

Example 20 with GroupsException

use of org.apereo.portal.groups.GroupsException in project uPortal by Jasig.

the class GrouperEntityGroupStore method findEntitiesForGroup.

/* (non-Javadoc)
     * @see org.apereo.portal.groups.IEntityGroupStore#findEntitiesForGroup(org.apereo.portal.groups.IEntityGroup)
     */
@SuppressWarnings("unchecked")
public Iterator findEntitiesForGroup(IEntityGroup group) throws GroupsException {
    if (LOGGER.isDebugEnabled()) {
        LOGGER.debug("Searching Grouper for members of the group with key: " + group.getKey());
    }
    try {
        // execute a search for members of the specified group
        GcGetMembers getGroupsMembers = new GcGetMembers();
        getGroupsMembers.addGroupName(group.getLocalKey());
        getGroupsMembers.assignIncludeSubjectDetail(true);
        WsGetMembersResults results = getGroupsMembers.execute();
        if (results == null || results.getResults() == null || results.getResults().length == 0 || results.getResults()[0].getWsSubjects() == null) {
            LOGGER.debug("No members found for Grouper group with key " + group.getLocalKey());
            return Collections.<IGroupMember>emptyList().iterator();
        }
        WsSubject[] gInfos = results.getResults()[0].getWsSubjects();
        final List<IGroupMember> members = new ArrayList<IGroupMember>(gInfos.length);
        // add each result to the member list
        for (WsSubject gInfo : gInfos) {
            // if the member is not a group (aka person)
            if (!StringUtils.equals(gInfo.getSourceId(), "g:gsa")) {
                if (LOGGER.isDebugEnabled()) {
                    LOGGER.debug("creating leaf member:" + gInfo.getId() + " and name: " + gInfo.getName() + " from group: " + group.getLocalKey());
                }
                // use the name instead of id as it shows better in the display
                IGroupMember member = new EntityImpl(gInfo.getName(), IPerson.class);
                members.add(member);
            }
        }
        // return an iterator for the assembled group
        return members.iterator();
    } catch (Exception e) {
        LOGGER.warn("Exception while attempting to retrieve " + "member entities of group with key " + group.getKey() + " from Grouper web services: " + e.getMessage());
        return Collections.<IGroupMember>emptyList().iterator();
    }
}
Also used : IGroupMember(org.apereo.portal.groups.IGroupMember) EntityImpl(org.apereo.portal.groups.EntityImpl) ArrayList(java.util.ArrayList) WsSubject(edu.internet2.middleware.grouperClient.ws.beans.WsSubject) GcGetMembers(edu.internet2.middleware.grouperClient.api.GcGetMembers) WsGetMembersResults(edu.internet2.middleware.grouperClient.ws.beans.WsGetMembersResults) GroupsException(org.apereo.portal.groups.GroupsException)

Aggregations

GroupsException (org.apereo.portal.groups.GroupsException)23 ArrayList (java.util.ArrayList)15 EntityIdentifier (org.apereo.portal.EntityIdentifier)13 IEntityGroup (org.apereo.portal.groups.IEntityGroup)7 List (java.util.List)5 WsSubject (edu.internet2.middleware.grouperClient.ws.beans.WsSubject)4 File (java.io.File)4 Iterator (java.util.Iterator)4 IGroupMember (org.apereo.portal.groups.IGroupMember)4 WsGroup (edu.internet2.middleware.grouperClient.ws.beans.WsGroup)3 IOException (java.io.IOException)3 Collection (java.util.Collection)3 LinkedList (java.util.LinkedList)3 GcGetMembers (edu.internet2.middleware.grouperClient.api.GcGetMembers)2 GcGetSubjects (edu.internet2.middleware.grouperClient.api.GcGetSubjects)2 WsGetMembersResults (edu.internet2.middleware.grouperClient.ws.beans.WsGetMembersResults)2 WsGetSubjectsResults (edu.internet2.middleware.grouperClient.ws.beans.WsGetSubjectsResults)2 FilenameFilter (java.io.FilenameFilter)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2