View Javadoc

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.tinkerpop.blueprints.Direction;
23  import com.tinkerpop.blueprints.Vertex;
24  import com.tinkerpop.frames.Adjacency;
25  import com.tinkerpop.frames.modules.javahandler.JavaHandler;
26  import com.tinkerpop.frames.modules.javahandler.JavaHandlerContext;
27  import eu.ehri.project.definitions.Ontology;
28  import eu.ehri.project.models.Group;
29  import eu.ehri.project.models.PermissionGrant;
30  import eu.ehri.project.models.utils.JavaHandlerUtils;
31  
32  /**
33   * An entity that can access items and be granted permissions.
34   *
35  
36   */
37  public interface Accessor extends Identifiable {
38  
39      @JavaHandler
40      boolean isAdmin();
41  
42      @JavaHandler
43      boolean isAnonymous();
44  
45      @Adjacency(label = Ontology.ACCESSOR_BELONGS_TO_GROUP)
46      Iterable<Accessor> getParents();
47  
48      @JavaHandler
49      Iterable<Accessor> getAllParents();
50  
51      @Adjacency(label = Ontology.PERMISSION_GRANT_HAS_SUBJECT, direction=Direction.IN)
52      Iterable<PermissionGrant> getPermissionGrants();
53  
54      @Adjacency(label = Ontology.PERMISSION_GRANT_HAS_SUBJECT, direction=Direction.IN)
55      void addPermissionGrant(PermissionGrant grant);
56  
57      abstract class Impl implements JavaHandlerContext<Vertex>, Accessor {
58  
59          public boolean isAdmin() {
60              return it().getProperty(Ontology.IDENTIFIER_KEY).equals(Group.ADMIN_GROUP_IDENTIFIER);
61          }
62  
63          public boolean isAnonymous() {
64              return false;
65          }
66  
67          public Iterable<Accessor> getAllParents() {
68              return frameVertices(gremlin().as("n")
69                      .out(Ontology.ACCESSOR_BELONGS_TO_GROUP)
70                      .loop("n", JavaHandlerUtils.defaultMaxLoops, JavaHandlerUtils.noopLoopFunc));
71          }
72      }
73  }