private AccessRuleViolationAction ParseViolationAction <TAccessRule>(IEntityAccessRestrictable <TAccessRule> dbEntity) where TAccessRule : IEntityAccessRule { var violationAction = EnumParser.ParseOrNull <AccessRuleViolationAction>(dbEntity.AccessRuleViolationActionId); if (violationAction == null) { _logger.LogWarning( "AccessRuleViolationAction of value {AccessRuleViolationAction} could not be parsed on rule type {TAccessRule}.", dbEntity.AccessRuleViolationActionId, typeof(TAccessRule).Name ); } return(violationAction ?? AccessRuleViolationAction.Error); }
public EntityAccessRuleSet Map <TAccessRule>(IEntityAccessRestrictable <TAccessRule> entity) where TAccessRule : IEntityAccessRule { if (entity == null) { throw new ArgumentNullException(nameof(entity)); } MissingIncludeException.ThrowIfNull(entity, e => e.AccessRules); if (!entity.AccessRules.Any()) { // no rules, so return null rather than an empty ruleset return(null); } var violationAction = EnumParser.ParseOrNull <AccessRuleViolationAction>(entity.AccessRuleViolationActionId); if (violationAction == null) { _logger.LogWarning( "AccessRuleViolationAction of value {AccessRuleViolationAction} could not be parsed on rule type {TAccessRule}.", entity.AccessRuleViolationActionId, typeof(TAccessRule).Name ); } var result = new EntityAccessRuleSet() { ViolationAction = violationAction ?? AccessRuleViolationAction.Error }; if (!string.IsNullOrWhiteSpace(entity.UserAreaCodeForSignInRedirect)) { var userArea = _userAreaDefinitionRepository.GetRequiredByCode(entity.UserAreaCodeForSignInRedirect); result.UserAreaCodeForSignInRedirect = userArea.UserAreaCode; } result.AccessRules = entity .AccessRules .OrderByDefault() .Select(r => MapAccessRule(r)) .ToArray(); return(result); }
public async Task <UpdatePageDirectoryAccessRuleSetCommand> ExecuteAsync(GetPatchableCommandByIdQuery <UpdatePageDirectoryAccessRuleSetCommand> query, IExecutionContext executionContext) { var dbPageDirectory = await _dbContext .PageDirectories .AsNoTracking() .Include(r => r.AccessRules) .FilterById(query.Id) .SingleOrDefaultAsync(); if (dbPageDirectory == null) { return(null); } var violationAction = EnumParser.ParseOrNull <AccessRuleViolationAction>(dbPageDirectory.AccessRuleViolationActionId); if (!violationAction.HasValue) { throw new InvalidOperationException($"{nameof(AccessRuleViolationAction)} of value {dbPageDirectory.AccessRuleViolationActionId} could not be parsed on a page directory with an id of {dbPageDirectory.PageDirectoryId}."); } var command = new UpdatePageDirectoryAccessRuleSetCommand() { PageDirectoryId = dbPageDirectory.PageDirectoryId, UserAreaCodeForSignInRedirect = dbPageDirectory.UserAreaCodeForSignInRedirect, ViolationAction = violationAction.Value }; command.AccessRules = dbPageDirectory .AccessRules .Select(r => new UpdatePageDirectoryAccessRuleSetCommand.AddOrUpdatePageDirectoryAccessRuleCommand() { PageDirectoryAccessRuleId = r.PageDirectoryAccessRuleId, UserAreaCode = r.UserAreaCode, RoleId = r.RoleId }) .ToList(); return(command); }
public void ParseOrNull_WhenInvalidInt_ReturnsNull() { var result = EnumParser.ParseOrNull <TestEnum>(13); result.Should().BeNull(); }
public void ParseOrNull_WhenValidInt_Parses() { var result = EnumParser.ParseOrNull <TestEnum>(5); result.Should().Be(TestEnum.Value5); }
public void ParseOrNull_WhenInvalidString_ReturnsNull(string value) { var result = EnumParser.ParseOrNull <TestEnum>(value); result.Should().BeNull(); }