blob: eb6cf5344e8c92ff752bb5845d6c1b05a5aa97fc [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.user;
2
margaretha45667922018-01-25 21:23:03 +01003import java.io.Serializable;
margaretha45667922018-01-25 21:23:03 +01004import java.util.HashMap;
5import java.util.List;
6import java.util.Map;
7
8import org.joda.time.DateTime;
9
Michael Hanlca740d72015-06-16 10:04:58 +020010import com.fasterxml.jackson.databind.JsonNode;
margaretha45667922018-01-25 21:23:03 +010011
Michael Hanl00b64e02016-05-24 20:24:27 +020012import de.ids_mannheim.korap.config.Attributes;
Michael Hanl1e18cb42015-08-06 20:57:35 +020013import de.ids_mannheim.korap.config.ParamFields;
margaretha894a7d72017-11-08 19:24:20 +010014import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlca740d72015-06-16 10:04:58 +020015import de.ids_mannheim.korap.utils.JsonUtils;
16import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanle17eaa52016-01-22 20:55:05 +010017import de.ids_mannheim.korap.web.utils.KustvaktMap;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010018import lombok.AccessLevel;
Michael Hanlca740d72015-06-16 10:04:58 +020019import lombok.Data;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010020import lombok.Getter;
21import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020022
23@Data
24public abstract class User implements Serializable {
25
margaretha58e18632018-02-15 13:04:42 +010026 //EM: add
27 private String email;
28 //EM: finish
29
Michael Hanlca740d72015-06-16 10:04:58 +020030 private Integer id;
31 // in local its username, in shib it's edupersonPrincipalName
32 private String username;
33 private Long accountCreation;
34 private boolean isAccountLocked;
35 private int type;
Michael Hanl1e18cb42015-08-06 20:57:35 +020036 private ParamFields fields;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010037 @Getter(AccessLevel.PRIVATE)
38 @Setter(AccessLevel.PRIVATE)
margaretha0bcde4c2019-01-23 19:08:51 +010039 private UserSettingProcessor settings;
Michael Hanldd729d72016-01-27 23:22:28 +010040 //todo: remove!
Michael Hanl5fac8ab2016-01-29 16:33:04 +010041 @Getter(AccessLevel.PRIVATE)
42 @Setter(AccessLevel.PRIVATE)
Michael Hanlca740d72015-06-16 10:04:58 +020043 private UserDetails details;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010044 @Getter(AccessLevel.PRIVATE)
45 @Setter(AccessLevel.PRIVATE)
Michael Hanlca740d72015-06-16 10:04:58 +020046 private List<UserQuery> queries;
47
margaretha4fa4b062019-01-28 19:43:30 +010048 private UserSettingProcessor userSettingProcessor;
Michael Hanl4f9002d2016-01-27 23:21:45 +010049
margaretha4edc70e2018-03-14 22:34:29 +010050// private boolean isSystemAdmin;
Michael Hanl8abaf9e2016-05-23 16:46:35 +020051
Bodmoca3dcfb2017-05-24 16:36:00 +020052 // Values for corpusAccess:
53 public enum CorpusAccess {
54 FREE, // Access to licence free corpora only, without login
55 PUB, // Access to public (= öffentliche Korpora) only, externes Login.
56 ALL // Access to all corpora, internes Login.
57 };
58
59 @Getter
60 @Setter
61 private CorpusAccess corpusAccess = CorpusAccess.FREE;
62
63 // values for location (set using the X-forwarded-for Header):
64 public enum Location {
65 INTERN, // KorAP accessed by internal Client (inside intranet).
66 EXTERN // KorAP accessed by external Client (outside intranet).
67 };
68
69 @Getter
70 @Setter
71 private Location location = Location.EXTERN;
Bodmo946832f2017-05-24 17:42:17 +020072
Bodmoca3dcfb2017-05-24 16:36:00 +020073
Michael Hanl8abaf9e2016-05-23 16:46:35 +020074 protected User () {
Michael Hanl0f6ffd72015-08-27 19:23:15 +020075 this.fields = new ParamFields();
Michael Hanlca740d72015-06-16 10:04:58 +020076 this.accountCreation = TimeUtils.getNow().getMillis();
77 this.isAccountLocked = false;
78 this.username = "";
Michael Hanlca740d72015-06-16 10:04:58 +020079 this.id = -1;
Bodmoca3dcfb2017-05-24 16:36:00 +020080 this.location = Location.EXTERN;
81 this.corpusAccess = CorpusAccess.FREE;
Michael Hanlca740d72015-06-16 10:04:58 +020082 }
83
Michael Hanl8abaf9e2016-05-23 16:46:35 +020084
85 protected User (int type) {
Michael Hanlca740d72015-06-16 10:04:58 +020086 this();
87 this.type = type;
88 }
89
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090
91 protected User (String username, int type) {
Michael Hanlca740d72015-06-16 10:04:58 +020092 this(type);
93 this.username = username;
94 }
95
Michael Hanl8abaf9e2016-05-23 16:46:35 +020096
97 public void addField (ParamFields.Param param) {
Michael Hanl1e18cb42015-08-06 20:57:35 +020098 this.fields.add(param);
Michael Hanlca740d72015-06-16 10:04:58 +020099 }
100
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200101
102 public <T extends ParamFields.Param> T getField (Class<T> cl) {
Michael Hanl1e18cb42015-08-06 20:57:35 +0200103 return this.fields.get(cl);
Michael Hanlca740d72015-06-16 10:04:58 +0200104 }
105
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200106 public void setId (Integer id) {
Michael Hanlca740d72015-06-16 10:04:58 +0200107 this.id = id;
Michael Hanl4f9002d2016-01-27 23:21:45 +0100108 // if (this.settings != null)
109 // this.settings.setUserID(this.id);
110 // if (this.details != null)
111 // this.details.setUserID(this.id);
Michael Hanlca740d72015-06-16 10:04:58 +0200112 }
113
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200114
115 public Map<String, Object> toMap () {
Michael Hanlca740d72015-06-16 10:04:58 +0200116 Map map = new HashMap();
117 map.put(Attributes.USERNAME, this.username);
118 //TimeUtils.format(new DateTime(this.accountCreation))
119 map.put(Attributes.ACCOUNT_CREATION, this.accountCreation);
120
Michael Hanl4f9002d2016-01-27 23:21:45 +0100121 // if (this.getDetails() != null)
122 // map.putAll(this.getDetails().toMap());
Michael Hanlca740d72015-06-16 10:04:58 +0200123 return map;
124 }
125
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200126
127 public Map toCache () {
Michael Hanlca740d72015-06-16 10:04:58 +0200128 Map map = new HashMap();
Michael Hanl19390652016-01-16 11:01:24 +0100129 map.put(Attributes.ID, this.id);
Michael Hanlca740d72015-06-16 10:04:58 +0200130 map.put(Attributes.USERNAME, this.username);
131 map.put(Attributes.ACCOUNT_CREATION,
132 TimeUtils.format(new DateTime(this.accountCreation)));
133 map.put(Attributes.ACCOUNTLOCK, this.isAccountLocked);
134 map.put(Attributes.TYPE, this.type);
135 return map;
136 }
137
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200138
Michael Hanlca740d72015-06-16 10:04:58 +0200139 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200140 public boolean equals (Object o) {
Michael Hanlca740d72015-06-16 10:04:58 +0200141 if (this == o)
142 return true;
143 if (!(o instanceof User))
144 return false;
145 User user = (User) o;
146 if (!username.equals(user.username))
147 return false;
148 return true;
149 }
150
margaretha62055f72017-04-11 19:17:43 +0200151// public boolean isAdmin () {
152// return this.getUsername().equals(ADMINISTRATOR_ID);
153// }
Michael Hanlca740d72015-06-16 10:04:58 +0200154
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200155
156 protected abstract User clone ();
157
Michael Hanlca740d72015-06-16 10:04:58 +0200158
159 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200160 public String toString () {
Michael Hanlca740d72015-06-16 10:04:58 +0200161 final StringBuffer sb = new StringBuffer();
162 sb.append("id='").append(id).append('\'');
163 sb.append(", username='").append(username).append('\'');
164 return sb.toString();
165 }
166
Bodmoc125bf12017-06-01 16:23:59 +0200167 public String locationtoString()
168
169 {
170 if( this.location == Location.INTERN)
171 return "INTERN";
172 else if( this.location == Location.EXTERN )
173 return "EXTERN";
174 else
175 return "???";
176 }
177
178 public String accesstoString()
179
180 {
181 if( this.corpusAccess == CorpusAccess.ALL )
182 return "ALL";
183 else if( this.corpusAccess == CorpusAccess.PUB )
184 return "PUB";
185 else if( this.corpusAccess == CorpusAccess.FREE )
186 return "FREE";
187 else
188 return "???";
189 }
190
Michael Hanlca740d72015-06-16 10:04:58 +0200191 public static class UserFactory {
192
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200193 public static KorAPUser getUser (String username) {
Michael Hanlca740d72015-06-16 10:04:58 +0200194 return new KorAPUser(username);
195 }
196
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200197
198 public static KorAPUser getUser (String username, String password) {
Michael Hanlf21773f2015-10-16 23:02:31 +0200199 KorAPUser user = new KorAPUser(username);
200 user.setPassword(password);
201 return user;
202 }
203
margaretha62055f72017-04-11 19:17:43 +0200204// public static KorAPUser getAdmin () {
205// return new KorAPUser(ADMINISTRATOR_ID, ADMINISTRATOR_NAME);
206// }
Michael Hanlca740d72015-06-16 10:04:58 +0200207
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200208
209 public static DemoUser getDemoUser () {
Michael Hanlca740d72015-06-16 10:04:58 +0200210 return new DemoUser();
211 }
212
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200213
214 public static DemoUser getDemoUser (Integer id) {
Michael Hanlca740d72015-06-16 10:04:58 +0200215 DemoUser demo = new DemoUser();
216 demo.setId(id);
217 return demo;
218 }
219
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200220
221 public static boolean isDemo (String username) {
Michael Hanlc4446022016-02-12 18:03:17 +0100222 return new DemoUser().getUsername().equalsIgnoreCase(username);
223 }
224
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200225
margarethafb027f92018-04-23 20:00:13 +0200226// public static ShibUser getShibInstance (String eduPersonID,
227// String mail, String cn) {
228// ShibUser u = new ShibUser(eduPersonID);
229// u.setAffiliation("");
230// u.setMail(mail);
231// u.setUsername(eduPersonID);
232// u.setCn(cn);
233// return u;
234// }
Michael Hanlca740d72015-06-16 10:04:58 +0200235
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200236
237 public static KorAPUser toKorAPUser (Map<String, Object> map) {
238 KorAPUser user = UserFactory.getUser((String) map
239 .get(Attributes.USERNAME));
Michael Hanl7368aa42016-02-05 18:15:47 +0100240 user.setPassword((String) map.get(Attributes.PASSWORD));
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200241 int id = map.get(Attributes.ID) == null ? -1 : (int) map
242 .get(Attributes.ID);
Michael Hanl7368aa42016-02-05 18:15:47 +0100243 if (id != -1)
244 user.setId(id);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200245 long cr = map.get(Attributes.ACCOUNT_CREATION) == null ? -1
246 : (long) map.get(Attributes.ACCOUNT_CREATION);
Michael Hanl7368aa42016-02-05 18:15:47 +0100247 if (cr != -1)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200248 user.setAccountCreation((Long) map
249 .get(Attributes.ACCOUNT_CREATION));
Michael Hanl7368aa42016-02-05 18:15:47 +0100250 return user;
251 }
252
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200253
254 public static User toUser (Map<String, Object> map) {
Michael Hanle17eaa52016-01-22 20:55:05 +0100255 KustvaktMap kmap = new KustvaktMap(map);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200256 int type = map.get(Attributes.TYPE) == null ? 0 : (Integer) kmap
257 .get(Attributes.TYPE, Integer.class);
Michael Hanlca740d72015-06-16 10:04:58 +0200258 User user;
Michael Hanl19390652016-01-16 11:01:24 +0100259 long created = -1;
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200260 int id = kmap.get(Attributes.ID, Integer.class) == null ? -1
261 : (Integer) kmap.get(Attributes.ID, Integer.class);
Michael Hanl7368aa42016-02-05 18:15:47 +0100262
Michael Hanl19390652016-01-16 11:01:24 +0100263 if (map.get(Attributes.ACCOUNT_CREATION) != null)
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200264 created = TimeUtils.getTime(kmap.get(Attributes.ACCOUNT_CREATION))
Michael Hanl19390652016-01-16 11:01:24 +0100265 .getMillis();
Michael Hanlca740d72015-06-16 10:04:58 +0200266 switch (type) {
267 case 0:
Michael Hanle17eaa52016-01-22 20:55:05 +0100268 user = UserFactory.getUser(kmap.get(Attributes.USERNAME));
Michael Hanl7368aa42016-02-05 18:15:47 +0100269 if (id != -1)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200270 user.setId((Integer) kmap.get(Attributes.ID,
271 Integer.class));
272 user.setAccountLocked(map.get(Attributes.ACCOUNTLOCK) == null ? false
273 : (Boolean) kmap.get(Attributes.ACCOUNTLOCK,
274 Boolean.class));
Michael Hanl19390652016-01-16 11:01:24 +0100275 user.setAccountCreation(created);
Michael Hanlca740d72015-06-16 10:04:58 +0200276 break;
277 default:
Michael Hanl7368aa42016-02-05 18:15:47 +0100278 user = UserFactory.getDemoUser();
Michael Hanl19390652016-01-16 11:01:24 +0100279 user.setAccountCreation(created);
Michael Hanlca740d72015-06-16 10:04:58 +0200280 }
281 return user;
282 }
283
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200284
margaretha894a7d72017-11-08 19:24:20 +0100285 public static KorAPUser toUser (String value) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200286 JsonNode node = JsonUtils.readTree(value);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200287 KorAPUser user = UserFactory.getUser(node.path(Attributes.USERNAME)
288 .asText());
289 user.setAccountLocked(node.path(Attributes.ACCOUNTLOCK).asBoolean());
Michael Hanlca740d72015-06-16 10:04:58 +0200290 user.setAccountLink(node.path(Attributes.ACCOUNTLINK).asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200291 user.setAccountCreation(node.path(Attributes.ACCOUNT_CREATION)
292 .asLong());
Michael Hanlca740d72015-06-16 10:04:58 +0200293 user.setPassword(node.path(Attributes.PASSWORD).asText());
294 return user;
295 }
296 }
297
298}