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 permissions on an individual item, including
30 * those inherited from groups to which they belong.
31 */
32 public class InheritedItemPermissionSet {
33 /**
34 * Builder class for InheritedItemPermissionSets.
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 * 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, 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 * Add an accessor from whom the user inherits permissions.
56 *
57 * @param accessorId The accessor's ID
58 * @param permissionSet The accessor's permissions
59 * @return The builder
60 */
61 public Builder withInheritedPermissions(String accessorId, List<PermissionType> permissionSet) {
62 perms.add(new AccessorPermissions<>(accessorId, permissionSet));
63 return this;
64 }
65
66 /**
67 * Construct the InheritedItemPermissionSet from the builder.
68 *
69 * @return A new InheritedItemPermissionSet
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 * Determine if the permission set contains a permission for the given item.
87 *
88 * @param permissionType The permission type
89 * @return Whether or not the permission exists
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 * Serialize the InheritedItemPermissionSet to a list
102 * containing a mappings of accessor ID to permissions.
103 *
104 * @return A list of accessor id -> permission mappings
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 * Fetch the accessor's ID for this permission set.
140 *
141 * @return a user ID string
142 */
143 public String accessorId() {
144 return accessorId;
145 }
146 }