示例#1
0
        /// <inheritdoc/>
        public async Task PrefillDataModel(PrefillContext prefillContext, object dataModel)
        {
            string jsonConfig = _appResourcesService.GetPrefillJson(prefillContext.Org, prefillContext.App);

            if (jsonConfig == null || jsonConfig == string.Empty)
            {
                return;
            }

            JObject prefillConfiguration = JObject.Parse(jsonConfig);
            JToken  allowOverwriteToken  = prefillConfiguration.SelectToken(ALLOW_OVERWRITE_KEY);

            if (allowOverwriteToken != null)
            {
                allowOverwrite = allowOverwriteToken.ToObject <bool>();
            }

            // Prefill from user profile
            JToken profilePrefill = prefillConfiguration.SelectToken(USER_PROFILE_KEY);
            Dictionary <string, string> userProfileDict;

            if (profilePrefill != null)
            {
                userProfileDict = profilePrefill.ToObject <Dictionary <string, string> >();
                if (userProfileDict.Count > 0)
                {
                    UserProfile userProfile = await _profile.GetUserProfile(prefillContext.UserId);

                    if (userProfile != null)
                    {
                        JObject userProfileJsonObject = JObject.FromObject(userProfile);
                        _logger.LogInformation($"Started prefill from {USER_PROFILE_KEY}");
                        LoopThroughDictionaryAndAssignValuesToDataModel(userProfileDict, userProfileJsonObject, dataModel);
                    }
                    else
                    {
                        string errorMessage = $"Could not  prefill from {USER_PROFILE_KEY}, user profile is not defined.";
                        _logger.LogError(errorMessage);
                        throw new Exception(errorMessage);
                    }
                }
            }

            // Prefill from ER (enhetsregisteret)
            JToken enhetsregisteret = prefillConfiguration.SelectToken(ER_KEY);
            Dictionary <string, string> enhetsregisterPrefill;

            if (enhetsregisteret != null)
            {
                enhetsregisterPrefill = enhetsregisteret.ToObject <Dictionary <string, string> >();
                if (enhetsregisterPrefill.Count > 0)
                {
                    if (prefillContext.Organization != null)
                    {
                        JObject orgJsonObject = JObject.FromObject(prefillContext.Organization);
                        _logger.LogInformation($"Started prefill from {ER_KEY}");
                        LoopThroughDictionaryAndAssignValuesToDataModel(enhetsregisterPrefill, orgJsonObject, dataModel);
                    }
                    else
                    {
                        string errorMessage = $"Could not  prefill from {ER_KEY}, organisation is not defined.";
                        _logger.LogError(errorMessage);
                        throw new Exception(errorMessage);
                    }
                }
            }

            // Prefill from DSF (det sentrale folkeregisteret)
            JToken folkeregisteret = prefillConfiguration.SelectToken(DSF_KEY);
            Dictionary <string, string> folkeregisterPrefill;

            if (folkeregisteret != null)
            {
                folkeregisterPrefill = folkeregisteret.ToObject <Dictionary <string, string> >();
                if (folkeregisterPrefill.Count > 0)
                {
                    if (prefillContext.Person != null)
                    {
                        JObject personJsonObject = JObject.FromObject(prefillContext.Person);
                        _logger.LogInformation($"Started prefill from {DSF_KEY}");
                        LoopThroughDictionaryAndAssignValuesToDataModel(folkeregisterPrefill, personJsonObject, dataModel);
                    }
                    else
                    {
                        string errorMessage = $"Could not  prefill from {DSF_KEY}, person is not defined.";
                        _logger.LogError(errorMessage);
                        throw new Exception(errorMessage);
                    }
                }
            }
        }
示例#2
0
        public async Task <dynamic> InstantiateApp(StartServiceModel startServiceModel)
        {
            // Dependency Injection: Getting the Service Specific Implementation based on the app parameter data store
            // Will compile code and load DLL in to memory for AltinnCore
            bool startService = true;
            IServiceImplementation serviceImplementation = _execution.GetServiceImplementation(startServiceModel.Org, startServiceModel.Service, startService);

            // Get the service context containing metadata about the app
            ServiceContext serviceContext = _execution.GetServiceContext(startServiceModel.Org, startServiceModel.Service, startService);

            // Create and populate the RequestContext object and make it available for the service implementation so
            // app developer can implement logic based on information about the request and the user performing
            // the request
            RequestContext requestContext = RequestHelper.GetRequestContext(Request.Query, Guid.Empty);

            requestContext.UserContext = await _userHelper.GetUserContext(HttpContext);

            // Populate the reportee information
            requestContext.UserContext.Party = await _register.GetParty(startServiceModel.PartyId);

            requestContext.Party = requestContext.UserContext.Party;

            // Checks if the reportee is allowed to instantiate the application
            Application application = await _application.GetApplication(startServiceModel.Org, startServiceModel.Service);

            if (application == null || (application != null && !InstantiationHelper.IsPartyAllowedToInstantiate(requestContext.UserContext.Party, application.PartyTypesAllowed)))
            {
                return(new StatusCodeResult(403));
            }

            // Create platform service and assign to service implementation making it possible for the service implementation
            // to use platform services. Also make it available in ViewBag so it can be used from Views
            serviceImplementation.SetPlatformServices(_platformSI);

            // Assign the different context information to the service implementation making it possible for
            // the app developer to take use of this information
            serviceImplementation.SetContext(requestContext, serviceContext, null, ModelState);

            object serviceModel = null;

            if (!string.IsNullOrEmpty(startServiceModel.PrefillKey))
            {
                _form.GetPrefill(
                    startServiceModel.Org,
                    startServiceModel.Service,
                    serviceImplementation.GetServiceModelType(),
                    startServiceModel.PartyId,
                    startServiceModel.PrefillKey);
            }

            if (serviceModel == null)
            {
                // If the service model was not loaded from prefill.
                serviceModel = serviceImplementation.CreateNewServiceModel();
            }

            // Assign service model to the implementation
            serviceImplementation.SetServiceModel(serviceModel);

            // Run prefill
            PrefillContext prefillContext = new PrefillContext
            {
                Organization = requestContext.UserContext.Party.Organization,
                Person       = requestContext.UserContext.Party.Person,
                UserId       = requestContext.UserContext.UserId,
                Org          = startServiceModel.Org,
                App          = startServiceModel.Service,
            };
            await _prefill.PrefillDataModel(prefillContext, serviceModel);

            // Run Instansiation event
            await serviceImplementation.RunServiceEvent(ServiceEventType.Instantiation);

            // Run validate Instansiation event where
            await serviceImplementation.RunServiceEvent(ServiceEventType.ValidateInstantiation);

            // If ValidateInstansiation event has not added any errors the new form is saved and user is redirercted to the correct
            if (ModelState.IsValid)
            {
                if (serviceContext.WorkFlow.Any() && serviceContext.WorkFlow[0].StepType.Equals(StepType.Lookup))
                {
                    return(JsonConvert.SerializeObject(
                               new
                    {
                        org = startServiceModel.Org,
                        service = startServiceModel.Service
                    }));
                }

                int instanceOwnerId = requestContext.UserContext.PartyId;

                // Create a new instance document
                Instance instance = await _instance.InstantiateInstance(startServiceModel, serviceModel, serviceImplementation);

                // Create and store the instance created event
                InstanceEvent instanceEvent = new InstanceEvent
                {
                    AuthenticationLevel = requestContext.UserContext.AuthenticationLevel,
                    EventType           = InstanceEventType.Created.ToString(),
                    InstanceId          = instance.Id,
                    InstanceOwnerId     = instanceOwnerId.ToString(),
                    UserId      = requestContext.UserContext.UserId,
                    ProcessInfo = instance.Process,
                };

                Enum.TryParse <WorkflowStep>(instance.Process.CurrentTask.Name, out WorkflowStep currentStep);

                return(JsonConvert.SerializeObject(
                           new
                {
                    instanceId = instance.Id,
                }));
            }

            startServiceModel.PartyList = _authorization.GetPartyList(requestContext.UserContext.UserId)
                                          .Select(x => new SelectListItem
            {
                Text  = (x.PartyTypeName == PartyType.Person) ? x.SSN + " " + x.Name : x.OrgNumber + " " + x.Name,
                Value = x.PartyId.ToString(),
            }).ToList();

            HttpContext.Response.Cookies.Append(_generalSettings.GetAltinnPartyCookieName, startServiceModel.PartyId.ToString());

            return(JsonConvert.SerializeObject(
                       new
            {
                redirectUrl = View(startServiceModel),
            }));
        }