Search in sources :

Example 1 with NAME

use of org.apache.solr.common.params.CommonParams.NAME in project lucene-solr by apache.

the class Permission method load.

static Permission load(Map m) {
    Permission p = new Permission();
    p.originalConfig = new LinkedHashMap<>(m);
    String name = (String) m.get(NAME);
    if (!m.containsKey("role"))
        throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, "role not specified");
    p.role = readValueAsSet(m, "role");
    if (PermissionNameProvider.Name.get(name) != null) {
        p.wellknownName = PermissionNameProvider.Name.get(name);
        HashSet<String> disAllowed = new HashSet<>(knownKeys);
        //these are the only
        disAllowed.remove("role");
        //allowed keys for well-known permissions
        disAllowed.remove(NAME);
        //allowed keys for well-known permissions
        disAllowed.remove("collection");
        disAllowed.remove("index");
        for (String s : disAllowed) {
            if (m.containsKey(s))
                throw new SolrException(SolrException.ErrorCode.BAD_REQUEST, s + " is not a valid key for the permission : " + name);
        }
    }
    p.name = name;
    p.path = readSetSmart(name, m, "path");
    p.collections = readSetSmart(name, m, "collection");
    p.method = readSetSmart(name, m, "method");
    Map<String, Object> paramRules = (Map<String, Object>) m.get("params");
    if (paramRules != null) {
        p.params = new LinkedHashMap<>();
        for (Map.Entry<String, Object> e : paramRules.entrySet()) {
            if (e.getValue() == null) {
                p.params.put(e.getKey(), (String[] val) -> val == null);
            } else {
                List<String> patternStrs = e.getValue() instanceof List ? (List) e.getValue() : singletonList(e.getValue().toString());
                List patterns = patternStrs.stream().map(it -> it.startsWith("REGEX:") ? Pattern.compile(String.valueOf(it.substring("REGEX:".length()))) : it).collect(Collectors.toList());
                p.params.put(e.getKey(), val -> {
                    if (val == null)
                        return false;
                    for (Object pattern : patterns) {
                        for (String s : val) {
                            if (pattern instanceof String) {
                                if (pattern.equals(s))
                                    return true;
                            } else if (pattern instanceof Pattern) {
                                if (((Pattern) pattern).matcher(s).find())
                                    return true;
                            }
                        }
                    }
                    return false;
                });
            }
        }
    }
    return p;
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Utils(org.apache.solr.common.util.Utils) Collection(java.util.Collection) Set(java.util.Set) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) Collections.singletonList(java.util.Collections.singletonList) HashSet(java.util.HashSet) LinkedHashMap(java.util.LinkedHashMap) SolrException(org.apache.solr.common.SolrException) NAME(org.apache.solr.common.params.CommonParams.NAME) List(java.util.List) Collections.singleton(java.util.Collections.singleton) Map(java.util.Map) Pattern(java.util.regex.Pattern) Collections(java.util.Collections) Pattern(java.util.regex.Pattern) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SolrException(org.apache.solr.common.SolrException) HashSet(java.util.HashSet)

Aggregations

ImmutableSet (com.google.common.collect.ImmutableSet)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Collections.singleton (java.util.Collections.singleton)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Set (java.util.Set)1 Function (java.util.function.Function)1 Pattern (java.util.regex.Pattern)1 Collectors (java.util.stream.Collectors)1 SolrException (org.apache.solr.common.SolrException)1 NAME (org.apache.solr.common.params.CommonParams.NAME)1 Utils (org.apache.solr.common.util.Utils)1