Skip to content

Commit

Permalink
Merge pull request #255 from 1iveowl/main
Browse files Browse the repository at this point in the history
Leverage C# 12 language improvements
  • Loading branch information
1iveowl authored Dec 27, 2023
2 parents 8405fa5 + 8f6026a commit 680f52b
Show file tree
Hide file tree
Showing 36 changed files with 101 additions and 261 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public class NewTenantRequest

internal Tenant ToTenant()
{
Tenant tenant = new Tenant()
Tenant tenant = new()
{
Name = Name,
Route = Route,
Expand Down
17 changes: 5 additions & 12 deletions src/Saas.Admin/Saas.Admin.Service/Controllers/TenantDTO.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public TenantDTO(Tenant tenant)

public Tenant ToTenant()
{
Tenant tenant = new Tenant()
Tenant tenant = new()
{
Id = Id,
Name = Name,
Expand Down Expand Up @@ -68,16 +68,9 @@ public void CopyTo(Tenant target)
public string? Version { get; set; }
}

public class TenantDTOPage
public class TenantDTOPage(IEnumerable<TenantDTO> tenants, int totalCount, int startIndex)
{
public TenantDTOPage(IEnumerable<TenantDTO> tenants, int totalCount, int startIndex)
{
Tenants = tenants;
TotalCount = totalCount;
StartIndex = startIndex;
}

public IEnumerable<TenantDTO> Tenants { get; }
public int TotalCount { get; }
public int StartIndex { get; }
public IEnumerable<TenantDTO> Tenants { get; } = tenants;
public int TotalCount { get; } = totalCount;
public int StartIndex { get; } = startIndex;
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public TenantInfoDTO(Tenant? tenant)

public Tenant ToTenant()
{
Tenant tenant = new Tenant()
Tenant tenant = new()
{
Id = Id,
Name = Name,
Expand Down
1 change: 1 addition & 0 deletions src/Saas.Admin/Saas.Admin.Service/Data/Tenant.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public class Tenant
public int CategoryId { get; set; }
public string CreatorEmail { get; set; } = string.Empty;
public DateTime? CreatedTime { get; set; }

[Timestamp]
public byte[]? ConcurrencyToken { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public ItemNotFoundExcepton(string? message) : base(message)
public ItemNotFoundExcepton(string? message, Exception? innerException) : base(message, innerException)
{
}

protected ItemNotFoundExcepton(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
3 changes: 3 additions & 0 deletions src/Saas.Admin/Saas.Admin.Service/Services/ITenantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ public interface ITenantService
Task<IEnumerable<TenantDTO>> GetAllTenantsAsync();

Task<TenantDTO> GetTenantAsync(Guid tenantId);

Task<IEnumerable<TenantDTO>> GetTenantsByIdAsync(IEnumerable<Guid> ids);

Task<TenantDTO> AddTenantAsync(NewTenantRequest newTenantRequest, Guid adminId);
Expand All @@ -16,6 +17,8 @@ public interface ITenantService
Task DeleteTenantAsync(Guid tenantId);

Task<TenantInfoDTO> GetTenantInfoByRouteAsync(string route);

Task<bool> TenantExistsAsync(Guid tenantId);

Task<bool> CheckPathExists(string path);
}
15 changes: 4 additions & 11 deletions src/Saas.Admin/Saas.Admin.Service/Services/TenantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,11 @@

namespace Saas.Admin.Service.Services;

public class TenantService : ITenantService
public class TenantService(TenantsContext tenantContext, IPermissionsServiceClient permissionService, ILogger<TenantService> logger) : ITenantService
{
private readonly TenantsContext _context;
private readonly IPermissionsServiceClient _permissionService;
private readonly ILogger _logger;

public TenantService(TenantsContext tenantContext, IPermissionsServiceClient permissionService, ILogger<TenantService> logger)
{
_context = tenantContext;
_permissionService = permissionService;
_logger = logger;
}
private readonly TenantsContext _context = tenantContext;
private readonly IPermissionsServiceClient _permissionService = permissionService;
private readonly ILogger _logger = logger;

public async Task<IEnumerable<TenantDTO>> GetAllTenantsAsync()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,9 @@ namespace Saas.Admin.Service.Utilities;

// This is to use key name prefixes to only load in the secrets that pertain to this microservice
// https://docs.microsoft.com/en-us/aspnet/core/security/key-vault-configuration?view=aspnetcore-6.0#use-a-key-name-prefix
public class CustomPrefixKeyVaultSecretManager : KeyVaultSecretManager
public class CustomPrefixKeyVaultSecretManager(string prefix) : KeyVaultSecretManager
{
private readonly string _prefix;

public CustomPrefixKeyVaultSecretManager(string prefix)
{
_prefix = $"{prefix}-";
}
private readonly string _prefix = $"{prefix}-";

public override bool Load(SecretProperties properties)
{
Expand Down
4 changes: 2 additions & 2 deletions src/Saas.Application/Saas.Application.Web/AppHttpContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public static IServiceProvider? Services
}
set
{
if (services != null)
if (services is not null)
{
throw new Exception("Can't set once a value has already been set.");
}
Expand All @@ -31,7 +31,7 @@ public static HttpContext? Current
{
get
{
if(services != null)
if(services is not null)
{
IHttpContextAccessor? httpContextAccessor = services.GetService(typeof(IHttpContextAccessor)) as IHttpContextAccessor;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,10 @@ namespace Saas.Permissions.Service.Controllers;

[Route("api/[controller]")]
[ApiController]
public class CustomClaimsController : ControllerBase
public class CustomClaimsController(IPermissionsService permissionsService, ILogger<CustomClaimsController> logger) : ControllerBase
{
private readonly IPermissionsService _permissionsService;
private readonly ILogger _logger;

public CustomClaimsController(IPermissionsService permissionsService, ILogger<CustomClaimsController> logger)
{
_permissionsService = permissionsService;
_logger = logger;
}
private readonly IPermissionsService _permissionsService = permissionsService;
private readonly ILogger _logger = logger;

// This is the endpoint that is called by Azure AD B2C to get alle the custom claims defined for a specific user.
[HttpPost("permissions")]
Expand Down Expand Up @@ -74,7 +68,7 @@ public async Task<IActionResult> Roles(ClaimsRequest request)

RolesClaimResponse response = new()
{
Roles = Array.Empty<string>()
Roles = []
};

await Task.CompletedTask;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,14 @@ namespace Saas.Permissions.Service.Controllers;

[Route("api/[controller]")]
[ApiController]
public class PermissionsController : ControllerBase
public class PermissionsController(
IPermissionsService permissionsService,
IGraphAPIService graphAPIService, ILogger<PermissionsController> logger) : ControllerBase
{
private readonly ILogger _logger;
private readonly ILogger _logger = logger;

private readonly IPermissionsService _permissionsService;
private readonly IGraphAPIService _graphAPIService;

public PermissionsController(IPermissionsService permissionsService, IGraphAPIService graphAPIService, ILogger<PermissionsController> logger)
{
_permissionsService = permissionsService;
_graphAPIService = graphAPIService;
_logger = logger;
}
private readonly IPermissionsService _permissionsService = permissionsService;
private readonly IGraphAPIService _graphAPIService = graphAPIService;

[HttpGet]
[Produces("application/json")]
Expand All @@ -38,7 +33,7 @@ public async Task<ActionResult<IEnumerable<User>>> GetTenantUsers(Guid tenantId)
}
catch (Exception ex)
{
_logger.LogError("Unable to get Tenant Users.", ex);
_logger.LogError("Unable to get Tenant Users: {ex}", ex);
throw;
}

Expand Down Expand Up @@ -67,7 +62,7 @@ public async Task<ActionResult<IEnumerable<User>>> GetTenantUsers(Guid tenantId)
}
catch (Exception ex)
{
_logger.LogError("Unhandled exception", ex);
_logger.LogError("Unhandled exception: {ex}", ex);
throw;
}
}
Expand Down Expand Up @@ -229,7 +224,5 @@ public async Task<IActionResult> GetUsersByIds(string[] userIds)
throw;
}
}


}

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

namespace Saas.Permissions.Service.Data.Context;

public class SaasPermissionsContext : DbContext
public class SaasPermissionsContext(DbContextOptions<SaasPermissionsContext> options) : DbContext(options)
{
public SaasPermissionsContext(DbContextOptions<SaasPermissionsContext> options) : base(options)
{

}

public DbSet<SaasPermission> SaasPermissions { get; set; }
public DbSet<TenantPermission> TenantPermissions { get; set; }
public DbSet<UserPermission> UserPermissions { get; set; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public ItemAlreadyExistsException(string? message) : base(message)
public ItemAlreadyExistsException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected ItemAlreadyExistsException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public ItemNotFoundException(string? message) : base(message)
public ItemNotFoundException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected ItemNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,4 @@ public UserNotFoundException(string? message) : base(message)
public UserNotFoundException(string? message, Exception? innerException) : base(message, innerException)
{
}

protected UserNotFoundException(SerializationInfo info, StreamingContext context) : base(info, context)
{
}
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,12 @@

namespace Saas.Permissions.Service.Middleware;

public class ApiKeyMiddleware {

private readonly RequestDelegate _next;
public class ApiKeyMiddleware(IOptions<PermissionsApiOptions> permissionOptions, RequestDelegate next)
{
private readonly RequestDelegate _next = next;
private const string API_KEY = "x-api-key";
private readonly PermissionsApiOptions _permissionOptions;

public ApiKeyMiddleware(IOptions<PermissionsApiOptions> permissionOptions, RequestDelegate next) {
_next = next;
_permissionOptions = permissionOptions.Value;
}
private readonly PermissionsApiOptions _permissionOptions = permissionOptions.Value;

public async Task InvokeAsync(HttpContext context) {

if (!context.Request.Headers.TryGetValue(API_KEY, out var extractedApiKey)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Saas.Permissions.Service.Models;


public record PermissionsClaimResponse
{
public string[]? Permissions { get; init; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
namespace Saas.Permissions.Service.Models;


public record RolesClaimResponse
{
public string[]? Roles { get; set; }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
namespace Saas.Permissions.Service.Models;



public record UnauthorizedResponse
{
public UnauthorizedResponse(string _error)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,22 @@

namespace Saas.Permissions.Service.Services;

public class GraphAPIService : IGraphAPIService
public class GraphAPIService(
IOptions<AzureB2CPermissionsApiOptions> permissionApiOptions,
IGraphApiClientFactory graphClientFactory,
ILogger<GraphAPIService> logger) : IGraphAPIService
{
private readonly ILogger _logger;
private readonly ILogger _logger = logger;

// https://learn.microsoft.com/en-us/aspnet/core/fundamentals/logging/loggermessage?view=aspnetcore-7.0
private static readonly Action<ILogger, Exception> _logError = LoggerMessage.Define(
LogLevel.Error,
new EventId(1, nameof(GraphAPIService)),
"Client Assertion Signing Provider");

private readonly GraphServiceClient _graphServiceClient;
private readonly AzureB2CPermissionsApiOptions _permissionOptions;
private readonly GraphServiceClient _graphServiceClient = graphClientFactory.Create();
private readonly AzureB2CPermissionsApiOptions _permissionOptions = permissionApiOptions.Value;

public GraphAPIService(
IOptions<AzureB2CPermissionsApiOptions> permissionApiOptions,
IGraphApiClientFactory graphClientFactory,
ILogger<GraphAPIService> logger)
{
_logger= logger;
_graphServiceClient = graphClientFactory.Create();
_permissionOptions = permissionApiOptions.Value;
}
public async Task<string[]> GetAppRolesAsync(ClaimsRequest request)
{
try
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,14 @@

namespace Saas.Permissions.Service.Services;

public class GraphApiClientFactory : IGraphApiClientFactory
public class GraphApiClientFactory(
IOptions<MSGraphOptions> msGraphOptions,
IAuthenticationProvider authenticationProvider,
HttpClient httpClient) : IGraphApiClientFactory
{
private readonly IAuthenticationProvider _authenticationProvider;
private readonly MSGraphOptions _msGraphOptions;
private readonly HttpClient _httpClient;

public GraphApiClientFactory(
IOptions<MSGraphOptions> msGraphOptions,
IAuthenticationProvider authenticationProvider,
HttpClient httpClient)
{
_msGraphOptions = msGraphOptions.Value;
_authenticationProvider = authenticationProvider;
_httpClient = httpClient;
}
private readonly IAuthenticationProvider _authenticationProvider = authenticationProvider;
private readonly MSGraphOptions _msGraphOptions = msGraphOptions.Value;
private readonly HttpClient _httpClient = httpClient;

public GraphServiceClient Create() =>
new(_httpClient, _authenticationProvider, _msGraphOptions.BaseUrl);
Expand Down
Loading

0 comments on commit 680f52b

Please sign in to comment.