Add pattern based search for authorities constrained to zones

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14829 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Andrew Hind
2009-06-22 12:45:00 +00:00
parent 5fe2c1507b
commit de477c27df
6 changed files with 179 additions and 68 deletions

View File

@@ -145,9 +145,10 @@ public interface AuthorityDAO
* *
* @param type * @param type
* @param namePattern * @param namePattern
* @param zones - may be null to indicate all zones
* @return * @return
*/ */
public Set<String> findAuthorities(AuthorityType type, String namePattern); public Set<String> findAuthorities(AuthorityType type, String namePattern, Set<String> zones);
/** /**
* Gets or creates an authority zone node with the specified name * Gets or creates an authority zone node with the specified name

View File

@@ -183,10 +183,10 @@ public class AuthorityDAOImpl implements AuthorityDAO
public Set<String> getAllAuthorities(AuthorityType type) public Set<String> getAllAuthorities(AuthorityType type)
{ {
return findAuthorities(type, null); return findAuthorities(type, null, null);
} }
public Set<String> findAuthorities(AuthorityType type, String namePattern) public Set<String> findAuthorities(AuthorityType type, String namePattern, Set<String> zones)
{ {
Pattern pattern = null; Pattern pattern = null;
if (namePattern != null) if (namePattern != null)
@@ -209,12 +209,32 @@ public class AuthorityDAOImpl implements AuthorityDAO
// For other types, we just look directly under the authority container // For other types, we just look directly under the authority container
if (type == null || !type.equals(AuthorityType.USER)) if (type == null || !type.equals(AuthorityType.USER))
{ {
NodeRef container = getAuthorityContainer(); if (zones == null)
if (container != null)
{ {
for (ChildAssociationRef childRef : nodeService.getChildAssocs(container)) NodeRef container = getAuthorityContainer();
if (container != null)
{ {
addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, pattern); for (ChildAssociationRef childRef : nodeService.getChildAssocs(container))
{
addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, pattern);
}
}
}
else
{
for (String zone : zones)
{
NodeRef container = getOrCreateZone(zone);
if (container != null)
{
if (container != null)
{
for (ChildAssociationRef childRef : nodeService.getChildAssocs(container))
{
addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, pattern);
}
}
}
} }
} }
} }

View File

@@ -244,25 +244,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
public Set<String> findAuthorities(AuthorityType type, String namePattern) public Set<String> findAuthorities(AuthorityType type, String namePattern)
{ {
Set<String> authorities = new HashSet<String>(); return findAuthoritiesInZone(type, namePattern, null);
switch (type)
{
case ADMIN:
case EVERYONE:
case GUEST:
throw new UnsupportedOperationException();
case GROUP:
authorities.addAll(authorityDAO.findAuthorities(type, namePattern));
break;
case OWNER:
case ROLE:
throw new UnsupportedOperationException();
case USER:
throw new UnsupportedOperationException();
default:
break;
}
return authorities;
} }
@@ -441,4 +423,38 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
{ {
return authorityDAO.getAllRootAuthoritiesInZone(zoneName, type); return authorityDAO.getAllRootAuthoritiesInZone(zoneName, type);
} }
public Set<String> findAuthoritiesByShortNameInZone(AuthorityType type, String shortNamePattern, String zone)
{
String fullNamePattern = getName(type, shortNamePattern);
return findAuthoritiesInZone(type, fullNamePattern, zone);
}
public Set<String> findAuthoritiesInZone(AuthorityType type, String namePattern, String zone)
{
Set<String> authorities = new HashSet<String>();
switch (type)
{
case ADMIN:
case EVERYONE:
case GUEST:
throw new UnsupportedOperationException();
case GROUP:
Set<String> zones = null;
if(zone != null)
{
zones = Collections.singleton(zone);
}
authorities.addAll(authorityDAO.findAuthorities(type, namePattern, zones));
break;
case OWNER:
case ROLE:
throw new UnsupportedOperationException();
case USER:
throw new UnsupportedOperationException();
default:
break;
}
return authorities;
}
} }

View File

@@ -25,6 +25,7 @@
package org.alfresco.repo.security.authority; package org.alfresco.repo.security.authority;
import java.io.Serializable; import java.io.Serializable;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.Map; import java.util.Map;
@@ -160,7 +161,6 @@ public class AuthorityServiceTest extends TestCase
super.tearDown(); super.tearDown();
} }
public void testZones() public void testZones()
{ {
assertNull(pubAuthorityService.getAuthorityZones("GROUP_DEFAULT")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_DEFAULT"));
@@ -251,42 +251,85 @@ public class AuthorityServiceTest extends TestCase
{ {
long before, after; long before, after;
char end = 'd'; char end = 'd';
for (char i = 'a'; i <= end; i++) String[] zones = new String[] { null, "ONE", "TWO", "THREE" };
for (String zone : zones)
{ {
for (char j = 'a'; j <= end; j++) for (char i = 'a'; i <= end; i++)
{ {
for (char k = 'a'; k <= end; k++) for (char j = 'a'; j <= end; j++)
{ {
StringBuilder name = new StringBuilder(); for (char k = 'a'; k <= end; k++)
name.append("__").append(i).append(j).append(k); {
pubAuthorityService.createAuthority(AuthorityType.GROUP, name.toString()); StringBuilder name = new StringBuilder();
name.append("__").append(zone).append("__").append(i).append(j).append(k);
if (zone == null)
{
pubAuthorityService.createAuthority(AuthorityType.GROUP, name.toString());
}
else
{
pubAuthorityService.createAuthority(AuthorityType.GROUP, name.toString(), name.toString(), Collections.singleton(zone));
}
}
} }
} }
} }
int size = end - 'a' + 1; int size = end - 'a' + 1;
before = System.nanoTime(); before = System.nanoTime();
Set<String> matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___a*"); Set<String> matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*__a*");
after = System.nanoTime(); after = System.nanoTime();
System.out.println("GROUP___a* in "+((after-before)/1000000000.0f)); System.out.println("GROUP___a* in " + ((after - before) / 1000000000.0f));
assertEquals(size*size, matches.size()); assertEquals(size * size * zones.length, matches.size());
before = System.nanoTime(); before = System.nanoTime();
matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___aa*"); matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*__aa*");
after = System.nanoTime(); after = System.nanoTime();
System.out.println("GROUP___aa* in "+((after-before)/1000000000.0f)); System.out.println("GROUP___aa* in " + ((after - before) / 1000000000.0f));
assertEquals(size, matches.size()); assertEquals(size * zones.length, matches.size());
before = System.nanoTime(); before = System.nanoTime();
matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*aa"); matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*__*aa");
after = System.nanoTime(); after = System.nanoTime();
System.out.println("GROUP___*aa in "+((after-before)/1000000000.0f)); System.out.println("GROUP___*aa in " + ((after - before) / 1000000000.0f));
assertEquals(size, matches.size()); assertEquals(size * zones.length, matches.size());
before = System.nanoTime(); before = System.nanoTime();
matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*a"); matches = pubAuthorityService.findAuthorities(AuthorityType.GROUP, "GROUP___*__*a");
after = System.nanoTime(); after = System.nanoTime();
System.out.println("GROUP___*a in "+((after-before)/1000000000.0f)); System.out.println("GROUP___*a in " + ((after - before) / 1000000000.0f));
assertEquals(size*size, matches.size()); assertEquals(size * size * zones.length, matches.size());
// Zone specific
for (String zone : zones)
{
if (zone != null)
{
before = System.nanoTime();
matches = pubAuthorityService.findAuthoritiesInZone(AuthorityType.GROUP, "GROUP___*__a*", zone);
after = System.nanoTime();
System.out.println("GROUP___a* in " + ((after - before) / 1000000000.0f));
assertEquals(size * size, matches.size());
before = System.nanoTime();
matches = pubAuthorityService.findAuthoritiesInZone(AuthorityType.GROUP, "GROUP___*__aa*", zone);
after = System.nanoTime();
System.out.println("GROUP___aa* in " + ((after - before) / 1000000000.0f));
assertEquals(size, matches.size());
before = System.nanoTime();
matches = pubAuthorityService.findAuthoritiesInZone(AuthorityType.GROUP, "GROUP___*__*aa", zone);
after = System.nanoTime();
System.out.println("GROUP___*aa in " + ((after - before) / 1000000000.0f));
assertEquals(size, matches.size());
before = System.nanoTime();
matches = pubAuthorityService.findAuthoritiesInZone(AuthorityType.GROUP, "GROUP___*__*a", zone);
after = System.nanoTime();
System.out.println("GROUP___*a in " + ((after - before) / 1000000000.0f));
assertEquals(size * size, matches.size());
}
}
} }

View File

@@ -330,4 +330,14 @@ public class SimpleAuthorityServiceImpl implements AuthorityService
{ {
} }
public Set<String> findAuthoritiesByShortNameInZone(AuthorityType type, String shortNamePattern, String zone)
{
return Collections.<String>emptySet();
}
public Set<String> findAuthoritiesInZone(AuthorityType type, String namePattern, String zone)
{
return Collections.<String>emptySet();
}
} }

View File

@@ -379,4 +379,25 @@ public interface AuthorityService
*/ */
@NotAuditable @NotAuditable
public Set<String> getDefaultZones(); public Set<String> getDefaultZones();
/**
* Find authorities by pattern matching (* and ?) against the full authority name in a particular zone
* @param type - the authority type
* @param namePattern - the pattern which will be matched against the full authority name.
* @param zone - the zone
* @return the names of the authorities matching the pattern and type.
*/
@Auditable(parameters = {"type"})
public Set<String> findAuthoritiesInZone(AuthorityType type, String namePattern, String zone);
/**
* Find authorities by pattern matching (* and ?) against the authority name.
* @param type - the authority type
* @param shortNamePattern - the pattern which will be matched against the shortName.
* @param zone
* @return the names of the authorities matching the pattern and type.
*/
@Auditable(parameters = {"type"})
public Set<String> findAuthoritiesByShortNameInZone(AuthorityType type, String shortNamePattern, String zone);
} }