mirror of
https://github.com/Alfresco/alfresco-community-repo.git
synced 2025-08-14 17:58:59 +00:00
REPO-1506: Update Person - implement
- added implementation for update personService - added tests git-svn-id: https://svn.alfresco.com/repos/alfresco-enterprise/alfresco/BRANCHES/DEV/5.2.N/root@132117 c4b6b30b-aa2e-2d43-bbcb-ca4b014f7261
This commit is contained in:
@@ -52,5 +52,12 @@ public interface People
|
||||
*/
|
||||
Person create(PersonUpdate person);
|
||||
|
||||
//Person updatePerson(String personId, Person person);
|
||||
/**
|
||||
* Update the given person's details.
|
||||
*
|
||||
* @param personId The identifier of a person.
|
||||
* @param person The person details.
|
||||
* @return The updated person details.
|
||||
*/
|
||||
Person update(String personId, Person person);
|
||||
}
|
||||
|
@@ -36,6 +36,7 @@ import org.alfresco.rest.api.model.PersonUpdate;
|
||||
import org.alfresco.rest.framework.core.exceptions.ConstraintViolatedException;
|
||||
import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
|
||||
import org.alfresco.rest.framework.core.exceptions.InvalidArgumentException;
|
||||
import org.alfresco.rest.framework.resource.parameters.Parameters;
|
||||
import org.alfresco.service.cmr.repository.*;
|
||||
import org.alfresco.service.cmr.security.AuthenticationService;
|
||||
import org.alfresco.service.cmr.security.MutableAuthenticationService;
|
||||
@@ -300,26 +301,38 @@ public class PeopleImpl implements People
|
||||
mas.createAuthentication(person.getUserName(), person.getPassword().toCharArray());
|
||||
mas.setAuthenticationEnabled(person.getUserName(), person.isEnabled());
|
||||
NodeRef nodeRef = personService.createPerson(props);
|
||||
|
||||
// Write the contents of PersonUpdate.getDescription() text to a content file
|
||||
// and store the content URL in ContentModel.PROP_PERSONDESC
|
||||
if (person.getDescription() != null)
|
||||
{
|
||||
AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
|
||||
{
|
||||
@Override
|
||||
public Void doWork() throws Exception
|
||||
{
|
||||
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_PERSONDESC, true);
|
||||
writer.putContent(person.getDescription());
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
// Return a fresh retrieval
|
||||
return getPerson(person.getUserName());
|
||||
}
|
||||
// Write the contents of PersonUpdate.getDescription() text to a content file
|
||||
// and store the content URL in ContentModel.PROP_PERSONDESC
|
||||
if (person.getDescription() != null)
|
||||
{
|
||||
savePersonDescription(person.getDescription(), nodeRef);
|
||||
}
|
||||
|
||||
// Return a fresh retrieval
|
||||
return getPerson(person.getUserName());
|
||||
}
|
||||
|
||||
/**
|
||||
* Write the description to a content file and store the content URL in
|
||||
* ContentModel.PROP_PERSONDESC
|
||||
*
|
||||
* @param description
|
||||
* @param nodeRef
|
||||
*/
|
||||
private void savePersonDescription(final String description, final NodeRef nodeRef)
|
||||
{
|
||||
AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
|
||||
{
|
||||
@Override
|
||||
public Void doWork() throws Exception
|
||||
{
|
||||
ContentWriter writer = contentService.getWriter(nodeRef, ContentModel.PROP_PERSONDESC, true);
|
||||
writer.putContent(description);
|
||||
return null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private void validateCreatePersonData(PersonUpdate person)
|
||||
{
|
||||
@@ -337,28 +350,38 @@ public class PeopleImpl implements People
|
||||
throw new InvalidArgumentException("Field '"+fieldName+"' is null, but is required.");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
public Person updatePerson(String personId, final Person person)
|
||||
public Person update(String personId, final Person person)
|
||||
{
|
||||
personId = validatePerson(personId);
|
||||
MutableAuthenticationService mutableAuthenticationService = (MutableAuthenticationService) authenticationService;
|
||||
|
||||
final Map<QName, Serializable> properties = toProperties(person);
|
||||
final String personIdToUpdate = validatePerson(personId);
|
||||
final Map<QName, Serializable> properties = person.toProperties();
|
||||
|
||||
final String pId = personId;
|
||||
AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
|
||||
{
|
||||
@Override
|
||||
public Void doWork() throws Exception
|
||||
{
|
||||
personService.setPersonProperties(pId, properties, false);
|
||||
return null;
|
||||
}
|
||||
|
||||
});
|
||||
if (person.getPassword() != null && !person.getPassword().isEmpty())
|
||||
{
|
||||
// an Admin user can update without knowing the original pass - but
|
||||
// must know their own!
|
||||
mutableAuthenticationService.setAuthentication(personIdToUpdate, person.getPassword().toCharArray());
|
||||
}
|
||||
|
||||
return getPerson(personId);
|
||||
if (person.isEnabled() != null)
|
||||
{
|
||||
mutableAuthenticationService.setAuthenticationEnabled(personIdToUpdate, person.isEnabled());
|
||||
}
|
||||
|
||||
if (person.getDescription() != null)
|
||||
{
|
||||
// Remove person description from saved properties
|
||||
properties.remove(ContentModel.PROP_PERSONDESC);
|
||||
|
||||
// Custom save for person description.
|
||||
NodeRef personNodeRef = personService.getPerson(personIdToUpdate, false);
|
||||
savePersonDescription(person.getDescription(), personNodeRef);
|
||||
}
|
||||
|
||||
personService.setPersonProperties(personIdToUpdate, properties, false);
|
||||
|
||||
return getPerson(personId);
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
@@ -359,6 +359,10 @@ public class Person
|
||||
addToMap(properties, ContentModel.PROP_SIZE_QUOTA, getQuota());
|
||||
addToMap(properties, ContentModel.PROP_SIZE_CURRENT, getQuotaUsed());
|
||||
addToMap(properties, ContentModel.PROP_PERSONDESC, getDescription());
|
||||
addToMap(properties, ContentModel.PROP_ENABLED, isEnabled());
|
||||
|
||||
Boolean isEmailNotificationsEnabled = isEmailNotificationsEnabled();
|
||||
addToMap(properties, ContentModel.PROP_EMAIL_FEED_DISABLED, (isEmailNotificationsEnabled == null ? null : !isEmailNotificationsEnabled.booleanValue()));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -50,7 +50,7 @@ import java.util.List;
|
||||
* @author Gethin James
|
||||
*/
|
||||
@EntityResource(name="people", title = "People")
|
||||
public class PeopleEntityResource implements EntityResourceAction.ReadById<Person>, EntityResourceAction.Create<Person>, InitializingBean
|
||||
public class PeopleEntityResource implements EntityResourceAction.ReadById<Person>, EntityResourceAction.Create<Person>, EntityResourceAction.Update<Person>, InitializingBean
|
||||
{
|
||||
private static Log logger = LogFactory.getLog(PeopleEntityResource.class);
|
||||
|
||||
@@ -133,4 +133,48 @@ public class PeopleEntityResource implements EntityResourceAction.ReadById<Perso
|
||||
result.add(people.create(person));
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
@WebApiDescription(title="Update person", description="Update the given person's details")
|
||||
public Person update(String personId, Person person, Parameters parameters)
|
||||
{
|
||||
validateNonUpdatableFieldsExistence(person);
|
||||
|
||||
return people.update(personId, person);
|
||||
}
|
||||
|
||||
/**
|
||||
* Explicitly test for the presence of fields that are present on Person but
|
||||
* shouldn't be updatable (until REPO-110 is solved).
|
||||
*
|
||||
* @param person
|
||||
*/
|
||||
private void validateNonUpdatableFieldsExistence(Person person)
|
||||
{
|
||||
|
||||
if (person.getUserName() != null)
|
||||
{
|
||||
throw new InvalidArgumentException("Unsupported field: userName");
|
||||
}
|
||||
|
||||
if (person.getStatusUpdatedAt() != null)
|
||||
{
|
||||
throw new InvalidArgumentException("Unsupported field: statusUpdatedAt");
|
||||
}
|
||||
|
||||
if (person.getAvatarId() != null)
|
||||
{
|
||||
throw new InvalidArgumentException("Unsupported field: avatarId");
|
||||
}
|
||||
|
||||
if (person.getQuota() != null)
|
||||
{
|
||||
throw new InvalidArgumentException("Unsupported field: quota");
|
||||
}
|
||||
|
||||
if (person.getQuotaUsed() != null)
|
||||
{
|
||||
throw new InvalidArgumentException("Unsupported field: quotaUsed");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user