1 /*
2 * Copyright 2015 Data Archiving and Networked Services (an institute of
3 * Koninklijke Nederlandse Akademie van Wetenschappen), King's College London,
4 * Georg-August-Universitaet Goettingen Stiftung Oeffentlichen Rechts
5 *
6 * Licensed under the EUPL, Version 1.1 or – as soon they will be approved by
7 * the European Commission - subsequent versions of the EUPL (the "Licence");
8 * You may not use this work except in compliance with the Licence.
9 * You may obtain a copy of the Licence at:
10 *
11 * https://joinup.ec.europa.eu/software/page/eupl
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the Licence is distributed on an "AS IS" basis,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the Licence for the specific language governing
17 * permissions and limitations under the Licence.
18 */
19
20 package eu.ehri.project.models.base;
21
22 import com.google.common.collect.Lists;
23 import com.tinkerpop.blueprints.Direction;
24 import com.tinkerpop.blueprints.Vertex;
25 import com.tinkerpop.frames.Adjacency;
26 import com.tinkerpop.frames.modules.javahandler.JavaHandler;
27 import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
28 import eu.ehri.project.definitions.Ontology;
29 import eu.ehri.project.models.PermissionGrant;
30 import eu.ehri.project.models.utils.JavaHandlerUtils;
31
32 import java.util.Collection;
33 import java.util.List;
34
35 /**
36 * The scope of permissions granted to users. A permission scope always has an identifier.
37 */
38 public interface PermissionScope extends Identifiable {
39
40 /**
41 * Get all permission grants that apply directly to this scope.
42 *
43 * @return an iterable of permission grant frames
44 */
45 @Adjacency(label = Ontology.PERMISSION_GRANT_HAS_SCOPE, direction = Direction.IN)
46 Iterable<PermissionGrant> getPermissionGrants();
47
48 /**
49 * Get an iterable of the parent and all the parents higher permission
50 * scopes, recursively.
51 *
52 * @return an iterable of parent scope items
53 */
54 @JavaHandler
55 Iterable<PermissionScope> getPermissionScopes();
56
57 /**
58 * Get an iterable of all items immediately within this scope.
59 *
60 * @return an iterable of lower scoped items
61 */
62 @Adjacency(label = Ontology.HAS_PERMISSION_SCOPE, direction = Direction.IN)
63 Iterable<Accessible> getContainedItems();
64
65 /**
66 * Get an iterable of all items within this scope, recursively down
67 * to all lower levels.
68 *
69 * @return an iterable of lower scoped items to all depths
70 */
71 @JavaHandler
72 Iterable<Accessible> getAllContainedItems();
73
74 /**
75 * Get the path of the permission scope as an ordered collection of strings.
76 * @return an ordered Collection of Strings that forms the 'path'.
77 */
78 @JavaHandler
79 Collection<String> idPath();
80
81 abstract class Impl implements JavaHandlerContext<Vertex>, PermissionScope {
82 public Iterable<Accessible> getAllContainedItems() {
83 return frameVertices(gremlin().as("n")
84 .in(Ontology.HAS_PERMISSION_SCOPE)
85 .loop("n", JavaHandlerUtils.noopLoopFunc, JavaHandlerUtils.noopLoopFunc));
86 }
87
88 public Iterable<PermissionScope> getPermissionScopes() {
89 return frameVertices(gremlin().as("n")
90 .out(Ontology.HAS_PERMISSION_SCOPE)
91 .loop("n", JavaHandlerUtils.defaultMaxLoops, JavaHandlerUtils.noopLoopFunc));
92 }
93
94 public Collection<String> idPath() {
95 // Sigh - duplication...
96 List<String> pIds = Lists.reverse(gremlin().as("n")
97 .out(Ontology.HAS_PERMISSION_SCOPE)
98 .loop("n", JavaHandlerUtils.defaultMaxLoops, JavaHandlerUtils.noopLoopFunc)
99 .transform(vertex -> vertex.<String>getProperty(Ontology.IDENTIFIER_KEY))
100 .toList());
101 pIds.add(it().<String>getProperty(Ontology.IDENTIFIER_KEY));
102 return pIds;
103 }
104 }
105 }