Search in sources :

Example 6 with DuplicateEntryException

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);
    }
}
Also used : ListResponse(org.gluu.oxtrust.model.scim2.ListResponse) Response(javax.ws.rs.core.Response) VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) User(org.gluu.oxtrust.model.scim2.User) ScimPatchUser(org.gluu.oxtrust.model.scim2.ScimPatchUser) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) URI(java.net.URI) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) Path(javax.ws.rs.Path) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) ApiOperation(com.wordnik.swagger.annotations.ApiOperation) PUT(javax.ws.rs.PUT)

Example 7 with DuplicateEntryException

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;
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException)

Example 8 with DuplicateEntryException

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;
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) User(org.gluu.oxtrust.model.scim2.User) ScimPatchUser(org.gluu.oxtrust.model.scim2.ScimPatchUser) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) DateTimeFormatter(org.joda.time.format.DateTimeFormatter) Date(java.util.Date)

Example 9 with DuplicateEntryException

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());
    }
}
Also used : DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) GluuGroup(org.gluu.oxtrust.model.GluuGroup)

Example 10 with DuplicateEntryException

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());
    }
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) Date(java.util.Date)

Aggregations

DuplicateEntryException (org.gluu.site.ldap.exception.DuplicateEntryException)18 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)15 Consumes (javax.ws.rs.Consumes)10 Produces (javax.ws.rs.Produces)10 Response (javax.ws.rs.core.Response)10 VirtualListViewResponse (org.xdi.ldap.model.VirtualListViewResponse)10 URI (java.net.URI)8 PersonRequiredFieldsException (org.gluu.oxtrust.exception.PersonRequiredFieldsException)8 GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)7 GluuGroup (org.gluu.oxtrust.model.GluuGroup)7 ApiOperation (com.wordnik.swagger.annotations.ApiOperation)6 DefaultValue (javax.ws.rs.DefaultValue)6 HeaderParam (javax.ws.rs.HeaderParam)6 PUT (javax.ws.rs.PUT)6 Path (javax.ws.rs.Path)6 ListResponse (org.gluu.oxtrust.model.scim2.ListResponse)6 User (org.gluu.oxtrust.model.scim2.User)5 POST (javax.ws.rs.POST)4 Group (org.gluu.oxtrust.model.scim2.Group)4 ScimPatchUser (org.gluu.oxtrust.model.scim2.ScimPatchUser)4