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()]);
}
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);
}
}
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()]);
}
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);
}
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()]);
}
Aggregations