Changeset 10:90911b2da322 in AnnotationManagerN4J for src


Ignore:
Timestamp:
Jul 12, 2012, 3:01:32 PM (12 years ago)
Author:
casties
Branch:
default
Message:

more work on permissions...

Location:
src/main/java/de/mpiwg/itgroup/annotations
Files:
2 added
5 edited

Legend:

Unmodified
Added
Removed
  • src/main/java/de/mpiwg/itgroup/annotations/Actor.java

    r9 r10  
    44package de.mpiwg.itgroup.annotations;
    55
     6import de.mpiwg.itgroup.annotations.old.NS;
     7
    68/**
    79 * @author casties
    810 *
    911 */
    10 public class Actor {
     12public abstract class Actor {
    1113
    12     public boolean isGroup;
    1314    public String uri;
    1415    public String name;
     16    public String id;
    1517   
    1618    /**
    17      * @param isGroup
    18      * @param id
    19      * @param uri
    20      * @param name
     19     * @return if this Actor is a Group
    2120     */
    22     public Actor(boolean isGroup, String uri, String name) {
    23         super();
    24         this.isGroup = isGroup;
    25         this.uri = uri;
    26         this.name = name;
    27     }
     21    public abstract boolean isGroup();
    2822   
    29     /**
    30      * @return the isGroup
    31      */
    32     public boolean isGroup() {
    33         return isGroup;
    34     }
    35     /**
    36      * @param isGroup the isGroup to set
    37      */
    38     public void setGroup(boolean isGroup) {
    39         this.isGroup = isGroup;
    40     }
    4123    /**
    4224     * @return the uri
     
    6345        this.name = name;
    6446    }
     47
     48    /**
     49     * @return the id
     50     */
     51    public String getId() {
     52        return id;
     53    }
     54   
     55    /**
     56     * Returns id as a String starting with "group:" for groups.
     57     *
     58     * @return
     59     */
     60    public abstract String getIdString();
     61   
     62    /**
     63     * @param id the id to set
     64     */
     65    public void setId(String id) {
     66        this.id = id;
     67    }
     68   
     69    /**
     70     * Returns a short id from an uri.
     71     *
     72     * @param uri
     73     * @return
     74     */
     75    public static String getIdFromUri(String uri, boolean isGroup) {
     76        String id = null;
     77        String prefix = NS.MPIWG_PERSONS_URL;
     78        if (isGroup) {
     79            prefix = NS.MPIWG_GROUPS_URL;
     80        }
     81        if (uri != null && uri.startsWith(prefix)) {
     82            id = uri.replace(prefix, "");
     83        }
     84        return id;
     85    }
     86
     87    /**
     88     * Returns an uri from a short id.
     89     *
     90     * @param id
     91     * @return
     92     */
     93    public static String getUriFromId(String id, boolean isGroup) {
     94        String uri = null;
     95        String prefix = NS.MPIWG_PERSONS_URL;
     96        if (isGroup) {
     97            prefix = NS.MPIWG_GROUPS_URL;
     98        }
     99        if (id != null && ! id.startsWith("http://")) {
     100            uri = prefix + id;
     101        }
     102        return uri;
     103    }
     104
    65105}
  • src/main/java/de/mpiwg/itgroup/annotations/Annotation.java

    r9 r10  
    5757    protected String created;
    5858
    59     protected String adminPermission;
     59    /**
     60     * The user or group that has admin permissions.
     61     * null means any user.
     62     */
     63    protected Actor adminPermission;
     64   
     65    /**
     66     * The user or group that has delete permissions.
     67     * null means any user.
     68     */
     69    protected Actor deletePermission;
     70   
     71    /**
     72     * The user or group that has update permissions.
     73     * null means any user.
     74     */
     75    protected Actor updatePermission;
     76   
     77    /**
     78     * The user or group that has read permissions.
     79     * null means any user.
     80     */
     81    protected Actor readPermission;
    6082   
    6183    /**
     
    190212        this.created = created;
    191213    }
     214
     215    /**
     216     * @return the adminPermission
     217     */
     218    public Actor getAdminPermission() {
     219        return adminPermission;
     220    }
     221
     222    /**
     223     * @param adminPermission the adminPermission to set
     224     */
     225    public void setAdminPermission(Actor adminPermission) {
     226        this.adminPermission = adminPermission;
     227    }
     228
     229    /**
     230     * @return the deletePermission
     231     */
     232    public Actor getDeletePermission() {
     233        return deletePermission;
     234    }
     235
     236    /**
     237     * @param deletePermission the deletePermission to set
     238     */
     239    public void setDeletePermission(Actor deletePermission) {
     240        this.deletePermission = deletePermission;
     241    }
     242
     243    /**
     244     * @return the updatePermission
     245     */
     246    public Actor getUpdatePermission() {
     247        return updatePermission;
     248    }
     249
     250    /**
     251     * @param updatePermission the updatePermission to set
     252     */
     253    public void setUpdatePermission(Actor updatePermission) {
     254        this.updatePermission = updatePermission;
     255    }
     256
     257    /**
     258     * @return the readPermission
     259     */
     260    public Actor getReadPermission() {
     261        return readPermission;
     262    }
     263
     264    /**
     265     * @param readPermission the readPermission to set
     266     */
     267    public void setReadPermission(Actor readPermission) {
     268        this.readPermission = readPermission;
     269    }
    192270   
    193271   
  • src/main/java/de/mpiwg/itgroup/annotations/neo4j/AnnotationStore.java

    r9 r10  
    2121import de.mpiwg.itgroup.annotations.Annotation;
    2222import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
     23import de.mpiwg.itgroup.annotations.Person;
    2324
    2425/**
     
    103104                        String uri = (String) creatorNode.getProperty("uri", null);
    104105                        String name = (String) creatorNode.getProperty("name", null);
    105             Actor creator = new Actor(false, uri, name);
     106            Actor creator = new Person(uri, name);
    106107            annot.setCreator(creator);
    107108                        // just the first one
     
    164165
    165166                        /*
    166                          * The name of the creator of this annotation.
    167                          */
    168                         String creatorName = annot.getCreatorName();
    169 
    170                         /*
    171                          * The URI of the creator of this annotation.
    172                          */
    173                         String creatorUri = annot.getCreatorUri();
    174                         if (creatorUri != null) {
    175                                 Node creator = getOrCreatePersonNode(creatorUri, creatorName);
    176                                 getOrCreateRelation(creator, RelationTypes.CREATED, annotNode);
     167                         * The creator of this annotation.
     168                         */
     169                        Actor creator = annot.getCreator();
     170                        if (creator != null) {
     171                                Node creatorNode = getOrCreatePersonNode(creator);
     172                                getOrCreateRelation(creatorNode, RelationTypes.CREATED, annotNode);
    177173                        }
    178174
     
    342338        }
    343339
    344         protected Node getOrCreatePersonNode(String uri, String name) {
     340        protected Node getOrCreatePersonNode(Actor actor) {
     341            /*
    345342                // Person is identified by URI
    346343                Index<Node> idx = getNodeIndex(NodeTypes.PERSON);
     
    364361                }
    365362                return person;
     363                */
     364            return null;
    366365        }
    367366
  • src/main/java/de/mpiwg/itgroup/annotations/old/NS.java

    r4 r10  
    33public class NS {
    44    public static final String MPIWG_PERSONS_URL = "http://entities.mpiwg-berlin.mpg.de/persons/";
    5     // public static String
    6     // ANNOTATION_TYPE="http://www.w3.org/2000/10/annotationType#";
     5    public static final String MPIWG_GROUPS_URL = "http://entities.mpiwg-berlin.mpg.de/groups/";
    76    public static final String OAC_NS = "http://www.openannotation.org/ns/";
    87    public static final String CNT_NS = "http://www.w3.org/2011/content#";
  • src/main/java/de/mpiwg/itgroup/annotations/restlet/AnnotatorResourceImpl.java

    r9 r10  
    3737import de.mpiwg.itgroup.annotations.Annotation;
    3838import de.mpiwg.itgroup.annotations.Annotation.FragmentTypes;
     39import de.mpiwg.itgroup.annotations.Group;
     40import de.mpiwg.itgroup.annotations.Person;
    3941import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
    4042import de.mpiwg.itgroup.annotations.old.NS;
     
    189191                // create user object
    190192                JSONObject userObject = new JSONObject();
     193                Actor creator = annot.getCreator();
    191194                // save creator as uri
    192                 userObject.put("uri", annot.getCreatorUri());
     195                userObject.put("uri", creator.getUri());
    193196                // make short user id
    194                 String userId = annot.getCreatorUri();
    195                 // remove namespace from user uri to get id
    196                 if (userId != null && userId.startsWith(NS.MPIWG_PERSONS_URL)) {
    197                     userId = userId.replace(NS.MPIWG_PERSONS_URL, "");
    198                 }
     197                String userId = creator.getIdString();
    199198                // set as id
    200199                userObject.put("id", userId);
    201200                // get full name
    202                 String userName = annot.getCreatorName();
     201                String userName = creator.getName();
    203202                if (userName == null) {
    204203                    RestServer restServer = (RestServer) getApplication();
     
    224223                }
    225224            }
     225           
     226            // permissions
     227            JSONObject perms = new JSONObject();
     228            jo.put("permissions", perms);
     229            // admin
     230            JSONArray adminPerms = new JSONArray();
     231            perms.put("admin", adminPerms);
     232            Actor adminPerm = annot.getAdminPermission();
     233            if (adminPerm != null) {
     234                adminPerms.put(adminPerm.getIdString());
     235            }
     236            // delete
     237            JSONArray deletePerms = new JSONArray();
     238            perms.put("delete", deletePerms);
     239            Actor deletePerm = annot.getDeletePermission();
     240            if (deletePerm != null) {
     241                deletePerms.put(deletePerm.getIdString());
     242            }
     243            // update
     244            JSONArray updatePerms = new JSONArray();
     245            perms.put("update", updatePerms);
     246            Actor updatePerm = annot.getUpdatePermission();
     247            if (updatePerm != null) {
     248                updatePerms.put(updatePerm.getIdString());
     249            }
     250            // read
     251            JSONArray readPerms = new JSONArray();
     252            perms.put("read", readPerms);
     253            Actor readPerm = annot.getReadPermission();
     254            if (readPerm != null) {
     255                readPerms.put(readPerm.getIdString());
     256            }
     257           
    226258            // encode Annotation URL (=id) in base64
    227259            String annotUrl = annot.getUri();
     
    246278        try {
    247279            for (String xpointer : xpointers) {
    248                 //String decoded = URLDecoder.decode(xpointer, "utf-8");
     280                // String decoded = URLDecoder.decode(xpointer, "utf-8");
    249281                String decoded = xpointer;
    250282                Matcher m = rg.matcher(decoded);
     
    284316        try {
    285317            for (String xpointer : xpointers) {
    286                 //String decoded = URLDecoder.decode(xpointer, "utf-8");
     318                // String decoded = URLDecoder.decode(xpointer, "utf-8");
    287319                String decoded = xpointer;
    288320                Matcher m = rg.matcher(decoded);
     
    291323                    {
    292324                        JSONObject jo = new JSONObject();
     325                        @SuppressWarnings("unused")
    293326                        String unit = m.group(1);
    294327                        jo.put("x", m.group(2));
     
    393426        Actor creator = annot.getCreator();
    394427        if (creator == null) {
    395             creator = new Actor(false, null, null);
     428            creator = new Person();
    396429            annot.setCreator(creator);
    397430        }
    398431        // username not required, if no username given authuser will be used
    399432        String username = null;
    400         String userUri = annot.getCreatorUri();
     433        String userUri = creator.getUri();
    401434        if (jo.has("user")) {
    402435            if (jo.get("user") instanceof String) {
    403436                // user is just a String
    404437                username = jo.getString("user");
     438                creator.setId(username);
    405439                // TODO: what if username and authUser are different?
    406440            } else {
     
    408442                JSONObject user = jo.getJSONObject("user");
    409443                if (user.has("id")) {
    410                     username = user.getString("id");
     444                    String id = user.getString("id");
     445                    creator.setId(id);
     446                    username = id;
    411447                }
    412448                if (user.has("uri")) {
     
    436472            creator.setUri(userUri);
    437473        }
    438        
     474
    439475        if (annot.getCreated() == null) {
    440476            // set creation date
     
    457493            annot.setTargetFragment(fragment);
    458494        }
     495
     496        // permissions
     497        if (jo.has("permissions")) {
     498            JSONObject permissions = jo.getJSONObject("permissions");
     499            if (permissions.has("admin")) {
     500                JSONArray perms = permissions.getJSONArray("admin");
     501                Actor actor = getActorFromPermissions(perms);
     502                annot.setAdminPermission(actor);
     503            }
     504            if (permissions.has("delete")) {
     505                JSONArray perms = permissions.getJSONArray("delete");
     506                Actor actor = getActorFromPermissions(perms);
     507                annot.setDeletePermission(actor);
     508            }
     509            if (permissions.has("update")) {
     510                JSONArray perms = permissions.getJSONArray("update");
     511                Actor actor = getActorFromPermissions(perms);
     512                annot.setUpdatePermission(actor);
     513            }
     514            if (permissions.has("read")) {
     515                JSONArray perms = permissions.getJSONArray("read");
     516                Actor actor = getActorFromPermissions(perms);
     517                annot.setReadPermission(actor);
     518            }
     519        }
     520
    459521        return annot;
    460522    }
    461523
     524    @SuppressWarnings("unused")
     525    protected Actor getActorFromPermissions(JSONArray perms) throws JSONException {
     526        Actor actor = null;
     527        for (int i = 0; i < perms.length(); ++i) {
     528            String perm = perms.getString(i);
     529            if (perm.toLowerCase().startsWith("group:")) {
     530                String groupId = perm.substring(6);
     531                actor = new Group(groupId);
     532            } else {
     533                actor = new Person(perm);
     534            }
     535            // we just take the first one
     536            break;
     537        }
     538        return actor;
     539    }
     540
    462541}
Note: See TracChangeset for help on using the changeset viewer.