Changeset 15:58357a4b86de in AnnotationManagerN4J


Ignore:
Timestamp:
Aug 28, 2012, 6:23:12 PM (12 years ago)
Author:
casties
Branch:
default
Message:

ASSIGNED - # 249: Annotations shared in groups
https://it-dev.mpiwg-berlin.mpg.de/tracs/mpdl-project-software/ticket/249

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  
    44package de.mpiwg.itgroup.annotations;
    55
     6import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    67import de.mpiwg.itgroup.annotations.old.NS;
    78
     
    2223
    2324    /**
    24      * Returns if this Actor is equivalent to an Actor with this id. If this is
     25     * Returns if this Actor is equivalent to Person person. If this is
    2526     * a Group returns true when the Person is in the Group.
    2627     *
    27      * @param userId
     28     * @param person
     29     * @param store AnnotationStore to check group membership
    2830     * @return
    2931     */
    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())) {
    3335            return true;
    3436        }
    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);           
    3740        }
    3841        return false;
  • src/main/java/de/mpiwg/itgroup/annotations/Annotation.java

    r14 r15  
    33 */
    44package de.mpiwg.itgroup.annotations;
     5
     6import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    57
    68/**
     
    8688     *
    8789     * @param action
    88      * @param userId
     90     * @param user
     91     * @param store AnnotationStore to check group membership
    8992     * @return
    9093     */
    91     public boolean isActionAllowed(String action, String userId) {
     94    public boolean isActionAllowed(String action, Person user, AnnotationStore store) {
    9295        if (action.equals("read")) {
    9396            Actor reader = getReadPermission();
     
    9598                return true;
    9699            } else {
    97                 return reader.isEquivalentWith(userId);
     100                return reader.isEquivalentWith(user, store);
    98101            }
    99102        } else if (action.equals("update")) {
    100103            // require at least an authenticated user
    101             if (userId == null) return false;
     104            if (user == null) return false;
    102105            Actor updater = getUpdatePermission();
    103106            if (updater == null) {
    104107                return true;
    105108            } else {
    106                 return updater.isEquivalentWith(userId);
     109                return updater.isEquivalentWith(user, store);
    107110            }
    108111        } else if (action.equals("delete")) {
    109112            // require at least an authenticated user
    110             if (userId == null) return false;
     113            if (user == null) return false;
    111114            Actor updater = getUpdatePermission();
    112115            if (updater == null) {
    113116                return true;
    114117            } else {
    115                 return updater.isEquivalentWith(userId);
     118                return updater.isEquivalentWith(user, store);
    116119            }
    117120        } else if (action.equals("admin")) {
    118121            // require at least an authenticated user
    119             if (userId == null) return false;
     122            if (user == null) return false;
    120123            Actor admin = getAdminPermission();
    121124            if (admin == null) {
    122125                return true;
    123126            } else {
    124                 return admin.isEquivalentWith(userId);
     127                return admin.isEquivalentWith(user, store);
    125128            }
    126129        }
  • src/main/java/de/mpiwg/itgroup/annotations/Group.java

    r12 r15  
    2121    }
    2222
     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
    2331    @Override
    2432    public boolean isGroup() {
  • src/main/java/de/mpiwg/itgroup/annotations/Person.java

    r12 r15  
    2424    }
    2525
     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
    2633    @Override
    2734    public boolean isGroup() {
     
    3643    }
    3744
     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    }
    3857}
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r14 r15  
    3535
    3636    public static enum NodeTypes {
    37         ANNOTATION, PERSON, TARGET
     37        ANNOTATION, PERSON, TARGET, GROUP
    3838    }
    3939
     
    4141
    4242    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
    4444    }
    4545
     
    5454        nodeIndexes.add(NodeTypes.PERSON.ordinal(), graphDb.index().forNodes("persons"));
    5555        nodeIndexes.add(NodeTypes.TARGET.ordinal(), graphDb.index().forNodes("targets"));
     56        nodeIndexes.add(NodeTypes.GROUP.ordinal(), graphDb.index().forNodes("groups"));
    5657    }
    5758
     
    6061    }
    6162
     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   
    62117    /**
    63118     * Returns the Annotation with the given id.
     
    143198     */
    144199    protected Actor createActorFromNode(Node actorNode) {
     200        String id = (String) actorNode.getProperty("id", null);
    145201        String uri = (String) actorNode.getProperty("uri", null);
    146202        String name = (String) actorNode.getProperty("name", null);
    147203        String type = (String) actorNode.getProperty("TYPE", null);
    148204        if (type != null && type.equals("PERSON")) {
    149             return new Person(uri, name);
     205            return new Person(id, uri, name);
    150206        } else if (type != null && type.equals("GROUP")) {
    151             return new Group(uri, name);
     207            return new Group(id, uri, name);
    152208        }
    153209        return null;
     
    210266            Actor creator = annot.getCreator();
    211267            if (creator != null) {
    212                 Node creatorNode = getOrCreatePersonNode(creator);
     268                Node creatorNode = getOrCreateActorNode(creator);
    213269                getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
    214270            }
     
    301357        if (userUri != null) {
    302358            // there should be only one
    303             Node person = getNodeIndex(NodeTypes.PERSON).get("uri", userUri).getSingle();
     359            Node person = getPersonNodeByUri(userUri);
    304360            if (person != null) {
    305361                Iterable<Relationship> relations = person.getRelationships(RelationTypes.CREATED);
     
    381437    }
    382438
    383     protected Node getOrCreatePersonNode(Actor actor) {
    384         // Person is identified by URI
     439    protected Node getOrCreateActorNode(Actor actor) {
     440        // Person/Group is identified by URI or id
    385441        String uri = actor.getUriString();
    386442        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        }
    388450        IndexHits<Node> persons = idx.get("uri", uri);
    389451        Node person = persons.getSingle();
     
    393455            try {
    394456                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                }
    396462                person.setProperty("uri", uri);
    397463                idx.add(person, "uri", uri);
     
    399465                    person.setProperty("name", name);
    400466                }
     467                if (id != null) {
     468                    person.setProperty("id", id);
     469                }
    401470                tx.success();
    402471            } finally {
     
    417486        Node newActorNode = null;
    418487        if (actor != null) {
    419             newActorNode = getOrCreatePersonNode(actor);
     488            newActorNode = getOrCreateActorNode(actor);
    420489        }
    421490        Relationship rel = getRelation(annotNode, type, null);
     
    464533    }
    465534
     535    /** returns the (first) Relationship of RelationTypes type from Node start.
     536     * 
     537     * @param start
     538     * @param type
     539     * @param direction
     540     * @return
     541     */
    466542    protected Relationship getRelation(Node start, RelationTypes type, Direction direction) {
    467543        Iterable<Relationship> rels;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorAnnotations.java

    r14 r15  
    77import java.io.IOException;
    88
    9 import org.json.JSONArray;
    109import org.json.JSONException;
    1110import org.json.JSONObject;
     
    1918
    2019import de.mpiwg.itgroup.annotations.Annotation;
     20import de.mpiwg.itgroup.annotations.Person;
    2121import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    2222
     
    5252
    5353        // do authentication
    54         String authUser = this.checkAuthToken(entity);
     54        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    5555        logger.debug("request authenticated=" + authUser);
    5656
    5757        Annotation annot = getAnnotationStore().getAnnotationById(id);
    5858        if (annot != null) {
    59             if (! annot.isActionAllowed("read", authUser)) {
     59            if (! annot.isActionAllowed("read", authUser, null)) {
    6060                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    6161                return null;
     
    8484       
    8585        // do authentication TODO: who's allowed to create?
    86         String authUser = this.checkAuthToken(entity);
     86        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    8787        logger.debug("request authenticated=" + authUser);
    8888        if (authUser == null) {
     
    142142
    143143        // do authentication
    144         String authUser = this.checkAuthToken(entity);
     144        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    145145        logger.debug("request authenticated=" + authUser);
    146146
     
    160160                return null;
    161161            }
    162             if (! storedAnnot.isActionAllowed("update", authUser)) {
     162            if (! storedAnnot.isActionAllowed("update", authUser, null)) {
    163163                setStatus(Status.CLIENT_ERROR_FORBIDDEN);
    164164                return null;
     
    205205
    206206        // do authentication
    207         String authUser = this.checkAuthToken(entity);
     207        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    208208        logger.debug("request authenticated=" + authUser);
    209209        Annotation annot = getAnnotationStore().getAnnotationById(id);
    210210        if (annot != null) {
    211             if (! annot.isActionAllowed("delete", authUser)) {
     211            if (! annot.isActionAllowed("delete", authUser, null)) {
    212212                setStatus(Status.CLIENT_ERROR_FORBIDDEN, "Not Authorized!");
    213213                return null;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r14 r15  
    533533    }
    534534
    535     @SuppressWarnings("unused")
     535    @SuppressWarnings("unused") // i in for loop
    536536    protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
    537537        Actor actor = null;
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorSearch.java

    r14 r15  
    1616
    1717import de.mpiwg.itgroup.annotations.Annotation;
     18import de.mpiwg.itgroup.annotations.Person;
     19import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    1820
    1921/**
     
    4244        setCorsHeaders();
    4345        // do authentication
    44         String authUser = this.checkAuthToken(entity);
     46        Person authUser = Person.createPersonWithId(this.checkAuthToken(entity));
    4547        logger.debug("request authenticated=" + authUser);
    4648
     
    5456        // do search
    5557        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);
    5760        for (Annotation annot : annots) {
    5861            // check permission
    59             if (!annot.isActionAllowed("read", authUser)) continue;
     62            if (!annot.isActionAllowed("read", authUser, store)) continue;
    6063            JSONObject jo = createAnnotatorJson(annot, (authUser == null));
    6164            if (jo != null) {
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/RestServer.java

    r6 r15  
    166166        router.attach("/annotator/annotations/{id}", AnnotatorAnnotations.class);
    167167        router.attach("/annotator/search", AnnotatorSearch.class);
     168        router.attach("/annotator/groups", AnnotatorGroups.class);
    168169
    169170        // router.attach("",redirector); router.attach("/annotator",
Note: See TracChangeset for help on using the changeset viewer.