View Javadoc

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.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   * Convenience wrapper for the permission set data structure, which
33   * looks like:
34   *
35   *  {
36   *      contentType -> [perms...],
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 }