use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class UserWebService method updateUser.
@Path("{id}")
@PUT
@Consumes({ Constants.MEDIA_TYPE_SCIM_JSON, MediaType.APPLICATION_JSON })
@Produces({ Constants.MEDIA_TYPE_SCIM_JSON + "; charset=utf-8", MediaType.APPLICATION_JSON + "; charset=utf-8" })
@HeaderParam("Accept")
@DefaultValue(Constants.MEDIA_TYPE_SCIM_JSON)
@ApiOperation(value = "Update user", notes = "Update user (https://tools.ietf.org/html/rfc7644#section-3.5.1)", response = User.class)
public Response updateUser(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_TEST_MODE_OAUTH2_TOKEN) final String token, @PathParam("id") String id, @ApiParam(value = "User", required = true) User user, @QueryParam(OxTrustConstants.QUERY_PARAMETER_ATTRIBUTES) final String attributesArray) throws Exception {
Response authorizationResponse;
if (jsonConfigurationService.getOxTrustappConfiguration().isScimTestMode()) {
log.info(" ##### SCIM Test Mode is ACTIVE");
authorizationResponse = processTestModeAuthorization(token);
} else {
authorizationResponse = processAuthorization(authorization);
}
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
User updatedUser = scim2UserService.updateUser(id, user);
// Serialize to JSON
String json = serializeToJson(updatedUser, attributesArray);
URI location = new URI(updatedUser.getMeta().getLocation());
return Response.ok(json).location(location).build();
} catch (EntryPersistenceException ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found");
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage());
} catch (Exception ex) {
log.error("Failed to update user", ex);
ex.printStackTrace();
return getErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, INTERNAL_SERVER_ERROR_MESSAGE);
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class Scim2UserService method validUsernameByInum.
private GluuCustomPerson validUsernameByInum(User user, String id) throws DuplicateEntryException {
GluuCustomPerson gluuPerson = personService.getPersonByInum(id);
if (gluuPerson == null) {
throw new EntryPersistenceException("Scim2UserService.updateUser(): " + "Resource " + id + " not found");
} else {
// Validate if attempting to update userName of a different id
if (user.getUserName() != null) {
GluuCustomPerson personToFind = new GluuCustomPerson();
personToFind.setUid(user.getUserName());
List<GluuCustomPerson> foundPersons = personService.findPersons(personToFind, 2);
if (foundPersons != null && foundPersons.size() > 0) {
for (GluuCustomPerson foundPerson : foundPersons) {
if (foundPerson != null && !foundPerson.getInum().equalsIgnoreCase(gluuPerson.getInum())) {
throw new DuplicateEntryException("Cannot update userName of a different id: " + user.getUserName());
}
}
}
}
}
return gluuPerson;
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class Scim2UserService method updateUser.
public User updateUser(String id, User user) throws Exception {
GluuCustomPerson gluuPerson = personService.getPersonByInum(id);
if (gluuPerson == null) {
throw new EntryPersistenceException("Scim2UserService.updateUser(): " + "Resource " + id + " not found");
} else {
// Validate if attempting to update userName of a different id
if (user.getUserName() != null) {
GluuCustomPerson personToFind = new GluuCustomPerson();
personToFind.setUid(user.getUserName());
List<GluuCustomPerson> foundPersons = personService.findPersons(personToFind, 2);
if (foundPersons != null && foundPersons.size() > 0) {
for (GluuCustomPerson foundPerson : foundPersons) {
if (foundPerson != null && !foundPerson.getInum().equalsIgnoreCase(gluuPerson.getInum())) {
throw new DuplicateEntryException("Cannot update userName of a different id: " + user.getUserName());
}
}
}
}
}
GluuCustomPerson updatedGluuPerson = copyUtils2.copy(user, gluuPerson, true);
if (user.getGroups().size() > 0) {
serviceUtil.groupMembersAdder(updatedGluuPerson, personService.getDnForPerson(id));
}
log.info(" Setting meta: update user ");
// Date should be in UTC format
DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.dateTime().withZoneUTC();
Date dateLastModified = DateTime.now().toDate();
updatedGluuPerson.setAttribute("oxTrustMetaLastModified", dateTimeFormatter.print(dateLastModified.getTime()));
if (updatedGluuPerson.getAttribute("oxTrustMetaLocation") == null || (updatedGluuPerson.getAttribute("oxTrustMetaLocation") != null && updatedGluuPerson.getAttribute("oxTrustMetaLocation").isEmpty())) {
String relativeLocation = "/scim/v2/Users/" + id;
updatedGluuPerson.setAttribute("oxTrustMetaLocation", relativeLocation);
}
// Sync email, forward ("oxTrustEmail" -> "mail")
updatedGluuPerson = serviceUtil.syncEmailForward(updatedGluuPerson, true);
// For custom script: update user
if (externalScimService.isEnabled()) {
externalScimService.executeScimUpdateUserMethods(updatedGluuPerson);
}
personService.updatePerson(updatedGluuPerson);
log.debug(" person updated ");
User updatedUser = copyUtils2.copy(updatedGluuPerson, null);
return updatedUser;
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class GroupService method addGroup.
/* (non-Javadoc)
* @see org.gluu.oxtrust.ldap.service.IGroupService#addGroup(org.gluu.oxtrust.model.GluuGroup)
*/
@Override
public void addGroup(GluuGroup group) throws Exception {
GluuGroup displayNameGroup = new GluuGroup();
displayNameGroup.setDisplayName(group.getDisplayName());
List<GluuGroup> groups = findGroups(displayNameGroup, 1);
if (groups == null || groups.size() == 0) {
ldapEntryManager.persist(group);
} else {
throw new DuplicateEntryException("Duplicate displayName: " + group.getDisplayName());
}
}
use of org.gluu.site.ldap.exception.DuplicateEntryException in project oxTrust by GluuFederation.
the class PersonService method addPerson.
/* (non-Javadoc)
* @see org.gluu.oxtrust.ldap.service.IPersonService#addPerson(org.gluu.oxtrust.model.GluuCustomPerson)
*/
// TODO: Review this methods. We need to check if uid is unique in outside
// method
@Override
public void addPerson(GluuCustomPerson person) throws Exception {
GluuCustomPerson uidPerson = new GluuCustomPerson();
uidPerson.setUid(person.getUid());
List<GluuCustomPerson> persons = findPersons(uidPerson, 1);
if (persons == null || persons.size() == 0) {
person.setCreationDate(new Date());
ldapEntryManager.persist(person);
} else {
throw new DuplicateEntryException("Duplicate UID value: " + person.getUid());
}
}
Aggregations