Changeset 15:58357a4b86de in AnnotationManagerN4J
- Timestamp:
- Aug 28, 2012, 6:23:12 PM (12 years ago)
- Branch:
- default
- Location:
- src/main/java/de/mpiwg/itgroup/annotations
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
src/main/java/de/mpiwg/itgroup/annotations/Actor.java
r14 r15 4 4 package de.mpiwg.itgroup.annotations; 5 5 6 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 6 7 import de.mpiwg.itgroup.annotations.old.NS; 7 8 … … 22 23 23 24 /** 24 * Returns if this Actor is equivalent to an Actor with this id. If this is25 * Returns if this Actor is equivalent to Person person. If this is 25 26 * a Group returns true when the Person is in the Group. 26 27 * 27 * @param userId 28 * @param person 29 * @param store AnnotationStore to check group membership 28 30 * @return 29 31 */ 30 public boolean isEquivalentWith( String userId) {31 if ( userId== null) return false;32 if ( userId.equals(getIdString())) {32 public boolean isEquivalentWith(Person person, AnnotationStore store) { 33 if (person == null) return false; 34 if (person.equals(getIdString())) { 33 35 return true; 34 36 } 35 if (isGroup()) { 36 // TODO: check if person in group 37 if (isGroup() && store != null) { 38 // check if person in group 39 return store.isPersonInGroup(person, (Group) this); 37 40 } 38 41 return false; -
src/main/java/de/mpiwg/itgroup/annotations/Annotation.java
r14 r15 3 3 */ 4 4 package de.mpiwg.itgroup.annotations; 5 6 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 5 7 6 8 /** … … 86 88 * 87 89 * @param action 88 * @param userId 90 * @param user 91 * @param store AnnotationStore to check group membership 89 92 * @return 90 93 */ 91 public boolean isActionAllowed(String action, String userId) {94 public boolean isActionAllowed(String action, Person user, AnnotationStore store) { 92 95 if (action.equals("read")) { 93 96 Actor reader = getReadPermission(); … … 95 98 return true; 96 99 } else { 97 return reader.isEquivalentWith(user Id);100 return reader.isEquivalentWith(user, store); 98 101 } 99 102 } else if (action.equals("update")) { 100 103 // require at least an authenticated user 101 if (user Id== null) return false;104 if (user == null) return false; 102 105 Actor updater = getUpdatePermission(); 103 106 if (updater == null) { 104 107 return true; 105 108 } else { 106 return updater.isEquivalentWith(user Id);109 return updater.isEquivalentWith(user, store); 107 110 } 108 111 } else if (action.equals("delete")) { 109 112 // require at least an authenticated user 110 if (user Id== null) return false;113 if (user == null) return false; 111 114 Actor updater = getUpdatePermission(); 112 115 if (updater == null) { 113 116 return true; 114 117 } else { 115 return updater.isEquivalentWith(user Id);118 return updater.isEquivalentWith(user, store); 116 119 } 117 120 } else if (action.equals("admin")) { 118 121 // require at least an authenticated user 119 if (user Id== null) return false;122 if (user == null) return false; 120 123 Actor admin = getAdminPermission(); 121 124 if (admin == null) { 122 125 return true; 123 126 } else { 124 return admin.isEquivalentWith(user Id);127 return admin.isEquivalentWith(user, store); 125 128 } 126 129 } -
src/main/java/de/mpiwg/itgroup/annotations/Group.java
r12 r15 21 21 } 22 22 23 public Group(String id, String uri, String name) { 24 super(); 25 this.id = id; 26 this.uri = uri; 27 this.name = name; 28 } 29 30 23 31 @Override 24 32 public boolean isGroup() { -
src/main/java/de/mpiwg/itgroup/annotations/Person.java
r12 r15 24 24 } 25 25 26 public Person(String id, String uri, String name) { 27 super(); 28 this.id = id; 29 this.uri = uri; 30 this.name = name; 31 } 32 26 33 @Override 27 34 public boolean isGroup() { … … 36 43 } 37 44 45 /** 46 * Returns a Person with this id or null. 47 * 48 * @param id 49 * @return 50 */ 51 public static Person createPersonWithId(String id) { 52 if (id != null) { 53 return new Person(id); 54 } 55 return null; 56 } 38 57 } -
src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java
r14 r15 35 35 36 36 public static enum NodeTypes { 37 ANNOTATION, PERSON, TARGET 37 ANNOTATION, PERSON, TARGET, GROUP 38 38 } 39 39 … … 41 41 42 42 public static enum RelationTypes implements RelationshipType { 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ 43 ANNOTATES, CREATED, PERMITS_ADMIN, PERMITS_DELETE, PERMITS_UPDATE, PERMITS_READ, MEMBER_OF 44 44 } 45 45 … … 54 54 nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons")); 55 55 nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets")); 56 nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups")); 56 57 } 57 58 … … 60 61 } 61 62 63 /** 64 * @param userUri 65 * @return 66 */ 67 public Node getPersonNodeByUri(String userUri) { 68 if (userUri == null) return null; 69 Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle(); 70 return person; 71 } 72 73 74 /** 75 * Returns List of Groups the person is member of. 76 * 77 * @param person 78 * @return 79 */ 80 public List<Group> getGroupsForPersonNode(Node person) { 81 ArrayList<Group> groups = new ArrayList<Group>(); 82 Iterable<Relationship> rels = person.getRelationships(RelationTypes.MEMBER_OF); 83 for (Relationship rel : rels) { 84 Node groupNode = rel.getEndNode(); 85 Actor group = createActorFromNode(groupNode); 86 // make sure we're getting a group 87 if (!(group instanceof Group)) { 88 logger.error("target of MEMBER_OF is not GROUP! rel="+rel); 89 continue; 90 } 91 groups.add((Group) group); 92 } 93 return groups; 94 } 95 96 /** 97 * Returns if person with uri is in Group group. 98 * 99 * @param person 100 * @param group 101 * @return 102 */ 103 public boolean isPersonInGroup(Person person, Group group) { 104 Node pn = getPersonNodeByUri(person.getUriString()); 105 if (pn == null) return false; 106 // optimised version of getGroupsForPersonNode 107 Iterable<Relationship> rels = pn.getRelationships(RelationTypes.MEMBER_OF); 108 for (Relationship rel : rels) { 109 Node gn = rel.getEndNode(); 110 if (gn.getProperty("uri", "").equals(group.getUriString()) || gn.getProperty("id", "").equals(group.getId())) { 111 return true; 112 } 113 } 114 return false; 115 } 116 62 117 /** 63 118 * Returns the Annotation with the given id. … … 143 198 */ 144 199 protected Actor createActorFromNode(Node actorNode) { 200 String id = (String) actorNode.getProperty("id", null); 145 201 String uri = (String) actorNode.getProperty("uri", null); 146 202 String name = (String) actorNode.getProperty("name", null); 147 203 String type = (String) actorNode.getProperty("TYPE", null); 148 204 if (type != null && type.equals("PERSON")) { 149 return new Person( uri, name);205 return new Person(id, uri, name); 150 206 } else if (type != null && type.equals("GROUP")) { 151 return new Group( uri, name);207 return new Group(id, uri, name); 152 208 } 153 209 return null; … … 210 266 Actor creator = annot.getCreator(); 211 267 if (creator != null) { 212 Node creatorNode = getOrCreate PersonNode(creator);268 Node creatorNode = getOrCreateActorNode(creator); 213 269 getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode); 214 270 } … … 301 357 if (userUri != null) { 302 358 // there should be only one 303 Node person = get NodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();359 Node person = getPersonNodeByUri(userUri); 304 360 if (person != null) { 305 361 Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED); … … 381 437 } 382 438 383 protected Node getOrCreate PersonNode(Actor actor) {384 // Person is identified by URI439 protected Node getOrCreateActorNode(Actor actor) { 440 // Person/Group is identified by URI or id 385 441 String uri = actor.getUriString(); 386 442 String name = actor.getName(); 387 Index<Node> idx = getNodeIndex(NodeTypes.PERSON); 443 String id = actor.getId(); 444 Index<Node> idx; 445 if (actor.isGroup()) { 446 idx = getNodeIndex(NodeTypes.GROUP); 447 } else { 448 idx = getNodeIndex(NodeTypes.PERSON); 449 } 388 450 IndexHits<Node> persons = idx.get("uri", uri); 389 451 Node person = persons.getSingle(); … … 393 455 try { 394 456 person = graphDb.createNode(); 395 person.setProperty("TYPE", NodeTypes.PERSON.name()); 457 if (actor.isGroup()) { 458 person.setProperty("TYPE", NodeTypes.GROUP.name()); 459 } else { 460 person.setProperty("TYPE", NodeTypes.PERSON.name()); 461 } 396 462 person.setProperty("uri", uri); 397 463 idx.add(person, "uri", uri); … … 399 465 person.setProperty("name", name); 400 466 } 467 if (id != null) { 468 person.setProperty("id", id); 469 } 401 470 tx.success(); 402 471 } finally { … … 417 486 Node newActorNode = null; 418 487 if (actor != null) { 419 newActorNode = getOrCreate PersonNode(actor);488 newActorNode = getOrCreateActorNode(actor); 420 489 } 421 490 Relationship rel = getRelation(annotNode, type, null); … … 464 533 } 465 534 535 /** returns the (first) Relationship of RelationTypes type from Node start. 536 * 537 * @param start 538 * @param type 539 * @param direction 540 * @return 541 */ 466 542 protected Relationship getRelation(Node start, RelationTypes type, Direction direction) { 467 543 Iterable<Relationship> rels; -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java
r14 r15 7 7 import java.io.IOException; 8 8 9 import org.json.JSONArray;10 9 import org.json.JSONException; 11 10 import org.json.JSONObject; … … 19 18 20 19 import de.mpiwg.itgroup.annotations.Annotation; 20 import de.mpiwg.itgroup.annotations.Person; 21 21 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 22 22 … … 52 52 53 53 // do authentication 54 String authUser = this.checkAuthToken(entity);54 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 55 55 logger.debug("request authenticated=" + authUser); 56 56 57 57 Annotation annot = getAnnotationStore().getAnnotationById(id); 58 58 if (annot != null) { 59 if (! annot.isActionAllowed("read", authUser )) {59 if (! annot.isActionAllowed("read", authUser, null)) { 60 60 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 61 61 return null; … … 84 84 85 85 // do authentication TODO: who's allowed to create? 86 String authUser = this.checkAuthToken(entity);86 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 87 87 logger.debug("request authenticated=" + authUser); 88 88 if (authUser == null) { … … 142 142 143 143 // do authentication 144 String authUser = this.checkAuthToken(entity);144 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 145 145 logger.debug("request authenticated=" + authUser); 146 146 … … 160 160 return null; 161 161 } 162 if (! storedAnnot.isActionAllowed("update", authUser )) {162 if (! storedAnnot.isActionAllowed("update", authUser, null)) { 163 163 setStatus(Status.CLIENT_ERROR_FORBIDDEN); 164 164 return null; … … 205 205 206 206 // do authentication 207 String authUser = this.checkAuthToken(entity);207 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 208 208 logger.debug("request authenticated=" + authUser); 209 209 Annotation annot = getAnnotationStore().getAnnotationById(id); 210 210 if (annot != null) { 211 if (! annot.isActionAllowed("delete", authUser )) {211 if (! annot.isActionAllowed("delete", authUser, null)) { 212 212 setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!"); 213 213 return null; -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java
r14 r15 533 533 } 534 534 535 @SuppressWarnings("unused") 535 @SuppressWarnings("unused") // i in for loop 536 536 protected Actor getActorFromPermissions(JSONArray perms) throws JSONException { 537 537 Actor actor = null; -
src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java
r14 r15 16 16 17 17 import de.mpiwg.itgroup.annotations.Annotation; 18 import de.mpiwg.itgroup.annotations.Person; 19 import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore; 18 20 19 21 /** … … 42 44 setCorsHeaders(); 43 45 // do authentication 44 String authUser = this.checkAuthToken(entity);46 Person authUser = Person.createPersonWithId(this.checkAuthToken(entity)); 45 47 logger.debug("request authenticated=" + authUser); 46 48 … … 54 56 // do search 55 57 logger.debug(String.format("searching for uri=%s user=%s", uri, user)); 56 List<Annotation> annots = getAnnotationStore().searchByUriUser(uri, user, limit, offset); 58 AnnotationStore store = getAnnotationStore(); 59 List<Annotation> annots = store.searchByUriUser(uri, user, limit, offset); 57 60 for (Annotation annot : annots) { 58 61 // check permission 59 if (!annot.isActionAllowed("read", authUser )) continue;62 if (!annot.isActionAllowed("read", authUser, store)) continue; 60 63 JSONObject jo = createAnnotatorJson(annot, (authUser == null)); 61 64 if (jo != null) { -
src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java
r6 r15 166 166 router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class); 167 167 router.attach("/annotator/search", AnnotatorSearch.class); 168 router.attach("/annotator/groups", AnnotatorGroups.class); 168 169 169 170 // router.attach("",redirector); router.attach("/annotator",
Note: See TracChangeset
for help on using the changeset viewer.