mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-07-31 17:39:05 +00:00
Site Service: GET, PUT and DELETE methods implemented and unit tested for Membership resource
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9063 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<webscript>
|
||||
<shortname>Membership</shortname>
|
||||
<description>Get the membership details for a user</description>
|
||||
<url>/api/sites/{shortname}/memberships/{username}</url>
|
||||
<format default="json"/>
|
||||
<authentication>guest</authentication>
|
||||
<transaction>required</transaction>
|
||||
</webscript>
|
@@ -0,0 +1,21 @@
|
||||
function main()
|
||||
{
|
||||
// Get the url values
|
||||
var urlElements = url.extension.split("/");
|
||||
var shortName = urlElements[0];
|
||||
var userName = urlElements[2];
|
||||
|
||||
// Get the site
|
||||
var site = siteService.getSite(shortName);
|
||||
if (site == null)
|
||||
{
|
||||
// Site cannot be found
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The site " + shortName + " does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Remove the user from the site
|
||||
site.removeMembership(userName);
|
||||
}
|
||||
|
||||
main();
|
@@ -0,0 +1,8 @@
|
||||
<webscript>
|
||||
<shortname>Membership</shortname>
|
||||
<description>Get the membership details for a user</description>
|
||||
<url>/api/sites/{shortname}/memberships/{username}</url>
|
||||
<format default="json"/>
|
||||
<authentication>guest</authentication>
|
||||
<transaction>required</transaction>
|
||||
</webscript>
|
@@ -0,0 +1,41 @@
|
||||
function main()
|
||||
{
|
||||
// Get the url values
|
||||
var urlElements = url.extension.split("/");
|
||||
var shortName = urlElements[0];
|
||||
var userName = urlElements[2];
|
||||
|
||||
// Get the site
|
||||
var site = siteService.getSite(shortName);
|
||||
if (site == null)
|
||||
{
|
||||
// Site cannot be found
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The site " + shortName + " does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
var person = people.getPerson(userName);
|
||||
if (person == null)
|
||||
{
|
||||
// Person cannot be found
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The person with user name " + userName + " does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the role of the user
|
||||
var role = site.getMembersRole(userName);
|
||||
if (role == null)
|
||||
{
|
||||
// Person is not a member of the site
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The person with user name " + userName + " is not a member of the site " + shortName);
|
||||
return;
|
||||
}
|
||||
|
||||
// Pass the values to the template
|
||||
model.site = site;
|
||||
model.person = person;
|
||||
model.role = role;
|
||||
}
|
||||
|
||||
main();
|
||||
|
@@ -0,0 +1,2 @@
|
||||
<#import "membership.lib.ftl" as membershipLib/>
|
||||
<@membershipLib.membershipJSON site=site role=role person=person/>
|
@@ -0,0 +1,8 @@
|
||||
<webscript>
|
||||
<shortname>Membership</shortname>
|
||||
<description>Get the membership details for a user</description>
|
||||
<url>/api/sites/{shortname}/memberships/{username}</url>
|
||||
<format default="json"/>
|
||||
<authentication>guest</authentication>
|
||||
<transaction>required</transaction>
|
||||
</webscript>
|
@@ -0,0 +1,44 @@
|
||||
function main()
|
||||
{
|
||||
// Get the site
|
||||
var shortName = url.extension.split("/")[0];
|
||||
var site = siteService.getSite(shortName);
|
||||
if (site == null)
|
||||
{
|
||||
// Site cannot be found
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The site " + shortName + " does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the role
|
||||
var role = json.get("role");
|
||||
if (role == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The role has not been set.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the user name
|
||||
var userName = json.getJSONObject("person").get("userName");
|
||||
if (userName == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The user name has not been set.");
|
||||
return;
|
||||
}
|
||||
var person = people.getPerson(userName);
|
||||
if (person == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The person with user name " + userName + " could not be found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the membership details
|
||||
site.setMembership(userName, role);
|
||||
|
||||
// Pass the details to the template
|
||||
model.site = site;
|
||||
model.role = role;
|
||||
model.person = person;
|
||||
}
|
||||
|
||||
main();
|
@@ -0,0 +1,2 @@
|
||||
<#import "membership.lib.ftl" as membershipLib/>
|
||||
<@membershipLib.membershipJSON site=site role=role person=person/>
|
@@ -1,15 +1,44 @@
|
||||
// Get the site
|
||||
var shortName = url.extension.split("/")[0];
|
||||
var site = siteService.getSite(shortName);
|
||||
function main()
|
||||
{
|
||||
// Get the site
|
||||
var shortName = url.extension.split("/")[0];
|
||||
var site = siteService.getSite(shortName);
|
||||
if (site == null)
|
||||
{
|
||||
// Site cannot be found
|
||||
status.setCode(status.STATUS_NOT_FOUND, "The site " + shortName + " does not exist.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the role
|
||||
var role = json.get("role");
|
||||
if (role == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The role has not been set.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the user name
|
||||
var userName = json.getJSONObject("person").get("userName");
|
||||
if (userName == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The user name has not been set.");
|
||||
return;
|
||||
}
|
||||
var person = people.getPerson(userName);
|
||||
if (person == null)
|
||||
{
|
||||
status.setCode(status.STATUS_BAD_REQUEST, "The person with user name " + userName + " could not be found.");
|
||||
return;
|
||||
}
|
||||
|
||||
// Set the membership details
|
||||
site.setMembership(userName, role);
|
||||
|
||||
// Pass the details to the template
|
||||
model.site = site;
|
||||
model.role = role;
|
||||
model.person = person;
|
||||
}
|
||||
|
||||
// Get the role
|
||||
var role = json.get("role");
|
||||
var userName = json.getJSONObject("person").get("userName");
|
||||
|
||||
// Set the membership details
|
||||
site.setMembership(userName, role);
|
||||
|
||||
// Pass the details to the template
|
||||
model.site = site;
|
||||
model.role = role;
|
||||
model.person = people.getPerson(userName);
|
||||
main();
|
@@ -251,4 +251,83 @@ public class SiteServiceTest extends BaseWebScriptTest
|
||||
assertNotNull(result2);
|
||||
assertEquals(2, result2.length());
|
||||
}
|
||||
|
||||
public void testGetMembership() throws Exception
|
||||
{
|
||||
// Create a site
|
||||
String shortName = GUID.generate();
|
||||
createSite("myPreset", shortName, "myTitle", "myDescription", true, 200);
|
||||
|
||||
// Test error conditions
|
||||
getRequest(URL_SITES + "/badsite" + URL_MEMBERSHIPS + "/" + USER_ONE, 404);
|
||||
getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/baduser", 404);
|
||||
getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, 404);
|
||||
|
||||
MockHttpServletResponse response = getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_ONE, 200);
|
||||
JSONObject result = new JSONObject(response.getContentAsString());
|
||||
|
||||
// Check the result
|
||||
assertEquals(SiteModel.SITE_MANAGER, result.get("role"));
|
||||
assertEquals(USER_ONE, result.getJSONObject("person").get("userName"));
|
||||
}
|
||||
|
||||
public void testPutMembership() throws Exception
|
||||
{
|
||||
// Create a site
|
||||
String shortName = GUID.generate();
|
||||
createSite("myPreset", shortName, "myTitle", "myDescription", true, 200);
|
||||
|
||||
// Test error conditions
|
||||
// TODO
|
||||
|
||||
// Build the JSON membership object
|
||||
JSONObject membership = new JSONObject();
|
||||
membership.put("role", SiteModel.SITE_CONSUMER);
|
||||
JSONObject person = new JSONObject();
|
||||
person.put("userName", USER_TWO);
|
||||
membership.put("person", person);
|
||||
|
||||
// Post the memebership
|
||||
MockHttpServletResponse response = postRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, 200, membership.toString(), "application/json");
|
||||
JSONObject newMember = new JSONObject(response.getContentAsString());
|
||||
|
||||
// Update the role
|
||||
newMember.put("role", SiteModel.SITE_COLLABORATOR);
|
||||
response = putRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, 200, newMember.toString(), "application/json");
|
||||
JSONObject result = new JSONObject(response.getContentAsString());
|
||||
|
||||
// Check the result
|
||||
assertEquals(SiteModel.SITE_COLLABORATOR, result.get("role"));
|
||||
assertEquals(USER_TWO, result.getJSONObject("person").get("userName"));
|
||||
|
||||
// Double check and get the membership for user two
|
||||
response = getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, 200);
|
||||
result = new JSONObject(response.getContentAsString());
|
||||
assertEquals(SiteModel.SITE_COLLABORATOR, result.get("role"));
|
||||
assertEquals(USER_TWO, result.getJSONObject("person").get("userName"));
|
||||
}
|
||||
|
||||
public void testDeleteMembership() throws Exception
|
||||
{
|
||||
// Create a site
|
||||
String shortName = GUID.generate();
|
||||
createSite("myPreset", shortName, "myTitle", "myDescription", true, 200);
|
||||
|
||||
// Build the JSON membership object
|
||||
JSONObject membership = new JSONObject();
|
||||
membership.put("role", SiteModel.SITE_CONSUMER);
|
||||
JSONObject person = new JSONObject();
|
||||
person.put("userName", USER_TWO);
|
||||
membership.put("person", person);
|
||||
|
||||
// Post the membership
|
||||
postRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, 200, membership.toString(), "application/json");
|
||||
|
||||
// Delete the membership
|
||||
deleteRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, 200);
|
||||
|
||||
// Check that the membership has been deleted
|
||||
getRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, 404);
|
||||
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user