mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-07 17:49:17 +00:00
Person Service REST API call PUT /people/{userName} to update a person's details, including supporting fix for POST /people/ (create person Web Script) using "jobTitle" property on person ScriptNode instead of all lower case "jobtitle"
git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/HEAD/root@9886 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -3,6 +3,6 @@
|
|||||||
<description>Adds a new person based on the details provided.</description>
|
<description>Adds a new person based on the details provided.</description>
|
||||||
<url>/api/people</url>
|
<url>/api/people</url>
|
||||||
<format default="json" />
|
<format default="json" />
|
||||||
<authentication>user</authentication>
|
<authentication>admin</authentication>
|
||||||
<transaction>required</transaction>
|
<transaction>required</transaction>
|
||||||
</webscript>
|
</webscript>
|
@@ -23,7 +23,7 @@ function main()
|
|||||||
person.properties["firstName"] = json.get("firstName");
|
person.properties["firstName"] = json.get("firstName");
|
||||||
person.properties["lastName"] = json.get("lastName");
|
person.properties["lastName"] = json.get("lastName");
|
||||||
person.properties["organization"] = json.get("organisation");
|
person.properties["organization"] = json.get("organisation");
|
||||||
person.properties["jobTitle"] = json.get("jobTitle");
|
person.properties["jobtitle"] = json.get("jobtitle");
|
||||||
person.properties["email"] = json.get("email");
|
person.properties["email"] = json.get("email");
|
||||||
person.save();
|
person.save();
|
||||||
|
|
||||||
|
@@ -23,9 +23,9 @@
|
|||||||
"organisation" : undefined,
|
"organisation" : undefined,
|
||||||
</#if>
|
</#if>
|
||||||
<#if person.properties.jobtitle??>
|
<#if person.properties.jobtitle??>
|
||||||
"jobTitle" : "${person.properties.jobtitle}",
|
"jobtitle" : "${person.properties.jobtitle}",
|
||||||
<#else>
|
<#else>
|
||||||
"jobTitle" : undefined,
|
"jobtitle" : undefined,
|
||||||
</#if>
|
</#if>
|
||||||
<#if person.properties.email??>
|
<#if person.properties.email??>
|
||||||
"email" : "${person.properties.email}"
|
"email" : "${person.properties.email}"
|
||||||
|
@@ -0,0 +1,8 @@
|
|||||||
|
<webscript>
|
||||||
|
<shortname>Update Person</shortname>
|
||||||
|
<description>Update the details of a person.</description>
|
||||||
|
<url>/api/people/{userName}</url>
|
||||||
|
<format default="json"/>
|
||||||
|
<authentication>user</authentication>
|
||||||
|
<transaction>required</transaction>
|
||||||
|
</webscript>
|
@@ -0,0 +1,34 @@
|
|||||||
|
function main()
|
||||||
|
{
|
||||||
|
// Extract the person's user name from the URL
|
||||||
|
var userName = url.extension;
|
||||||
|
var person = people.getPerson(userName);
|
||||||
|
|
||||||
|
// if person is found matching the given user name
|
||||||
|
// then update that person's details with the details
|
||||||
|
// provided within the JSON object passed in
|
||||||
|
if (person != null)
|
||||||
|
{
|
||||||
|
// Update the person's details
|
||||||
|
person.properties["title"] = json.get("title");
|
||||||
|
person.properties["firstName"] = json.get("firstName");
|
||||||
|
person.properties["lastName"] = json.get("lastName");
|
||||||
|
person.properties["organization"] = json.get("organisation");
|
||||||
|
person.properties["jobtitle"] = json.get("jobtitle");
|
||||||
|
person.properties["email"] = json.get("email");
|
||||||
|
person.save();
|
||||||
|
|
||||||
|
// Put the updated person on the model to pass to the template
|
||||||
|
model.person = person;
|
||||||
|
}
|
||||||
|
// else if no person was found matching the given user name,
|
||||||
|
// then return HTTP error status "not found"
|
||||||
|
else
|
||||||
|
{
|
||||||
|
status.setCode(status.STATUS_NOT_FOUND, "Person " + userName
|
||||||
|
+ " does not exist");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
main();
|
@@ -0,0 +1,2 @@
|
|||||||
|
<#import "person.lib.ftl" as personLib/>
|
||||||
|
<@personLib.personJSON person=person/>
|
@@ -33,6 +33,7 @@ import org.alfresco.repo.web.scripts.BaseWebScriptTest;
|
|||||||
import org.alfresco.service.cmr.security.AuthenticationService;
|
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||||
import org.alfresco.service.cmr.security.PersonService;
|
import org.alfresco.service.cmr.security.PersonService;
|
||||||
import org.alfresco.util.PropertyMap;
|
import org.alfresco.util.PropertyMap;
|
||||||
|
import org.alfresco.web.scripts.Status;
|
||||||
import org.apache.commons.lang.RandomStringUtils;
|
import org.apache.commons.lang.RandomStringUtils;
|
||||||
import org.json.JSONObject;
|
import org.json.JSONObject;
|
||||||
import org.springframework.mock.web.MockHttpServletResponse;
|
import org.springframework.mock.web.MockHttpServletResponse;
|
||||||
@@ -122,6 +123,27 @@ public class PersonServiceTest extends BaseWebScriptTest
|
|||||||
this.createdPeople.clear();
|
this.createdPeople.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void testUpdatePerson() throws Exception
|
||||||
|
{
|
||||||
|
// Create a new person
|
||||||
|
String userName = RandomStringUtils.randomNumeric(6);
|
||||||
|
createPerson(userName, "myTitle", "myFirstName", "myLastName", "myOrganisation",
|
||||||
|
"myJobTitle", "firstName.lastName@email.com", "myBio", "images/avatar.jpg", 200);
|
||||||
|
|
||||||
|
// Update the person's details
|
||||||
|
JSONObject result = updatePerson(userName, "updatedTitle", "updatedFirstName", "updatedLastName",
|
||||||
|
"updatedOrganisation", "updatedJobTitle", "updatedFN.updatedLN@email.com", "updatedBio",
|
||||||
|
"images/updatedAvatar.jpg", 200);
|
||||||
|
|
||||||
|
assertEquals(userName, result.get("userName"));
|
||||||
|
assertEquals("updatedTitle", result.get("title"));
|
||||||
|
assertEquals("updatedFirstName", result.get("firstName"));
|
||||||
|
assertEquals("updatedLastName", result.get("lastName"));
|
||||||
|
assertEquals("updatedOrganisation", result.get("organisation"));
|
||||||
|
assertEquals("updatedJobTitle", result.get("jobtitle"));
|
||||||
|
assertEquals("updatedFN.updatedLN@email.com", result.get("email"));
|
||||||
|
}
|
||||||
|
|
||||||
public void testCreatePerson() throws Exception
|
public void testCreatePerson() throws Exception
|
||||||
{
|
{
|
||||||
String userName = RandomStringUtils.randomNumeric(6);
|
String userName = RandomStringUtils.randomNumeric(6);
|
||||||
@@ -134,7 +156,7 @@ public class PersonServiceTest extends BaseWebScriptTest
|
|||||||
assertEquals("myFirstName", result.get("firstName"));
|
assertEquals("myFirstName", result.get("firstName"));
|
||||||
assertEquals("myLastName", result.get("lastName"));
|
assertEquals("myLastName", result.get("lastName"));
|
||||||
assertEquals("myOrganisation", result.get("organisation"));
|
assertEquals("myOrganisation", result.get("organisation"));
|
||||||
assertEquals("myJobTitle", result.get("jobTitle"));
|
assertEquals("myJobTitle", result.get("jobtitle"));
|
||||||
assertEquals("firstName.lastName@email.com", result.get("email"));
|
assertEquals("firstName.lastName@email.com", result.get("email"));
|
||||||
|
|
||||||
// Check for duplicate names
|
// Check for duplicate names
|
||||||
@@ -142,6 +164,33 @@ public class PersonServiceTest extends BaseWebScriptTest
|
|||||||
"myJobTitle", "myEmail", "myBio", "images/avatar.jpg", 500);
|
"myJobTitle", "myEmail", "myBio", "images/avatar.jpg", 500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private JSONObject updatePerson(String userName, String title, String firstName, String lastName,
|
||||||
|
String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus)
|
||||||
|
throws Exception
|
||||||
|
{
|
||||||
|
// switch to admin user to create a person
|
||||||
|
String currentUser = this.authenticationComponent.getCurrentUserName();
|
||||||
|
String adminUser = this.authenticationComponent.getSystemUserName();
|
||||||
|
this.authenticationComponent.setCurrentUser(adminUser);
|
||||||
|
|
||||||
|
JSONObject person = new JSONObject();
|
||||||
|
person.put("userName", userName);
|
||||||
|
person.put("title", title);
|
||||||
|
person.put("firstName", firstName);
|
||||||
|
person.put("lastName", lastName);
|
||||||
|
person.put("organisation", organisation);
|
||||||
|
person.put("jobtitle", jobTitle);
|
||||||
|
person.put("email", email);
|
||||||
|
|
||||||
|
MockHttpServletResponse response = putRequest(
|
||||||
|
URL_PEOPLE + "/" + userName, expectedStatus, person.toString(), "application/json");
|
||||||
|
|
||||||
|
// switch back to non-admin user
|
||||||
|
this.authenticationComponent.setCurrentUser(currentUser);
|
||||||
|
|
||||||
|
return new JSONObject(response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
private JSONObject createPerson(String userName, String title, String firstName, String lastName,
|
private JSONObject createPerson(String userName, String title, String firstName, String lastName,
|
||||||
String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus)
|
String organisation, String jobTitle, String email, String bio, String avatarUrl, int expectedStatus)
|
||||||
throws Exception
|
throws Exception
|
||||||
@@ -157,7 +206,7 @@ public class PersonServiceTest extends BaseWebScriptTest
|
|||||||
person.put("firstName", firstName);
|
person.put("firstName", firstName);
|
||||||
person.put("lastName", lastName);
|
person.put("lastName", lastName);
|
||||||
person.put("organisation", organisation);
|
person.put("organisation", organisation);
|
||||||
person.put("jobTitle", jobTitle);
|
person.put("jobtitle", jobTitle);
|
||||||
person.put("email", email);
|
person.put("email", email);
|
||||||
|
|
||||||
MockHttpServletResponse response = postRequest(URL_PEOPLE, expectedStatus, person.toString(), "application/json");
|
MockHttpServletResponse response = postRequest(URL_PEOPLE, expectedStatus, person.toString(), "application/json");
|
||||||
@@ -187,7 +236,7 @@ public class PersonServiceTest extends BaseWebScriptTest
|
|||||||
String userName = RandomStringUtils.randomNumeric(6);
|
String userName = RandomStringUtils.randomNumeric(6);
|
||||||
JSONObject result = createPerson(userName, "myTitle", "myFirstName", "myLastName", "myOrganisation",
|
JSONObject result = createPerson(userName, "myTitle", "myFirstName", "myLastName", "myOrganisation",
|
||||||
"myJobTitle", "myEmailAddress", "myBio", "images/avatar.jpg", 200);
|
"myJobTitle", "myEmailAddress", "myBio", "images/avatar.jpg", 200);
|
||||||
response = getRequest(URL_PEOPLE + "/" + userName, 200);
|
response = getRequest(URL_PEOPLE + "/" + userName, Status.STATUS_OK);
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user