private async Task ApplyIndividualDefaultApplication(SecurityContractDefaultConfigurationApplication defaultApplication, Guid updatedById) { // Ensure that the application actually exists. Obtain all the application function relations too, as they will be used for validation. var application = await applicationRepository.GetByNameAsync(defaultApplication.Name); if (application == null) { logger.Warn($"Application '{defaultApplication.Name}' does not exist! Skipping and ignoring default application configuration for this application."); return; } if (defaultApplication.Functions == null || defaultApplication.Functions.Count == 0) { logger.Warn($"Default application '{defaultApplication.Name}' has no 'functions' configuration. Ignoring default configuration for this application."); return; } // Use the currrent state of the assigned application functions to determine which ones are potentially no longer in the YAML. await DetectFunctionsRemovedFromSecurityContractDefaultsAndRemoveFromApplication(application, defaultApplication.Functions); // Reset the state of the application functions, as the security contract declares the desired state and we have already used to the historic state // to clear any functions that don't appear within the security contract. application.Functions = new List <FunctionModel>(); application.ChangedBy = updatedById; foreach (var defaultFunction in defaultApplication.Functions) { if (defaultFunction.Permissions == null || defaultFunction.Permissions.Count == 0) { break; } var functionModelToAdd = new FunctionModel(); // check to see if there is an existing function. var existingFunction = await functionRepository.GetByNameAsync(defaultFunction.Name); if (existingFunction != null) { functionModelToAdd = existingFunction; } functionModelToAdd.Application = application; functionModelToAdd.Description = defaultFunction.Description; functionModelToAdd.Name = defaultFunction.Name; functionModelToAdd.ChangedBy = updatedById; // Clear the current permissions assigned to the function as they are to be re-created. functionModelToAdd.FunctionPermissions = new List <FunctionPermissionModel>(); AddPermissionsToFunctionsEnsuringTheyExistsAndAreAssigedToTheApplication(application, defaultFunction, defaultApplication, functionModelToAdd, updatedById); } // Update the application with its new application function state. await applicationRepository.Update(application); }
public async Task <Function> CreateAsync(FunctionSubmit functionSubmit, Guid createdByGuid) { // Start transactions to allow complete rollback in case of an error InitSharedTransaction(); try { FunctionModel existingFunction = await functionRepository.GetByNameAsync(functionSubmit.Name); if (existingFunction != null) { throw new ItemNotProcessableException($"Function with Name '{functionSubmit.Name}' already exist."); } var function = new FunctionModel { Name = functionSubmit.Name, Description = functionSubmit.Description, FunctionPermissions = new List <FunctionPermissionModel>(), ChangedBy = createdByGuid }; await CheckForApplicationAndAssignToFunctionIfExists(function, functionSubmit); await CheckForSubRealmAndAssignToFunctionIfExists(function, functionSubmit); await CheckThatPermissionsExistAndAssignToFunction(function, functionSubmit); // All successful CommitTransaction(); return(mapper.Map <Function>(await functionRepository.CreateAsync(function))); } catch { RollbackTransaction(); throw; } }
public async Task <Function> UpdateAsync(FunctionSubmit functionSubmit, Guid updatedByGuid) { // Start transactions to allow complete rollback in case of an error BeginAllTransactions(); try { var function = await functionRepository.GetByIdAsync(functionSubmit.Uuid); if (function == null) { throw new ItemNotFoundException($"Function {functionSubmit.Uuid} not found!"); } if (function.Name != functionSubmit.Name) { // Confirm the new name is available var checkExistingNameModel = await functionRepository.GetByNameAsync(functionSubmit.Name); if (checkExistingNameModel != null) { throw new ItemNotProcessableException($"Function with name '{functionSubmit.Name}' already exists."); } } function.Name = functionSubmit.Name; function.Description = functionSubmit.Description; function.FunctionPermissions = new List <FunctionPermissionModel>(); function.ChangedBy = updatedByGuid; await CheckForApplicationAndAssignToFunctionIfExists(function, functionSubmit); await CheckThatPermissionsExistAndAssignToFunction(function, functionSubmit); // All successful CommitAllTransactions(); return(mapper.Map <Function>(await functionRepository.UpdateAsync(function))); } catch (Exception ex) { logger.Error(ex); RollbackAllTransactions(); throw; } }