private async Task ApplyFunctionsToDefaultRole(SecurityContractDefaultConfigurationRole defaultRole, RoleModel defaultRoleToApply, Guid updatedById)
        {
            defaultRoleToApply.RoleFunctions = new List <RoleFunctionModel>();

            if (defaultRole.Functions != null && defaultRole.Functions.Count > 0)
            {
                foreach (var functionToAdd in defaultRole.Functions)
                {
                    logger.Debug($"Attempting to add function: '{functionToAdd}' to role '{defaultRole.Name}'.");
                    // Ensure that the function exists.
                    var existingFunction = await functionRepository.GetByNameAsync(functionToAdd);

                    if (existingFunction == null)
                    {
                        logger.Warn($"Function '{functionToAdd}' does not exists. Not assinging it to role '{defaultRole.Name}'.");
                        break;
                    }

                    logger.Debug($"Function '{functionToAdd}' exists and is being assigned to role '{defaultRole.Name}'.");
                    defaultRoleToApply.RoleFunctions.Add(new RoleFunctionModel
                    {
                        Role      = defaultRoleToApply,
                        Function  = existingFunction,
                        ChangedBy = updatedById
                    });
                }
            }
        }
        private async Task ApplyParentRolesToDefaultRole(SecurityContractDefaultConfigurationRole defaultRole, RoleModel defaultRoleToApply, Guid updatedById)
        {
            defaultRoleToApply.ChildRoles = new List <RoleRoleModel>();

            if (defaultRole.Roles != null && defaultRole.Roles.Count > 0)
            {
                foreach (var roleToAdd in defaultRole.Roles)
                {
                    logger.Debug($"Attempting to add child role: '{roleToAdd}' to role '{defaultRole.Name}'.");

                    // Ensure that the role exists.
                    var existingchildRole = await roleRepository.GetByNameAsync(roleToAdd);

                    if (existingchildRole == null)
                    {
                        logger.Warn($"Child role '{existingchildRole}' does not exists. Not assinging it to role '{defaultRole.Name}'.");
                        break;
                    }

                    logger.Debug($"Child role '{existingchildRole}' exists and is being assigned to role '{defaultRole.Name}'.");
                    defaultRoleToApply.ChildRoles.Add(new RoleRoleModel
                    {
                        ParentRole = defaultRoleToApply,
                        ChildRole  = existingchildRole,
                        ChangedBy  = updatedById
                    });
                }
            }
        }
        private async Task ApplyDefaultRole(SecurityContractDefaultConfigurationRole defaultRole, Guid updatedById)
        {
            var  defaultRoleToApply = new RoleModel();
            bool roleIsNew          = false;

            logger.Debug($"Applying default role: '{defaultRole.Name}'");
            var existingRole = await roleRepository.GetByNameAsync(defaultRole.Name);

            if (existingRole != null)
            {
                logger.Debug($"Default role: '{defaultRole.Name}' already exist. Updating it.");
                defaultRoleToApply = existingRole;
            }
            else
            {
                logger.Debug($"Default role: '{defaultRole.Name}' does not exist. Creating it.");
                roleIsNew = true;
            }

            // Overwrite any assigned functions with the new list. If the list is empty, we still want to clear any assigned functions.
            defaultRoleToApply.Name        = defaultRole.Name;
            defaultRoleToApply.Description = defaultRole.Description;
            defaultRoleToApply.ChangedBy   = updatedById;

            await ApplyFunctionsToDefaultRole(defaultRole, defaultRoleToApply, updatedById);
            await ApplyParentRolesToDefaultRole(defaultRole, defaultRoleToApply, updatedById);

            if (roleIsNew)
            {
                logger.Debug($"Persisting new Role into the database: '{defaultRole.Name}'.");
                await roleRepository.CreateAsync(defaultRoleToApply);
            }
            else
            {
                logger.Debug($"Persisting updated exsiting Role into the database: '{defaultRole.Name}'.");
                await roleRepository.UpdateAsync(defaultRoleToApply);
            }
        }
        private SecurityContractDefaultConfigurationRole RetrieveIndividualDefaultConfigRole(RoleModel role)
        {
            logger.Debug($"Retrieving default configuration contract definition for Role [{role.Name}].");

            var contractRole = new SecurityContractDefaultConfigurationRole()
            {
                Name        = role.Name,
                Description = role.Description,
                Functions   = new List <string>(),
                Roles       = new List <string>()
            };

            foreach (var function in role.RoleFunctions.OrderBy(o => o.Function.SysPeriod.LowerBound))
            {
                contractRole.Functions.Add(function.Function.Name);
            }

            foreach (var childRole in role.ChildRoles.OrderBy(o => o.ChildRole.SysPeriod.LowerBound))
            {
                contractRole.Roles.Add(childRole.ChildRole.Name);
            }

            return(contractRole);
        }