org.apache.solr.security.AutorizationEditOperation Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.solr.security;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
import org.apache.solr.common.util.CommandOperation;
import static org.apache.solr.common.util.Utils.getDeepCopy;
import static org.apache.solr.handler.admin.SecurityConfHandler.getListValue;
import static org.apache.solr.handler.admin.SecurityConfHandler.getMapValue;
enum AutorizationEditOperation {
SET_USER_ROLE("set-user-role") {
@Override
public Map edit(Map latestConf, CommandOperation op) {
Map roleMap = getMapValue(latestConf, "user-role");
Map map = op.getDataMap();
if (op.hasError()) return null;
for (Map.Entry e : map.entrySet()) {
if (e.getValue() == null) {
roleMap.remove(e.getKey());
continue;
}
if (e.getValue() instanceof String || e.getValue() instanceof List) {
roleMap.put(e.getKey(), e.getValue());
} else {
op.addError("Unexpected value ");
return null;
}
}
return latestConf;
}
},
SET_PERMISSION("set-permission") {
@Override
public Map edit(Map latestConf, CommandOperation op) {
Integer index = op.getInt("index", null);
Integer beforeIdx = op.getInt("before",null);
Map dataMap = op.getDataMap();
if (op.hasError()) return null;
dataMap = getDeepCopy(dataMap, 3);
dataMap.remove("before");
if (beforeIdx != null && index != null) {
op.addError("Cannot use 'index' and 'before together ");
return null;
}
for (String key : dataMap.keySet()) {
if (!Permission.knownKeys.contains(key)) op.addError("Unknown key, " + key);
}
try {
Permission.load(dataMap);
} catch (Exception e) {
op.addError(e.getMessage());
return null;
}
if(op.hasError()) return null;
List