Search in sources :

Example 81 with GluuCustomPerson

use of org.gluu.oxtrust.model.GluuCustomPerson in project oxTrust by GluuFederation.

the class UserWebService method updateUser.

@Path("{id}")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateUser(@HeaderParam("Authorization") String authorization, @PathParam("id") String id, ScimPerson person) throws Exception {
    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
        return authorizationResponse;
    }
    try {
        GluuCustomPerson gluuPerson = personService.getPersonByInum(id);
        if (gluuPerson == null) {
            return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
        } else {
            // Validate if attempting to update userName of a different id
            if (person.getUserName() != null) {
                GluuCustomPerson personToFind = new GluuCustomPerson();
                personToFind.setUid(person.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: " + person.getUserName());
                        }
                    }
                }
            }
        }
        GluuCustomPerson newGluuPerson = copyUtils.copy(person, gluuPerson, true);
        if (person.getGroups().size() > 0) {
            serviceUtil.groupMembersAdder(newGluuPerson, personService.getDnForPerson(id));
        }
        // Sync email, forward ("oxTrustEmail" -> "mail")
        newGluuPerson = ServiceUtil.syncEmailForward(newGluuPerson, false);
        // For custom script: update user
        if (externalScimService.isEnabled()) {
            externalScimService.executeScimUpdateUserMethods(newGluuPerson);
        }
        personService.updatePerson(newGluuPerson);
        log.debug(" person updated ");
        ScimPerson newPerson = copyUtils.copy(newGluuPerson, null);
        // person_update = copyUtils.copy(gluuPerson, null, attributes);
        URI location = new URI("/Users/" + id);
        return Response.ok(newPerson).location(location).build();
    } catch (EntryPersistenceException ex) {
        ex.printStackTrace();
        return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
    } catch (DuplicateEntryException ex) {
        log.error("Failed to update user", ex);
        ex.printStackTrace();
        return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
    } catch (Exception ex) {
        log.error("Failed to update user", ex);
        ex.printStackTrace();
        return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
}
Also used : VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) 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) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces) PUT(javax.ws.rs.PUT)

Example 82 with GluuCustomPerson

use of org.gluu.oxtrust.model.GluuCustomPerson in project oxTrust by GluuFederation.

the class UserWebService method getUserByUid.

@Path("{uid}")
@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response getUserByUid(@HeaderParam("Authorization") String authorization, @PathParam("uid") String uid) throws Exception {
    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
        return authorizationResponse;
    }
    try {
        GluuCustomPerson gluuPerson = personService.getPersonByInum(uid);
        if (gluuPerson == null) {
            // sets HTTP status code 404 Not Found
            return getErrorResponse("Resource " + uid + " not found", Response.Status.NOT_FOUND.getStatusCode());
        }
        ScimPerson person = copyUtils.copy(gluuPerson, null);
        URI location = new URI("/Users/" + uid);
        return Response.ok(person).location(location).build();
    } catch (EntryPersistenceException ex) {
        ex.printStackTrace();
        return getErrorResponse("Resource " + uid + " not found", Response.Status.NOT_FOUND.getStatusCode());
    } catch (Exception ex) {
        ex.printStackTrace();
        return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
}
Also used : VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) 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) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 83 with GluuCustomPerson

use of org.gluu.oxtrust.model.GluuCustomPerson in project oxTrust by GluuFederation.

the class UserWebService method createUser.

@POST
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response createUser(@HeaderParam("Authorization") String authorization, ScimPerson person) throws Exception {
    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
        return authorizationResponse;
    }
    try {
        log.debug(" copying gluuperson ");
        GluuCustomPerson gluuPerson = copyUtils.copy(person, null, false);
        if (gluuPerson == null) {
            return getErrorResponse("Failed to create user", Response.Status.BAD_REQUEST.getStatusCode());
        }
        log.debug(" generating inum ");
        // inumService.generateInums(Configuration.INUM_TYPE_PEOPLE_SLUG);
        String inum = personService.generateInumForNewPerson();
        // //personService.generateInumForNewPerson();
        log.debug(" getting DN ");
        String dn = personService.getDnForPerson(inum);
        log.debug(" getting iname ");
        String iname = personService.generateInameForNewPerson(person.getUserName());
        log.debug(" setting dn ");
        gluuPerson.setDn(dn);
        log.debug(" setting inum ");
        gluuPerson.setInum(inum);
        log.debug(" setting iname ");
        gluuPerson.setIname(iname);
        log.debug(" setting commonName ");
        gluuPerson.setCommonName(gluuPerson.getGivenName() + " " + gluuPerson.getSurname());
        log.info("gluuPerson.getMemberOf().size() : " + gluuPerson.getMemberOf().size());
        if (person.getGroups().size() > 0) {
            log.info(" jumping to groupMembersAdder ");
            log.info("gluuPerson.getDn() : " + gluuPerson.getDn());
            serviceUtil.groupMembersAdder(gluuPerson, gluuPerson.getDn());
        }
        // Sync email, forward ("oxTrustEmail" -> "mail")
        gluuPerson = ServiceUtil.syncEmailForward(gluuPerson, false);
        // For custom script: create user
        if (externalScimService.isEnabled()) {
            externalScimService.executeScimCreateUserMethods(gluuPerson);
        }
        log.debug("adding new GluuPerson");
        personService.addPerson(gluuPerson);
        ScimPerson newPerson = copyUtils.copy(gluuPerson, null);
        String uri = "/Users/" + newPerson.getId();
        return Response.created(URI.create(uri)).entity(newPerson).build();
    } catch (DuplicateEntryException ex) {
        log.error("Failed to create user", ex);
        ex.printStackTrace();
        return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
    } catch (PersonRequiredFieldsException ex) {
        log.error("PersonRequiredFieldsException: ", ex);
        return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
    } catch (Exception ex) {
        log.error("Failed to create user", ex);
        ex.printStackTrace();
        return getErrorResponse(ex.getMessage(), Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
}
Also used : VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) PersonRequiredFieldsException(org.gluu.oxtrust.exception.PersonRequiredFieldsException) EntryPersistenceException(org.gluu.site.ldap.persistence.exception.EntryPersistenceException) DuplicateEntryException(org.gluu.site.ldap.exception.DuplicateEntryException) POST(javax.ws.rs.POST) Consumes(javax.ws.rs.Consumes) Produces(javax.ws.rs.Produces)

Example 84 with GluuCustomPerson

use of org.gluu.oxtrust.model.GluuCustomPerson in project oxTrust by GluuFederation.

the class UserWebService method searchPersons.

@GET
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@HeaderParam("Accept")
@DefaultValue(MediaType.APPLICATION_JSON)
public Response searchPersons(@HeaderParam("Authorization") String authorization, @QueryParam(OxTrustConstants.QUERY_PARAMETER_FILTER) final String filterString, @QueryParam(OxTrustConstants.QUERY_PARAMETER_START_INDEX) final int startIndex, @QueryParam(OxTrustConstants.QUERY_PARAMETER_COUNT) final int count, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_BY) final String sortBy, @QueryParam(OxTrustConstants.QUERY_PARAMETER_SORT_ORDER) final String sortOrder, @QueryParam(OxTrustConstants.QUERY_PARAMETER_ATTRIBUTES) final String attributesArray) throws Exception {
    Response authorizationResponse = processAuthorization(authorization);
    if (authorizationResponse != null) {
        return authorizationResponse;
    }
    try {
        if (count > getMaxCount()) {
            String detail = "Too many results (=" + count + ") would be returned; max is " + getMaxCount() + " only.";
            return getErrorResponse(detail, Response.Status.BAD_REQUEST.getStatusCode());
        } else {
            log.info(" Searching persons from LDAP ");
            VirtualListViewResponse vlvResponse = new VirtualListViewResponse();
            List<GluuCustomPerson> gluuCustomPersons = search(personService.getDnForPerson(null), GluuCustomPerson.class, filterString, startIndex, count, sortBy, sortOrder, vlvResponse, attributesArray);
            // List<GluuCustomPerson> personList = personService.findAllPersons(null);
            GluuCustomPersonList personsList = new GluuCustomPersonList();
            List<String> schema = new ArrayList<String>();
            schema.add(Constants.SCIM1_CORE_SCHEMA_ID);
            log.info(" setting schema");
            personsList.setSchemas(schema);
            // Set total
            personsList.setTotalResults(vlvResponse.getTotalResults());
            if (count > 0 && gluuCustomPersons != null && !gluuCustomPersons.isEmpty()) {
                for (GluuCustomPerson gluuPerson : gluuCustomPersons) {
                    ScimPerson person = copyUtils.copy(gluuPerson, null);
                    log.info(" person to be added id : " + person.getUserName());
                    personsList.getResources().add(person);
                    log.info(" person added? : " + personsList.getResources().contains(person));
                }
                // Set the rest of results info
                personsList.setItemsPerPage(vlvResponse.getItemsPerPage());
                personsList.setStartIndex(vlvResponse.getStartIndex());
            }
            URI location = new URI(appConfiguration.getBaseEndpoint() + "/scim/v1/Users");
            // Serialize to JSON
            ObjectMapper mapper = new ObjectMapper();
            mapper.disable(SerializationConfig.Feature.FAIL_ON_EMPTY_BEANS);
            SimpleModule customScimFilterModule = new SimpleModule("CustomScim1PersonFilterModule", new Version(1, 0, 0, ""));
            GluuCustomPersonListSerializer serializer = new GluuCustomPersonListSerializer();
            serializer.setAttributesArray(attributesArray);
            customScimFilterModule.addSerializer(ScimPerson.class, serializer);
            mapper.registerModule(customScimFilterModule);
            String json = mapper.writeValueAsString(personsList);
            return Response.ok(json).location(location).build();
        }
    } catch (Exception ex) {
        log.error("Error in searchPersons", ex);
        ex.printStackTrace();
        return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
    }
}
Also used : VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) ArrayList(java.util.ArrayList) GluuCustomPersonListSerializer(org.gluu.oxtrust.service.antlr.scimFilter.util.GluuCustomPersonListSerializer) 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) VirtualListViewResponse(org.xdi.ldap.model.VirtualListViewResponse) Response(javax.ws.rs.core.Response) GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) Version(org.codehaus.jackson.Version) ScimPerson(org.gluu.oxtrust.model.scim.ScimPerson) GluuCustomPersonList(org.gluu.oxtrust.model.GluuCustomPersonList) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) SimpleModule(org.codehaus.jackson.map.module.SimpleModule) DefaultValue(javax.ws.rs.DefaultValue) HeaderParam(javax.ws.rs.HeaderParam) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 85 with GluuCustomPerson

use of org.gluu.oxtrust.model.GluuCustomPerson in project oxTrust by GluuFederation.

the class BaseScimWebService method getAuthorizedUser.

protected boolean getAuthorizedUser() {
    try {
        GluuCustomPerson authUser = identity.getUser();
        if (authUser == null) {
            return false;
        }
        GluuAppliance appliance = applianceService.getAppliance();
        if (appliance == null) {
            return false;
        }
        if (!(GluuBoolean.TRUE.equals(appliance.getScimEnabled()) || GluuBoolean.ENABLED.equals(appliance.getScimEnabled()))) {
            return false;
        }
        return true;
    } catch (Exception ex) {
        log.error("Exception: ", ex);
        return false;
    }
}
Also used : GluuCustomPerson(org.gluu.oxtrust.model.GluuCustomPerson) GluuAppliance(org.gluu.oxtrust.model.GluuAppliance)

Aggregations

GluuCustomPerson (org.gluu.oxtrust.model.GluuCustomPerson)133 ArrayList (java.util.ArrayList)42 ScimPerson (org.gluu.oxtrust.model.scim.ScimPerson)27 Test (org.testng.annotations.Test)22 EntryPersistenceException (org.gluu.site.ldap.persistence.exception.EntryPersistenceException)21 ConfigurableTest (org.gluu.oxtrust.action.test.ConfigurableTest)18 Produces (javax.ws.rs.Produces)17 Response (javax.ws.rs.core.Response)17 ScimPersonGroups (org.gluu.oxtrust.model.scim.ScimPersonGroups)14 ScimPersonIms (org.gluu.oxtrust.model.scim.ScimPersonIms)14 ScimPersonPhones (org.gluu.oxtrust.model.scim.ScimPersonPhones)14 ScimPersonPhotos (org.gluu.oxtrust.model.scim.ScimPersonPhotos)14 ScimRoles (org.gluu.oxtrust.model.scim.ScimRoles)14 PersonMeta (org.gluu.oxtrust.model.scim.PersonMeta)13 ScimEntitlements (org.gluu.oxtrust.model.scim.ScimEntitlements)13 ScimName (org.gluu.oxtrust.model.scim.ScimName)13 ScimPersonAddresses (org.gluu.oxtrust.model.scim.ScimPersonAddresses)13 ScimPersonEmails (org.gluu.oxtrust.model.scim.ScimPersonEmails)13 ScimCustomAttributes (org.gluu.oxtrust.model.scim.ScimCustomAttributes)12 Scimx509Certificates (org.gluu.oxtrust.model.scim.Scimx509Certificates)12