use of org.gluu.oxtrust.model.GluuGroup in project oxTrust by GluuFederation.
the class CopyUtils2 method copy.
/**
* Copy data from GluuGroup object to ScimGroup object
*
* @param source
* @param destination
* @return
* @throws Exception
*/
public Group copy(GluuGroup source, Group destination) throws Exception {
if (source == null) {
return null;
}
if (destination == null) {
destination = new Group();
}
destination.setDisplayName(source.getDisplayName());
destination.setId(source.getInum());
if (source.getMembers() != null) {
if (source.getMembers().size() > 0) {
Set<MemberRef> memberRefSet = new HashSet<MemberRef>();
List<String> membersList = source.getMembers();
for (String oneMember : membersList) {
if (oneMember != null && !oneMember.isEmpty()) {
GluuCustomPerson gluuCustomPerson = personService.getPersonByDn(oneMember);
MemberRef memberRef = new MemberRef();
memberRef.setValue(gluuCustomPerson.getInum());
memberRef.setDisplay(gluuCustomPerson.getDisplayName());
String reference = appConfiguration.getBaseEndpoint() + "/scim/v2/Users/" + gluuCustomPerson.getInum();
memberRef.setReference(reference);
memberRefSet.add(memberRef);
}
}
destination.setMembers(memberRefSet);
}
}
log.trace(" getting meta ");
Meta meta = (destination.getMeta() != null) ? destination.getMeta() : new Meta();
if (source.getAttribute("oxTrustMetaVersion") != null) {
meta.setVersion(source.getAttribute("oxTrustMetaVersion"));
}
String location = source.getAttribute("oxTrustMetaLocation");
if (location != null && !location.isEmpty()) {
if (!location.startsWith("https://") && !location.startsWith("http://")) {
location = appConfiguration.getBaseEndpoint() + location;
}
} else {
location = appConfiguration.getBaseEndpoint() + "/scim/v2/Groups/" + source.getInum();
}
meta.setLocation(location);
if (source.getAttribute("oxTrustMetaCreated") != null && !source.getAttribute("oxTrustMetaCreated").isEmpty()) {
try {
DateTime dateTimeUtc = new DateTime(source.getAttribute("oxTrustMetaCreated"), DateTimeZone.UTC);
meta.setCreated(dateTimeUtc.toDate());
} catch (Exception e) {
log.error(" Date parse exception (NEW format), continuing...", e);
// For backward compatibility
try {
meta.setCreated(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(source.getAttribute("oxTrustMetaCreated")));
} catch (Exception ex) {
log.error(" Date parse exception (OLD format)", ex);
}
}
}
if (source.getAttribute("oxTrustMetaLastModified") != null && !source.getAttribute("oxTrustMetaLastModified").isEmpty()) {
try {
DateTime dateTimeUtc = new DateTime(source.getAttribute("oxTrustMetaLastModified"), DateTimeZone.UTC);
meta.setLastModified(dateTimeUtc.toDate());
} catch (Exception e) {
log.error(" Date parse exception (NEW format), continuing...", e);
// For backward compatibility
try {
meta.setLastModified(new SimpleDateFormat("EEE MMM dd HH:mm:ss zzz yyyy").parse(source.getAttribute("oxTrustMetaLastModified")));
} catch (Exception ex) {
log.error(" Date parse exception (OLD format)", ex);
}
}
}
destination.setMeta(meta);
return destination;
}
use of org.gluu.oxtrust.model.GluuGroup in project oxTrust by GluuFederation.
the class ServiceUtil method deleteUserFromGroup.
/**
* Delete a person from a group
*
* @return void
* @throws Exception
*/
public void deleteUserFromGroup(GluuCustomPerson person, String dn) throws Exception {
List<String> groups = person.getMemberOf();
for (String oneGroup : groups) {
GluuGroup aGroup = groupService.getGroupByDn(oneGroup);
List<String> groupMembers = aGroup.getMembers();
List<String> tempGroupMembers = new ArrayList<String>();
if (groupMembers != null && !groupMembers.isEmpty()) {
for (String aMember : groupMembers) {
tempGroupMembers.add(aMember);
}
}
for (String oneMember : tempGroupMembers) {
if (oneMember.equalsIgnoreCase(dn)) {
tempGroupMembers.remove(oneMember);
break;
}
}
List<String> cleanGroupMembers = new ArrayList<String>();
for (String aMember : tempGroupMembers) {
cleanGroupMembers.add(aMember);
}
aGroup.setMembers(cleanGroupMembers);
if (aGroup.getMembers() != null && aGroup.getMembers().isEmpty()) {
// Reset to no members
aGroup.setMembers(null);
}
groupService.updateGroup(aGroup);
}
}
use of org.gluu.oxtrust.model.GluuGroup in project oxTrust by GluuFederation.
the class BulkWebService method processGroupOperation.
private BulkOperation processGroupOperation(BulkOperation operation, Map<String, String> processedBulkIds) throws Exception {
log.info(" Operation is for Group ");
// Intercept bulkId
Group group = null;
if (operation.getData() != null) {
// Required in a request when
// "method" is "POST", "PUT", or
// "PATCH".
String serializedData = serialize(operation.getData());
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
serializedData = serializedData.replaceAll(key, entry.getValue());
}
group = deserializeToGroup(serializedData);
}
String groupRootEndpoint = appConfiguration.getBaseEndpoint() + "/scim/v2/Groups/";
if (operation.getMethod().equalsIgnoreCase(HttpMethod.POST)) {
log.info(" Method is POST ");
try {
group = scim2GroupService.createGroup(group);
GluuGroup gluuGroup = groupService.getGroupByDisplayName(group.getDisplayName());
String id = gluuGroup.getInum();
// String location = (new
// StringBuilder()).append(domain).append("/Groups/").append(id).toString();
String location = groupRootEndpoint + id;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.CREATED.getStatusCode()));
operation.setResponse(group);
// Set aside successfully-processed bulkId
// bulkId is only required in POST
processedBulkIds.put(operation.getBulkId(), group.getId());
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to create group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.PUT)) {
log.info(" Method is PUT ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
group = scim2GroupService.updateGroup(id, group);
// String location = (new
// StringBuilder()).append(domain).append("/Groups/").append(groupiD).toString();
String location = groupRootEndpoint + id;
operation.setLocation(location);
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse(group);
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), group.getId());
}
} catch (EntryPersistenceException ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, ErrorScimType.INVALID_VALUE, "Resource " + id + " not found"));
} catch (DuplicateEntryException ex) {
log.error("DuplicateEntryException", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.CONFLICT.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.CONFLICT, ErrorScimType.UNIQUENESS, ex.getMessage()));
} catch (Exception ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
} else if (operation.getMethod().equalsIgnoreCase(HttpMethod.DELETE)) {
log.info(" Method is DELETE ");
String path = operation.getPath();
String id = getId(path);
for (Map.Entry<String, String> entry : processedBulkIds.entrySet()) {
String key = "bulkId:" + entry.getKey();
if (id.equalsIgnoreCase(key)) {
id = id.replaceAll(key, entry.getValue());
break;
}
}
try {
scim2GroupService.deleteGroup(id);
// Location may be omitted on DELETE
operation.setStatus(String.valueOf(Response.Status.OK.getStatusCode()));
operation.setResponse("Group " + id + " deleted");
// bulkId is only required in POST
if (operation.getBulkId() != null) {
processedBulkIds.put(operation.getBulkId(), id);
}
} catch (EntryPersistenceException ex) {
log.error("Failed to delete group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.NOT_FOUND.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.NOT_FOUND, null, "Resource " + id + " not found"));
} catch (Exception ex) {
log.error("Failed to delete group", ex);
ex.printStackTrace();
operation.setStatus(String.valueOf(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode()));
operation.setResponse(createErrorResponse(Response.Status.INTERNAL_SERVER_ERROR, null, INTERNAL_SERVER_ERROR_MESSAGE));
}
}
return operation;
}
use of org.gluu.oxtrust.model.GluuGroup in project oxTrust by GluuFederation.
the class GroupWebService method updateGroup.
@Path("{id}")
@PUT
@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response updateGroup(@HeaderParam("Authorization") String authorization, @PathParam("id") String id, ScimGroup group) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
GluuGroup gluuGroup = groupService.getGroupByInum(id);
if (gluuGroup == null) {
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} else {
// Validate if attempting to update displayName of a different id
if (gluuGroup.getDisplayName() != null) {
GluuGroup groupToFind = new GluuGroup();
groupToFind.setDisplayName(group.getDisplayName());
List<GluuGroup> foundGroups = groupService.findGroups(groupToFind, 2);
if (foundGroups != null && foundGroups.size() > 0) {
for (GluuGroup foundGroup : foundGroups) {
if (foundGroup != null && !foundGroup.getInum().equalsIgnoreCase(gluuGroup.getInum())) {
throw new DuplicateEntryException("Cannot update displayName of a different id: " + group.getDisplayName());
}
}
}
}
}
GluuGroup newGluuGroup = copyUtils.copy(group, gluuGroup, true);
if (group.getMembers().size() > 0) {
serviceUtil.personMembersAdder(newGluuGroup, groupService.getDnForGroup(id));
}
// For custom script: update group
if (externalScimService.isEnabled()) {
externalScimService.executeScimUpdateGroupMethods(newGluuGroup);
}
groupService.updateGroup(newGluuGroup);
log.debug(" group updated ");
ScimGroup newGroup = copyUtils.copy(newGluuGroup, null);
URI location = new URI("/Groups/" + id);
return Response.ok(newGroup).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 group", ex);
ex.printStackTrace();
return getErrorResponse(ex.getMessage(), Response.Status.BAD_REQUEST.getStatusCode());
} catch (Exception ex) {
log.error("Failed to update group", ex);
ex.printStackTrace();
return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
use of org.gluu.oxtrust.model.GluuGroup in project oxTrust by GluuFederation.
the class GroupWebService method deleteGroup.
@Path("{id}")
@DELETE
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
public Response deleteGroup(@HeaderParam("Authorization") String authorization, @PathParam("id") String id) throws Exception {
Response authorizationResponse = processAuthorization(authorization);
if (authorizationResponse != null) {
return authorizationResponse;
}
try {
log.info(" Checking if the group exists ");
log.info(" id : " + id);
GluuGroup gluuGroup = groupService.getGroupByInum(id);
if (gluuGroup == null) {
log.info(" the group is null ");
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} else {
// For custom script: delete group
if (externalScimService.isEnabled()) {
externalScimService.executeScimDeleteGroupMethods(gluuGroup);
}
log.info(" getting started to delete members from groups ");
if (gluuGroup.getMembers() != null) {
if (gluuGroup.getMembers().size() > 0) {
log.info(" getting dn for group ");
String dn = groupService.getDnForGroup(id);
log.info(" DN : " + dn);
serviceUtil.deleteGroupFromPerson(gluuGroup, dn);
}
}
log.info(" removing the group ");
groupService.removeGroup(gluuGroup);
}
return Response.ok().build();
} catch (EntryPersistenceException ex) {
ex.printStackTrace();
return getErrorResponse("Resource " + id + " not found", Response.Status.NOT_FOUND.getStatusCode());
} catch (Exception ex) {
ex.printStackTrace();
return getErrorResponse(INTERNAL_SERVER_ERROR_MESSAGE, Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
}
}
Aggregations