NamespaceManager.set(NAMESPACE);
PrivilegeEntity entity = ofy().load().type(PrivilegeEntity.class).id(KEY).get();
if (entity == null)
throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
"Privilege [" + privilege + "] does not exist. There are no existing privileges");
if (! entity.privileges.contains(privilege))
throw new APIException(Status.ERROR_RESOURCE_DOES_NOT_EXIST,
"Privilage [" + privilege + "] does not exist");
//ensure no role has this privilege
TreeSet<RoleProp> props = RoleRepository.getAllRoles();
String rolesWithPrivilege = "";
for (RoleProp roleProp : props) {
if (roleProp.privileges.contains(privilege))
rolesWithPrivilege += roleProp.role + ", ";
}
if (! rolesWithPrivilege.equals(""))
throw new APIException(Status.ERROR_PRECONDITION_FAILURE,
"Privilege [" + privilege + "] is assigned to following roles - [" +
rolesWithPrivilege + "]. Please unassign privilege [" + privilege +
"] from these roles first.");
//ensure no user has this privilege
TreeSet<UserProp> allUsers = UserRepository.getAllUsers();
String usersWithPrivilege = "";
for (UserProp userProp : allUsers) {
if (userProp.privileges.contains(privilege))
usersWithPrivilege += userProp.email + ", ";
}
if (! usersWithPrivilege.equals("")) {
throw new APIException(Status.ERROR_PRECONDITION_FAILURE,
"Privilege [" + privilege + "] is assigned to following users - [" +
usersWithPrivilege + "]. Please unassign privilege [" + privilege +
"] from these users first.");
}