Search in sources :

Example 56 with EntityIdentifier

use of org.apereo.portal.EntityIdentifier 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.
     *
     * @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, int 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 IS:
                nameFilter = query;
                break;
            case STARTS_WITH:
                nameFilter = query + ".*";
                break;
            case ENDS_WITH:
                nameFilter = ".*" + query;
                break;
            case CONTAINS:
                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 57 with EntityIdentifier

use of org.apereo.portal.EntityIdentifier in project uPortal by Jasig.

the class LDAPGroupStore method searchForEntities.

public EntityIdentifier[] searchForEntities(String query, int method, Class type) throws GroupsException {
    if (type != group && type != iperson)
        return new EntityIdentifier[0];
    // Guarantee that LDAP injection is prevented by replacing LDAP special characters
    // with escaped versions of the character
    query = LdapEncoder.filterEncode(query);
    ArrayList ids = new ArrayList();
    switch(method) {
        case STARTS_WITH:
            query = query + "*";
            break;
        case ENDS_WITH:
            query = "*" + query;
            break;
        case CONTAINS:
            query = "*" + query + "*";
            break;
    }
    query = namefield + "=" + query;
    DirContext context = getConnection();
    NamingEnumeration userlist = null;
    SearchControls sc = new SearchControls();
    sc.setSearchScope(SearchControls.SUBTREE_SCOPE);
    sc.setReturningAttributes(new String[] { keyfield });
    try {
        userlist = context.search(usercontext, query, sc);
        ArrayList keys = new ArrayList();
        processLdapResults(userlist, keys);
        String[] k = (String[]) keys.toArray(new String[0]);
        for (int i = 0; i < k.length; i++) {
            ids.add(new EntityIdentifier(k[i], iperson));
        }
        return (EntityIdentifier[]) ids.toArray(new EntityIdentifier[0]);
    } catch (NamingException nex) {
        throw new GroupsException("LDAPGroupStore: Unable to perform filter " + query, nex);
    }
}
Also used : GroupsException(org.apereo.portal.groups.GroupsException) ArrayList(java.util.ArrayList) NamingEnumeration(javax.naming.NamingEnumeration) SearchControls(javax.naming.directory.SearchControls) NamingException(javax.naming.NamingException) InitialDirContext(javax.naming.directory.InitialDirContext) DirContext(javax.naming.directory.DirContext) EntityIdentifier(org.apereo.portal.EntityIdentifier)

Example 58 with EntityIdentifier

use of org.apereo.portal.EntityIdentifier 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 59 with EntityIdentifier

use of org.apereo.portal.EntityIdentifier in project uPortal by Jasig.

the class IdentitySwapperManagerImpl method canImpersonateUser.

@Override
public boolean canImpersonateUser(IPerson currentUser, String targetUsername) {
    final EntityIdentifier ei = currentUser.getEntityIdentifier();
    final IAuthorizationPrincipal ap = authorizationService.newPrincipal(ei.getKey(), ei.getType());
    return canImpersonateUser(ap, targetUsername);
}
Also used : EntityIdentifier(org.apereo.portal.EntityIdentifier)

Example 60 with EntityIdentifier

use of org.apereo.portal.EntityIdentifier 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)

Aggregations

EntityIdentifier (org.apereo.portal.EntityIdentifier)93 IAuthorizationPrincipal (org.apereo.portal.security.IAuthorizationPrincipal)31 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 IPerson (org.apereo.portal.security.IPerson)17 GroupsException (org.apereo.portal.groups.GroupsException)16 IEntityGroup (org.apereo.portal.groups.IEntityGroup)16 Set (java.util.Set)14 IPortletDefinition (org.apereo.portal.portlet.om.IPortletDefinition)13 Iterator (java.util.Iterator)12 IGroupMember (org.apereo.portal.groups.IGroupMember)12 List (java.util.List)6 Element (net.sf.ehcache.Element)6 PortletCategory (org.apereo.portal.portlet.om.PortletCategory)6 HashMap (java.util.HashMap)5 InvalidNameException (javax.naming.InvalidNameException)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 LinkedList (java.util.LinkedList)3 Map (java.util.Map)3 GcFindGroups (edu.internet2.middleware.grouperClient.api.GcFindGroups)2