blob: 871beb8ab6a26a01431fb9ace70ee69ca30290f0 [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;
4import java.util.ArrayList;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import org.joda.time.DateTime;
10
Michael Hanlca740d72015-06-16 10:04:58 +020011import com.fasterxml.jackson.databind.JsonNode;
margaretha45667922018-01-25 21:23:03 +010012
Michael Hanl00b64e02016-05-24 20:24:27 +020013import de.ids_mannheim.korap.config.Attributes;
Michael Hanl1e18cb42015-08-06 20:57:35 +020014import de.ids_mannheim.korap.config.ParamFields;
margaretha894a7d72017-11-08 19:24:20 +010015import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlca740d72015-06-16 10:04:58 +020016import de.ids_mannheim.korap.utils.JsonUtils;
17import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanle17eaa52016-01-22 20:55:05 +010018import de.ids_mannheim.korap.web.utils.KustvaktMap;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010019import lombok.AccessLevel;
Michael Hanlca740d72015-06-16 10:04:58 +020020import lombok.Data;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010021import lombok.Getter;
22import lombok.Setter;
Michael Hanlca740d72015-06-16 10:04:58 +020023
24@Data
25public abstract class User implements Serializable {
26
margaretha58e18632018-02-15 13:04:42 +010027 //EM: add
28 private String email;
29 //EM: finish
30
Michael Hanlca740d72015-06-16 10:04:58 +020031 private Integer id;
32 // in local its username, in shib it's edupersonPrincipalName
33 private String username;
34 private Long accountCreation;
35 private boolean isAccountLocked;
36 private int type;
Michael Hanl1e18cb42015-08-06 20:57:35 +020037 private ParamFields fields;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010038 @Getter(AccessLevel.PRIVATE)
39 @Setter(AccessLevel.PRIVATE)
Michael Hanlca740d72015-06-16 10:04:58 +020040 private UserSettings settings;
Michael Hanldd729d72016-01-27 23:22:28 +010041 //todo: remove!
Michael Hanl5fac8ab2016-01-29 16:33:04 +010042 @Getter(AccessLevel.PRIVATE)
43 @Setter(AccessLevel.PRIVATE)
Michael Hanlca740d72015-06-16 10:04:58 +020044 private UserDetails details;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010045 @Getter(AccessLevel.PRIVATE)
46 @Setter(AccessLevel.PRIVATE)
Michael Hanlca740d72015-06-16 10:04:58 +020047 private List<UserQuery> queries;
48
Michael Hanlc2a9f622016-01-28 16:40:06 +010049 private List<Userdata> userdata;
Michael Hanl4f9002d2016-01-27 23:21:45 +010050
margaretha4edc70e2018-03-14 22:34:29 +010051// private boolean isSystemAdmin;
Michael Hanl8abaf9e2016-05-23 16:46:35 +020052
Bodmoca3dcfb2017-05-24 16:36:00 +020053 // Values for corpusAccess:
54 public enum CorpusAccess {
55 FREE, // Access to licence free corpora only, without login
56 PUB, // Access to public (= öffentliche Korpora) only, externes Login.
57 ALL // Access to all corpora, internes Login.
58 };
59
60 @Getter
61 @Setter
62 private CorpusAccess corpusAccess = CorpusAccess.FREE;
63
64 // values for location (set using the X-forwarded-for Header):
65 public enum Location {
66 INTERN, // KorAP accessed by internal Client (inside intranet).
67 EXTERN // KorAP accessed by external Client (outside intranet).
68 };
69
70 @Getter
71 @Setter
72 private Location location = Location.EXTERN;
Bodmo946832f2017-05-24 17:42:17 +020073
Bodmoca3dcfb2017-05-24 16:36:00 +020074
Michael Hanl8abaf9e2016-05-23 16:46:35 +020075 protected User () {
Michael Hanl0f6ffd72015-08-27 19:23:15 +020076 this.fields = new ParamFields();
Michael Hanlca740d72015-06-16 10:04:58 +020077 this.accountCreation = TimeUtils.getNow().getMillis();
78 this.isAccountLocked = false;
79 this.username = "";
Michael Hanlca740d72015-06-16 10:04:58 +020080 this.id = -1;
Michael Hanl5fac8ab2016-01-29 16:33:04 +010081 this.userdata = new ArrayList<>();
Bodmoca3dcfb2017-05-24 16:36:00 +020082 this.location = Location.EXTERN;
83 this.corpusAccess = CorpusAccess.FREE;
Michael Hanlca740d72015-06-16 10:04:58 +020084 }
85
Michael Hanl8abaf9e2016-05-23 16:46:35 +020086
87 protected User (int type) {
Michael Hanlca740d72015-06-16 10:04:58 +020088 this();
89 this.type = type;
90 }
91
Michael Hanl8abaf9e2016-05-23 16:46:35 +020092
93 protected User (String username, int type) {
Michael Hanlca740d72015-06-16 10:04:58 +020094 this(type);
95 this.username = username;
96 }
97
Michael Hanl8abaf9e2016-05-23 16:46:35 +020098
99 public void addField (ParamFields.Param param) {
Michael Hanl1e18cb42015-08-06 20:57:35 +0200100 this.fields.add(param);
Michael Hanlca740d72015-06-16 10:04:58 +0200101 }
102
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200103
104 public <T extends ParamFields.Param> T getField (Class<T> cl) {
Michael Hanl1e18cb42015-08-06 20:57:35 +0200105 return this.fields.get(cl);
Michael Hanlca740d72015-06-16 10:04:58 +0200106 }
107
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200108
109 public void addUserData (Userdata data) {
Michael Hanlc2a9f622016-01-28 16:40:06 +0100110 if (data != null) {
111 for (Userdata d : this.userdata) {
112 // already has an object of that type!
113 if (d.getClass().equals(data.getClass()))
114 return;
115 }
116 userdata.add(data);
117 }
118 }
119
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200120
121 public void setId (Integer id) {
Michael Hanlca740d72015-06-16 10:04:58 +0200122 this.id = id;
Michael Hanl4f9002d2016-01-27 23:21:45 +0100123 // if (this.settings != null)
124 // this.settings.setUserID(this.id);
125 // if (this.details != null)
126 // this.details.setUserID(this.id);
Michael Hanlca740d72015-06-16 10:04:58 +0200127 }
128
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200129
130 public Map<String, Object> toMap () {
Michael Hanlca740d72015-06-16 10:04:58 +0200131 Map map = new HashMap();
132 map.put(Attributes.USERNAME, this.username);
133 //TimeUtils.format(new DateTime(this.accountCreation))
134 map.put(Attributes.ACCOUNT_CREATION, this.accountCreation);
135
Michael Hanl4f9002d2016-01-27 23:21:45 +0100136 // if (this.getDetails() != null)
137 // map.putAll(this.getDetails().toMap());
Michael Hanlca740d72015-06-16 10:04:58 +0200138 return map;
139 }
140
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200141
142 public Map toCache () {
Michael Hanlca740d72015-06-16 10:04:58 +0200143 Map map = new HashMap();
Michael Hanl19390652016-01-16 11:01:24 +0100144 map.put(Attributes.ID, this.id);
Michael Hanlca740d72015-06-16 10:04:58 +0200145 map.put(Attributes.USERNAME, this.username);
146 map.put(Attributes.ACCOUNT_CREATION,
147 TimeUtils.format(new DateTime(this.accountCreation)));
148 map.put(Attributes.ACCOUNTLOCK, this.isAccountLocked);
149 map.put(Attributes.TYPE, this.type);
150 return map;
151 }
152
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200153
Michael Hanlca740d72015-06-16 10:04:58 +0200154 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200155 public boolean equals (Object o) {
Michael Hanlca740d72015-06-16 10:04:58 +0200156 if (this == o)
157 return true;
158 if (!(o instanceof User))
159 return false;
160 User user = (User) o;
161 if (!username.equals(user.username))
162 return false;
163 return true;
164 }
165
margaretha62055f72017-04-11 19:17:43 +0200166// public boolean isAdmin () {
167// return this.getUsername().equals(ADMINISTRATOR_ID);
168// }
Michael Hanlca740d72015-06-16 10:04:58 +0200169
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200170
171 protected abstract User clone ();
172
Michael Hanlca740d72015-06-16 10:04:58 +0200173
174 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200175 public String toString () {
Michael Hanlca740d72015-06-16 10:04:58 +0200176 final StringBuffer sb = new StringBuffer();
177 sb.append("id='").append(id).append('\'');
178 sb.append(", username='").append(username).append('\'');
179 return sb.toString();
180 }
181
Bodmoc125bf12017-06-01 16:23:59 +0200182 public String locationtoString()
183
184 {
185 if( this.location == Location.INTERN)
186 return "INTERN";
187 else if( this.location == Location.EXTERN )
188 return "EXTERN";
189 else
190 return "???";
191 }
192
193 public String accesstoString()
194
195 {
196 if( this.corpusAccess == CorpusAccess.ALL )
197 return "ALL";
198 else if( this.corpusAccess == CorpusAccess.PUB )
199 return "PUB";
200 else if( this.corpusAccess == CorpusAccess.FREE )
201 return "FREE";
202 else
203 return "???";
204 }
205
Michael Hanlca740d72015-06-16 10:04:58 +0200206 public static class UserFactory {
207
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200208 public static KorAPUser getUser (String username) {
Michael Hanlca740d72015-06-16 10:04:58 +0200209 return new KorAPUser(username);
210 }
211
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200212
213 public static KorAPUser getUser (String username, String password) {
Michael Hanlf21773f2015-10-16 23:02:31 +0200214 KorAPUser user = new KorAPUser(username);
215 user.setPassword(password);
216 return user;
217 }
218
margaretha62055f72017-04-11 19:17:43 +0200219// public static KorAPUser getAdmin () {
220// return new KorAPUser(ADMINISTRATOR_ID, ADMINISTRATOR_NAME);
221// }
Michael Hanlca740d72015-06-16 10:04:58 +0200222
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200223
224 public static DemoUser getDemoUser () {
Michael Hanlca740d72015-06-16 10:04:58 +0200225 return new DemoUser();
226 }
227
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200228
229 public static DemoUser getDemoUser (Integer id) {
Michael Hanlca740d72015-06-16 10:04:58 +0200230 DemoUser demo = new DemoUser();
231 demo.setId(id);
232 return demo;
233 }
234
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200235
236 public static boolean isDemo (String username) {
Michael Hanlc4446022016-02-12 18:03:17 +0100237 return new DemoUser().getUsername().equalsIgnoreCase(username);
238 }
239
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200240
241 public static ShibUser getShibInstance (String eduPersonID,
242 String mail, String cn) {
Michael Hanlca740d72015-06-16 10:04:58 +0200243 ShibUser u = new ShibUser(eduPersonID);
244 u.setAffiliation("");
245 u.setMail(mail);
246 u.setUsername(eduPersonID);
247 u.setCn(cn);
248 return u;
249 }
250
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200251
252 public static KorAPUser toKorAPUser (Map<String, Object> map) {
253 KorAPUser user = UserFactory.getUser((String) map
254 .get(Attributes.USERNAME));
Michael Hanl7368aa42016-02-05 18:15:47 +0100255 user.setPassword((String) map.get(Attributes.PASSWORD));
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200256 int id = map.get(Attributes.ID) == null ? -1 : (int) map
257 .get(Attributes.ID);
Michael Hanl7368aa42016-02-05 18:15:47 +0100258 if (id != -1)
259 user.setId(id);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200260 long cr = map.get(Attributes.ACCOUNT_CREATION) == null ? -1
261 : (long) map.get(Attributes.ACCOUNT_CREATION);
Michael Hanl7368aa42016-02-05 18:15:47 +0100262 if (cr != -1)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200263 user.setAccountCreation((Long) map
264 .get(Attributes.ACCOUNT_CREATION));
Michael Hanl7368aa42016-02-05 18:15:47 +0100265 return user;
266 }
267
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200268
269 public static User toUser (Map<String, Object> map) {
Michael Hanle17eaa52016-01-22 20:55:05 +0100270 KustvaktMap kmap = new KustvaktMap(map);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200271 int type = map.get(Attributes.TYPE) == null ? 0 : (Integer) kmap
272 .get(Attributes.TYPE, Integer.class);
Michael Hanlca740d72015-06-16 10:04:58 +0200273 User user;
Michael Hanl19390652016-01-16 11:01:24 +0100274 long created = -1;
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200275 int id = kmap.get(Attributes.ID, Integer.class) == null ? -1
276 : (Integer) kmap.get(Attributes.ID, Integer.class);
Michael Hanl7368aa42016-02-05 18:15:47 +0100277
Michael Hanl19390652016-01-16 11:01:24 +0100278 if (map.get(Attributes.ACCOUNT_CREATION) != null)
Michael Hanlc0ed00f2016-06-23 14:33:10 +0200279 created = TimeUtils.getTime(kmap.get(Attributes.ACCOUNT_CREATION))
Michael Hanl19390652016-01-16 11:01:24 +0100280 .getMillis();
Michael Hanlca740d72015-06-16 10:04:58 +0200281 switch (type) {
282 case 0:
Michael Hanle17eaa52016-01-22 20:55:05 +0100283 user = UserFactory.getUser(kmap.get(Attributes.USERNAME));
Michael Hanl7368aa42016-02-05 18:15:47 +0100284 if (id != -1)
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200285 user.setId((Integer) kmap.get(Attributes.ID,
286 Integer.class));
287 user.setAccountLocked(map.get(Attributes.ACCOUNTLOCK) == null ? false
288 : (Boolean) kmap.get(Attributes.ACCOUNTLOCK,
289 Boolean.class));
Michael Hanl19390652016-01-16 11:01:24 +0100290 user.setAccountCreation(created);
Michael Hanlca740d72015-06-16 10:04:58 +0200291 break;
292 default:
Michael Hanl7368aa42016-02-05 18:15:47 +0100293 user = UserFactory.getDemoUser();
Michael Hanl19390652016-01-16 11:01:24 +0100294 user.setAccountCreation(created);
Michael Hanlca740d72015-06-16 10:04:58 +0200295 }
296 return user;
297 }
298
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200299
margaretha894a7d72017-11-08 19:24:20 +0100300 public static KorAPUser toUser (String value) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +0200301 JsonNode node = JsonUtils.readTree(value);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200302 KorAPUser user = UserFactory.getUser(node.path(Attributes.USERNAME)
303 .asText());
304 user.setAccountLocked(node.path(Attributes.ACCOUNTLOCK).asBoolean());
Michael Hanlca740d72015-06-16 10:04:58 +0200305 user.setAccountLink(node.path(Attributes.ACCOUNTLINK).asText());
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200306 user.setAccountCreation(node.path(Attributes.ACCOUNT_CREATION)
307 .asLong());
Michael Hanlca740d72015-06-16 10:04:58 +0200308 user.setPassword(node.path(Attributes.PASSWORD).asText());
309 return user;
310 }
311 }
312
313}