source: AnnotationManagerN4J/src/main/java/de/mpiwg/itgroup/annotations/Annotation.java @ 105:7417f5915181

tip
Last change on this file since 105:7417f5915181 was 105:7417f5915181, checked in by casties, 7 years ago

check admin permission before changing permissions.
Enum for typesafe actions.

File size: 10.6 KB
Line 
1/**
2 *
3 */
4package de.mpiwg.itgroup.annotations;
5
6/*
7 * #%L
8 * AnnotationManager
9 * %%
10 * Copyright (C) 2012 - 2014 MPIWG Berlin
11 * %%
12 * This program is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License as
14 * published by the Free Software Foundation, either version 3 of the
15 * License, or (at your option) any later version.
16 *
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20 * GNU General Lesser Public License for more details.
21 *
22 * You should have received a copy of the GNU General Lesser Public
23 * License along with this program.  If not, see
24 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
25 * #L%
26 */
27
28import java.io.UnsupportedEncodingException;
29import java.util.Set;
30
31import org.apache.commons.codec.binary.Base64;
32
33import de.mpiwg.itgroup.annotations.neo4j.AnnotationStore;
34
35/**
36 * @author casties
37 *
38 */
39public class Annotation {
40    /**
41     * The URI of this annotation.
42     */
43    protected String uri;
44
45    /**
46     * The annotation (body) text.
47     */
48    protected String bodyText;
49
50    /**
51     * The URI of the annotation text
52     */
53    protected String bodyUri;
54   
55    /**
56     * The annotation target.
57     */
58    protected Target target;
59   
60    /**
61     * The fragment part of the annotation target.
62     */
63    protected String targetFragment;
64   
65    /**
66     * The types of annotation target fragments.
67     *
68     */
69    public static enum FragmentTypes {
70        XPOINTER, AREA, WKT
71    };
72   
73    /**
74     * The type of the annotation target fragment.
75     */
76    protected FragmentTypes fragmentType;
77   
78    /**
79     * The selected text of the annotation target.
80     */
81    protected String quote;
82   
83    /**
84     * The Resource that is annotated e.g. a book.
85     * The target is part of this resource e.g. a page of a book.
86     */
87    protected Resource resource;
88   
89    /**
90     * The creator of this annotation.
91     */
92    protected Actor creator;
93   
94    /**
95     * The creation date of this annotation.
96     */
97    protected String created;
98
99    /**
100     * The last update date of this annotation.
101     */
102    protected String updated;
103
104    /**
105     * The user or group that has admin permissions.
106     * null means any user.
107     */
108    protected Actor adminPermission;
109   
110    /**
111     * The user or group that has delete permissions.
112     * null means any user.
113     */
114    protected Actor deletePermission;
115   
116    /**
117     * The user or group that has update permissions.
118     * null means any user.
119     */
120    protected Actor updatePermission;
121   
122    /**
123     * The user or group that has read permissions.
124     * null means any user.
125     */
126    protected Actor readPermission;
127       
128    /**
129     * List of tags on this Annotation.
130     */
131    protected Set<String> tags;
132   
133    /**
134     * Enum of actions (for permissions).
135     */
136    public static enum Action {
137        read, update, create, delete, admin
138    }
139   
140    /**
141     * Returns if the requested action is allowed for the given user on this annotation.
142     *
143     * @param action
144     * @param user
145     * @param store AnnotationStore to check group membership
146     * @return
147     */
148    public boolean isActionAllowed(Action action, Person user, AnnotationStore store) {
149        if (action == Action.read) {
150            Actor reader = getReadPermission();
151            if (reader == null) {
152                // if not specified then everybody is allowed
153                return true;
154            } else {
155                return reader.isEquivalentWith(user, store);
156            }
157        } else if (action == Action.update) {
158            // require at least an authenticated user
159            if (user == null) return false;
160            Actor updater = getUpdatePermission();
161            if (updater == null) {
162                // if not specified then everybody is allowed
163                return true;
164            } else {
165                return updater.isEquivalentWith(user, store);
166            }
167        } else if (action == Action.delete) {
168            // require at least an authenticated user
169            if (user == null) return false;
170            Actor deleter = getDeletePermission();
171            if (deleter == null) {
172                // if not specified then only creator is allowed
173                deleter = creator;
174            }
175            return deleter.isEquivalentWith(user, store);
176        } else if (action == Action.admin) {
177            // require at least an authenticated user
178            if (user == null) return false;
179            Actor admin = getAdminPermission();
180            if (admin == null) {
181                // if not specified then only creator is allowed
182                admin = creator;
183            }
184            return admin.isEquivalentWith(user, store);
185        }
186        return false;
187    }
188   
189    /**
190     * @return the uri
191     */
192    public String getUri() {
193        return uri;
194    }
195
196    /**
197     * @param uri the uri to set
198     */
199    public void setUri(String uri) {
200        this.uri = uri;
201    }
202
203    /**
204     * Returns an URL-compatible id.
205     * Currently the uri as base64 encoded string.
206     * @return
207     */
208    public String getUrlId() {
209        if (uri == null) return null;
210        try {
211            return Base64.encodeBase64URLSafeString(uri.getBytes("UTF-8"));
212        } catch (UnsupportedEncodingException e) {
213            return null;
214        }
215    }
216
217    public static String decodeId(String id) {
218        if (id == null) return null;
219        try {
220            return new String(Base64.decodeBase64(id), "UTF-8");
221        } catch (UnsupportedEncodingException e) {
222            return null;
223        }
224    }
225
226    /**
227     * @return the bodyText
228     */
229    public String getBodyText() {
230        return bodyText;
231    }
232
233    /**
234     * @param bodyText the bodyText to set
235     */
236    public void setBodyText(String bodyText) {
237        this.bodyText = bodyText;
238    }
239
240    /**
241     * @return the bodyUri
242     */
243    public String getBodyUri() {
244        return bodyUri;
245    }
246
247    /**
248     * @param bodyUri the bodyUri to set
249     */
250    public void setBodyUri(String bodyUri) {
251        this.bodyUri = bodyUri;
252    }
253
254    /**
255     * @return the target
256     */
257    public Target getTarget() {
258        return target;
259    }
260
261    /**
262     * @param target the target to set
263     */
264    public void setTarget(Target target) {
265        this.target = target;
266    }
267
268    /**
269     * @return the targetBaseUri
270     */
271    public String getTargetBaseUri() {
272        if (target == null) return null;
273        return target.getUri();
274    }
275
276    /**
277     * @return the targetFragment
278     */
279    public String getTargetFragment() {
280        return targetFragment;
281    }
282
283    /**
284     * @param targetFragment the targetFragment to set
285     */
286    public void setTargetFragment(String targetFragment) {
287        this.targetFragment = targetFragment;
288    }
289
290    /**
291     * @return the targetType
292     */
293    public FragmentTypes getFragmentType() {
294        return fragmentType;
295    }
296
297    /**
298     * @param fragmentType the fragmentType to set
299     */
300    public void setFragmentType(FragmentTypes fragmentType) {
301        this.fragmentType = fragmentType;
302    }
303
304    /**
305     * @return the quote
306     */
307    public String getQuote() {
308        return quote;
309    }
310
311    /**
312     * @param quote the quote to set
313     */
314    public void setQuote(String quote) {
315        this.quote = quote;
316    }
317
318    /**
319     * @return the resource
320     */
321    public Resource getResource() {
322        return resource;
323    }
324
325    /**
326     * @param resource the resource to set
327     */
328    public void setResource(Resource resource) {
329        this.resource = resource;
330    }
331
332    /**
333     * @return the resourceUri
334     */
335    public String getResourceUri() {
336        if (resource == null) return null;
337        return resource.getUri();
338    }
339
340    /**
341     * @return the creator
342     */
343    public Actor getCreator() {
344        return creator;
345    }
346
347    /**
348     * @param creator the creator to set
349     */
350    public void setCreator(Actor creator) {
351        this.creator = creator;
352    }
353
354    /**
355     * @return the creatorUri
356     */
357    public String getCreatorUri() {
358        if (creator != null) {
359            return creator.getUri();
360        }
361        return null;
362    }
363
364    /**
365     * @return the creatorName
366     */
367    public String getCreatorName() {
368        if (creator != null) {
369            return creator.getName();
370        }
371        return null;
372    }
373
374    /**
375     * @return the created
376     */
377    public String getCreated() {
378        return created;
379    }
380
381    /**
382     * @param created the created to set
383     */
384    public void setCreated(String created) {
385        this.created = created;
386    }
387
388    public String getUpdated() {
389                return updated;
390        }
391
392        public void setUpdated(String updated) {
393                this.updated = updated;
394        }
395
396        /**
397     * @return the adminPermission
398     */
399    public Actor getAdminPermission() {
400        if (adminPermission != null) {
401            return adminPermission;
402        } else {
403            // if not specified then only creator is allowed
404            return this.creator;
405        }
406    }
407
408    /**
409     * @param adminPermission the adminPermission to set
410     */
411    public void setAdminPermission(Actor adminPermission) {
412        this.adminPermission = adminPermission;
413    }
414
415    /**
416     * @return the deletePermission
417     */
418    public Actor getDeletePermission() {
419        if (deletePermission != null) {
420            return deletePermission;
421        } else {
422            // if not specified then only creator is allowed
423            return this.creator;
424        }
425    }
426
427    /**
428     * @param deletePermission the deletePermission to set
429     */
430    public void setDeletePermission(Actor deletePermission) {
431        this.deletePermission = deletePermission;
432    }
433
434    /**
435     * @return the updatePermission
436     */
437    public Actor getUpdatePermission() {
438        return updatePermission;
439    }
440
441    /**
442     * @param updatePermission the updatePermission to set
443     */
444    public void setUpdatePermission(Actor updatePermission) {
445        this.updatePermission = updatePermission;
446    }
447
448    /**
449     * @return the readPermission
450     */
451    public Actor getReadPermission() {
452        return readPermission;
453    }
454
455    /**
456     * @param readPermission the readPermission to set
457     */
458    public void setReadPermission(Actor readPermission) {
459        this.readPermission = readPermission;
460    }
461
462    /**
463     * @return the tags
464     */
465    public Set<String> getTags() {
466        return tags;
467    }
468
469    /**
470     * @param tags the tags to set
471     */
472    public void setTags(Set<String> tags) {
473        this.tags = tags;
474    }
475   
476   
477}
Note: See TracBrowser for help on using the repository browser.