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.JsonValue;
23 import com.google.common.collect.Lists;
24
25 import java.util.List;
26 import java.util.Map;
27
28
29
30
31
32 public class InheritedGlobalPermissionSet {
33
34
35
36
37 public static class Builder {
38 private final String accessorId;
39 private final List<AccessorPermissions<GlobalPermissionSet>> perms = Lists.newArrayList();
40
41
42
43
44
45
46
47 public Builder(String accessorId, GlobalPermissionSet permissionSet) {
48 AccessorPermissions<GlobalPermissionSet> permissions
49 = new AccessorPermissions<>(accessorId, permissionSet);
50 this.accessorId = accessorId;
51 perms.add(permissions);
52 }
53
54
55
56
57
58
59
60
61 public Builder withInheritedPermissions(String accessorId, GlobalPermissionSet permissionSet) {
62 perms.add(new AccessorPermissions<>(accessorId, permissionSet));
63 return this;
64 }
65
66
67
68
69
70
71 public InheritedGlobalPermissionSet build() {
72 return new InheritedGlobalPermissionSet(accessorId, perms);
73 }
74 }
75
76 private final String accessorId;
77 private final List<AccessorPermissions<GlobalPermissionSet>> permissionsList;
78
79 private InheritedGlobalPermissionSet(String accessorId,
80 List<AccessorPermissions<GlobalPermissionSet>> permissionsList) {
81 this.accessorId = accessorId;
82 this.permissionsList = permissionsList;
83 }
84
85
86
87
88
89
90
91
92 public boolean has(ContentTypes contentType, PermissionType permissionType) {
93 for (AccessorPermissions<GlobalPermissionSet> accessorPermissions : permissionsList) {
94 if (accessorPermissions.permissionSet.has(contentType, permissionType)) {
95 return true;
96 }
97 }
98 return false;
99 }
100
101 @Override
102 public boolean equals(Object o) {
103 if (this == o) return true;
104 if (o == null || getClass() != o.getClass()) return false;
105
106 InheritedGlobalPermissionSet that = (InheritedGlobalPermissionSet) o;
107
108 return accessorId.equals(that.accessorId)
109 && permissionsList.equals(that.permissionsList);
110
111 }
112
113 @Override
114 public int hashCode() {
115 int result = accessorId.hashCode();
116 result = 31 * result + permissionsList.hashCode();
117 return result;
118 }
119
120
121
122
123
124
125
126 @JsonValue
127 public List<Map<String, GlobalPermissionSet>> serialize() {
128 List<Map<String, GlobalPermissionSet>> tmp = Lists.newArrayList();
129 for (AccessorPermissions<GlobalPermissionSet> accessorPermissions : permissionsList) {
130 tmp.add(accessorPermissions.asMap());
131 }
132 return tmp;
133 }
134
135 @Override
136 public String toString() {
137 return permissionsList.toString();
138 }
139
140
141
142
143
144
145 public String accessorId() {
146 return accessorId;
147 }
148 }