}
public static void checkAccess(DatastoreService datastore, Entity e, AccessType accessType) {
UserService userService = UserServiceFactory.getUserService();
if (!userService.isUserLoggedIn()) {
throw new NotAuthorizedException();
}
if (e == null) {
return;
}
if (userService.isUserAdmin()) {
return;
}
// Check read access by kind
if (accessType == AccessType.READ) {
Query q = new Query(KIND_ACCESS_CONTROL_KIND);
Filter filter = new FilterPredicate("Kind", FilterOperator.EQUAL, e.getKind());
q.setFilter(filter);
Entity kac = datastore.prepare(q).asSingleEntity();
if (kac != null) {
Long access = (Long) kac.getProperty("Access");
if (access.equals(UserGroup.USER.groupId)) {
return;
}
}
}
// Check write access by entity
else if (accessType == AccessType.WRITE) {
Query q = new Query(ENTITY_ACCESS_CONTROL_KIND);
Filter filter = new FilterPredicate("Entity", FilterOperator.EQUAL, e);
q.setFilter(filter);
Entity eac = datastore.prepare(q).asSingleEntity();
User owner = (User) eac.getProperty("owner");
if (owner.compareTo(userService.getCurrentUser()) == 0) {
return;
}
}
throw new NotAuthorizedException();
}