RM-722 (REST API - Add and remove authorities from roles)

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/modules/recordsmanagement/HEAD@50274 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Tuna Aksoy
2013-05-22 15:36:44 +00:00
parent b82d57d75d
commit 615afda2ff
13 changed files with 782 additions and 329 deletions

View File

@@ -539,4 +539,22 @@
<property name="siteService" ref="SiteService" />
</bean>
<!-- REST impl for POST Children for RM -->
<bean id="webscript.org.alfresco.repository.groups.rm-children.post"
class="org.alfresco.repo.web.scripts.groups.RmChildrenPost"
parent="webscript">
<property name="filePlanService" ref="FilePlanService" />
<property name="filePlanRoleService" ref="FilePlanRoleService" />
<property name="authorityService" ref="AuthorityService" />
</bean>
<!-- REST impl for DELETE Children for RM -->
<bean id="webscript.org.alfresco.repository.groups.rm-children.delete"
class="org.alfresco.repo.web.scripts.groups.RmChildrenDelete"
parent="webscript">
<property name="filePlanService" ref="FilePlanService" />
<property name="filePlanRoleService" ref="FilePlanRoleService" />
<property name="authorityService" ref="AuthorityService" />
</bean>
</beans>

View File

@@ -0,0 +1,12 @@
<webscript>
<shortname>Remove a group or a user from a role</shortname>
<description><![CDATA[
Removes a group or a user from a role.
]]>
</description>
<url>/api/rm/role/{roleId}/children/{authorityName}</url>
<url>/api/rm/{store_type}/{store_id}/{id}/role/{roleId}/children/{authorityName}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,12 @@
<webscript>
<shortname>Add a group or a user to a role</shortname>
<description><![CDATA[
Adds a group or a user to a role.
]]>
</description>
<url>/api/rm/role/{roleId}/children/{authorityName}</url>
<url>/api/rm/{store_type}/{store_id}/{id}/role/{roleId}/children/{authorityName}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
</webscript>

View File

@@ -0,0 +1,99 @@
/*
* Copyright (C) 2005-2013 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.web.scripts.groups;
import java.util.Map;
import org.alfresco.module.org_alfresco_module_rm.script.admin.RoleDeclarativeWebScript;
import org.alfresco.service.cmr.repository.NodeRef;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptException;
import org.springframework.extensions.webscripts.WebScriptRequest;
import org.tuckey.web.filters.urlrewrite.utils.StringUtils;
/**
* Abstract class for adding/removing a user/group to/from a role
* This class contains the common methods needed in the sub classes.
*
* @author Tuna Aksoy
* @since 2.1
*/
public class RmChildrenAbstract extends RoleDeclarativeWebScript
{
/** Constants for the url parameters */
private static final String ROLE_ID = "roleId";
private static final String AUTHORITY_NAME = "authorityName";
/**
* Util method for getting the nodeRef from the request
*
* @param req The webscript request
* @return The nodeRef passed in the request
*/
protected NodeRef getFilePlan(WebScriptRequest req)
{
NodeRef filePlan = super.getFilePlan(req);
if (filePlan == null)
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "No filePlan was provided on the URL.");
}
return filePlan;
}
/**
* Util method for getting the roleId from the request
*
* @param req The webscript request
* @return The role id passed in the request
*/
protected String getRoleId(WebScriptRequest req)
{
return getParamValue(req, ROLE_ID);
}
/**
* Util method for getting the authorityName from the request
*
* @param req The webscript request
* @return The authorityName passed in the request
*/
protected String getAuthorityName(WebScriptRequest req)
{
return getParamValue(req, AUTHORITY_NAME);
}
/**
* Helper method to get the value of parameter from the request
*
* @param req The webscript request
* @param param The name of the parameter for which the value is requested
* @return The value for the requested parameter
*/
private String getParamValue(WebScriptRequest req, String param)
{
Map<String, String> templateVars = req.getServiceMatch().getTemplateVars();
String authorityName = templateVars.get(param);
if (StringUtils.isBlank(authorityName))
{
throw new WebScriptException(Status.STATUS_NOT_FOUND, "No '" + param + "' was provided on the URL.");
}
return authorityName;
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2005-2013 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.web.scripts.groups;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.cmr.repository.NodeRef;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* Webscript for removing a user or a group from a role
*
* @author Tuna Aksoy
* @since 2.1
*/
public class RmChildrenDelete extends RmChildrenAbstract
{
/**
* @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest,
* org.springframework.extensions.webscripts.Status,
* org.springframework.extensions.webscripts.Cache)
*/
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
NodeRef filePlan = getFilePlan(req);
String roleId = getRoleId(req);
String authorityName = getAuthorityName(req);
filePlanRoleService.unassignRoleFromAuthority(filePlan, roleId, authorityName);
return new HashMap<String, Object>();
}
}

View File

@@ -0,0 +1,53 @@
/*
* Copyright (C) 2005-2013 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.web.scripts.groups;
import java.util.HashMap;
import java.util.Map;
import org.alfresco.service.cmr.repository.NodeRef;
import org.springframework.extensions.webscripts.Cache;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.WebScriptRequest;
/**
* Webscript for adding a user or a group to a role
*
* @author Tuna Aksoy
* @since 2.1
*/
public class RmChildrenPost extends RmChildrenAbstract
{
/**
* @see org.springframework.extensions.webscripts.DeclarativeWebScript#executeImpl(org.springframework.extensions.webscripts.WebScriptRequest,
* org.springframework.extensions.webscripts.Status,
* org.springframework.extensions.webscripts.Cache)
*/
@Override
protected Map<String, Object> executeImpl(WebScriptRequest req, Status status, Cache cache)
{
NodeRef filePlan = getFilePlan(req);
String roleId = getRoleId(req);
String authorityName = getAuthorityName(req);
filePlanRoleService.assignRoleToAuthority(filePlan, roleId, authorityName);
return new HashMap<String, Object>();
}
}

View File

@@ -27,6 +27,7 @@ import org.alfresco.module.org_alfresco_module_rm.test.webscript.EmailMapScriptT
import org.alfresco.module.org_alfresco_module_rm.test.webscript.EventRestApiTest;
import org.alfresco.module.org_alfresco_module_rm.test.webscript.RMCaveatConfigScriptTest;
import org.alfresco.module.org_alfresco_module_rm.test.webscript.RMConstraintScriptTest;
import org.alfresco.module.org_alfresco_module_rm.test.webscript.RmChildrenRestApiTest;
import org.alfresco.module.org_alfresco_module_rm.test.webscript.RoleRestApiTest;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
@@ -51,10 +52,10 @@ import org.junit.runners.Suite.SuiteClasses;
EmailMapScriptTest.class,
EmailMapKeysRestApiTest.class,
CapabilitiesRestApiTest.class,
ActionDefinitionsRestApiTest.class
ActionDefinitionsRestApiTest.class,
//RmClassesRestApiTest.class,
//RmPropertiesRestApiTest.class
//RmPropertiesRestApiTest.class,
RmChildrenRestApiTest.class
})
public class WebScriptTestSuite
{

View File

@@ -51,6 +51,7 @@ import org.alfresco.service.cmr.repository.Period;
import org.alfresco.service.cmr.repository.StoreRef;
import org.alfresco.service.cmr.search.SearchService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.site.SiteInfo;
@@ -60,6 +61,7 @@ import org.alfresco.service.cmr.tagging.TaggingService;
import org.alfresco.service.namespace.NamespaceService;
import org.alfresco.service.namespace.QName;
import org.alfresco.service.transaction.TransactionService;
import org.alfresco.util.PropertyMap;
import org.springframework.context.ApplicationContext;
/**
@@ -228,7 +230,7 @@ public class BaseRMWebScriptTestCase extends BaseWebScriptTest
@Override
public Object execute() throws Throwable
{
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getSystemUserName());
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
setupTestDataImpl();
return null;
}
@@ -330,4 +332,40 @@ public class BaseRMWebScriptTestCase extends BaseWebScriptTest
assertNotNull("Collaboration site document library component was not successfully created.", documentLibrary);
}
protected void createUser(String userName)
{
if (authenticationService.authenticationExists(userName) == false)
{
authenticationService.createAuthentication(userName, "PWD".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
personService.createPerson(ppOne);
}
}
protected void deleteUser(String userName)
{
personService.deletePerson(userName);
}
protected void createGroup(String groupName)
{
if (authorityService.authorityExists(groupName) == false)
{
authorityService.createAuthority(AuthorityType.GROUP, groupName);
}
}
protected void deleteGroup(String groupName)
{
authorityService.deleteAuthority(groupName, true);
}
}

View File

@@ -45,10 +45,7 @@ import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
@SuppressWarnings("unused")
public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase
{
private MutableAuthenticationService authenticationService;
private RMCaveatConfigService caveatConfigService;
private PersonService personService;
protected final static String RM_LIST = "rmc:smListTest";
protected final static String RM_LIST_URI_ELEM = "rmc_smListTest";
@@ -61,29 +58,8 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase
super.initServices();
this.caveatConfigService = (RMCaveatConfigService)getServer().getApplicationContext().getBean("CaveatConfigService");
this.authenticationService = (MutableAuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService");
this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService");
}
private void createUser(String userName)
{
if (this.authenticationService.authenticationExists(userName) == false)
{
this.authenticationService.createAuthentication(userName, "PWD".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
this.personService.createPerson(ppOne);
}
}
public void testGetRMConstraints() throws Exception
{
{
@@ -194,9 +170,9 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase
sendRequest(new GetRequest(url), Status.STATUS_NOT_FOUND);
}
personService.deletePerson("fbloggs");
personService.deletePerson("jrogers");
personService.deletePerson("jdoe");
deleteUser("fbloggs");
deleteUser("jrogers");
deleteUser("jdoe");
@@ -623,9 +599,9 @@ public class RMCaveatConfigScriptTest extends BaseRMWebScriptTestCase
assertTrue("noforn not found", nofornFound);
}
personService.deletePerson("fbloggs");
personService.deletePerson("jrogers");
personService.deletePerson("jdoe");
deleteUser("fbloggs");
deleteUser("jrogers");
deleteUser("jdoe");
}

View File

@@ -21,13 +21,9 @@ package org.alfresco.module.org_alfresco_module_rm.test.webscript;
import java.util.ArrayList;
import java.util.List;
import org.alfresco.model.ContentModel;
import org.alfresco.module.org_alfresco_module_rm.caveat.RMCaveatConfigService;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase;
import org.alfresco.repo.security.authentication.AuthenticationUtil;
import org.alfresco.service.cmr.security.MutableAuthenticationService;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.util.PropertyMap;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer.GetRequest;
@@ -40,9 +36,7 @@ import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
*/
public class RMConstraintScriptTest extends BaseRMWebScriptTestCase
{
private MutableAuthenticationService authenticationService;
private RMCaveatConfigService caveatConfigService;
private PersonService personService;
protected final static String RM_LIST = "rmc:smListTest";
protected final static String RM_LIST_URI_ELEM = "rmc_smListTest";
@@ -55,8 +49,6 @@ public class RMConstraintScriptTest extends BaseRMWebScriptTestCase
super.initServices();
this.caveatConfigService = (RMCaveatConfigService)getServer().getApplicationContext().getBean("CaveatConfigService");
this.authenticationService = (MutableAuthenticationService)getServer().getApplicationContext().getBean("AuthenticationService");
this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService");
}
/**
@@ -108,30 +100,12 @@ public class RMConstraintScriptTest extends BaseRMWebScriptTestCase
AuthenticationUtil.setFullyAuthenticatedUser(AuthenticationUtil.getAdminUserName());
personService.deletePerson("fbloggs");
personService.deletePerson("jrogers");
personService.deletePerson("jdoe");
deleteUser("fbloggs");
deleteUser("jrogers");
deleteUser("jdoe");
}
private void createUser(String userName)
{
if (this.authenticationService.authenticationExists(userName) == false)
{
this.authenticationService.createAuthentication(userName, "PWD".toCharArray());
PropertyMap ppOne = new PropertyMap(4);
ppOne.put(ContentModel.PROP_USERNAME, userName);
ppOne.put(ContentModel.PROP_AUTHORITY_DISPLAY_NAME, "title" + userName);
ppOne.put(ContentModel.PROP_FIRSTNAME, "firstName");
ppOne.put(ContentModel.PROP_LASTNAME, "lastName");
ppOne.put(ContentModel.PROP_EMAIL, "email@email.com");
ppOne.put(ContentModel.PROP_JOBTITLE, "jobTitle");
this.personService.createPerson(ppOne);
}
}
}

View File

@@ -0,0 +1,215 @@
/*
* Copyright (C) 2005-2013 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.module.org_alfresco_module_rm.test.webscript;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Set;
import org.alfresco.module.org_alfresco_module_rm.role.FilePlanRoleService;
import org.alfresco.module.org_alfresco_module_rm.test.util.BaseRMWebScriptTestCase;
import org.alfresco.service.cmr.repository.StoreRef;
import org.json.JSONException;
import org.json.JSONObject;
import org.springframework.extensions.webscripts.Status;
import org.springframework.extensions.webscripts.TestWebScriptServer.DeleteRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.PostRequest;
import org.springframework.extensions.webscripts.TestWebScriptServer.Response;
/**
* REST API Tests for adding/removing users/groups to/from a role
*
* @author Tuna Aksoy
* @since 2.1
*/
public class RmChildrenRestApiTest extends BaseRMWebScriptTestCase
{
/** URL for the REST APIs */
private static final String RM_CHILDREN_URL = "/api/rm/%s/role/%s/children/%s";
/** Constant for the content type */
private static final String APPLICATION_JSON = "application/json";
/**
* Test the REST API to add/remove a user to/from a role
*
* @throws IOException
* @throws JSONException
*/
public void testRmAddRemoveUser() throws IOException, JSONException
{
// Create a test user
String userName = "geshjsjuasftg";
createUser(userName);
// Check if the user is already assigned to the role
assertFalse(getUsersAssignedToRole().contains(userName));
// Format url and send request
String url = getFormattedUrlString(userName);
Response response = postRequest(url);
// Check the content from the response
checkContent(response);
// The user should be added to the role
assertTrue(getUsersAssignedToRole().contains(userName));
// Remove the user from the role
response = deleteRequest(url);
// Check the content from the response
checkContent(response);
// The user should be removed from the role
assertFalse(getUsersAssignedToRole().contains(userName));
// Delete the user
deleteUser(userName);
}
/**
* Test the REST API to add/remove a group to/from a role
*
* @throws IOException
* @throws JSONException
*/
public void testRmAddRemoveGroup() throws IOException, JSONException
{
// Create a group
String groupName = "arhweurawy";
createGroup(groupName);
// Check if the group is already assigned to the role
assertFalse(getGroupsAssignedToRole().contains(groupName));
// Format url and send request
String url = getFormattedUrlString(groupName);
Response response = postRequest(url);
// Check the content from the response
checkContent(response);
// The group should be added to the role
assertTrue(getGroupsAssignedToRole().contains(groupName));
// Remove the group from the role
response = deleteRequest(url);
// Check the content from the response
checkContent(response);
// The user should be removed from the role
assertFalse(getGroupsAssignedToRole().contains(groupName));
// Delete the group
deleteGroup(groupName);
}
/**
* Util method to get a set of groups assigned to a role
*
* @return Returns a set of groups assigned to a role
*/
private Set<String> getGroupsAssignedToRole()
{
return filePlanRoleService.getGroupsAssignedToRole(filePlan, FilePlanRoleService.ROLE_SECURITY_OFFICER);
}
/**
* Util method to get a set of users assigned to a role
*
* @return Returns a set of users assigned to a role
*/
private Set<String> getUsersAssignedToRole()
{
return filePlanRoleService.getUsersAssignedToRole(filePlan, FilePlanRoleService.ROLE_SECURITY_OFFICER);
}
/**
* Util method to get a formatted nodeRef string
*
* @return Returns a formatted nodeRef string
*/
private String getFormattedFilePlanString()
{
StoreRef storeRef = filePlan.getStoreRef();
String storeType = storeRef.getProtocol();
String storeId = storeRef.getIdentifier();
String id = filePlan.getId();
StringBuffer sb = new StringBuffer();
sb.append(storeType);
sb.append("/");
sb.append(storeId);
sb.append("/");
sb.append(id);
return sb.toString();
}
/**
* Util method to get a formatted url string
*
* @param authorityName The name of the authority which should be added/removed to/from a role
* @return Returns a formatted url string
*/
private String getFormattedUrlString(String authorityName)
{
return String.format(RM_CHILDREN_URL, getFormattedFilePlanString(), FilePlanRoleService.ROLE_SECURITY_OFFICER, authorityName);
}
/**
* Util method to send a post request
*
* @param url The url which should be used to make the post request
* @return Returns the response from the server
* @throws UnsupportedEncodingException
* @throws IOException
*/
private Response postRequest(String url) throws UnsupportedEncodingException, IOException
{
return sendRequest(new PostRequest(url, new JSONObject().toString(), APPLICATION_JSON), Status.STATUS_OK);
}
/**
* Util method to send a delete request
*
* @param url The url which should be used to make the delete request
* @return Returns the response from the server
* @throws IOException
*/
private Response deleteRequest(String url) throws IOException
{
return sendRequest(new DeleteRequest(url), Status.STATUS_OK);
}
/**
* Util method to check the server response
*
* @param response The server response
* @throws UnsupportedEncodingException
*/
private void checkContent(Response response) throws UnsupportedEncodingException
{
String contentAsString = response.getContentAsString();
assertNotNull(contentAsString);
assertTrue(contentAsString.equals("{}"));
}
}