Files
alfresco-community-repo/source/test-java/org/alfresco/repo/security/person/TestPersonManager.java
Alan Davis 01d3791d72 Merged 5.0.N (5.0.3) to HEAD (5.1/Cloud)
108605: MNT-14413: Merged 5.0.1 (5.0.1.4) to 5.0.N (5.0.3)
      108254: MNT-14366 : Re-created user does not have access to his existing workflows
         - get owner property from initiatorhome nodeRef if initiator nodeRef no longer exists,
      108318: MNT-14366 : Re-created user does not have access to his existing workflows
         - fix for failed tests,
      108467: MNT-14366 : Re-created user does not have access to his existing workflows
         - test to check functionality is working 


git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@108616 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
2015-07-21 06:31:08 +00:00

158 lines
5.7 KiB
Java

/*
* Copyright (C) 2005-2010 Alfresco Software Limited.
*
* This file is part of Alfresco
*
* 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/>.
*/
package org.alfresco.repo.security.person;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.model.ContentModel;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.PropertyMap;
/**
* Utility class to help write tests which require creation of people.
* @author Nick Smith
*
*/
public class TestPersonManager
{
private static final String ORGANISATION_SUFFIX = "Organisation";
private static final String JOB_SUFFIX = "JobTitle";
private static final String EMAIL_SUFFIX = "@email.com";
private static final String LAST_NAME_SUFFIX = "LastName";
private static final String FIRST_NAME_SUFFIX = "FirstName";
private final MutableAuthenticationService authenticationService;
private final PersonService personService;
private final NodeService nodeService;
private final Map<String, NodeRef> people = new HashMap<String, NodeRef>();
public TestPersonManager(MutableAuthenticationService authenticationService,
PersonService personService,
NodeService nodeService)
{
this.authenticationService = authenticationService;
this.personService = personService;
this.nodeService = nodeService;
}
public NodeRef createPerson(final String userName)
{
return AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<NodeRef>()
{
public NodeRef doWork() throws Exception
{
if (authenticationService.authenticationExists(userName) == false)
{
authenticationService.createAuthentication(userName, "password".toCharArray());
return makePersonNode(userName);
}
else
{
return personService.getPerson(userName);
}
}
}, AuthenticationUtil.getSystemUserName());
}
public void deletePerson(final String userName)
{
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>()
{
public Void doWork() throws Exception
{
personService.deletePerson(userName);
return null;
}
}, AuthenticationUtil.getSystemUserName());
people.remove(userName);
AuthenticationUtil.clearCurrentSecurityContext();
}
private NodeRef makePersonNode(String userName)
{
PropertyMap personProps = makePersonProperties(userName);
NodeRef person = personService.createPerson(personProps);
people.put(userName, person);
return person;
}
public static PropertyMap makePersonProperties(String userName)
{
PropertyMap personProps = new PropertyMap();
personProps.put(ContentModel.PROP_USERNAME, userName);
personProps.put(ContentModel.PROP_FIRSTNAME, userName+FIRST_NAME_SUFFIX);
personProps.put(ContentModel.PROP_LASTNAME, userName+LAST_NAME_SUFFIX);
personProps.put(ContentModel.PROP_EMAIL, userName+EMAIL_SUFFIX);
personProps.put(ContentModel.PROP_JOBTITLE, userName+JOB_SUFFIX);
personProps.put(ContentModel.PROP_JOBTITLE, userName+ORGANISATION_SUFFIX);
return personProps;
}
public NodeRef get(String userName)
{
NodeRef person = people.get(userName);
if(person !=null)
return person;
else throw new IllegalArgumentException("Cannot get user as unregistered person:"+userName);
}
public void setUser(String userName)
{
if(people.containsKey(userName))
{
AuthenticationUtil.setFullyAuthenticatedUser(userName);
}
else throw new IllegalArgumentException("Cannot set user as unregistered person: "+userName);
}
public void clearPeople()
{
AuthenticationUtil.runAs(new AuthenticationUtil.RunAsWork<Void>()
{
public Void doWork() throws Exception
{
for (String user : people.keySet()) {
personService.deletePerson(user);
}
return null;
}
}, AuthenticationUtil.getSystemUserName());
AuthenticationUtil.clearCurrentSecurityContext();
}
public String getFirstName(String userName)
{
NodeRef person = get(userName);
return (String) nodeService.getProperty(person, ContentModel.PROP_FIRSTNAME);
}
public String getLastName(String userName)
{
NodeRef person = get(userName);
return (String) nodeService.getProperty(person, ContentModel.PROP_LASTNAME);
}
}