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 com.linecorp.centraldogma.internal.Util.APP_IDENTITY_EMAIL_SUFFIX;
20  import static java.util.Objects.requireNonNull;
21  
22  import com.google.common.base.MoreObjects;
23  
24  /**
25   * A {@link User} which accesses the API with an {@link AppIdentity}.
26   */
27  public final class UserWithAppIdentity extends User {
28  
29      private static final long serialVersionUID = 6021146546653491444L;
30  
31      private final AppIdentity appIdentity;
32  
33      /**
34       * Creates a new instance.
35       */
36      public UserWithAppIdentity(AppIdentity appIdentity) {
37          super(requireNonNull(appIdentity, "appIdentity").appId(),
38                appIdentity.appId() + APP_IDENTITY_EMAIL_SUFFIX);
39          this.appIdentity = appIdentity;
40      }
41  
42      /**
43       * Returns the {@link AppIdentity} of the user.
44       */
45      public AppIdentity appIdentity() {
46          return appIdentity;
47      }
48  
49      @Override
50      public boolean isSystemAdmin() {
51          return appIdentity.isSystemAdmin();
52      }
53  
54      @Override
55      public int hashCode() {
56          return super.hashCode() * 31 + appIdentity.hashCode();
57      }
58  
59      @Override
60      public boolean equals(Object o) {
61          if (this == o) {
62              return true;
63          }
64          if (!(o instanceof UserWithAppIdentity)) {
65              return false;
66          }
67          if (!super.equals(o)) {
68              return false;
69          }
70  
71          final UserWithAppIdentity that = (UserWithAppIdentity) o;
72          return appIdentity.equals(that.appIdentity);
73      }
74  
75      @Override
76      public String toString() {
77          return MoreObjects.toStringHelper(this)
78                            .add("appIdentity", appIdentity)
79                            .toString();
80      }
81  }