RINF 50: Authority Service - minor refactor

- ALF-9128 - refactor internals (to clearly split "find" from "list")

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@28417 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Jan Vonka
2011-06-16 08:19:34 +00:00
parent 266afe56d7
commit 9dd2d98dd8
4 changed files with 69 additions and 55 deletions

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2010 Alfresco Software Limited. * Copyright (C) 2005-2011 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -125,7 +125,16 @@ public interface AuthorityDAO
* @param authorityDisplayName * @param authorityDisplayName
*/ */
void setAuthorityDisplayName(String authorityName, String authorityDisplayName); void setAuthorityDisplayName(String authorityName, String authorityDisplayName);
/**
* Get root authorities
*
* @param type
* @param zoneName
* @return
*/
public Set<String> getRootAuthorities(AuthorityType type, String zoneName);
/** /**
* Find authorities by display name pattern. * Find authorities by display name pattern.
* *

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2010 Alfresco Software Limited. * Copyright (C) 2005-2011 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -279,7 +279,18 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
} }
return authorities; return authorities;
} }
public Set<String> getRootAuthorities(AuthorityType type, String zoneName)
{
NodeRef container = (zoneName == null ? getAuthorityContainer() : getZone(zoneName));
if (container == null)
{
// The zone doesn't even exist so there are no root authorities
return Collections.emptySet();
}
return getRootAuthoritiesUnderContainer(container, type);
}
public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate,
String displayNamePattern, String zoneName) String displayNamePattern, String zoneName)
@@ -287,24 +298,18 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
Pattern pattern = displayNamePattern == null ? null : Pattern.compile(SearchLanguageConversion.convert( Pattern pattern = displayNamePattern == null ? null : Pattern.compile(SearchLanguageConversion.convert(
SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, displayNamePattern), SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, displayNamePattern),
Pattern.CASE_INSENSITIVE); Pattern.CASE_INSENSITIVE);
// Use SQL to determine root authorities // Use SQL to determine root authorities
Set<String> rootAuthorities = null; Set<String> rootAuthorities = null;
if (parentAuthority == null && immediate) if (parentAuthority == null && immediate)
{ {
NodeRef container = zoneName == null ? getAuthorityContainer() : getZone(zoneName); rootAuthorities = getRootAuthorities(type, zoneName);
if (container == null)
{
// The zone doesn't even exist so there are no root authorities
return Collections.emptySet();
}
rootAuthorities = getRootAuthoritiesUnderContainer(container, type);
if (pattern == null) if (pattern == null)
{ {
return rootAuthorities; return rootAuthorities;
} }
} }
// Use a Lucene search for other criteria // Use a Lucene search for other criteria
Set<String> authorities = new TreeSet<String>(); Set<String> authorities = new TreeSet<String>();
SearchParameters sp = new SearchParameters(); SearchParameters sp = new SearchParameters();
@@ -441,9 +446,9 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
{ {
throw new UnknownAuthorityException("An authority was not found for " + name); throw new UnknownAuthorityException("An authority was not found for " + name);
} }
Set<String> authorities = new TreeSet<String>(); Set<String> authorities = new TreeSet<String>();
findAuthorities(type, nodeRef, authorities, false, !immediate, false); listAuthorities(type, nodeRef, authorities, false, !immediate, false);
return authorities; return authorities;
} }
} }
@@ -481,8 +486,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
if (authorities == null) if (authorities == null)
{ {
authorities = new TreeSet<String>(); authorities = new TreeSet<String>();
findAuthorities(null, name, authorities, true, true); listAuthorities(null, name, authorities, true, true);
userAuthorityCache.put(name, authorities); userAuthorityCache.put(name, authorities);
} }
// If we wanted the unfiltered set we are done // If we wanted the unfiltered set we are done
if (type == null) if (type == null)
@@ -500,8 +505,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
// Otherwise, crawl the DB for the answer // Otherwise, crawl the DB for the answer
else else
{ {
Set<String> authorities = new TreeSet<String>(); Set<String> authorities = new TreeSet<String>();
findAuthorities(type, name, authorities, true, !immediate); listAuthorities(type, name, authorities, true, !immediate);
return authorities; return authorities;
} }
} }
@@ -565,8 +570,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
} }
} }
} }
private void findAuthorities(AuthorityType type, String name, Set<String> authorities, boolean parents, boolean recursive) private void listAuthorities(AuthorityType type, String name, Set<String> authorities, boolean parents, boolean recursive)
{ {
AuthorityType localType = AuthorityType.getAuthorityType(name); AuthorityType localType = AuthorityType.getAuthorityType(name);
if (localType.equals(AuthorityType.GUEST)) if (localType.equals(AuthorityType.GUEST))
@@ -576,10 +581,10 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
else else
{ {
NodeRef ref = getAuthorityOrNull(name); NodeRef ref = getAuthorityOrNull(name);
if (ref != null) if (ref != null)
{ {
findAuthorities(type, ref, authorities, parents, recursive, false); listAuthorities(type, ref, authorities, parents, recursive, false);
} }
else if (!localType.equals(AuthorityType.USER)) else if (!localType.equals(AuthorityType.USER))
{ {
@@ -589,8 +594,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
} }
} }
} }
private void findAuthorities(AuthorityType type, NodeRef nodeRef, Set<String> authorities, boolean parents, boolean recursive, boolean includeNode) private void listAuthorities(AuthorityType type, NodeRef nodeRef, Set<String> authorities, boolean parents, boolean recursive, boolean includeNode)
{ {
if (includeNode) if (includeNode)
{ {
@@ -600,24 +605,24 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
: ContentModel.PROP_USERNAME)); : ContentModel.PROP_USERNAME));
addAuthorityNameIfMatches(authorities, authorityName, type, null); addAuthorityNameIfMatches(authorities, authorityName, type, null);
} }
// Loop over children if we want immediate children or are in recursive mode // Loop over children if we want immediate children or are in recursive mode
if (!includeNode || recursive) if (!includeNode || recursive)
{ {
if (parents) if (parents)
{ {
List<ChildAssociationRef> cars = nodeService.getParentAssocs(nodeRef, ContentModel.ASSOC_MEMBER, RegexQNamePattern.MATCH_ALL); List<ChildAssociationRef> cars = nodeService.getParentAssocs(nodeRef, ContentModel.ASSOC_MEMBER, RegexQNamePattern.MATCH_ALL);
for (ChildAssociationRef car : cars) for (ChildAssociationRef car : cars)
{ {
findAuthorities(type, car.getParentRef(), authorities, true, recursive, true); listAuthorities(type, car.getParentRef(), authorities, true, recursive, true);
} }
} }
else else
{ {
List<ChildAssociationRef> cars = nodeService.getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, List<ChildAssociationRef> cars = nodeService.getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL,
RegexQNamePattern.MATCH_ALL, false); RegexQNamePattern.MATCH_ALL, false);
// Take advantage of the fact that the authority name is on the child association // Take advantage of the fact that the authority name is on the child association
for (ChildAssociationRef car : cars) for (ChildAssociationRef car : cars)
{ {
@@ -625,14 +630,14 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
AuthorityType childType = AuthorityType.getAuthorityType(childName); AuthorityType childType = AuthorityType.getAuthorityType(childName);
addAuthorityNameIfMatches(authorities, childName, type, null); addAuthorityNameIfMatches(authorities, childName, type, null);
if (recursive && childType != AuthorityType.USER) if (recursive && childType != AuthorityType.USER)
{ {
findAuthorities(type, car.getChildRef(), authorities, false, true, false); listAuthorities(type, car.getChildRef(), authorities, false, true, false);
} }
} }
} }
} }
} }
private NodeRef getAuthorityOrNull(String name) private NodeRef getAuthorityOrNull(String name)
{ {
try try
@@ -902,16 +907,16 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor
if (type != null && type.equals(AuthorityType.USER)) if (type != null && type.equals(AuthorityType.USER))
{ {
return Collections.<String> emptySet(); return Collections.<String> emptySet();
} }
Collection<ChildAssociationRef> childRefs = nodeService.getChildAssocsWithoutParentAssocsOfType(container, ContentModel.ASSOC_MEMBER); Collection<ChildAssociationRef> childRefs = nodeService.getChildAssocsWithoutParentAssocsOfType(container, ContentModel.ASSOC_MEMBER);
Set<String> authorities = new TreeSet<String>(); Set<String> authorities = new TreeSet<String>();
for (ChildAssociationRef childRef : childRefs) for (ChildAssociationRef childRef : childRefs)
{ {
addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, null); addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, null);
} }
return authorities; return authorities;
} }
// Listen out for person removals so that we can clear cached authorities // Listen out for person removals so that we can clear cached authorities
public void beforeDeleteNode(NodeRef nodeRef) public void beforeDeleteNode(NodeRef nodeRef)
{ {

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2010 Alfresco Software Limited. * Copyright (C) 2005-2011 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -207,15 +207,15 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
// Check named guest and admin users // Check named guest and admin users
Set<String> adminUsers = this.authenticationService.getDefaultAdministratorUserNames(); Set<String> adminUsers = this.authenticationService.getDefaultAdministratorUserNames();
Set<String> guestUsers = this.authenticationService.getDefaultGuestUserNames(); Set<String> guestUsers = this.authenticationService.getDefaultGuestUserNames();
String defaultGuestName = AuthenticationUtil.getGuestUserName(); String defaultGuestName = AuthenticationUtil.getGuestUserName();
if (defaultGuestName != null && defaultGuestName.length() > 0) if (defaultGuestName != null && defaultGuestName.length() > 0)
{ {
guestUsers.add(defaultGuestName); guestUsers.add(defaultGuestName);
} }
// Check for name matches using MT + case sensitivity rules // Check for name matches using MT + case sensitivity rules
boolean isAdminUser = containsMatch(adminUsers, currentUserName); boolean isAdminUser = containsMatch(adminUsers, currentUserName);
boolean isGuestUser = containsMatch(guestUsers, currentUserName); boolean isGuestUser = containsMatch(guestUsers, currentUserName);
@@ -296,7 +296,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
} }
return authorities; return authorities;
} }
public void addAuthority(String parentName, String childName) public void addAuthority(String parentName, String childName)
{ {
addAuthority(Collections.singleton(parentName), childName); addAuthority(Collections.singleton(parentName), childName);
@@ -377,7 +377,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
public Set<String> getAllRootAuthorities(AuthorityType type) public Set<String> getAllRootAuthorities(AuthorityType type)
{ {
return authorityDAO.findAuthorities(type, null, true, null, null); return getAllRootAuthoritiesInZone(null, type);
} }
public Set<String> getContainedAuthorities(AuthorityType type, String name, boolean immediate) public Set<String> getContainedAuthorities(AuthorityType type, String name, boolean immediate)
@@ -469,7 +469,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean
public Set<String> getAllRootAuthoritiesInZone(String zoneName, AuthorityType type) public Set<String> getAllRootAuthoritiesInZone(String zoneName, AuthorityType type)
{ {
return authorityDAO.findAuthorities(type, null, true, null, zoneName); return authorityDAO.getRootAuthorities(type, zoneName);
} }
public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate,

View File

@@ -1,5 +1,5 @@
/* /*
* Copyright (C) 2005-2010 Alfresco Software Limited. * Copyright (C) 2005-2011 Alfresco Software Limited.
* *
* This file is part of Alfresco * This file is part of Alfresco
* *
@@ -374,13 +374,13 @@ public interface AuthorityService
public Set<String> getAllAuthoritiesInZone(String zoneName, AuthorityType type); public Set<String> getAllAuthoritiesInZone(String zoneName, AuthorityType type);
/** /**
* Gets the names of all authorities in a zone, optionally filtered by type. * Gets the names of all root authorities in a zone, optionally filtered by type.
* *
* @param zoneName * @param zoneName
* the zone name * the zone name
* @param type * @param type
* the authority type to filter by or <code>null</code> for all authority types * the authority type to filter by or <code>null</code> for all authority types
* @return the names of all authorities in a zone, optionally filtered by type * @return the names of all root authorities in a zone, optionally filtered by type
*/ */
@Auditable(parameters = {"zoneName", "type"}) @Auditable(parameters = {"zoneName", "type"})
public Set<String> getAllRootAuthoritiesInZone(String zoneName, AuthorityType type); public Set<String> getAllRootAuthoritiesInZone(String zoneName, AuthorityType type);
@@ -408,9 +408,10 @@ public interface AuthorityService
@NotAuditable @NotAuditable
public Set<String> getDefaultZones(); public Set<String> getDefaultZones();
/** /**
* Find authorities by pattern matching (* and ?) against the authority name. * Search for authorities by pattern matching (* and ?) against the authority name.
* Note: This will use a search index to find the results (eg. via Lucene / SOLR).
*
* @param type * @param type
* @param parentAuthority if non-null, will look only for authorities who are a child of the named parent * @param parentAuthority if non-null, will look only for authorities who are a child of the named parent
* @param immediate if <code>true</code> then only search root groups if parentAuthority is null, or immediate children of parentAuthority if it is non-null. * @param immediate if <code>true</code> then only search root groups if parentAuthority is null, or immediate children of parentAuthority if it is non-null.
@@ -419,6 +420,5 @@ public interface AuthorityService
* @return * @return
*/ */
@Auditable(parameters = {"type"}) @Auditable(parameters = {"type"})
public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, public Set<String> findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, String displayNamePattern, String zoneName);
String displayNamePattern, String zoneName);
} }