1   /*
2    * Copyright 2019 LINE Corporation
3    *
4    * LINE Corporation licenses this file to you under the Apache License,
5    * version 2.0 (the "License"); you may not use this file except in compliance
6    * with the License. You may obtain a copy of the License at:
7    *
8    *   https://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations
14   * under the License.
15   */
16  
17  package com.linecorp.centraldogma.server.metadata;
18  
19  import static java.util.Objects.requireNonNull;
20  
21  import java.util.Collection;
22  import java.util.EnumSet;
23  import java.util.Objects;
24  import java.util.Set;
25  
26  import com.fasterxml.jackson.annotation.JsonCreator;
27  import com.fasterxml.jackson.annotation.JsonProperty;
28  import com.google.common.base.MoreObjects;
29  import com.google.common.collect.Sets;
30  
31  import com.linecorp.centraldogma.server.storage.repository.Repository;
32  
33  /**
34   * A default permission for a {@link Repository}.
35   */
36  public class PerRolePermissions {
37  
38      /**
39       * {@link Permission}s for administrators.
40       */
41      public static final Collection<Permission> ALL_PERMISSION = EnumSet.allOf(Permission.class);
42  
43      public static final Collection<Permission> READ_WRITE = EnumSet.of(Permission.READ, Permission.WRITE);
44      public static final Collection<Permission> READ_ONLY = EnumSet.of(Permission.READ);
45      public static final Collection<Permission> NO_PERMISSION = EnumSet.noneOf(Permission.class);
46  
47      /**
48       * The default permission.
49       *
50       * @deprecated Use {@link #ofDefault()}.
51       */
52      @Deprecated
53      public static final PerRolePermissions DEFAULT =
54              new PerRolePermissions(READ_WRITE, READ_WRITE, NO_PERMISSION);
55      private static final PerRolePermissions internalPermissions =
56              new PerRolePermissions(READ_WRITE, NO_PERMISSION, NO_PERMISSION);
57  
58      /**
59       * Creates a {@link PerRolePermissions} which allows read/write a repository from a owner.
60       */
61      public static PerRolePermissions ofInternal() {
62          return internalPermissions;
63      }
64  
65      /**
66       * Creates a {@link PerRolePermissions} which allows read/write to owners and members.
67       */
68      public static PerRolePermissions ofDefault() {
69          return DEFAULT;
70      }
71  
72      /**
73       * Creates a {@link PerRolePermissions} which allows accessing a repository from everyone.
74       */
75      public static PerRolePermissions ofPublic() {
76          return new PerRolePermissions(READ_WRITE, READ_WRITE, READ_WRITE);
77      }
78  
79      /**
80       * Creates a {@link PerRolePermissions} which allows accessing a repository from a project member.
81       */
82      public static PerRolePermissions ofPrivate() {
83          return new PerRolePermissions(READ_WRITE, READ_WRITE, NO_PERMISSION);
84      }
85  
86      /**
87       * {@link Permission}s for a {@link ProjectRole#OWNER}.
88       */
89      private final Set<Permission> owner;
90  
91      /**
92       * {@link Permission}s for a {@link ProjectRole#MEMBER}.
93       */
94      private final Set<Permission> member;
95  
96      /**
97       * {@link Permission}s for a {@link ProjectRole#GUEST}.
98       */
99      private final Set<Permission> guest;
100 
101     /**
102      * Creates an instance.
103      */
104     @JsonCreator
105     public PerRolePermissions(@JsonProperty("owner") Iterable<Permission> owner,
106                               @JsonProperty("member") Iterable<Permission> member,
107                               @JsonProperty("guest") Iterable<Permission> guest) {
108         this.owner = Sets.immutableEnumSet(requireNonNull(owner, "owner"));
109         this.member = Sets.immutableEnumSet(requireNonNull(member, "member"));
110         this.guest = Sets.immutableEnumSet(requireNonNull(guest, "guest"));
111     }
112 
113     /**
114      * Returns the permissions granted to owners.
115      */
116     @JsonProperty
117     public Set<Permission> owner() {
118         return owner;
119     }
120 
121     /**
122      * Returns the permissions granted to members.
123      */
124     @JsonProperty
125     public Set<Permission> member() {
126         return member;
127     }
128 
129     /**
130      * Returns the permissions granted to guests.
131      */
132     @JsonProperty
133     public Set<Permission> guest() {
134         return guest;
135     }
136 
137     @Override
138     public int hashCode() {
139         return Objects.hash(owner, member, guest);
140     }
141 
142     @Override
143     public boolean equals(Object o) {
144         if (this == o) {
145             return true;
146         }
147         if (o == null || getClass() != o.getClass()) {
148             return false;
149         }
150 
151         final PerRolePermissions that = (PerRolePermissions) o;
152         return owner.equals(that.owner) &&
153                member.equals(that.member) &&
154                guest.equals(that.guest);
155     }
156 
157     @Override
158     public String toString() {
159         return MoreObjects.toStringHelper(this)
160                           .add("owner", owner())
161                           .add("member", member())
162                           .add("guest", guest())
163                           .toString();
164     }
165 }