Skip to content

Commit

Permalink
ACS-5506 Add properties to authority service
Browse files Browse the repository at this point in the history
  • Loading branch information
MichalKinas committed Jan 17, 2024
1 parent 9dff65f commit e27eac5
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 50 deletions.
34 changes: 17 additions & 17 deletions remote-api/src/main/java/org/alfresco/rest/api/impl/GroupsImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import static org.alfresco.repo.security.authentication.AuthenticationUtil.runAsSystem;

import java.io.Serializable;
import java.text.Collator;
import java.util.AbstractList;
import java.util.ArrayList;
Expand All @@ -40,6 +41,7 @@
import java.util.Set;
import java.util.stream.Collectors;

import org.alfresco.model.ContentModel;
import org.alfresco.query.CannedQueryPageDetails;
import org.alfresco.query.EmptyPagingResults;
import org.alfresco.query.PagingRequest;
Expand Down Expand Up @@ -74,7 +76,6 @@
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.AlfrescoCollator;
import org.alfresco.util.Pair;
Expand All @@ -94,7 +95,6 @@ public class GroupsImpl implements Groups
private static final String ZONE = "zone";
private static final String AUTHORITY_NAME = "authorityName";
private static final String ERR_MSG_MODIFY_FIXED_AUTHORITY = "Trying to modify a fixed authority";
private static final QName PROP_DESCRIPTION = QName.createQName(NamespaceService.CONTENT_MODEL_1_0_URI, "description");

private final static Map<String, String> SORT_PARAMS_TO_NAMES;
static
Expand Down Expand Up @@ -158,7 +158,13 @@ public Group create(Group group, Parameters parameters)
authorityDisplayName = group.getDisplayName();
}

String authority = authorityService.createAuthority(AuthorityType.GROUP, group.getId(), authorityDisplayName, authorityZones);
HashMap<QName, Serializable> props = new HashMap<>();
if (StringUtils.isNotEmpty(group.getDescription()))
{
props.put(ContentModel.PROP_DESCRIPTION, group.getDescription());
}

String authority = authorityService.createAuthority(AuthorityType.GROUP, group.getId(), authorityDisplayName, authorityZones, props);

// Set a given child authority to be included by the given parent
// authorities.
Expand All @@ -167,12 +173,6 @@ public Group create(Group group, Parameters parameters)
authorityService.addAuthority(group.getParentIds(), authority);
}

if (group.getDescription() != null && !group.getDescription().isEmpty())
{
NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(authority);
nodeService.setProperty(groupNodeRef, PROP_DESCRIPTION, group.getDescription());
}

return getGroup(authority, parameters);
}

Expand All @@ -190,10 +190,11 @@ public Group update(String groupId, Group group, Parameters parameters)
handleAuthorityException(ae);
}

if (group.getDescription() != null && !group.getDescription().isEmpty())
if (StringUtils.isNotEmpty(group.getDescription()))
{
NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(authorityService.getName(AuthorityType.GROUP, groupId));
nodeService.setProperty(groupNodeRef, PROP_DESCRIPTION, group.getDescription());
HashMap<QName, Serializable> props = new HashMap<>();
props.put(ContentModel.PROP_DESCRIPTION, group.getDescription());
authorityDAO.setAuthorityProperties(authorityService.getName(AuthorityType.GROUP, groupId), props);
}

return getGroup(groupId, parameters);
Expand Down Expand Up @@ -615,9 +616,9 @@ private Group getGroup(AuthorityInfo authorityInfo, List<String> includeParam, S
group.setHasSubgroups(!authorityService.getContainedAuthorities(AuthorityType.GROUP, authorityInfo.getAuthorityName(), true).isEmpty());

NodeRef groupNodeRef = authorityService.getAuthorityNodeRef(authorityInfo.getAuthorityName());
String description = nodeService.getProperty(groupNodeRef, PROP_DESCRIPTION) != null ?
nodeService.getProperty(groupNodeRef, PROP_DESCRIPTION).toString() :
"";
String description = nodeService.getProperty(groupNodeRef, ContentModel.PROP_DESCRIPTION) != null ?
nodeService.getProperty(groupNodeRef, ContentModel.PROP_DESCRIPTION).toString() :
null;
group.setDescription(description);

// Optionally include
Expand Down Expand Up @@ -886,8 +887,7 @@ public void deleteGroupMembership(String groupId, String groupMemberId)
validateGroupMemberId(groupMemberId);

// Verify if groupMemberId is member of groupId
AuthorityType authorityType = AuthorityType.getAuthorityType(groupMemberId);
Set<String> parents = authorityService.getContainingAuthorities(authorityType, groupMemberId, true);
Set<String> parents = authorityService.getContainingAuthorities(AuthorityType.GROUP, groupMemberId, true);
if (!parents.contains(groupId))
{
throw new NotFoundException(groupMemberId + " is not member of " + groupId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
/*
* #%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 <http://www.gnu.org/licenses/>.
* #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 <http://www.gnu.org/licenses/>.
* #L%
*/
package org.alfresco.repo.security.authority;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.alfresco.model.ContentModel;
Expand All @@ -34,6 +36,7 @@
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.AuthorityService.AuthorityFilter;
import org.alfresco.service.namespace.QName;

public interface AuthorityDAO
{
Expand Down Expand Up @@ -61,6 +64,11 @@ public interface AuthorityDAO
*/
void createAuthority(String name, String authorityDisplayName, Set<String> authorityZones);

/**
* Create an authority with properties.
*/
void createAuthority(String name, String authorityDisplayName, Set<String> authorityZones, Map<QName, Serializable> properties);

/**
* Delete an authority.
*/
Expand Down Expand Up @@ -142,6 +150,11 @@ public interface AuthorityDAO
* Set the display name for an authority
*/
void setAuthorityDisplayName(String authorityName, String authorityDisplayName);

/**
* Set the properties for an authority
*/
void setAuthorityProperties(String authorityName, Map<QName, Serializable> properties);

/**
* Get root authorities
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
import org.alfresco.util.SearchLanguageConversion;
import org.alfresco.util.registry.NamedObjectRegistry;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
Expand Down Expand Up @@ -378,27 +379,35 @@ public void addAuthority(Collection<String> parentNames, String childName)
}
}

public void createAuthority(String name, String authorityDisplayName, Set<String> authorityZones)
public void createAuthority(String name, String authorityDisplayName, Set<String> authorityZones) {
createAuthority(name, authorityDisplayName, authorityZones, null);
}

public void createAuthority(String name, String authorityDisplayName, Set<String> authorityZones, Map<QName, Serializable> properties)
{
HashMap<QName, Serializable> props = new HashMap<QName, Serializable>();
HashMap<QName, Serializable> props = new HashMap<>();
/* MNT-11749 : Alfresco allows to create authorities with different char cases, but disallow duplicates */
props.put(ContentModel.PROP_NAME, DigestUtils.md5Hex(name));
props.put(ContentModel.PROP_AUTHORITY_NAME, name);
props.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, authorityDisplayName);
if (MapUtils.isNotEmpty(properties))
{
props.putAll(properties);
}
NodeRef childRef;
NodeRef authorityContainerRef = getAuthorityContainer();
childRef = nodeService.createNode(authorityContainerRef, ContentModel.ASSOC_CHILDREN, QName.createQName("cm", name, namespacePrefixResolver),
ContentModel.TYPE_AUTHORITY_CONTAINER, props).getChildRef();
if (authorityZones != null)
{
Set<NodeRef> zoneRefs = new HashSet<NodeRef>(authorityZones.size() * 2);
Set<NodeRef> zoneRefs = new HashSet<>(authorityZones.size() * 2);
String currentUserDomain = tenantService.getCurrentUserDomain();
for (String authorityZone : authorityZones)
{
zoneRefs.add(getOrCreateZone(authorityZone));
zoneAuthorityCache.remove(new Pair<String, String>(currentUserDomain, authorityZone));
zoneAuthorityCache.remove(new Pair<>(currentUserDomain, authorityZone));
}
zoneAuthorityCache.remove(new Pair<String, String>(currentUserDomain, null));
zoneAuthorityCache.remove(new Pair<>(currentUserDomain, null));
nodeService.addChild(zoneRefs, childRef, ContentModel.ASSOC_IN_ZONE, QName.createQName("cm", name, namespacePrefixResolver));
}
authorityLookupCache.put(cacheKey(name), childRef);
Expand Down Expand Up @@ -1434,6 +1443,16 @@ public void setAuthorityDisplayName(String authorityName, String authorityDispla

}

public void setAuthorityProperties(String authorityName, Map<QName, Serializable> properties)
{
NodeRef ref = getAuthorityOrNull(authorityName);
if (ref == null)
{
return;
}
properties.forEach((key, value) -> nodeService.setProperty(ref, key, value));
}

public NodeRef getOrCreateZone(String zoneName)
{
return getOrCreateZone(zoneName, true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
*/
package org.alfresco.repo.security.authority;

import java.io.Serializable;
import java.util.AbstractSet;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeSet;

Expand All @@ -53,6 +55,7 @@
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PermissionService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.namespace.QName;
import org.alfresco.util.Pair;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.extensions.surf.util.ParameterCheck;
Expand Down Expand Up @@ -543,6 +546,14 @@ public String createAuthority(AuthorityType type, String shortName)
{
return createAuthority(type, shortName, shortName, getDefaultZones());
}

/**
* {@inheritDoc}
*/
public String createAuthority(AuthorityType type, String shortName, Map<QName, Serializable> properties)
{
return createAuthority(type, shortName, shortName, getDefaultZones(), properties);
}

/**
* {@inheritDoc}
Expand Down Expand Up @@ -643,12 +654,21 @@ public boolean authorityExists(String name)
*/
public String createAuthority(AuthorityType type, String shortName, String authorityDisplayName,
Set<String> authorityZones)
{
return createAuthority(type, shortName, authorityDisplayName, authorityZones, null);
}

/**
* {@inheritDoc}
*/
public String createAuthority(AuthorityType type, String shortName, String authorityDisplayName,
Set<String> authorityZones, Map<QName, Serializable> properties)
{
checkTypeIsMutable(type);
String name = getName(type, shortName);

authorityDAO.createAuthority(name, authorityDisplayName, authorityZones);
authorityDAO.createAuthority(name, authorityDisplayName, authorityZones, properties);

return name;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
*/
package org.alfresco.service.cmr.security;

import java.io.Serializable;
import java.util.Collection;
import java.util.Map;
import java.util.Set;

import org.alfresco.api.AlfrescoPublicApi;
Expand All @@ -35,6 +37,7 @@
import org.alfresco.service.Auditable;
import org.alfresco.service.NotAuditable;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.namespace.QName;

/**
* The service that encapsulates authorities granted to users.
Expand Down Expand Up @@ -225,6 +228,24 @@ public interface AuthorityService
@Auditable(parameters = {"type", "shortName"})
public String createAuthority(AuthorityType type, String shortName);

/**
* Create an authority with properties.
*
* @param type -
* the type of the authority
* @param shortName -
* the short name of the authority to create
* this will also be set as the default display name for the authority
*
* @param properties -
* properties that will be added to authority
*
* @return the name of the authority (this will be the prefix, if any
* associated with the type appended with the short name)
*/
@Auditable(parameters = {"type", "shortName"})
public String createAuthority(AuthorityType type, String shortName, Map<QName, Serializable> properties);

/**
* Create an authority with a display name and zone.
*
Expand All @@ -242,6 +263,27 @@ public interface AuthorityService
@Auditable(parameters = {"type", "shortName", "authorityDisplayName", "authorityZones"})
public String createAuthority(AuthorityType type, String shortName, String authorityDisplayName, Set<String> authorityZones);

/**
* Create an authority with a display name and zone.
*
* @param type
* the type of the authority
* @param shortName
* the short name of the authority to create
* @param authorityDisplayName
* the display name for the authority
* @param authorityZones
* identifier for external user registry owning the authority or <code>null</code> if not applicable
*
* @param properties -
* properties that will be added to authority
*
* @return the full name of the authority (this will be the prefix, if any associated with the type appended with
* the short name)
*/
@Auditable(parameters = {"type", "shortName", "authorityDisplayName", "authorityZones"})
public String createAuthority(AuthorityType type, String shortName, String authorityDisplayName, Set<String> authorityZones, Map<QName, Serializable> properties);

/**
* Set an authority to include another authority. For example, adding a
* group to a group or adding a user to a group.
Expand Down
Loading

0 comments on commit e27eac5

Please sign in to comment.