public static async Task <FHIRResponse> callFHIRServer(string requestBody, HttpRequest req, ILogger log, string res, string id, string hist, string vid)
        {
            FHIRClient fhirClient = FHIRClientFactory.getClient(log);

            FHIRResponse fhirresp = null;

            if (req.Method.Equals("GET"))
            {
                var           qs = req.QueryString.HasValue ? req.QueryString.Value : null;
                StringBuilder sb = new StringBuilder();
                sb.Append(res);
                if (!string.IsNullOrEmpty(id))
                {
                    sb.Append("/" + id);
                    if (!string.IsNullOrEmpty(hist))
                    {
                        sb.Append("/" + hist);
                        if (!string.IsNullOrEmpty(vid))
                        {
                            sb.Append("/" + vid);
                        }
                    }
                }
                fhirresp = await fhirClient.LoadResource(sb.ToString(), qs, false, req.Headers);
            }
            else
            {
                if (req.Method.Equals("DELETE"))
                {
                    fhirresp = await fhirClient.DeleteResource(res + (id == null ? "" : "/" + id), req.Headers);
                }
                else if (req.Method.Equals("POST") && !string.IsNullOrEmpty(id) && id.StartsWith("_search"))
                {
                    var qs = req.QueryString.HasValue ? req.QueryString.Value : null;
                    fhirresp = await fhirClient.PostCommand(res + "/" + id, requestBody, qs, req.Headers);
                }
                else
                {
                    fhirresp = await fhirClient.SaveResource(res, requestBody, req.Method, req.Headers);
                }
            }
            return(fhirresp);
        }
示例#2
0
        public static async Task <IActionResult> Run(
            [HttpTrigger(AuthorizationLevel.Anonymous, "get", "post", Route = "manage/{cmd}/{res}/{id}/{name}")] HttpRequest req,
            ILogger log, ClaimsPrincipal principal, string cmd, string res, string id, string name)
        {
            log.LogInformation("SecureLink Function Invoked");
            //Is the principal authenticated
            if (!Utils.isServerAccessAuthorized(req))
            {
                return(new ContentResult()
                {
                    Content = "User is not Authenticated", StatusCode = (int)System.Net.HttpStatusCode.Unauthorized
                });
            }

            if (!Utils.inServerAccessRole(req, "A"))
            {
                return(new ContentResult()
                {
                    Content = "User does not have suffiecient rights (Administrator required)", StatusCode = (int)System.Net.HttpStatusCode.Unauthorized
                });
            }
            if (string.IsNullOrEmpty(cmd) || !validcmds.Any(cmd.Contains))
            {
                return(new BadRequestObjectResult("Invalid Command....Valid commands are link, unlink and list"));
            }
            //Are we linking the correct resource type
            if (string.IsNullOrEmpty(res) || !allowedresources.Any(res.Contains))
            {
                return(new BadRequestObjectResult("Resource must be Patient,Practitioner or RelatedPerson"));
            }

            ClaimsIdentity ci      = (ClaimsIdentity)principal.Identity;
            string         aadten  = (string.IsNullOrEmpty(ci.Tenant()) ? "Unknown" : ci.Tenant());
            FhirJsonParser _parser = new FhirJsonParser();

            _parser.Settings.AcceptUnknownMembers   = true;
            _parser.Settings.AllowUnrecognizedEnums = true;
            //Get a FHIR Client so we can talk to the FHIR Server
            log.LogInformation($"Instanciating FHIR Client Proxy");
            FHIRClient fhirClient  = FHIRClientFactory.getClient(log);
            int        i_link_days = 0;

            int.TryParse(System.Environment.GetEnvironmentVariable("FP-LINK-DAYS"), out i_link_days);
            if (i_link_days == 0)
            {
                i_link_days = 365;
            }
            //Load the resource to Link
            var fhirresp = await fhirClient.LoadResource(res + "/" + id, null, false, req.Headers);

            var lres = _parser.Parse <Resource>((string)fhirresp.Content);

            if (lres.ResourceType == Hl7.Fhir.Model.ResourceType.OperationOutcome)
            {
                return(new BadRequestObjectResult(lres.ToString()));
            }
            CloudTable table = Utils.getTable();

            switch (cmd)
            {
            case "link":
                LinkEntity linkentity = new LinkEntity(res, aadten + "-" + name);
                linkentity.ValidUntil       = DateTime.Now.AddDays(i_link_days);
                linkentity.LinkedResourceId = id;
                Utils.setLinkEntity(table, linkentity);
                return(new OkObjectResult($"Identity: {name} in directory {aadten} is now linked to {res}/{id}"));

            case "unlink":
                LinkEntity delentity = Utils.getLinkEntity(table, res, aadten + "-" + name);
                if (delentity == null)
                {
                    return(new OkObjectResult($"Resource {res}/{id} has no links to Identity {name} in directory {aadten}"));
                }
                Utils.deleteLinkEntity(table, delentity);
                return(new OkObjectResult($"Identity: {name} in directory {aadten} has been unlinked from {res}/{id}"));

            case "list":
                LinkEntity entity = Utils.getLinkEntity(table, res, aadten + "-" + name);
                if (entity != null)
                {
                    return(new OkObjectResult($"Resource {res}/{id} is linked to Identity: {name} in directory {aadten}"));
                }
                else
                {
                    return(new OkObjectResult($"Resource {res}/{id} has no links to Identity {name} in directory {aadten}"));
                }
            }
            return(new OkObjectResult($"No action taken Identity: {name}"));
        }