From 9dd2d98dd8464c2a63660cf86f414580196e1d0e Mon Sep 17 00:00:00 2001 From: Jan Vonka Date: Thu, 16 Jun 2011 08:19:34 +0000 Subject: [PATCH] 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 --- .../repo/security/authority/AuthorityDAO.java | 13 +++- .../security/authority/AuthorityDAOImpl.java | 73 ++++++++++--------- .../authority/AuthorityServiceImpl.java | 24 +++--- .../cmr/security/AuthorityService.java | 14 ++-- 4 files changed, 69 insertions(+), 55 deletions(-) diff --git a/source/java/org/alfresco/repo/security/authority/AuthorityDAO.java b/source/java/org/alfresco/repo/security/authority/AuthorityDAO.java index b4eb378d1c..e0bef59603 100644 --- a/source/java/org/alfresco/repo/security/authority/AuthorityDAO.java +++ b/source/java/org/alfresco/repo/security/authority/AuthorityDAO.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2011 Alfresco Software Limited. * * This file is part of Alfresco * @@ -125,7 +125,16 @@ public interface AuthorityDAO * @param authorityDisplayName */ void setAuthorityDisplayName(String authorityName, String authorityDisplayName); - + + /** + * Get root authorities + * + * @param type + * @param zoneName + * @return + */ + public Set getRootAuthorities(AuthorityType type, String zoneName); + /** * Find authorities by display name pattern. * diff --git a/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java b/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java index 6e999b2d68..2c4e32c274 100644 --- a/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java +++ b/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2011 Alfresco Software Limited. * * This file is part of Alfresco * @@ -279,7 +279,18 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor } return authorities; } - + + public Set 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 findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, String displayNamePattern, String zoneName) @@ -287,24 +298,18 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor Pattern pattern = displayNamePattern == null ? null : Pattern.compile(SearchLanguageConversion.convert( SearchLanguageConversion.DEF_LUCENE, SearchLanguageConversion.DEF_REGEX, displayNamePattern), Pattern.CASE_INSENSITIVE); - + // Use SQL to determine root authorities Set rootAuthorities = null; if (parentAuthority == null && immediate) { - 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(); - } - rootAuthorities = getRootAuthoritiesUnderContainer(container, type); + rootAuthorities = getRootAuthorities(type, zoneName); if (pattern == null) { return rootAuthorities; } } - + // Use a Lucene search for other criteria Set authorities = new TreeSet(); 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); } - + Set authorities = new TreeSet(); - findAuthorities(type, nodeRef, authorities, false, !immediate, false); + listAuthorities(type, nodeRef, authorities, false, !immediate, false); return authorities; } } @@ -481,8 +486,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor if (authorities == null) { authorities = new TreeSet(); - findAuthorities(null, name, authorities, true, true); - userAuthorityCache.put(name, authorities); + listAuthorities(null, name, authorities, true, true); + userAuthorityCache.put(name, authorities); } // If we wanted the unfiltered set we are done if (type == null) @@ -500,8 +505,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor // Otherwise, crawl the DB for the answer else { - Set authorities = new TreeSet(); - findAuthorities(type, name, authorities, true, !immediate); + Set authorities = new TreeSet(); + listAuthorities(type, name, authorities, true, !immediate); return authorities; } } @@ -565,8 +570,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor } } } - - private void findAuthorities(AuthorityType type, String name, Set authorities, boolean parents, boolean recursive) + + private void listAuthorities(AuthorityType type, String name, Set authorities, boolean parents, boolean recursive) { AuthorityType localType = AuthorityType.getAuthorityType(name); if (localType.equals(AuthorityType.GUEST)) @@ -576,10 +581,10 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor else { NodeRef ref = getAuthorityOrNull(name); - + if (ref != null) { - findAuthorities(type, ref, authorities, parents, recursive, false); + listAuthorities(type, ref, authorities, parents, recursive, false); } 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 authorities, boolean parents, boolean recursive, boolean includeNode) + + private void listAuthorities(AuthorityType type, NodeRef nodeRef, Set authorities, boolean parents, boolean recursive, boolean includeNode) { if (includeNode) { @@ -600,24 +605,24 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor : ContentModel.PROP_USERNAME)); addAuthorityNameIfMatches(authorities, authorityName, type, null); } - + // Loop over children if we want immediate children or are in recursive mode if (!includeNode || recursive) { if (parents) { List cars = nodeService.getParentAssocs(nodeRef, ContentModel.ASSOC_MEMBER, RegexQNamePattern.MATCH_ALL); - + for (ChildAssociationRef car : cars) { - findAuthorities(type, car.getParentRef(), authorities, true, recursive, true); + listAuthorities(type, car.getParentRef(), authorities, true, recursive, true); } } else { List cars = nodeService.getChildAssocs(nodeRef, RegexQNamePattern.MATCH_ALL, RegexQNamePattern.MATCH_ALL, false); - + // Take advantage of the fact that the authority name is on the child association for (ChildAssociationRef car : cars) { @@ -625,14 +630,14 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor AuthorityType childType = AuthorityType.getAuthorityType(childName); addAuthorityNameIfMatches(authorities, childName, type, null); 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) { try @@ -902,16 +907,16 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor if (type != null && type.equals(AuthorityType.USER)) { return Collections. emptySet(); - } + } Collection childRefs = nodeService.getChildAssocsWithoutParentAssocsOfType(container, ContentModel.ASSOC_MEMBER); Set authorities = new TreeSet(); for (ChildAssociationRef childRef : childRefs) { addAuthorityNameIfMatches(authorities, childRef.getQName().getLocalName(), type, null); } - return authorities; + return authorities; } - + // Listen out for person removals so that we can clear cached authorities public void beforeDeleteNode(NodeRef nodeRef) { diff --git a/source/java/org/alfresco/repo/security/authority/AuthorityServiceImpl.java b/source/java/org/alfresco/repo/security/authority/AuthorityServiceImpl.java index 8f4c9443a7..f6c5b7d46c 100644 --- a/source/java/org/alfresco/repo/security/authority/AuthorityServiceImpl.java +++ b/source/java/org/alfresco/repo/security/authority/AuthorityServiceImpl.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2011 Alfresco Software Limited. * * This file is part of Alfresco * @@ -207,15 +207,15 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean // Check named guest and admin users Set adminUsers = this.authenticationService.getDefaultAdministratorUserNames(); - + Set guestUsers = this.authenticationService.getDefaultGuestUserNames(); - - String defaultGuestName = AuthenticationUtil.getGuestUserName(); - if (defaultGuestName != null && defaultGuestName.length() > 0) - { - guestUsers.add(defaultGuestName); - } - + + String defaultGuestName = AuthenticationUtil.getGuestUserName(); + if (defaultGuestName != null && defaultGuestName.length() > 0) + { + guestUsers.add(defaultGuestName); + } + // Check for name matches using MT + case sensitivity rules boolean isAdminUser = containsMatch(adminUsers, currentUserName); boolean isGuestUser = containsMatch(guestUsers, currentUserName); @@ -296,7 +296,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean } return authorities; } - + public void addAuthority(String parentName, String childName) { addAuthority(Collections.singleton(parentName), childName); @@ -377,7 +377,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean public Set getAllRootAuthorities(AuthorityType type) { - return authorityDAO.findAuthorities(type, null, true, null, null); + return getAllRootAuthoritiesInZone(null, type); } public Set getContainedAuthorities(AuthorityType type, String name, boolean immediate) @@ -469,7 +469,7 @@ public class AuthorityServiceImpl implements AuthorityService, InitializingBean public Set getAllRootAuthoritiesInZone(String zoneName, AuthorityType type) { - return authorityDAO.findAuthorities(type, null, true, null, zoneName); + return authorityDAO.getRootAuthorities(type, zoneName); } public Set findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, diff --git a/source/java/org/alfresco/service/cmr/security/AuthorityService.java b/source/java/org/alfresco/service/cmr/security/AuthorityService.java index 1018783815..2917ce74b8 100644 --- a/source/java/org/alfresco/service/cmr/security/AuthorityService.java +++ b/source/java/org/alfresco/service/cmr/security/AuthorityService.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2005-2010 Alfresco Software Limited. + * Copyright (C) 2005-2011 Alfresco Software Limited. * * This file is part of Alfresco * @@ -374,13 +374,13 @@ public interface AuthorityService public Set 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 * the zone name * @param type * the authority type to filter by or null 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"}) public Set getAllRootAuthoritiesInZone(String zoneName, AuthorityType type); @@ -408,9 +408,10 @@ public interface AuthorityService @NotAuditable public Set 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 parentAuthority if non-null, will look only for authorities who are a child of the named parent * @param immediate if true 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 */ @Auditable(parameters = {"type"}) - public Set findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, - String displayNamePattern, String zoneName); + public Set findAuthorities(AuthorityType type, String parentAuthority, boolean immediate, String displayNamePattern, String zoneName); } \ No newline at end of file