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.fasterxml.jackson.annotation.JsonCreator;
23 import com.fasterxml.jackson.annotation.JsonValue;
24 import com.google.common.collect.HashMultimap;
25 import com.google.common.collect.Lists;
26 import com.google.common.collect.Multimap;
27
28 import java.util.Collection;
29 import java.util.Map;
30
31
32
33
34
35
36
37
38
39
40 public final class GlobalPermissionSet {
41
42 public static class Builder {
43 private final HashMultimap<ContentTypes,PermissionType> m;
44
45 public Builder() {
46 m = HashMultimap.create();
47 }
48
49 public Builder(Multimap<ContentTypes,PermissionType> initial) {
50 m = HashMultimap.create(initial);
51 }
52
53 public Builder set(ContentTypes contentType, Collection<PermissionType> permissionTypes) {
54 m.putAll(contentType, permissionTypes);
55 return this;
56 }
57
58 public Builder set(ContentTypes contentType, PermissionType... permissionTypes) {
59 m.putAll(contentType, Lists.newArrayList(permissionTypes));
60 return this;
61 }
62
63 public GlobalPermissionSet build() {
64 return new GlobalPermissionSet(m);
65 }
66 }
67
68 private final HashMultimap<ContentTypes,PermissionType> matrix;
69
70 @JsonCreator
71 private GlobalPermissionSet(Multimap<ContentTypes,PermissionType> permissionMatrix) {
72 matrix = HashMultimap.create(permissionMatrix);
73 }
74
75 private GlobalPermissionSet() {
76 matrix = HashMultimap.create();
77 }
78
79 public static Builder newBuilder() {
80 return new Builder();
81 }
82
83 public static GlobalPermissionSet empty() {
84 return new GlobalPermissionSet();
85 }
86
87 public static GlobalPermissionSet from(Multimap<ContentTypes,PermissionType> permissionMatrix) {
88 return new GlobalPermissionSet(permissionMatrix);
89 }
90
91 public static GlobalPermissionSet from(Map<ContentTypes,Collection<PermissionType>> permissionMatrix) {
92 Multimap<ContentTypes,PermissionType> multimap = HashMultimap.create();
93 for (Map.Entry<ContentTypes,Collection<PermissionType>> entry : permissionMatrix.entrySet()) {
94 multimap.putAll(entry.getKey(), entry.getValue());
95 }
96 return from(multimap);
97 }
98
99 public boolean has(ContentTypes contentTypes, PermissionType permissionType) {
100 return matrix.get(contentTypes).contains(permissionType);
101 }
102
103 public Collection<PermissionType> get(ContentTypes type) {
104 return matrix.get(type);
105 }
106
107 public GlobalPermissionSet withPermission(ContentTypes contentType, PermissionType... permission) {
108 return new Builder(matrix).set(contentType, permission).build();
109 }
110
111 @JsonValue
112 public Map<ContentTypes,Collection<PermissionType>> asMap() {
113 return matrix.asMap();
114 }
115
116 @Override
117 public boolean equals(Object o) {
118 if (this == o) return true;
119 if (o == null || getClass() != o.getClass()) return false;
120
121 GlobalPermissionSet that = (GlobalPermissionSet) o;
122
123 return matrix.equals(that.matrix);
124 }
125
126 @Override
127 public int hashCode() {
128 return matrix.hashCode();
129 }
130
131 @Override
132 public String toString() {
133 return "<GlobalPermissions: " + matrix + ">";
134 }
135 }