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,16 +161,15 @@ 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"));
assertNull(pubAuthorityService.getAuthorityZones("GROUP_NULL")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_NULL"));
assertNull(pubAuthorityService.getAuthorityZones("GROUP_EMPTY")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_EMPTY"));
assertNull(pubAuthorityService.getAuthorityZones("GROUP_1")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_1"));
assertNull(pubAuthorityService.getAuthorityZones("GROUP_2")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_2"));
assertNull(pubAuthorityService.getAuthorityZones("GROUP_3")); assertNull(pubAuthorityService.getAuthorityZones("GROUP_3"));
pubAuthorityService.createAuthority(AuthorityType.GROUP, "DEFAULT"); pubAuthorityService.createAuthority(AuthorityType.GROUP, "DEFAULT");
Set<String> zones = pubAuthorityService.getAuthorityZones("GROUP_DEFAULT"); Set<String> zones = pubAuthorityService.getAuthorityZones("GROUP_DEFAULT");
assertEquals(2, zones.size()); assertEquals(2, zones.size());
@@ -177,47 +177,47 @@ public class AuthorityServiceTest extends TestCase
assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_DEFAULT").size()); assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_DEFAULT").size());
pubAuthorityService.addAuthorityToZones("GROUP_DEFAULT", zones); pubAuthorityService.addAuthorityToZones("GROUP_DEFAULT", zones);
assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_DEFAULT").size()); assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_DEFAULT").size());
HashSet<String> newZones = null; HashSet<String> newZones = null;
pubAuthorityService.createAuthority(AuthorityType.GROUP, "NULL", "NULL", newZones); pubAuthorityService.createAuthority(AuthorityType.GROUP, "NULL", "NULL", newZones);
assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_NULL").size()); assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_NULL").size());
newZones = new HashSet<String>(); newZones = new HashSet<String>();
pubAuthorityService.createAuthority(AuthorityType.GROUP, "EMPTY", "EMPTY", newZones); pubAuthorityService.createAuthority(AuthorityType.GROUP, "EMPTY", "EMPTY", newZones);
assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_EMPTY").size()); assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_EMPTY").size());
newZones.add("One"); newZones.add("One");
pubAuthorityService.createAuthority(AuthorityType.GROUP, "1", "1", newZones); pubAuthorityService.createAuthority(AuthorityType.GROUP, "1", "1", newZones);
assertEquals(1, pubAuthorityService.getAuthorityZones("GROUP_1").size()); assertEquals(1, pubAuthorityService.getAuthorityZones("GROUP_1").size());
newZones.add("Two"); newZones.add("Two");
pubAuthorityService.createAuthority(AuthorityType.GROUP, "2", "2", newZones); pubAuthorityService.createAuthority(AuthorityType.GROUP, "2", "2", newZones);
assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_2").size()); assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_2").size());
newZones.add("Three"); newZones.add("Three");
pubAuthorityService.createAuthority(AuthorityType.GROUP, "3", "3", newZones); pubAuthorityService.createAuthority(AuthorityType.GROUP, "3", "3", newZones);
assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size());
HashSet<String> toRemove = null; HashSet<String> toRemove = null;
pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove); pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove);
assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size());
toRemove = new HashSet<String>(); toRemove = new HashSet<String>();
pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove); pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove);
assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size());
toRemove.add("Three"); toRemove.add("Three");
pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove); pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove);
assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(2, pubAuthorityService.getAuthorityZones("GROUP_3").size());
toRemove.add("Two"); toRemove.add("Two");
pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove); pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove);
assertEquals(1, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(1, pubAuthorityService.getAuthorityZones("GROUP_3").size());
toRemove.add("One"); toRemove.add("One");
pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove); pubAuthorityService.removeAuthorityFromZones("GROUP_3", toRemove);
assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(0, pubAuthorityService.getAuthorityZones("GROUP_3").size());
pubAuthorityService.addAuthorityToZones("GROUP_3", newZones); pubAuthorityService.addAuthorityToZones("GROUP_3", newZones);
assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size()); assertEquals(3, pubAuthorityService.getAuthorityZones("GROUP_3").size());
assertEquals(3, pubAuthorityService.getAllAuthoritiesInZone("One", null).size()); assertEquals(3, pubAuthorityService.getAllAuthoritiesInZone("One", null).size());
@@ -226,19 +226,19 @@ public class AuthorityServiceTest extends TestCase
assertEquals(3, pubAuthorityService.getAllAuthoritiesInZone("One", AuthorityType.GROUP).size()); assertEquals(3, pubAuthorityService.getAllAuthoritiesInZone("One", AuthorityType.GROUP).size());
assertEquals(2, pubAuthorityService.getAllAuthoritiesInZone("Two", AuthorityType.GROUP).size()); assertEquals(2, pubAuthorityService.getAllAuthoritiesInZone("Two", AuthorityType.GROUP).size());
assertEquals(1, pubAuthorityService.getAllAuthoritiesInZone("Three", AuthorityType.GROUP).size()); assertEquals(1, pubAuthorityService.getAllAuthoritiesInZone("Three", AuthorityType.GROUP).size());
assertEquals(3, pubAuthorityService.getAllRootAuthoritiesInZone("One", null).size()); assertEquals(3, pubAuthorityService.getAllRootAuthoritiesInZone("One", null).size());
assertEquals(2, pubAuthorityService.getAllRootAuthoritiesInZone("Two", null).size()); assertEquals(2, pubAuthorityService.getAllRootAuthoritiesInZone("Two", null).size());
assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("Three", null).size()); assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("Three", null).size());
assertEquals(3, pubAuthorityService.getAllRootAuthoritiesInZone("One", AuthorityType.GROUP).size()); assertEquals(3, pubAuthorityService.getAllRootAuthoritiesInZone("One", AuthorityType.GROUP).size());
assertEquals(2, pubAuthorityService.getAllRootAuthoritiesInZone("Two", AuthorityType.GROUP).size()); assertEquals(2, pubAuthorityService.getAllRootAuthoritiesInZone("Two", AuthorityType.GROUP).size());
assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("Three", AuthorityType.GROUP).size()); assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("Three", AuthorityType.GROUP).size());
// I am not convinced of the definition of root within zone ... // I am not convinced of the definition of root within zone ...
pubAuthorityService.addAuthority("GROUP_1", "GROUP_2"); pubAuthorityService.addAuthority("GROUP_1", "GROUP_2");
pubAuthorityService.addAuthority("GROUP_1", "GROUP_3"); pubAuthorityService.addAuthority("GROUP_1", "GROUP_3");
assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("One", null).size()); assertEquals(1, pubAuthorityService.getAllRootAuthoritiesInZone("One", null).size());
assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Two", null).size()); assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Two", null).size());
assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Three", null).size()); assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Three", null).size());
@@ -246,48 +246,91 @@ public class AuthorityServiceTest extends TestCase
assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Two", AuthorityType.GROUP).size()); assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Two", AuthorityType.GROUP).size());
assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Three", AuthorityType.GROUP).size()); assertEquals(0, pubAuthorityService.getAllRootAuthoritiesInZone("Three", AuthorityType.GROUP).size());
} }
public void testGroupWildcards() public void testGroupWildcards()
{ {
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());
}
}
} }
public void testNonAdminUser() public void testNonAdminUser()
@@ -702,7 +745,7 @@ public class AuthorityServiceTest extends TestCase
pubAuthorityService.addAuthority(auth3, auth2); pubAuthorityService.addAuthority(auth3, auth2);
assertEquals(7, pubAuthorityService.getAllAuthorities(AuthorityType.GROUP).size()); assertEquals(7, pubAuthorityService.getAllAuthorities(AuthorityType.GROUP).size());
// Number of root authorities has been reduced since auth2 is no longer an orphan // Number of root authorities has been reduced since auth2 is no longer an orphan
assertEquals(3, pubAuthorityService.getAllRootAuthorities(AuthorityType.GROUP).size()); assertEquals(3, pubAuthorityService.getAllRootAuthorities(AuthorityType.GROUP).size());
// The next call looks for people not users :-) // The next call looks for people not users :-)

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);
} }