public void CreateXacmlJsonRequest_TC02() { // Arrange & Act XacmlJsonRequestRoot requestRoot = DecisionHelper.CreateXacmlJsonRequest(org, app, CreateUserClaims(true), actionType, partyId, null); XacmlJsonRequest request = requestRoot.Request; // Assert Assert.Equal(2, request.AccessSubject[0].Attribute.Count); Assert.Single(request.Action[0].Attribute); Assert.Equal(3, request.Resource[0].Attribute.Count); }
public async Task <ActionResult <ProcessState> > NextElement( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid, [FromQuery] string elementId = null) { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound("Cannot find instance!")); } if (instance.Process == null) { return(Conflict($"Process is not started. Use start!")); } if (instance.Process.Ended.HasValue) { return(Conflict($"Process is ended.")); } LoadProcessModel(org, app); if (!string.IsNullOrEmpty(elementId)) { ElementInfo elemInfo = processHelper.Process.GetElementInfo(elementId); if (elemInfo == null) { return(BadRequest($"Requested element id {elementId} is not found in process definition")); } } string altinnTaskType = instance.Process.CurrentTask?.AltinnTaskType; if (altinnTaskType == null) { return(Conflict($"Instance does not have current altinn task type information!")); } string actionType = GetActionType(altinnTaskType); XacmlJsonRequestRoot request = DecisionHelper.CreateXacmlJsonRequest(org, app, HttpContext.User, actionType, null, instanceOwnerPartyId + "/" + instanceGuid); bool authorized = await _pdp.GetDecisionForUnvalidateRequest(request, HttpContext.User); string currentElementId = instance.Process.CurrentTask?.ElementId; if (!authorized) { return(Forbid("Not Authorized")); } if (currentElementId == null) { return(Conflict($"Instance does not have current task information!")); } if (currentElementId.Equals(elementId)) { return(Conflict($"Requested process element {elementId} is same as instance's current task. Cannot change process.")); } string nextElement = processHelper.GetValidNextElementOrError(currentElementId, elementId, out ProcessError nextElementError); if (nextElementError != null) { return(Conflict(nextElementError.Text)); } if (await CanTaskBeEnded(instance, currentElementId)) { UserContext userContext = userHelper.GetUserContext(HttpContext).Result; ProcessResult nextResult = await _processService.ProcessNext(instance, nextElement, processHelper, userContext); if (nextResult != null) { NotifyAppAboutEvents(nextResult.Instance, nextResult.Events); Instance changedInstance = await _instanceService.UpdateInstance(nextResult.Instance); return(Ok(changedInstance.Process)); } } return(Conflict($"Cannot complete/close current task {currentElementId}. The data element(s) assigned to the task are not valid!")); }
public async Task <ActionResult <Instance> > Post( [FromRoute] string org, [FromRoute] string app, [FromQuery] int?instanceOwnerPartyId) { if (string.IsNullOrEmpty(org)) { return(BadRequest("The path parameter 'org' cannot be empty")); } if (string.IsNullOrEmpty(app)) { return(BadRequest("The path parameter 'app' cannot be empty")); } Application application = _appResourcesService.GetApplication(); if (application == null) { return(NotFound($"AppId {org}/{app} was not found")); } MultipartRequestReader parsedRequest = new MultipartRequestReader(Request); await parsedRequest.Read(); if (parsedRequest.Errors.Any()) { return(BadRequest($"Error when reading content: {JsonConvert.SerializeObject(parsedRequest.Errors)}")); } Instance instanceTemplate = ExtractInstanceTemplate(parsedRequest); if (!instanceOwnerPartyId.HasValue && instanceTemplate == null) { return(BadRequest("Cannot create an instance without an instanceOwner.partyId. Either provide instanceOwner party Id as a query parameter or an instanceTemplate object in the body.")); } if (instanceOwnerPartyId.HasValue && instanceTemplate?.InstanceOwner?.PartyId != null) { return(BadRequest("You cannot provide an instanceOwnerPartyId as a query param as well as an instance template in the body. Choose one or the other.")); } RequestPartValidator requestValidator = new RequestPartValidator(application); string multipartError = requestValidator.ValidateParts(parsedRequest.Parts); if (!string.IsNullOrEmpty(multipartError)) { return(BadRequest($"Error when comparing content to application metadata: {multipartError}")); } // extract or create instance template if (instanceTemplate != null) { InstanceOwner lookup = instanceTemplate.InstanceOwner; if (string.IsNullOrEmpty(instanceTemplate.InstanceOwner.PartyId) && (lookup == null || (lookup.PersonNumber == null && lookup.OrganisationNumber == null))) { return(BadRequest($"Error: instanceOwnerPartyId query parameter is empty and InstanceOwner is missing from instance template. You must populate instanceOwnerPartyId or InstanceOwner")); } } else { instanceTemplate = new Instance { InstanceOwner = new InstanceOwner { PartyId = instanceOwnerPartyId.Value.ToString() } }; } Party party; try { party = await LookupParty(instanceTemplate); } catch (Exception partyLookupException) { return(NotFound($"Cannot lookup party: {partyLookupException.Message}")); } XacmlJsonRequestRoot request = DecisionHelper.CreateXacmlJsonRequest(org, app, HttpContext.User, "instantiate", party.PartyId.ToString(), null); bool authorized = await _pdp.GetDecisionForUnvalidateRequest(request, HttpContext.User); if (!authorized) { return(Forbid()); } if (!InstantiationHelper.IsPartyAllowedToInstantiate(party, application.PartyTypesAllowed)) { return(Forbid($"Party {party?.PartyId} is not allowed to instantiate this application {org}/{app}")); } // use process controller to start process instanceTemplate.Process = null; Instance instance; try { instance = await _instanceService.CreateInstance(org, app, instanceTemplate); if (instance == null) { throw new PlatformClientException("Failure instantiating instance. UnknownError"); } } catch (Exception instanceException) { string message = $"Failure in multipart prefil. Could not create an instance of {org}/{app} for party {instanceTemplate.InstanceOwner.PartyId}."; _logger.LogError($"{message} - {instanceException}"); return(StatusCode(500, $"{message} - {instanceException.Message}")); } try { await StorePrefillParts(instance, application, parsedRequest.Parts); // get the updated instance instance = await _instanceService.GetInstance(app, org, int.Parse(instance.InstanceOwner.PartyId), Guid.Parse(instance.Id.Split("/")[1])); Instance instanceWithStartedProcess = await StartProcessAndGotoNextTask(instance); if (instanceWithStartedProcess != null) { instance = instanceWithStartedProcess; } else { return(Conflict($"Unable to start and move process to next task for instance {instance.Id}")); } } catch (Exception dataException) { string message = $"Failure storing multipart prefil data. Could not create a data element for {instance.Id} of {org}/{app}."; _logger.LogError($"{message} - {dataException}"); // todo add compensating transaction (delete instance) return(StatusCode(500, $"{message} Exception: {dataException.Message}")); } SelfLinkHelper.SetInstanceAppSelfLinks(instance, Request); string url = instance.SelfLinks.Apps; return(Created(url, instance)); }