001/*-
002 * #%L
003 * HAPI FHIR - Server Framework
004 * %%
005 * Copyright (C) 2014 - 2024 Smile CDR, Inc.
006 * %%
007 * Licensed under the Apache License, Version 2.0 (the "License");
008 * you may not use this file except in compliance with the License.
009 * You may obtain a copy of the License at
010 *
011 *      http://www.apache.org/licenses/LICENSE-2.0
012 *
013 * Unless required by applicable law or agreed to in writing, software
014 * distributed under the License is distributed on an "AS IS" BASIS,
015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
016 * See the License for the specific language governing permissions and
017 * limitations under the License.
018 * #L%
019 */
020package ca.uhn.fhir.rest.server.interceptor.auth;
021
022import ca.uhn.fhir.i18n.Msg;
023import jakarta.annotation.Nonnull;
024
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(
049                                                Msg.code(341) + code
050                                                                + " is not a valid search parameter. Search parameters must be in the form resourcetype:parametercode, e.g. 'Device:patient'");
051                        }
052                        String[] split = code.split(":");
053                        if (split.length != 2) {
054                                throw new IllegalArgumentException(
055                                                Msg.code(342) + code
056                                                                + " is not a valid search parameter. Search parameters must be in the form resourcetype:parametercode, e.g. 'Device:patient'");
057                        } else {
058                                myResourceTypeToParameterCodeMap
059                                                .computeIfAbsent(split[0].toLowerCase(), (key) -> new HashSet<>())
060                                                .add(split[1].toLowerCase());
061                        }
062                });
063        }
064
065        public Set<String> getSearchParamNamesForResourceType(@Nonnull String theResourceType) {
066                return myResourceTypeToParameterCodeMap.computeIfAbsent(
067                                theResourceType.toLowerCase(), (key) -> new HashSet<>());
068        }
069}