| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.user; |
| 2 | |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 3 | import java.io.Serializable; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 4 | import java.util.HashMap; |
| 5 | import java.util.List; |
| 6 | import java.util.Map; |
| 7 | |
| 8 | import org.joda.time.DateTime; |
| 9 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 10 | import com.fasterxml.jackson.databind.JsonNode; |
| margaretha | 4566792 | 2018-01-25 21:23:03 +0100 | [diff] [blame] | 11 | |
| Michael Hanl | 00b64e0 | 2016-05-24 20:24:27 +0200 | [diff] [blame] | 12 | import de.ids_mannheim.korap.config.Attributes; |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 13 | import de.ids_mannheim.korap.config.ParamFields; |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 14 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 15 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 16 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 17 | import de.ids_mannheim.korap.web.utils.KustvaktMap; |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 18 | import lombok.AccessLevel; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 19 | import lombok.Data; |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 20 | import lombok.Getter; |
| 21 | import lombok.Setter; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 22 | |
| 23 | @Data |
| 24 | public abstract class User implements Serializable { |
| 25 | |
| margaretha | 58e1863 | 2018-02-15 13:04:42 +0100 | [diff] [blame] | 26 | //EM: add |
| 27 | private String email; |
| 28 | //EM: finish |
| 29 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 30 | 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 Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 36 | private ParamFields fields; |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 37 | @Getter(AccessLevel.PRIVATE) |
| 38 | @Setter(AccessLevel.PRIVATE) |
| margaretha | 0bcde4c | 2019-01-23 19:08:51 +0100 | [diff] [blame] | 39 | private UserSettingProcessor settings; |
| Michael Hanl | dd729d7 | 2016-01-27 23:22:28 +0100 | [diff] [blame] | 40 | //todo: remove! |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 41 | @Getter(AccessLevel.PRIVATE) |
| 42 | @Setter(AccessLevel.PRIVATE) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 43 | private UserDetails details; |
| Michael Hanl | 5fac8ab | 2016-01-29 16:33:04 +0100 | [diff] [blame] | 44 | @Getter(AccessLevel.PRIVATE) |
| 45 | @Setter(AccessLevel.PRIVATE) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 46 | private List<UserQuery> queries; |
| 47 | |
| margaretha | 4fa4b06 | 2019-01-28 19:43:30 +0100 | [diff] [blame^] | 48 | private UserSettingProcessor userSettingProcessor; |
| Michael Hanl | 4f9002d | 2016-01-27 23:21:45 +0100 | [diff] [blame] | 49 | |
| margaretha | 4edc70e | 2018-03-14 22:34:29 +0100 | [diff] [blame] | 50 | // private boolean isSystemAdmin; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 51 | |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 52 | // 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; |
| Bodmo | 946832f | 2017-05-24 17:42:17 +0200 | [diff] [blame] | 72 | |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 73 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 74 | protected User () { |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 75 | this.fields = new ParamFields(); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 76 | this.accountCreation = TimeUtils.getNow().getMillis(); |
| 77 | this.isAccountLocked = false; |
| 78 | this.username = ""; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 79 | this.id = -1; |
| Bodmo | ca3dcfb | 2017-05-24 16:36:00 +0200 | [diff] [blame] | 80 | this.location = Location.EXTERN; |
| 81 | this.corpusAccess = CorpusAccess.FREE; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 82 | } |
| 83 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 84 | |
| 85 | protected User (int type) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 86 | this(); |
| 87 | this.type = type; |
| 88 | } |
| 89 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 90 | |
| 91 | protected User (String username, int type) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 92 | this(type); |
| 93 | this.username = username; |
| 94 | } |
| 95 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 96 | |
| 97 | public void addField (ParamFields.Param param) { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 98 | this.fields.add(param); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 99 | } |
| 100 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 101 | |
| 102 | public <T extends ParamFields.Param> T getField (Class<T> cl) { |
| Michael Hanl | 1e18cb4 | 2015-08-06 20:57:35 +0200 | [diff] [blame] | 103 | return this.fields.get(cl); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 104 | } |
| 105 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 106 | public void setId (Integer id) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 107 | this.id = id; |
| Michael Hanl | 4f9002d | 2016-01-27 23:21:45 +0100 | [diff] [blame] | 108 | // if (this.settings != null) |
| 109 | // this.settings.setUserID(this.id); |
| 110 | // if (this.details != null) |
| 111 | // this.details.setUserID(this.id); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 112 | } |
| 113 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 114 | |
| 115 | public Map<String, Object> toMap () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 116 | 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 Hanl | 4f9002d | 2016-01-27 23:21:45 +0100 | [diff] [blame] | 121 | // if (this.getDetails() != null) |
| 122 | // map.putAll(this.getDetails().toMap()); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 123 | return map; |
| 124 | } |
| 125 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 126 | |
| 127 | public Map toCache () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 128 | Map map = new HashMap(); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 129 | map.put(Attributes.ID, this.id); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 130 | 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 Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 138 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 139 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 140 | public boolean equals (Object o) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 141 | 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 | |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 151 | // public boolean isAdmin () { |
| 152 | // return this.getUsername().equals(ADMINISTRATOR_ID); |
| 153 | // } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 154 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 155 | |
| 156 | protected abstract User clone (); |
| 157 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 158 | |
| 159 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 160 | public String toString () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 161 | 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 | |
| Bodmo | c125bf1 | 2017-06-01 16:23:59 +0200 | [diff] [blame] | 167 | 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 Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 191 | public static class UserFactory { |
| 192 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 193 | public static KorAPUser getUser (String username) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 194 | return new KorAPUser(username); |
| 195 | } |
| 196 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 197 | |
| 198 | public static KorAPUser getUser (String username, String password) { |
| Michael Hanl | f21773f | 2015-10-16 23:02:31 +0200 | [diff] [blame] | 199 | KorAPUser user = new KorAPUser(username); |
| 200 | user.setPassword(password); |
| 201 | return user; |
| 202 | } |
| 203 | |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 204 | // public static KorAPUser getAdmin () { |
| 205 | // return new KorAPUser(ADMINISTRATOR_ID, ADMINISTRATOR_NAME); |
| 206 | // } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 207 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 208 | |
| 209 | public static DemoUser getDemoUser () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 210 | return new DemoUser(); |
| 211 | } |
| 212 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 213 | |
| 214 | public static DemoUser getDemoUser (Integer id) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 215 | DemoUser demo = new DemoUser(); |
| 216 | demo.setId(id); |
| 217 | return demo; |
| 218 | } |
| 219 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 220 | |
| 221 | public static boolean isDemo (String username) { |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 222 | return new DemoUser().getUsername().equalsIgnoreCase(username); |
| 223 | } |
| 224 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 225 | |
| margaretha | fb027f9 | 2018-04-23 20:00:13 +0200 | [diff] [blame] | 226 | // 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 Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 235 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 236 | |
| 237 | public static KorAPUser toKorAPUser (Map<String, Object> map) { |
| 238 | KorAPUser user = UserFactory.getUser((String) map |
| 239 | .get(Attributes.USERNAME)); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 240 | user.setPassword((String) map.get(Attributes.PASSWORD)); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 241 | int id = map.get(Attributes.ID) == null ? -1 : (int) map |
| 242 | .get(Attributes.ID); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 243 | if (id != -1) |
| 244 | user.setId(id); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 245 | long cr = map.get(Attributes.ACCOUNT_CREATION) == null ? -1 |
| 246 | : (long) map.get(Attributes.ACCOUNT_CREATION); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 247 | if (cr != -1) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 248 | user.setAccountCreation((Long) map |
| 249 | .get(Attributes.ACCOUNT_CREATION)); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 250 | return user; |
| 251 | } |
| 252 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 253 | |
| 254 | public static User toUser (Map<String, Object> map) { |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 255 | KustvaktMap kmap = new KustvaktMap(map); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 256 | int type = map.get(Attributes.TYPE) == null ? 0 : (Integer) kmap |
| 257 | .get(Attributes.TYPE, Integer.class); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 258 | User user; |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 259 | long created = -1; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 260 | int id = kmap.get(Attributes.ID, Integer.class) == null ? -1 |
| 261 | : (Integer) kmap.get(Attributes.ID, Integer.class); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 262 | |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 263 | if (map.get(Attributes.ACCOUNT_CREATION) != null) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 264 | created = TimeUtils.getTime(kmap.get(Attributes.ACCOUNT_CREATION)) |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 265 | .getMillis(); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 266 | switch (type) { |
| 267 | case 0: |
| Michael Hanl | e17eaa5 | 2016-01-22 20:55:05 +0100 | [diff] [blame] | 268 | user = UserFactory.getUser(kmap.get(Attributes.USERNAME)); |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 269 | if (id != -1) |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 270 | 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 Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 275 | user.setAccountCreation(created); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 276 | break; |
| 277 | default: |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 278 | user = UserFactory.getDemoUser(); |
| Michael Hanl | 1939065 | 2016-01-16 11:01:24 +0100 | [diff] [blame] | 279 | user.setAccountCreation(created); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 280 | } |
| 281 | return user; |
| 282 | } |
| 283 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 284 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 285 | public static KorAPUser toUser (String value) throws KustvaktException { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 286 | JsonNode node = JsonUtils.readTree(value); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 287 | KorAPUser user = UserFactory.getUser(node.path(Attributes.USERNAME) |
| 288 | .asText()); |
| 289 | user.setAccountLocked(node.path(Attributes.ACCOUNTLOCK).asBoolean()); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 290 | user.setAccountLink(node.path(Attributes.ACCOUNTLINK).asText()); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame] | 291 | user.setAccountCreation(node.path(Attributes.ACCOUNT_CREATION) |
| 292 | .asLong()); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 293 | user.setPassword(node.path(Attributes.PASSWORD).asText()); |
| 294 | return user; |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | } |