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.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 * A user's complete set of permissions, including
30 * those inherited from groups to which they belong.
31 */
32 public class InheritedGlobalPermissionSet {
33
34 /**
35 * Builder class for InheritedGlobalPermissionSets.
36 */
37 public static class Builder {
38 private final String accessorId;
39 private final List<AccessorPermissions<GlobalPermissionSet>> perms = Lists.newArrayList();
40
41 /**
42 * Create a new builder with the primary (subject) accessor.
43 *
44 * @param accessorId The primary accessor's ID
45 * @param permissionSet The primary accessor's own global permissions
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 * Add an accessor from whom the user inherits permissions.
56 *
57 * @param accessorId The accessor
58 * @param permissionSet The accessor's permissions
59 * @return The builder
60 */
61 public Builder withInheritedPermissions(String accessorId, GlobalPermissionSet permissionSet) {
62 perms.add(new AccessorPermissions<>(accessorId, permissionSet));
63 return this;
64 }
65
66 /**
67 * Construct the InheritedGlobalPermissionSet from the builder.
68 *
69 * @return A new InheritedGlobalPermissionSet
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 * Test if this inherited permission set contains a given permission.
87 *
88 * @param contentType The content type
89 * @param permissionType The permission type
90 * @return Whether or not the permission is present
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 * Serialize the InheritedGlobalPermissionSet to a
122 * list containing a mappings of accessor ID to permissions.
123 *
124 * @return A list of accessor id -> permission mappings
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 * Fetch the accessor's ID for this permission set.
142 *
143 * @return a user ID string
144 */
145 public String accessorId() {
146 return accessorId;
147 }
148 }