Exemplo n.º 1
0
        public async Task <ActionResult> GetResourcePolicies([FromBody] List <List <AttributeMatch> > appIdList, [FromQuery] string language)
        {
            List <ResourcePolicyResponse> resourcePolicyResponses = new List <ResourcePolicyResponse>();

            foreach (var attributeMatches in appIdList)
            {
                ResourcePolicyResponse response = new ResourcePolicyResponse {
                    AppId = attributeMatches
                };
                resourcePolicyResponses.Add(response);
                string org = attributeMatches.FirstOrDefault(match => match.Id == XacmlRequestAttribute.OrgAttribute)?.Value;
                string app = attributeMatches.FirstOrDefault(match => match.Id == XacmlRequestAttribute.AppAttribute)?.Value;
                if (string.IsNullOrWhiteSpace(org))
                {
                    response.ErrorResponse = "Organisation must be defined in the path";
                    continue;
                }

                if (string.IsNullOrWhiteSpace(app))
                {
                    response.ErrorResponse = "App must be defined in the path";
                    continue;
                }

                XacmlPolicy policy = await _prp.GetPolicyAsync(org, app);

                if (policy == null)
                {
                    response.ErrorResponse = $"No valid policy found for org '{org}' and app '{app}'";
                    continue;
                }

                response.MinimumAuthenticationLevel = PolicyHelper.GetMinimumAuthenticationLevelFromXacmlPolicy(policy);
                response.ResourcePolicies           = new List <ResourcePolicy>();
                List <ResourcePolicy> list = PolicyHelper.GetResourcePoliciesFromXacmlPolicy(policy, language);
                foreach (ResourcePolicy resourcePolicy in list)
                {
                    if (resourcePolicy.Resource.First(a => a.Id == XacmlRequestAttribute.OrgAttribute).Value == org &&
                        resourcePolicy.Resource.First(a => a.Id == XacmlRequestAttribute.AppAttribute).Value == app)
                    {
                        response.ResourcePolicies.Add(resourcePolicy);
                    }
                }
            }

            return(Ok(resourcePolicyResponses));
        }