blob: 295cb4d922dd63f36fea8d8b9b865689dd848c42 [file] [log] [blame]
Michael Hanlfb839b92015-09-19 21:32:34 +02001package de.ids_mannheim.korap.config;
2
margarethad4796662017-11-09 16:11:40 +01003import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlfb5bdd92016-06-08 11:29:47 +02004import de.ids_mannheim.korap.user.Userdata;
Michael Hanlf1e85e72016-01-21 16:55:45 +01005import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanlfb839b92015-09-19 21:32:34 +02006
7import java.util.ArrayList;
8import java.util.HashMap;
9import java.util.List;
10import java.util.Map;
11
12/**
13 * @author hanl
14 * @date 09/12/2014
15 */
16public class Scopes {
17
18 public enum Scope {
margaretha6672fe52023-09-28 09:24:25 +020019 profile, email, queries, account, preferences, search
Michael Hanlfb839b92015-09-19 21:32:34 +020020
21 }
22
23 private static final String[] profile = { Attributes.EMAIL,
24 Attributes.FIRSTNAME, Attributes.LASTNAME, Attributes.INSTITUTION,
25 Attributes.ADDRESS, Attributes.PHONE, Attributes.GENDER,
26 Attributes.COUNTRY };
27
Michael Hanlfb839b92015-09-19 21:32:34 +020028 private static final Enum[] SERVICE_DEFAULTS = { Scope.account,
29 Scope.preferences, Scope.search, Scope.queries };
30
Michael Hanlfb5bdd92016-06-08 11:29:47 +020031 public static Scopes getProfileScopes (Userdata values) {
Michael Hanlf1e85e72016-01-21 16:55:45 +010032 Scopes r = new Scopes();
Michael Hanlfb839b92015-09-19 21:32:34 +020033 for (String key : profile) {
34 Object v = values.get(key);
35 if (v != null)
Michael Hanlfb5bdd92016-06-08 11:29:47 +020036 r.values.put(key, v);
Michael Hanlfb839b92015-09-19 21:32:34 +020037 }
38 return r;
39 }
40
41 /**
42 * expects space separated values
Michael Hanl8abaf9e2016-05-23 16:46:35 +020043 *
Michael Hanlfb839b92015-09-19 21:32:34 +020044 * @param scopes
45 * @return
46 */
47 //todo: test
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048 public static Scope[] mapScopes (String scopes) {
Michael Hanlfb839b92015-09-19 21:32:34 +020049 List<Enum> s = new ArrayList<>();
50 for (String value : scopes.split(" "))
51 s.add(Scope.valueOf(value.toLowerCase()));
Michael Hanlf1e85e72016-01-21 16:55:45 +010052 return s.toArray(new Scope[s.size()]);
Michael Hanlfb839b92015-09-19 21:32:34 +020053 }
54
Michael Hanlfb5bdd92016-06-08 11:29:47 +020055 public static Scopes mapScopes (String scopes, Userdata details) {
Michael Hanlf1e85e72016-01-21 16:55:45 +010056 Scopes m = new Scopes();
Michael Hanlfb839b92015-09-19 21:32:34 +020057 if (scopes != null && !scopes.isEmpty()) {
Michael Hanlf1e85e72016-01-21 16:55:45 +010058 Scope[] scopearr = mapScopes(scopes);
59 for (Scope s : scopearr) {
Michael Hanl5fac8ab2016-01-29 16:33:04 +010060 Object v = details.get(s.toString());
Michael Hanlf1e85e72016-01-21 16:55:45 +010061 if (v != null)
Michael Hanlfb5bdd92016-06-08 11:29:47 +020062 m.values.put(s.toString(), v);
Michael Hanlf1e85e72016-01-21 16:55:45 +010063 }
Michael Hanlfb839b92015-09-19 21:32:34 +020064 if (scopes.contains(Scope.profile.toString()))
Michael Hanlfb5bdd92016-06-08 11:29:47 +020065 m.values.putAll(Scopes.getProfileScopes(details).values);
margaretha20f31232018-07-09 17:49:39 +020066 m.values.put(Attributes.SCOPE, scopes);
Michael Hanlfb839b92015-09-19 21:32:34 +020067 }
68 return m;
69 }
70
Michael Hanlfb5bdd92016-06-08 11:29:47 +020071 private Map<String, Object> values;
Michael Hanl19390652016-01-16 11:01:24 +010072
Michael Hanl8abaf9e2016-05-23 16:46:35 +020073 private Scopes () {
Michael Hanlfb5bdd92016-06-08 11:29:47 +020074 this.values = new HashMap<>();
Michael Hanlf1e85e72016-01-21 16:55:45 +010075 }
76
margarethad4796662017-11-09 16:11:40 +010077 public String toEntity () throws KustvaktException {
Michael Hanlfb5bdd92016-06-08 11:29:47 +020078 if (this.values.isEmpty())
Michael Hanlf1e85e72016-01-21 16:55:45 +010079 return "";
Michael Hanlfb5bdd92016-06-08 11:29:47 +020080 return JsonUtils.toJSON(this.values);
Michael Hanlf1e85e72016-01-21 16:55:45 +010081 }
82
Michael Hanl8abaf9e2016-05-23 16:46:35 +020083 public Map<String, Object> toMap () {
Michael Hanlfb5bdd92016-06-08 11:29:47 +020084 return new HashMap<>(this.values);
Michael Hanlf1e85e72016-01-21 16:55:45 +010085 }
Michael Hanl19390652016-01-16 11:01:24 +010086
Michael Hanlfb839b92015-09-19 21:32:34 +020087}