mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-10-15 15:02:20 +00:00
125603 rmunteanu: Merged 5.1.1 (5.1.1) to 5.1.N (5.1.2) 125484 slanglois: MNT-16155 Update source headers - remove old Copyrights from Java and JSP dource files git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@125781 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
144 lines
4.6 KiB
Java
144 lines
4.6 KiB
Java
package org.alfresco.repo.security.person;
|
|
|
|
import java.util.HashMap;
|
|
import java.util.HashSet;
|
|
import java.util.Map;
|
|
import java.util.Set;
|
|
|
|
import org.alfresco.service.cmr.repository.NodeRef;
|
|
import org.alfresco.service.cmr.repository.StoreRef;
|
|
import org.alfresco.service.cmr.search.ResultSet;
|
|
import org.alfresco.service.cmr.search.SearchService;
|
|
import org.alfresco.service.cmr.security.AuthorityService;
|
|
import org.alfresco.service.cmr.security.AuthorityType;
|
|
|
|
/**
|
|
* @since 3.4.e
|
|
* @author Nick Smith
|
|
*
|
|
*/
|
|
public class TestGroupManager
|
|
{
|
|
private final AuthorityService authorityService;
|
|
private final SearchService searchService;
|
|
|
|
private final Map<String, NodeRef> groups = new HashMap<String, NodeRef>();
|
|
|
|
public TestGroupManager(AuthorityService authorityService, SearchService searchService)
|
|
{
|
|
this.authorityService = authorityService;
|
|
this.searchService = searchService;
|
|
}
|
|
|
|
/**
|
|
* Creates a group with the given name if one does not already exist.
|
|
* @param groupShortName String
|
|
* @return The group's full name.
|
|
*/
|
|
public String createGroupIfNotExist(String groupShortName)
|
|
{
|
|
String fullName = authorityService.getName(AuthorityType.GROUP, groupShortName);
|
|
if (groups.containsKey(groupShortName) == false)
|
|
{
|
|
if (authorityService.authorityExists(fullName) == false)
|
|
{
|
|
Set<String> zones = new HashSet<String>(2, 1.0f);
|
|
zones.add(AuthorityService.ZONE_APP_DEFAULT);
|
|
fullName = authorityService.createAuthority(AuthorityType.GROUP, groupShortName, groupShortName, zones);
|
|
groups.put(groupShortName, findGroupNode(groupShortName));
|
|
}
|
|
}
|
|
return fullName;
|
|
}
|
|
|
|
/**
|
|
* Adds the child group as a sub-authority of the parent group. Creates the
|
|
* child group and parent group if they do not exist.
|
|
*
|
|
* @param parentGroupShortName String
|
|
* @param childGroupShortName String
|
|
* @return The full name of the child group.
|
|
*/
|
|
public String addGroupToParent(String parentGroupShortName, String childGroupShortName)
|
|
{
|
|
String parentFullName = createGroupIfNotExist(parentGroupShortName);
|
|
String groupFullName = createGroupIfNotExist(childGroupShortName);
|
|
authorityService.addAuthority(parentFullName, groupFullName);
|
|
return groupFullName;
|
|
}
|
|
|
|
/**
|
|
* Adds the user as a sub-authroity of the specified group.
|
|
* Creates the group if it doesn't exist.
|
|
*
|
|
* @param groupShortName String
|
|
* @param userName String
|
|
*/
|
|
public void addUserToGroup(String groupShortName, String userName)
|
|
{
|
|
String fullGroupName = createGroupIfNotExist(groupShortName);
|
|
authorityService.addAuthority(fullGroupName, userName);
|
|
}
|
|
|
|
public void deleteGroup(String groupShortName)
|
|
{
|
|
String groupFullName = authorityService.getName(AuthorityType.GROUP, groupShortName);
|
|
if (authorityService.authorityExists(groupFullName))
|
|
{
|
|
authorityService.deleteAuthority(groupFullName);
|
|
}
|
|
}
|
|
|
|
public void clearGroups()
|
|
{
|
|
for (String group : groups.keySet())
|
|
{
|
|
String fullName = authorityService.getName(AuthorityType.GROUP, group);
|
|
if(authorityService.authorityExists(fullName))
|
|
{
|
|
authorityService.deleteAuthority(fullName);
|
|
}
|
|
}
|
|
groups.clear();
|
|
}
|
|
|
|
public NodeRef get(String groupShortName)
|
|
{
|
|
NodeRef result = groups.get(groupShortName);
|
|
if(result == null)
|
|
{
|
|
result = findGroupNode(groupShortName);
|
|
}
|
|
return result;
|
|
}
|
|
|
|
private NodeRef findGroupNode(String groupShortName)
|
|
{
|
|
//TODO Use new AuthorityService.getNode() method on HEAD
|
|
NodeRef group = null;
|
|
|
|
String query = "+TYPE:\"cm:authorityContainer\" AND @cm\\:authorityName:*" + groupShortName;
|
|
|
|
ResultSet results = null;
|
|
try
|
|
{
|
|
results = searchService.query(
|
|
new StoreRef(StoreRef.PROTOCOL_WORKSPACE, "SpacesStore"),
|
|
SearchService.LANGUAGE_LUCENE, query);
|
|
|
|
if (results.length() > 0)
|
|
{
|
|
group = results.getNodeRefs().get(0);
|
|
}
|
|
}
|
|
finally
|
|
{
|
|
if (results != null)
|
|
{
|
|
results.close();
|
|
}
|
|
}
|
|
return group;
|
|
}
|
|
}
|