Skip to content

Commit

Permalink
HAWKULAR-636 HAWKULAR-637 - Extra checks when removing organizations.
Browse files Browse the repository at this point in the history
  • Loading branch information
jpkrohling committed Sep 28, 2015
1 parent 7953101 commit 03d94b6
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,15 @@ public Response deleteOrganization(@NotNull @PathParam("id") String id) {
return Response.status(Response.Status.BAD_REQUEST).entity(response).build();
}

// check if there are resources
if (resourceService.getByPersona(organization).size() > 0) {
ErrorResponse response = new ErrorResponse("This organization is the owner of resources. Please, remove " +
"or transfer those resources before removing this organization.");

return Response.status(Response.Status.BAD_REQUEST).entity(response).build();
}

// check if it's allowed to remove
if (permissionChecker.isAllowedTo(operationDelete, id, persona)) {
organizationService.deleteOrganization(organization);
return Response.ok().build();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
*/
package org.hawkular.accounts.api;

import java.util.List;

import org.hawkular.accounts.api.model.Persona;
import org.hawkular.accounts.api.model.Resource;

Expand Down Expand Up @@ -81,4 +83,11 @@ public interface ResourceService {
* @throws IllegalArgumentException if the given ID is null
*/
void delete(String id);

/**
* Lists all the resources that belong to a given persona.
* @param persona the persona that owns the resources
* @return a List of Resource containing the resources owned by the persona
*/
List<Resource> getByPersona(Persona persona);
}
Original file line number Diff line number Diff line change
Expand Up @@ -122,4 +122,19 @@ public void delete(String id) {
em.remove(resource);
}
}

@Override
public List<Resource> getByPersona(Persona persona) {
if (null == persona) {
throw new IllegalArgumentException("The given persona is invalid (null).");
}

CriteriaBuilder builder = em.getCriteriaBuilder();
CriteriaQuery<Resource> query = builder.createQuery(Resource.class);
Root<Resource> root = query.from(Resource.class);
query.select(root);
query.where(builder.equal(root.get(Resource_.persona), persona));

return em.createQuery(query).getResultList();
}
}

0 comments on commit 03d94b6

Please sign in to comment.