1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package eu.ehri.project.acl;
21
22 import com.google.common.base.Preconditions;
23 import com.tinkerpop.frames.FramedGraph;
24 import eu.ehri.project.core.GraphManager;
25 import eu.ehri.project.core.GraphManagerFactory;
26 import eu.ehri.project.exceptions.AccessDenied;
27 import eu.ehri.project.exceptions.ItemNotFound;
28 import eu.ehri.project.exceptions.PermissionDenied;
29 import eu.ehri.project.models.ContentType;
30 import eu.ehri.project.models.EntityClass;
31 import eu.ehri.project.models.base.Accessible;
32 import eu.ehri.project.models.base.Accessor;
33 import eu.ehri.project.models.base.PermissionScope;
34 import eu.ehri.project.models.utils.ClassUtils;
35
36 import java.util.Optional;
37
38
39
40
41 public final class PermissionUtils {
42
43 private final FramedGraph<?> graph;
44 private final PermissionScope scope;
45 private final AclManager acl;
46 private final GraphManager manager;
47
48 public PermissionUtils(FramedGraph<?> graph) {
49 this(graph, SystemScope.getInstance());
50 }
51
52 public PermissionUtils(FramedGraph<?> graph, PermissionScope scope) {
53 Preconditions.checkNotNull(scope);
54 this.graph = graph;
55 this.acl = new AclManager(graph, scope);
56 this.scope = scope;
57 this.manager = GraphManagerFactory.getInstance(graph);
58 }
59
60
61
62
63
64
65
66
67 public void checkContentPermission(Accessor accessor, ContentTypes contentType,
68 PermissionType permissionType) throws PermissionDenied {
69 if (!acl.hasPermission(contentType, permissionType, accessor)) {
70 throw new PermissionDenied(accessor.getId(), contentType.toString(), permissionType.toString(), scope.getId());
71 }
72 }
73
74
75
76
77
78
79
80
81 public void checkEntityPermission(Accessible entity,
82 Accessor accessor, PermissionType permissionType) throws PermissionDenied {
83 if (!acl.hasPermission(entity, permissionType, accessor)) {
84 throw new PermissionDenied(accessor.getId(), entity.getId(),
85 permissionType.toString(), scope.getId());
86 }
87 }
88
89
90
91
92
93
94
95 public void checkReadAccess(Accessible entity, Accessor accessor)
96 throws AccessDenied {
97 if (!acl.canAccess(entity, accessor)) {
98
99 throw new AccessDenied(accessor.getId(), entity.getId());
100 }
101 }
102
103
104
105
106
107
108
109 public ContentType getContentTypeNode(EntityClass entityClass) {
110 try {
111 return manager.getEntity(entityClass.getName(), ContentType.class);
112 } catch (ItemNotFound e) {
113 throw new RuntimeException(
114 String.format("No content entityClass node found for entityClass: '%s'",
115 entityClass.getName()), e);
116 }
117 }
118
119
120
121
122
123
124
125 public ContentTypes getContentTypeEnum(Class<?> cls) {
126 return ContentTypes.withName(ClassUtils.getEntityType(cls).getName());
127 }
128
129
130
131
132
133
134
135
136
137
138 public PermissionUtils setScope(PermissionScope scope) {
139 return new PermissionUtils(graph,
140 Optional.ofNullable(scope).orElse(SystemScope.INSTANCE));
141 }
142 }