Implementing Web Site Group Membership.

git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@14342 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
Mark Rogers
2009-05-18 12:44:04 +00:00
parent 6324d484cf
commit 495e767906
15 changed files with 465 additions and 116 deletions

View File

@@ -1,8 +1,13 @@
<webscript>
<shortname>Membership</shortname>
<description>Get the membership details for a user</description>
<url>/api/sites/{shortname}/memberships/{username}</url>
<shortname>Delete Web Site Membership</shortname>
<description> <![CDATA[
Delete membership from a web site.
<br />
'shortname' is the shortname of the web site, 'authorityname' is the full authority name for the membership.
]]></description>
<url>/api/sites/{shortname}/memberships/{authorityname}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>draft_public_api</lifecycle>
</webscript>

View File

@@ -1,8 +1,16 @@
<webscript>
<shortname>Membership</shortname>
<description>Get the membership details for a user</description>
<url>/api/sites/{shortname}/memberships/{username}</url>
<shortname>Get Web Site Membership</shortname>
<description> <![CDATA[
Get the membership details for a user or group
<br />
'shortname' is the shortname of the web site, 'authorityname' is the full authority name for the membership.
<br />
Returns a membership or Status.NOT_FOUND(404)
]]></description>
<url>/api/sites/{shortname}/memberships/{authorityname}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>draft_public_api</lifecycle>
</webscript>

View File

@@ -14,13 +14,27 @@ function main()
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;
}
var authority
if(userName.match("^GROUP_"))
{
authority = groups.getGroupForFullAuthorityName(userName);
if (authority == null)
{
// Person cannot be found
status.setCode(status.STATUS_NOT_FOUND, "The group with full name " + userName + " does not exist.");
return;
}
}
else
{
authority = people.getPerson(userName);
if (authority == 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);
@@ -33,7 +47,7 @@ function main()
// Pass the values to the template
model.site = site;
model.person = person;
model.authority = authority;
model.role = role;
}

View File

@@ -1,2 +1,2 @@
<#import "membership.lib.ftl" as membershipLib/>
<@membershipLib.membershipJSON site=site role=role person=person/>
<@membershipLib.membershipJSON site=site role=role authority=authority />

View File

@@ -1,24 +1,50 @@
<#macro membershipJSON site role person>
<#-- Web Site Membership renders an authority object which can be either a Java Script person or a Java Script group -->
<#macro membershipJSON site role authority>
<#escape x as jsonUtils.encodeJSONString(x)>
{
"role" : "${role}",
"person":
<#if authority.authorityType?? && authority.authorityType = "GROUP" >
<#-- this is a group authority type -->
"authority":
{
"userName" : "${person.properties.userName}",
"firstName" : "${person.properties.firstName}",
"lastName" : "${person.properties.lastName}",
<#if person.assocs["cm:avatar"]??>
"avatar" : "${"api/node/" + person.assocs["cm:avatar"][0].nodeRef?string?replace('://','/') + "/content/thumbnails/avatar"}",
</#if>
<#if person.properties.jobtitle??>
"jobtitle" : "${person.properties.jobtitle}",
</#if>
<#if person.properties.organization??>
"organization" : "${person.properties.organization}",
</#if>
"url" : "${url.serviceContext + "/api/people/" + person.properties.userName}"
"authorityType" : "${authority.authorityType}",
"shortName" : "${authority.shortName}",
"fullName" : "${authority.fullName}",
"displayName" : "${authority.displayName}",
"url" : "${url.serviceContext + "/api/groups/" + authority.shortName }"
},
"url" : "${url.serviceContext + "/api/sites/" + site.shortName + "/memberships/" + person.properties.userName}"
"url" : "${url.serviceContext + "/api/sites/" + site.shortName + "/memberships/" + authority.fullName}"
<#else >
<#-- this is a person authority type -->
"authority":
{
"authorityType" : "USER",
"fullName" : "${authority.properties.userName}",
"userName" : "${authority.properties.userName}",
"firstName" : "${authority.properties.firstName}",
"lastName" : "${authority.properties.lastName}",
<#if authority.assocs["cm:avatar"]??>
"avatar" : "${"api/node/" + authority.assocs["cm:avatar"][0].nodeRef?string?replace('://','/') + "/content/thumbnails/avatar"}",
</#if>
<#if authority.properties.jobtitle??>
"jobtitle" : "${authority.properties.jobtitle}",
</#if>
<#if authority.properties.organization??>
"organization" : "${authority.properties.organization}",
</#if>
"url" : "${url.serviceContext + "/api/people/" + authority.properties.userName}"
},
"url" : "${url.serviceContext + "/api/sites/" + site.shortName + "/memberships/" + authority.properties.userName}"
</#if>
}
</#escape>
</#macro>

View File

@@ -1,8 +1,31 @@
<webscript>
<shortname>Membership</shortname>
<description>Get the membership details for a user</description>
<url>/api/sites/{shortname}/memberships/{username}</url>
<shortname>Update Web Site Membership</shortname>
<description> <![CDATA[
Update the membership role for a user or group
<br />
'shortname' is the shortname of the web site, 'authorityname' is the full authority name for the membership.
<br />
Required parameters,
<br />
role, mandatory String, the new role name for this membership.
<br />
person object, with userName property
<br>
OR
group object, with fullName property
<br />
OR
authority object, with fullName property
<br />
Returns: The new authority.
]]>
</description>
<url>/api/sites/{shortname}/memberships/{authorityname}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>draft_public_api</lifecycle>
</webscript>

View File

@@ -1,2 +1,2 @@
<#import "membership.lib.ftl" as membershipLib/>
<@membershipLib.membershipJSON site=site role=role person=person/>
<@membershipLib.membershipJSON site=site role=role authority=authority/>

View File

@@ -18,27 +18,109 @@ function main()
return;
}
// Get the user name
var userName = json.getJSONObject("person").get("userName");
if (userName == null)
// Are we adding a person ?
if (json.has("person"))
{
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.");
// 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.authority = person;
return;
}
// Set the membership details
site.setMembership(userName, role);
// Are we adding a group ?
if (json.has("group"))
{
// Get the user name
var groupName = json.getJSONObject("group").get("fullName");
if (groupName == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The fullName for the group has not been set.");
return;
}
if(groupName.match("^GROUP_") == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "Group Authority names should begin with 'GROUP_'.");
return;
}
var group = groups.getGroupForFullAuthorityName(groupName);
if (group == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The group with group name " + groupName + " could not be found.");
return;
}
// Set the membership details
site.setMembership(groupName, role);
// Pass the details to the template
model.site = site;
model.role = role;
model.authority = group;
return;
}
if (json.has("authority"))
{
// Get the user name
var authorityName = json.getJSONObject("authority").get("fullName");
if (authorityName == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The fullName for the authority has not been set.");
return;
}
var authority;
if(authorityName.match("^GROUP_") != null)
{
// authority is a group
authority = groups.getGroupForFullAuthorityName(authorityName);
}
else
{
// assume a person
authority = people.getPerson(authorityName);
}
if (authority == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The authority with name " + authorityName + " could not be found.");
return;
}
// Set the membership details
site.setMembership(authorityName, role);
// Pass the details to the template
model.site = site;
model.role = role;
model.authority = authority;
return;
}
// Neither person or group specified.
status.setCode(status.STATUS_BAD_REQUEST, "person or group has not been set.");
return;
// Pass the details to the template
model.site = site;
model.role = role;
model.person = person;
}
main();

View File

@@ -1,8 +1,21 @@
<webscript>
<shortname>Memberships</shortname>
<description>Get a colleciton of a sites memberships.</description>
<url>/api/sites/{shortname}/memberships?nf={namefilter?}&amp;rf={rolefilter?}&amp;size={pagesize?}&amp;pos={position?}</url>
<shortname>List Web Site Memberships</shortname>
<description> <![CDATA[
Get a colleciton of a Web Site memberships.
<br />
'shortname' is the shortname of the web site
<br />
Parameters:
<ul>
<li>nf, Optional, namefilter</li>
<li>rf, Optional, rolefilter</li>
<li>size, Optional, page size</li>
<li>authorityType, Optional, returns either GROUP or USER authorities. If not specified returns all authorities.</li>
</ul>
]]></description>
<url>/api/sites/{shortname}/memberships?nf={namefilter?}&amp;rf={rolefilter?}&amp;size={pagesize?}&amp;pos={position?}&amp;authorityType={authorityType?}</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>draft_public_api</lifecycle>
</webscript>

View File

@@ -1,34 +1,66 @@
// Get the site id
var shortName = url.extension.split("/")[0];
var site = siteService.getSite(shortName);
/**
* Implementation of list memberships
*/
// get the filters
var nameFilter = args["nf"];
var roleFilter = args["rf"];
var sizeString = args["size"];
// Get the filtered memeberships
var memberships = site.listMembers(nameFilter, roleFilter, sizeString != null ? parseInt(sizeString) : 0);
// Get a list of all the users resolved to person nodes
var peopleList = Array(memberships.length);
for (userName in memberships)
function main()
{
var person = people.getPerson(userName);
// make sure the keys are strings - as usernames may be all numbers!
peopleList['_' + userName] = person;
// Get the site id
var shortName = url.extension.split("/")[0];
var site = siteService.getSite(shortName);
// get the filters
var nameFilter = args["nf"];
var roleFilter = args["rf"];
var authorityType = args["authorityType"];
var sizeString = args["size"];
if(authorityType != null )
{
authorityType.match("[USER|GROUP]" != null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The authorityType must be either USER or GROUP.");
return;
}
}
// Get the filtered memberships
// Comes back as a Map<String, String> containing the full authority name
// and role
var memberships = site.listMembers(nameFilter, roleFilter, sizeString != null ? parseInt(sizeString) : 0);
// Get a list of all the users resolved to person nodes
var authorities = Array(memberships.length);
var roles = {};
for (userName in memberships)
{
var membershipRole = memberships[userName];
if(userName.match("^GROUP_"))
{
if(authorityType == null || authorityType == "GROUP")
{
// make sure the keys are strings - as usernames may be all
// numbers!
authorities['_' + userName] = groups.getGroupForFullAuthorityName(userName);
roles['_' + userName] = membershipRole;
}
}
else
{
if(authorityType == null || authorityType == "USER")
{
// make sure the keys are strings - as usernames may be all
// numbers!
authorities['_' + userName] = people.getPerson(userName);
roles['_' + userName] = membershipRole;
}
}
}
// Pass the information to the template
model.site = site;
model.roles = roles;
model.authorities = authorities;
}
// also copy over the memberships.
var mems = {};
for (userName in memberships)
{
var membershipType = memberships[userName];
// make sure the keys are strings - as usernames may be all numbers!
mems['_' + userName] = membershipType;
}
// Pass the information to the template
model.site = site;
model.memberships = mems;
model.peoplelist = peopleList;
main();

View File

@@ -1,8 +1,10 @@
<#-- List memberships Implementation-->
<#import "membership.lib.ftl" as membershipLib />
<#assign userNames = memberships?keys />
<#assign userNames = roles?keys />
[
<#list userNames as userName>
<@membershipLib.membershipJSON site=site role=memberships[userName] person=peoplelist[userName] />
<@membershipLib.membershipJSON site=site role=roles[userName] authority=authorities[userName] />
<#if userName_has_next>,</#if>
</#list>
]

View File

@@ -1,8 +1,25 @@
<webscript>
<shortname>Memberships</shortname>
<description>Adds a new membership to the site</description>
<shortname>Add Web Site Membership</shortname>
<description> <![CDATA[
Adds a new membership to the web site
<br />
'shortname' is the shortname of the web site, 'authorityname' is the full authority name for the membership.
Required parameters,
<br />
role, mandatory String, the new role name for this membership.
<br />
person object, with userName property
<br>
OR
group object, with fullName property
<br />
OR
authority object, with fullName property
<br />
]]></description>
<url>/api/sites/{shortname}/memberships</url>
<format default="json">argument</format>
<authentication>user</authentication>
<transaction>required</transaction>
<lifecycle>draft_public_api</lifecycle>
</webscript>

View File

@@ -1,2 +1,2 @@
<#import "membership.lib.ftl" as membershipLib/>
<@membershipLib.membershipJSON site=site role=role person=person/>
<@membershipLib.membershipJSON site=site role=role authority=authority/>

View File

@@ -18,27 +18,69 @@ function main()
return;
}
// Get the user name
var userName = json.getJSONObject("person").get("userName");
if (userName == null)
// Are we adding a person ?
if (json.has("person"))
{
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.");
// 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.authority = person;
return;
}
// Set the membership details
site.setMembership(userName, role);
// Are we adding a group ?
if (json.has("group"))
{
// Get the user name
var groupName = json.getJSONObject("group").get("fullName");
if (groupName == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The fullName for the group has not been set.");
return;
}
if(groupName.match("^GROUP_") == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "Group Authority names should begin with 'GROUP_'.");
return;
}
var group = groups.getGroupForFullAuthorityName(groupName);
if (group == null)
{
status.setCode(status.STATUS_BAD_REQUEST, "The group with group name " + groupName + " could not be found.");
return;
}
// Set the membership details
site.setMembership(groupName, role);
// Pass the details to the template
model.site = site;
model.role = role;
model.authority = group;
return;
}
// Neither person or group specified.
status.setCode(status.STATUS_BAD_REQUEST, "person or group has not been set.");
return;
// Pass the details to the template
model.site = site;
model.role = role;
model.person = person;
}
main();

View File

@@ -40,6 +40,8 @@ import org.alfresco.repo.web.scripts.BaseWebScriptTest;
import org.alfresco.service.cmr.repository.NodeRef;
import org.alfresco.service.cmr.repository.NodeService;
import org.alfresco.service.cmr.security.AuthenticationService;
import org.alfresco.service.cmr.security.AuthorityService;
import org.alfresco.service.cmr.security.AuthorityType;
import org.alfresco.service.cmr.security.PersonService;
import org.alfresco.service.cmr.site.SiteInfo;
import org.alfresco.service.cmr.site.SiteService;
@@ -68,6 +70,7 @@ public class SiteServiceTest extends BaseWebScriptTest
private PersonService personService;
private SiteService siteService;
private NodeService nodeService;
private AuthorityService authorityService;
private static final String USER_ONE = "SiteTestOne";
private static final String USER_TWO = "SiteTestTwo";
@@ -89,6 +92,7 @@ public class SiteServiceTest extends BaseWebScriptTest
this.personService = (PersonService)getServer().getApplicationContext().getBean("PersonService");
this.siteService = (SiteService)getServer().getApplicationContext().getBean("SiteService");
this.nodeService = (NodeService)getServer().getApplicationContext().getBean("NodeService");
this.authorityService = (AuthorityService)getServer().getApplicationContext().getBean("AuthorityService");
this.authenticationComponent.setSystemUserAsCurrentUser();
@@ -309,7 +313,7 @@ public class SiteServiceTest extends BaseWebScriptTest
response = sendRequest(new GetRequest(URL_SITES + "/" + shortName), 404);
}
public void testGetMemeberships() throws Exception
public void testGetMemberships() throws Exception
{
// Create a site
String shortName = GUID.generate();
@@ -322,7 +326,7 @@ public class SiteServiceTest extends BaseWebScriptTest
assertEquals(1, result.length());
JSONObject membership = result.getJSONObject(0);
assertEquals(SiteModel.SITE_MANAGER, membership.get("role"));
assertEquals(USER_ONE, membership.getJSONObject("person").get("userName"));
assertEquals(USER_ONE, membership.getJSONObject("authority").get("userName"));
}
public void testPostMemberships() throws Exception
@@ -369,7 +373,7 @@ public class SiteServiceTest extends BaseWebScriptTest
// Check the result
assertEquals(SiteModel.SITE_MANAGER, result.get("role"));
assertEquals(USER_ONE, result.getJSONObject("person").get("userName"));
assertEquals(USER_ONE, result.getJSONObject("authority").get("userName"));
}
public void testPutMembership() throws Exception
@@ -378,9 +382,6 @@ public class SiteServiceTest extends BaseWebScriptTest
String shortName = GUID.generate();
createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
// Test error conditions
// TODO
// Build the JSON membership object
JSONObject membership = new JSONObject();
membership.put("role", SiteModel.SITE_CONSUMER);
@@ -392,20 +393,104 @@ public class SiteServiceTest extends BaseWebScriptTest
Response response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
JSONObject newMember = new JSONObject(response.getContentAsString());
// Update the role
// Update the role by returning the data.
newMember.put("role", SiteModel.SITE_COLLABORATOR);
response = sendRequest(new PutRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, newMember.toString(), "application/json"), 200);
JSONObject result = new JSONObject(response.getContentAsString());
// Check the result
assertEquals(SiteModel.SITE_COLLABORATOR, result.get("role"));
assertEquals(USER_TWO, result.getJSONObject("person").get("userName"));
assertEquals(USER_TWO, result.getJSONObject("authority").get("userName"));
// Double check and get the membership for user two
response = sendRequest(new 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"));
assertEquals(USER_TWO, result.getJSONObject("authority").get("userName"));
}
public void testGroupMembership() throws Exception
{
String testGroup = "SiteServiceTestGroupA";
String testGroupName = "GROUP_" + testGroup;
if(!authorityService.authorityExists(testGroup))
{
this.authenticationComponent.setSystemUserAsCurrentUser();
testGroupName = authorityService.createAuthority(AuthorityType.GROUP, null, testGroup, testGroup);
}
this.authenticationComponent.setCurrentUser(USER_ONE);
// CRUD a membership group for a web site
// Create a site
String shortName = GUID.generate();
createSite("myPreset", shortName, "myTitle", "myDescription", SiteVisibility.PUBLIC, 200);
// Build the JSON membership object
JSONObject membership = new JSONObject();
membership.put("role", SiteModel.SITE_CONSUMER);
JSONObject group = new JSONObject();
group.put("fullName", testGroupName);
membership.put("group", group);
// Create a new group membership
{
Response response = sendRequest(new PostRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS, membership.toString(), "application/json"), 200);
JSONObject newMember = new JSONObject(response.getContentAsString());
// Validate the return value
assertEquals("role not correct", SiteModel.SITE_CONSUMER, newMember.getString("role"));
JSONObject newGroup = newMember.getJSONObject("authority");
assertNotNull("newGroup");
assertEquals("full name not correct", testGroupName, newGroup.getString("fullName"));
assertEquals("authorityType not correct", "GROUP", newGroup.getString("authorityType"));
// Now send the returned value back with a new role (COLLABORATOR)
newMember.put("role", SiteModel.SITE_COLLABORATOR);
response = sendRequest(new PutRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + "/" + USER_TWO, newMember.toString(), "application/json"), 200);
JSONObject updateResult = new JSONObject(response.getContentAsString());
assertEquals("role not correct", SiteModel.SITE_COLLABORATOR, updateResult.getString("role"));
}
// Now List membership to show the group from above.
{
Response response = sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS), 200);
JSONArray listResult = new JSONArray(response.getContentAsString());
/**
* The result should have at least 2 elements, 1 for the user who created and 1 for the group added above
*/
assertTrue("result too small", listResult.length() >= 2);
for(int i = 0; i < listResult.length(); i++)
{
JSONObject obj = listResult.getJSONObject(i);
JSONObject authority = obj.getJSONObject("authority");
if(authority.getString("authorityType").equals("GROUP"))
{
assertEquals("full name not correct", testGroupName, authority.getString("fullName"));
}
if(authority.getString("authorityType").equals("USER"))
{
assertEquals("full name not correct", USER_ONE, authority.getString("fullName"));
}
}
}
// Now get the group membership from above
// Now List membership to show the group from above.
{
Response response = sendRequest(new GetRequest(URL_SITES + "/" + shortName + URL_MEMBERSHIPS + '/' + testGroupName), 200);
JSONObject getResult = new JSONObject(response.getContentAsString());
System.out.println(response.getContentAsString());
JSONObject grp = getResult.getJSONObject("authority");
assertEquals("full name not correct", testGroupName, grp.getString("fullName"));
}
}
public void testDeleteMembership() throws Exception