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());
}
}
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());
}
}
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());
}
}
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());
}
}
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;
}
}
Aggregations