1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.linecorp.centraldogma.server.metadata;
18
19 import static java.util.Objects.requireNonNull;
20
21 import java.io.Serializable;
22 import java.util.List;
23
24 import com.fasterxml.jackson.annotation.JsonCreator;
25 import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
26 import com.fasterxml.jackson.annotation.JsonInclude;
27 import com.fasterxml.jackson.annotation.JsonInclude.Include;
28 import com.fasterxml.jackson.annotation.JsonProperty;
29 import com.google.common.base.MoreObjects;
30 import com.google.common.base.Strings;
31 import com.google.common.collect.ImmutableList;
32
33 import com.linecorp.centraldogma.internal.Util;
34
35
36
37
38 @JsonIgnoreProperties(ignoreUnknown = true)
39 @JsonInclude(Include.NON_NULL)
40 public class User implements Identifiable, Serializable {
41
42 private static final long serialVersionUID = -5429782019985526549L;
43
44 private static final String LEVEL_USER_STR = "LEVEL_USER";
45 private static final String LEVEL_SYSTEM_ADMIN_STR = "LEVEL_SYSTEM_ADMIN";
46
47
48 public static final List<String> LEVEL_USER = ImmutableList.of(LEVEL_USER_STR);
49 public static final List<String> LEVEL_SYSTEM_ADMIN =
50 ImmutableList.of(LEVEL_SYSTEM_ADMIN_STR, LEVEL_USER_STR);
51
52 public static final User DEFAULT = new User("user@localhost.localdomain", LEVEL_USER);
53 public static final User SYSTEM_ADMIN = new User("admin@localhost.localdomain", LEVEL_SYSTEM_ADMIN);
54 public static final User SYSTEM = new User("system@localhost.localdomain", LEVEL_SYSTEM_ADMIN);
55
56 private final String login;
57 private final String name;
58 private final String email;
59 private final List<String> roles;
60
61 private final boolean isSystemAdmin;
62
63
64
65
66 @JsonCreator
67 public User(@JsonProperty("login") String login,
68 @JsonProperty("name") String name,
69 @JsonProperty("email") String email,
70 @JsonProperty("roles") List<String> roles) {
71 this.login = requireNonNull(login, "login");
72 this.name = requireNonNull(name, "name");
73 this.email = requireNonNull(email, "email");
74 this.roles = ImmutableList.copyOf(requireNonNull(roles, "roles"));
75 isSystemAdmin = roles.stream().anyMatch(LEVEL_SYSTEM_ADMIN_STR::equals);
76 }
77
78
79
80
81 public User(String login) {
82 this(login, LEVEL_USER);
83 }
84
85
86
87
88 public User(String login, List<String> roles) {
89 if (Strings.isNullOrEmpty(login)) {
90 throw new IllegalArgumentException("login");
91 }
92 requireNonNull(roles, "roles");
93
94 this.login = login;
95 email = Util.toEmailAddress(login, "login");
96 name = Util.emailToUsername(email, "login");
97
98 this.roles = ImmutableList.copyOf(roles);
99 isSystemAdmin = roles.stream().anyMatch(LEVEL_SYSTEM_ADMIN_STR::equals);
100 }
101
102
103
104
105 @JsonProperty
106 public String login() {
107 return login;
108 }
109
110
111
112
113 @JsonProperty
114 public String name() {
115 return name;
116 }
117
118
119
120
121 @JsonProperty
122 public String email() {
123 return email;
124 }
125
126
127
128
129 @JsonProperty
130 public List<String> roles() {
131 return roles;
132 }
133
134 @Override
135 public String id() {
136 return email();
137 }
138
139
140
141
142 public boolean isSystemAdmin() {
143 return isSystemAdmin;
144 }
145
146 @Override
147 public boolean equals(Object o) {
148 if (this == o) {
149 return true;
150 }
151 if (o == null || getClass() != o.getClass()) {
152 return false;
153 }
154
155 final User user = (User) o;
156 return login.equals(user.login);
157 }
158
159 @Override
160 public int hashCode() {
161 return login.hashCode();
162 }
163
164 @Override
165 public String toString() {
166 return MoreObjects.toStringHelper(this)
167 .add("login", login())
168 .add("name", name())
169 .add("email", email())
170 .add("roles", roles())
171 .add("isSystemAdmin", isSystemAdmin())
172 .toString();
173 }
174 }