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