001package ca.uhn.fhir.rest.server.interceptor.auth;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2017 University Health Network
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 java.util.Set;
024
025import org.hl7.fhir.instance.model.api.IBaseResource;
026import org.hl7.fhir.instance.model.api.IIdType;
027
028import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
029import ca.uhn.fhir.rest.method.RequestDetails;
030import ca.uhn.fhir.rest.server.interceptor.auth.AuthorizationInterceptor.Verdict;
031
032public class RuleImplConditional extends BaseRule implements IAuthRule {
033
034        private AppliesTypeEnum myAppliesTo;
035        private Set<?> myAppliesToTypes;
036        private RestOperationTypeEnum myOperationType;
037
038        public RuleImplConditional(String theRuleName) {
039                super(theRuleName);
040        }
041
042        @Override
043        public Verdict applyRule(RestOperationTypeEnum theOperation, RequestDetails theRequestDetails, IBaseResource theInputResource, IIdType theInputResourceId, IBaseResource theOutputResource,
044                        IRuleApplier theRuleApplier) {
045
046                if (theInputResourceId != null) {
047                        return null;
048                }
049                
050                if (theOperation == myOperationType) {
051                        switch (myAppliesTo) {
052                        case ALL_RESOURCES:
053                        case INSTANCES:
054                                break;
055                        case TYPES:
056                                if (theInputResource == null || !myAppliesToTypes.contains(theInputResource.getClass())) {
057                                        return null;
058                                }
059                                break;
060                        }
061
062                        if (theRequestDetails.getConditionalUrl(myOperationType) == null) {
063                                return null;
064                        }
065
066                        return newVerdict();
067                }
068
069                return null;
070        }
071
072        void setAppliesTo(AppliesTypeEnum theAppliesTo) {
073                myAppliesTo = theAppliesTo;
074        }
075
076        void setAppliesToTypes(Set<?> theAppliesToTypes) {
077                myAppliesToTypes = theAppliesToTypes;
078        }
079
080        void setOperationType(RestOperationTypeEnum theOperationType) {
081                myOperationType = theOperationType;
082        }
083
084}