public async Task <ActionResult> Get( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { EnforcementResult enforcementResult = await AuthorizeAction(org, app, instanceOwnerPartyId, instanceGuid, "read"); if (!enforcementResult.Authorized) { return(Forbidden(enforcementResult)); } try { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); SelfLinkHelper.SetInstanceAppSelfLinks(instance, Request); string userOrgClaim = User.GetOrg(); if (userOrgClaim == null || !org.Equals(userOrgClaim, StringComparison.InvariantCultureIgnoreCase)) { await _instanceService.UpdateReadStatus(instanceOwnerPartyId, instanceGuid, "read"); } return(Ok(instance)); } catch (Exception exception) { return(ExceptionResponse(exception, $"Get instance {instanceOwnerPartyId}/{instanceGuid} failed")); } }
public async Task <IActionResult> ValidateInstance( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound()); } string taskId = instance.Process?.CurrentTask?.ElementId; if (taskId == null) { throw new ValidationException("Unable to validate instance without a started process."); } List <ValidationIssue> messages = await _validationService.ValidateAndUpdateInstance(instance, taskId); await _instanceService.UpdateInstance(instance); return(Ok(messages)); }
public async Task <ActionResult> Get( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { EnforcementResult enforcementResult = await AuthorizeAction(org, app, instanceOwnerPartyId, "read"); if (!enforcementResult.Authorized) { if (enforcementResult.FailedObligations != null && enforcementResult.FailedObligations.Count > 0) { return(StatusCode((int)HttpStatusCode.Forbidden, enforcementResult.FailedObligations)); } return(StatusCode((int)HttpStatusCode.Forbidden)); } try { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); SelfLinkHelper.SetInstanceAppSelfLinks(instance, Request); return(Ok(instance)); } catch (Exception exception) { return(ExceptionResponse(exception, $"Get instance {instanceOwnerPartyId}/{instanceGuid} failed")); } }
public async Task <ActionResult <ProcessState> > GetProcessState( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerId, [FromRoute] Guid instanceGuid) { Instance instance = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid); ProcessState processState = instance.Process; return(Ok(processState)); }
public async Task <ActionResult> Create( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid, [FromQuery] string dataType) { if (string.IsNullOrWhiteSpace(dataType)) { return(BadRequest("Element type must be provided.")); } Application application = _appResourcesService.GetApplication(); if (application == null) { return(NotFound($"AppId {org}/{app} was not found")); } DataType dataTypeFromMetadata = application.DataTypes.FirstOrDefault(e => e.Id.Equals(dataType, StringComparison.InvariantCultureIgnoreCase)); if (dataTypeFromMetadata == null) { return(BadRequest($"Element type {dataType} not allowed for instance {instanceGuid}.")); } try { bool appLogic = dataTypeFromMetadata.AppLogic != null; Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound($"Did not find instance {instance}")); } if (appLogic) { return(await CreateAppModelData(org, app, instance, dataType)); } else { return(await CreateBinaryData(org, app, instance, dataType)); } } catch (PlatformHttpException pce) { return(ExceptionResponse(pce, $"Cannot create data element of {dataType} for {instanceOwnerPartyId}/{instanceGuid}")); } }
public async Task <ActionResult> GetDataElement( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerId, [FromRoute] Guid instanceGuid, [FromRoute] string elementType = "default") { IServiceImplementation serviceImplementation = await PrepareServiceImplementation(org, app, elementType); Instance instance = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid); if (instance == null) { return(NotFound("Did not find instance")); } // Assume that there is only one data element of a given type !! DataElement dataElement = instance.Data.Find(m => m.ElementType.Equals(elementType)); if (dataElement == null) { return(NotFound("Did not find data element")); } Guid dataId = Guid.Parse(dataElement.Id); // Get Form Data from data service. Assumes that the data element is form data. object serviceModel = dataService.GetFormData( instanceGuid, serviceImplementation.GetServiceModelType(), org, app, instanceOwnerId, dataId); if (serviceModel == null) { return(BadRequest($"Did not find form data for data element {dataId}")); } // Assing the populated service model to the service implementation serviceImplementation.SetServiceModel(serviceModel); // send events to trigger application business logic await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval); await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Calculation); return(Ok(serviceModel)); }
public async Task <ActionResult <ProcessState> > GetProcessState( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound()); } ProcessState processState = instance.Process; return(Ok(processState)); }
public async Task <ActionResult> Create( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerId, [FromRoute] Guid instanceGuid, [FromQuery] string elementType, [FromQuery] string attachmentName = "attachment") { if (string.IsNullOrWhiteSpace(elementType)) { return(BadRequest("Element type must be provided.")); } Application application = await appService.GetApplication(org, app); if (application == null) { return(NotFound($"AppId {org}/{app} was not found")); } ElementType elementTypeFromMetadata = application.ElementTypes.FirstOrDefault(e => e.Id.Equals(elementType, StringComparison.InvariantCultureIgnoreCase)); if (elementTypeFromMetadata == null) { return(BadRequest($"Element type {elementType} not allowed for instance {instanceGuid}.")); } bool appLogic = elementTypeFromMetadata.AppLogic; Instance instance = await instanceService.GetInstance(app, org, instanceOwnerId, instanceGuid); if (instance == null) { return(NotFound($"Did not find instance {instance}")); } if (appLogic) { return(await CreateFormData(org, app, instance, elementType)); } else { return(await CreateBinaryData(org, app, instance, elementType, attachmentName)); } }
public async Task <ActionResult <ProcessState> > GetProcessState( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { try { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound()); } ProcessState processState = instance.Process; return(Ok(processState)); } catch (Exception exception) { _logger.LogError($"Failed to access process for {instanceOwnerPartyId}/{instanceGuid}"); return(ExceptionResponse(exception, $"Failed to access process for {instanceOwnerPartyId}/{instanceGuid}")); } }
private async Task EnrichResourceAttributes(XacmlContextRequest request) { XacmlContextAttributes resourceContextAttributes = request.GetResourceAttributes(); XacmlResourceAttributes resourceAttributes = GetResourceAttributeValues(resourceContextAttributes); bool resourceAttributeComplete = false; if (!string.IsNullOrEmpty(resourceAttributes.OrgValue) && !string.IsNullOrEmpty(resourceAttributes.AppValue) && !string.IsNullOrEmpty(resourceAttributes.InstanceValue) && !string.IsNullOrEmpty(resourceAttributes.ResourcePartyValue) && !string.IsNullOrEmpty(resourceAttributes.TaskValue)) { // The resource attributes are complete resourceAttributeComplete = true; } else if (!string.IsNullOrEmpty(resourceAttributes.OrgValue) && !string.IsNullOrEmpty(resourceAttributes.AppValue) && string.IsNullOrEmpty(resourceAttributes.InstanceValue) && !string.IsNullOrEmpty(resourceAttributes.ResourcePartyValue) && string.IsNullOrEmpty(resourceAttributes.TaskValue)) { // The resource attributes are complete resourceAttributeComplete = true; } if (!resourceAttributeComplete) { Instance instanceData = await _instanceService.GetInstance(resourceAttributes.AppValue, resourceAttributes.OrgValue, Convert.ToInt32(resourceAttributes.InstanceValue.Split('/')[0]), new Guid(resourceAttributes.InstanceValue.Split('/')[1])); if (instanceData != null) { AddIfValueDoesNotExist(resourceContextAttributes, XacmlRequestAttribute.OrgAttribute, resourceAttributes.OrgValue, instanceData.Org); string app = instanceData.AppId.Split("/")[1]; AddIfValueDoesNotExist(resourceContextAttributes, XacmlRequestAttribute.AppAttribute, resourceAttributes.AppValue, app); if (instanceData.Process?.CurrentTask != null) { AddIfValueDoesNotExist(resourceContextAttributes, XacmlRequestAttribute.TaskAttribute, resourceAttributes.TaskValue, instanceData.Process.CurrentTask.ElementId); } else if (instanceData.Process?.EndEvent != null) { AddIfValueDoesNotExist(resourceContextAttributes, XacmlRequestAttribute.EndEventAttribute, null, instanceData.Process.EndEvent); } AddIfValueDoesNotExist(resourceContextAttributes, XacmlRequestAttribute.PartyAttribute, resourceAttributes.ResourcePartyValue, instanceData.InstanceOwner.PartyId); resourceAttributes.ResourcePartyValue = instanceData.InstanceOwner.PartyId; } } await EnrichSubjectAttributes(request, resourceAttributes.ResourcePartyValue); }
public async Task <ActionResult> Get( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { try { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound()); } SelfLinkHelper.SetInstanceAppSelfLinks(instance, Request); return(Ok(instance)); } catch (Exception ex) { return(StatusCode(500, $"{ex.Message}")); } }
public async Task <ActionResult> Get( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid) { try { Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return(NotFound()); } SelfLinkHelper.SetInstanceAppSelfLinks(instance, Request); return(Ok(instance)); } catch (Exception exception) { return(ExceptionResponse(exception, $"Get instance {instanceOwnerPartyId}/{instanceGuid} failed")); } }
public static T As <T>(this IInstance instance) where T : class { return(instance.GetInstance() as T); }
private async Task EnrichResourceAttributes(XacmlContextRequest request) { string orgAttributeValue = string.Empty; string appAttributeValue = string.Empty; string instanceAttributeValue = string.Empty; string resourcePartyAttributeValue = string.Empty; string taskAttributeValue = string.Empty; string endEventAttribute = string.Empty; XacmlContextAttributes resourceContextAttributes = request.GetResourceAttributes(); foreach (XacmlAttribute attribute in resourceContextAttributes.Attributes) { if (attribute.AttributeId.OriginalString.Equals(OrgAttributeId)) { orgAttributeValue = attribute.AttributeValues.First().Value; } if (attribute.AttributeId.OriginalString.Equals(AppAttributeId)) { appAttributeValue = attribute.AttributeValues.First().Value; } if (attribute.AttributeId.OriginalString.Equals(InstanceAttributeId)) { instanceAttributeValue = attribute.AttributeValues.First().Value; } if (attribute.AttributeId.OriginalString.Equals(TaskAttributeId)) { taskAttributeValue = attribute.AttributeValues.First().Value; } if (attribute.AttributeId.OriginalString.Equals(PartyAttributeId)) { resourcePartyAttributeValue = attribute.AttributeValues.First().Value; } if (attribute.AttributeId.OriginalString.Equals(endEventAttribute)) { endEventAttribute = attribute.AttributeValues.First().Value; } } bool resourceAttributeComplete = false; if (!string.IsNullOrEmpty(orgAttributeValue) && !string.IsNullOrEmpty(appAttributeValue) && !string.IsNullOrEmpty(instanceAttributeValue) && !string.IsNullOrEmpty(resourcePartyAttributeValue) && (!string.IsNullOrEmpty(taskAttributeValue) || !string.IsNullOrEmpty(endEventAttribute))) { // The resource attributes are complete resourceAttributeComplete = true; } else if (!string.IsNullOrEmpty(orgAttributeValue) && !string.IsNullOrEmpty(appAttributeValue) && string.IsNullOrEmpty(instanceAttributeValue) && !string.IsNullOrEmpty(resourcePartyAttributeValue) && (!string.IsNullOrEmpty(taskAttributeValue) || !string.IsNullOrEmpty(endEventAttribute))) { // The resource attributes are complete resourceAttributeComplete = true; } if (!resourceAttributeComplete) { Instance instanceData = await _instanceService.GetInstance(appAttributeValue, orgAttributeValue, Convert.ToInt32(instanceAttributeValue.Split('/')[0]), new Guid(instanceAttributeValue.Split('/')[1])); if (string.IsNullOrEmpty(orgAttributeValue) && instanceData != null) { resourceContextAttributes.Attributes.Add(GetOrgAttribute(instanceData)); } if (string.IsNullOrEmpty(appAttributeValue) && instanceData != null) { resourceContextAttributes.Attributes.Add(GetAppAttribute(instanceData)); } if (string.IsNullOrEmpty(taskAttributeValue) && instanceData?.Process?.CurrentTask != null) { resourceContextAttributes.Attributes.Add(GetProcessElementAttribute(instanceData)); } else if (string.IsNullOrEmpty(endEventAttribute) && instanceData?.Process?.EndEvent != null) { resourceContextAttributes.Attributes.Add(GetEndEventAttribute(instanceData)); } if (string.IsNullOrEmpty(resourcePartyAttributeValue) && instanceData != null) { resourceContextAttributes.Attributes.Add(GetPartyAttribute(instanceData)); } if (instanceData != null) { resourcePartyAttributeValue = instanceData.InstanceOwner.PartyId; } } await EnrichSubjectAttributes(request, resourcePartyAttributeValue); }
public async Task<ActionResult> Create( [FromRoute] string org, [FromRoute] string app, [FromRoute] int instanceOwnerPartyId, [FromRoute] Guid instanceGuid, [FromQuery] string dataType) { if (string.IsNullOrWhiteSpace(dataType)) { return BadRequest("Element type must be provided."); } /* The Body of the request is read much later when it has been made sure it is worth it. */ try { Application application = _appResourcesService.GetApplication(); if (application == null) { return NotFound($"AppId {org}/{app} was not found"); } DataType dataTypeFromMetadata = application.DataTypes.FirstOrDefault(e => e.Id.Equals(dataType, StringComparison.InvariantCultureIgnoreCase)); if (dataTypeFromMetadata == null) { return BadRequest($"Element type {dataType} not allowed for instance {instanceOwnerPartyId}/{instanceGuid}."); } if (!IsValidContributer(dataTypeFromMetadata, User)) { return Forbid(); } bool appLogic = dataTypeFromMetadata.AppLogic != null; Instance instance = await _instanceService.GetInstance(app, org, instanceOwnerPartyId, instanceGuid); if (instance == null) { return NotFound($"Did not find instance {instance}"); } if (!InstanceIsActive(instance)) { return Conflict($"Cannot upload data for archived or deleted instance {instanceOwnerPartyId}/{instanceGuid}"); } if (appLogic) { return await CreateAppModelData(org, app, instance, dataType); } else { if (!CompliesWithDataRestrictions(dataTypeFromMetadata, out string errorMessage)) { return BadRequest($"Invalid data provided. Error: {errorMessage}"); } return await CreateBinaryData(org, app, instance, dataType); } } catch (PlatformHttpException e) { return HandlePlatformHttpException(e, $"Cannot create data element of {dataType} for {instanceOwnerPartyId}/{instanceGuid}"); } }
public async Task <IActionResult> Gindex(string org, string service, int partyId, Guid instanceId) { // Getting the Service Specific Implementation contained in external DLL migrated from TUL IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false); // Create and populate the RequestContext object and make it available for the service implementation so // service developer can implement logic based on information about the request and the user performing // the request RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, instanceId); requestContext.UserContext = await _userHelper.GetUserContext(HttpContext); requestContext.Party = requestContext.UserContext.Party; // Get the serviceContext containing all metadata about current service ServiceContext serviceContext = _execution.GetServiceContext(org, service, false); // Assign data to the ViewBag so it is available to the service views or service implementation ViewBag.ServiceContext = serviceContext; ViewBag.RequestContext = requestContext; ViewBag.Org = org; ViewBag.Service = service; ViewBag.FormID = instanceId; // Assign the RequestContext to the serviceImplementation so // service developer can use the information in any of the service events that is called serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState); // Set the platform services to the ServiceImplementation so the AltinnCore service can take // use of the plattform services serviceImplementation.SetPlatformServices(_platformSI); ViewBag.PlatformServices = _platformSI; Instance instance = await _instance.GetInstance(service, org, requestContext.UserContext.PartyId, instanceId); Guid dataId = Guid.Parse(instance.Data.Find(m => m.ElementType.Equals(FORM_ID)).Id); // Getting the Form Data from datastore object serviceModel = this._data.GetFormData( instanceId, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.PartyId, dataId); // Assing the populated service model to the service implementation serviceImplementation.SetServiceModel(serviceModel); // ServiceEvent 1: HandleGetDataEvent // Runs the event where the service developer can implement functionality to retrieve data from internal/external sources // based on the data in the service model await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.DataRetrieval); // ServiceEvent 2: HandleCalculationEvent // Perform Calculation defined by the service developer await serviceImplementation.RunServiceEvent(AltinnCore.ServiceLibrary.Enums.ServiceEventType.Calculation); return(Ok(serviceModel)); }
public async Task <IActionResult> CompleteAndSendIn(string org, string service, int partyId, Guid instanceGuid, string view) { // Dependency Injection: Getting the Service Specific Implementation based on the service parameter data store // Will compile code and load DLL in to memory for AltinnCore IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(org, service, false); // Get the serviceContext containing all metadata about current service ServiceContext serviceContext = _execution.GetServiceContext(org, service, false); // Create and populate the RequestContext object and make it available for the service implementation so // service developer can implement logic based on information about the request and the user performing // the request RequestContext requestContext = await PopulateRequestContext(instanceGuid); serviceImplementation.SetPlatformServices(_platformSI); // Assign data to the ViewBag so it is available to the service views or service implementation PopulateViewBag(org, service, instanceGuid, 0, requestContext, serviceContext, _platformSI); // Getting the Form Data Instance instance = await _instance.GetInstance(service, org, requestContext.UserContext.PartyId, instanceGuid); Guid.TryParse(instance.Data.Find(m => m.ElementType == FORM_ID).Id, out Guid dataId); object serviceModel = _data.GetFormData(instanceGuid, serviceImplementation.GetServiceModelType(), org, service, requestContext.UserContext.PartyId, dataId); serviceImplementation.SetServiceModel(serviceModel); ViewBag.FormID = instanceGuid; ViewBag.ServiceContext = serviceContext; serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState); await serviceImplementation.RunServiceEvent(ServiceEventType.Validation); ApiResult apiResult = new ApiResult(); if (ModelState.IsValid) { ServiceState currentState = _workflowSI.MoveServiceForwardInWorkflow(instanceGuid, org, service, requestContext.UserContext.PartyId); if (currentState.State == WorkflowStep.Archived) { await _instance.ArchiveInstance(serviceModel, serviceImplementation.GetServiceModelType(), service, org, requestContext.UserContext.PartyId, instanceGuid); apiResult.NextState = currentState.State; } // Create and store the instance submitted event InstanceEvent instanceEvent = new InstanceEvent { AuthenticationLevel = requestContext.UserContext.AuthenticationLevel, EventType = InstanceEventType.Submited.ToString(), InstanceId = instance.Id, InstanceOwnerId = instance.InstanceOwnerId.ToString(), UserId = requestContext.UserContext.UserId, WorkflowStep = instance.Process.CurrentTask, }; await _event.SaveInstanceEvent(instanceEvent, org, service); } ModelHelper.MapModelStateToApiResult(ModelState, apiResult, serviceContext); if (apiResult.Status.Equals(ApiStatusType.ContainsError)) { Response.StatusCode = 202; } else { Response.StatusCode = 200; } return(new ObjectResult(apiResult)); }