Exemplo n.º 1
0
        public async Task <ActionResult <QueryResponse <Instance> > > GetInstances(
            [FromQuery] string org,
            [FromQuery] string appId,
            [FromQuery(Name = "process.currentTask")] string currentTaskId,
            [FromQuery(Name = "process.isComplete")] bool?processIsComplete,
            [FromQuery(Name = "process.endEvent")] string processEndEvent,
            [FromQuery(Name = "process.ended")] string processEnded,
            [FromQuery(Name = "instanceOwner.partyId")] int?instanceOwnerPartyId,
            [FromQuery] string lastChanged,
            [FromQuery] string created,
            [FromQuery(Name = "visibleAfter")] string visibleAfter,
            [FromQuery] string dueBefore,
            string continuationToken,
            int?size)
        {
            int    pageSize = size ?? 100;
            string selfContinuationToken = null;

            bool isOrgQuerying = false;

            // if user is org
            string orgClaim = User.GetOrg();
            int?   userId   = User.GetUserIdAsInt();

            if (orgClaim != null)
            {
                isOrgQuerying = true;

                if (!_authzHelper.ContainsRequiredScope(InstanceReadScope, User))
                {
                    return(Forbid());
                }

                if (string.IsNullOrEmpty(org) && string.IsNullOrEmpty(appId))
                {
                    return(BadRequest("Org or AppId must be defined."));
                }

                org = string.IsNullOrEmpty(org) ? appId.Split('/')[0] : org;

                if (!orgClaim.Equals(org, StringComparison.InvariantCultureIgnoreCase))
                {
                    return(Forbid());
                }
            }
            else if (userId != null)
            {
                if (instanceOwnerPartyId == null)
                {
                    return(BadRequest("InstanceOwnerPartyId must be defined."));
                }
            }
            else
            {
                return(BadRequest());
            }

            if (!string.IsNullOrEmpty(continuationToken))
            {
                selfContinuationToken = continuationToken;
                continuationToken     = HttpUtility.UrlDecode(continuationToken);
            }

            Dictionary <string, StringValues> queryParams = QueryHelpers.ParseQuery(Request.QueryString.Value);

            string host  = $"https://platform.{_generalSettings.Hostname}";
            string url   = Request.Path;
            string query = Request.QueryString.Value;

            try
            {
                InstanceQueryResponse result = await _instanceRepository.GetInstancesFromQuery(queryParams, continuationToken, pageSize);

                if (!string.IsNullOrEmpty(result.Exception))
                {
                    return(BadRequest(result.Exception));
                }

                if (!isOrgQuerying)
                {
                    int originalCount = result.Instances.Count;
                    result.Instances = await _authzHelper.AuthorizeInstances(User, result.Instances);

                    result.Count      = result.Instances.Count;
                    result.TotalHits -= originalCount - result.Instances.Count;
                }

                string nextContinuationToken = HttpUtility.UrlEncode(result.ContinuationToken);
                result.ContinuationToken = null;

                QueryResponse <Instance> response = new QueryResponse <Instance>
                {
                    Instances = result.Instances,
                    Count     = result.Instances.Count,
                    TotalHits = result.TotalHits ?? 0
                };

                if (continuationToken == null)
                {
                    string selfUrl = $"{host}{url}{query}";
                    response.Self = selfUrl;
                }
                else
                {
                    string selfQueryString = BuildQueryStringWithOneReplacedParameter(
                        queryParams,
                        "continuationToken",
                        selfContinuationToken);

                    string selfUrl = $"{host}{url}{selfQueryString}";

                    response.Self = selfUrl;
                }

                if (!string.IsNullOrEmpty(nextContinuationToken))
                {
                    string nextQueryString = BuildQueryStringWithOneReplacedParameter(
                        queryParams,
                        "continuationToken",
                        nextContinuationToken);

                    string nextUrl = $"{host}{url}{nextQueryString}";

                    response.Next = nextUrl;
                }

                // add self links to platform
                result.Instances.ForEach(i => i.SetPlatformSelfLinks(_storageBaseAndHost));

                return(Ok(response));
            }
            catch (Exception e)
            {
                _logger.LogError($"Unable to perform query on instances due to: {e}");
                return(StatusCode(500, $"Unable to perform query on instances due to: {e.Message}"));
            }
        }