001package ca.uhn.fhir.rest.server.interceptor.auth;
002
003/*-
004 * #%L
005 * HAPI FHIR - Server Framework
006 * %%
007 * Copyright (C) 2014 - 2022 Smile CDR, Inc.
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 *
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 *
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import ca.uhn.fhir.i18n.Msg;
024import javax.annotation.Nonnull;
025import java.util.Arrays;
026import java.util.HashMap;
027import java.util.HashSet;
028import java.util.Map;
029import java.util.Set;
030
031/**
032 * This class is used in RuleBuilder, as a way to provide a compartment permission additional resource search params that
033 * are to be included as "in" the given compartment. For example, if you were to populate this map with
034 * [device -> ["patient", "subject"]
035 * and apply it to compartment Patient/123, then any device with Patient/123 as its patient would be considered "in"
036 * the compartment, despite the fact that device is technically not part of the compartment definition for patient.
037 */
038public class AdditionalCompartmentSearchParameters {
039        private Map<String, Set<String>> myResourceTypeToParameterCodeMap;
040
041        public AdditionalCompartmentSearchParameters() {
042                myResourceTypeToParameterCodeMap = new HashMap<>();
043        }
044
045        public void addSearchParameters(@Nonnull String... theQualifiedSearchParameters) {
046                Arrays.stream(theQualifiedSearchParameters).forEach(code -> {
047                        if (code == null || !code.contains(":")) {
048                                throw new IllegalArgumentException(Msg.code(341) + code + " is not a valid search parameter. Search parameters must be in the form resourcetype:parametercode, e.g. 'Device:patient'");
049                        }
050                        String[] split = code.split(":");
051                        if (split.length != 2) {
052                                throw new IllegalArgumentException(Msg.code(342) + code + " is not a valid search parameter. Search parameters must be in the form resourcetype:parametercode, e.g. 'Device:patient'");
053                        } else {
054                                myResourceTypeToParameterCodeMap.computeIfAbsent(split[0].toLowerCase(), (key) -> new HashSet<>()).add(split[1].toLowerCase());
055                        }
056                });
057        }
058
059        public Set<String> getSearchParamNamesForResourceType(@Nonnull String theResourceType) {
060                return myResourceTypeToParameterCodeMap.computeIfAbsent(theResourceType.toLowerCase(), (key) -> new HashSet<>());
061        }
062}