diff --git a/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java b/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java index d4baaf8a68..4ad3b9ca10 100644 --- a/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java +++ b/source/java/org/alfresco/repo/security/authority/AuthorityDAOImpl.java @@ -1,28 +1,28 @@ -/* - * #%L - * Alfresco Repository - * %% - * Copyright (C) 2005 - 2016 Alfresco Software Limited - * %% - * This file is part of the Alfresco software. - * If the software was purchased under a paid Alfresco license, the terms of - * the paid license agreement will prevail. Otherwise, the software is - * provided under the following open source license terms: - * - * Alfresco is free software: you can redistribute it and/or modify - * it under the terms of the GNU Lesser General Public License as published by - * the Free Software Foundation, either version 3 of the License, or - * (at your option) any later version. - * - * Alfresco is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public License - * along with Alfresco. If not, see . - * #L% - */ +/* + * #%L + * Alfresco Repository + * %% + * Copyright (C) 2005 - 2016 Alfresco Software Limited + * %% + * This file is part of the Alfresco software. + * If the software was purchased under a paid Alfresco license, the terms of + * the paid license agreement will prevail. Otherwise, the software is + * provided under the following open source license terms: + * + * Alfresco is free software: you can redistribute it and/or modify + * it under the terms of the GNU Lesser General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Alfresco is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with Alfresco. If not, see . + * #L% + */ package org.alfresco.repo.security.authority; import java.io.Serializable; @@ -799,8 +799,8 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor childAuthorityCache.remove(parentRef); if (AuthorityType.getAuthorityType(childName) == AuthorityType.USER) { - // Normalize the user name - childName = (String) nodeService.getProperty(childRef, ContentModel.PROP_USERNAME); + // Normalize the user name + childName = (String) nodeService.getProperty(childRef, ContentModel.PROP_USERNAME); userAuthorityCache.remove(childName); } else @@ -860,17 +860,17 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor { // Get the unfiltered set of authorities from the cache or generate it Set authorities = userAuthorityCache.get(name); - // if not in the cache, normalize the user name and try again - if (authorities == null) - { - NodeRef personRef = getAuthorityOrNull(name); - if (personRef == null) - { - return new TreeSet(); // don't worry about missing person nodes. Just return an empty set - } - name = (String) nodeService.getProperty(personRef, ContentModel.PROP_USERNAME); - authorities = userAuthorityCache.get(name); - } + // if not in the cache, normalize the user name and try again + if (authorities == null) + { + NodeRef personRef = getAuthorityOrNull(name); + if (personRef == null) + { + return new TreeSet(); // don't worry about missing person nodes. Just return an empty set + } + name = (String) nodeService.getProperty(personRef, ContentModel.PROP_USERNAME); + authorities = userAuthorityCache.get(name); + } if (authorities == null) { authorities = new TreeSet(); @@ -882,7 +882,7 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor { listAuthorities(null, name, authorities, true, true); } - // Add the set back to the cache. Name has already been normalized. If the value is locked then nothing will happen. + // Add the set back to the cache. Name has already been normalized. If the value is locked then nothing will happen. userAuthorityCache.put(name, Collections.unmodifiableSet(authorities)); } // If we wanted the unfiltered set we are done @@ -1029,29 +1029,36 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor public String getName(AuthorityType type, String shortName) { - String authorityName = shortName; if (type.isFixedString()) { - authorityName = type.getFixedString(); + return type.getFixedString(); } else if (type.isPrefixed()) { - String prefix = type.getPrefixString(); + final String prefix = type.getPrefixString(); + final String authorityName = prefix + shortName; if (shortName.startsWith(prefix)) { String doublePrefixed = prefix + prefix; if (shortName.startsWith(doublePrefixed)) { - throw new AuthorityException("The authority name is double-prefixed"); + throw new AuthorityException("The authority name [" + shortName + "] is double-prefixed"); + } + // MNT-17824: We need this check, in case this method is invoked for retrieving an + // authority (rather than creating a new one), that was created with the shortName prefixed with 'GROUP_' before the MNT-14958 fix. + // As before MNT-14958, if the shortName starts with 'GROUP_', the authority name will end up as 'GROUP_GROUP_' + else if (getAuthorityOrNull(authorityName, type) != null) + { + return authorityName; } } else { - authorityName = prefix + shortName; + return authorityName; + } } - return authorityName; - + return shortName; } protected void addAuthorityNameIfMatches(Set authorities, String authorityName, AuthorityType type) @@ -1285,12 +1292,17 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor } } } - + private NodeRef getAuthorityOrNull(final String name) + { + final AuthorityType authType = AuthorityType.getAuthorityType(name); + return getAuthorityOrNull(name, authType); + } + + private NodeRef getAuthorityOrNull(final String name, final AuthorityType authType) { try { - final AuthorityType authType = AuthorityType.getAuthorityType(name); switch (authType) { case USER: @@ -1302,13 +1314,13 @@ public class AuthorityDAOImpl implements AuthorityDAO, NodeServicePolicies.Befor return null; default: { - Pair cacheKey = cacheKey(name); + Pair cacheKey = cacheKey(name); NodeRef result = authorityLookupCache.get(cacheKey); if (result == null) { - List results = nodeService.getChildAssocs(getAuthorityContainer(), - ContentModel.ASSOC_CHILDREN, QName.createQName("cm", name, namespacePrefixResolver), false); - result = results.isEmpty() ? NULL_NODEREF :results.get(0).getChildRef(); + List results = nodeService.getChildAssocs(getAuthorityContainer(), ContentModel.ASSOC_CHILDREN, + QName.createQName("cm", name, namespacePrefixResolver), false); + result = results.isEmpty() ? NULL_NODEREF : results.get(0).getChildRef(); authorityLookupCache.put(cacheKey, result); } return result.equals(NULL_NODEREF) ? null : result;