more auth and database stuff

This commit is contained in:
Mathias Beaulieu-Duncan 2025-01-22 12:54:43 -05:00
parent 11367e7ccc
commit a5d60cf2ca
Signed by: mathias
GPG Key ID: 8C3667DADE3B6303
8 changed files with 25 additions and 72 deletions

View File

@ -24,15 +24,15 @@ public class HasAccessToClientValidator : AbstractValidator<long>
.SetValidator(new DbEntityExistValidator<Client, long>(dbContext)) .SetValidator(new DbEntityExistValidator<Client, long>(dbContext))
.CustomAsync(async (clientId, validationContext, cancellationToken) => .CustomAsync(async (clientId, validationContext, cancellationToken) =>
{ {
var organizationClient = await _dbContext.OrganizationClients var client = await _dbContext.Clients
.AsNoTracking() .AsNoTracking()
.FirstOrDefaultAsync(organizationClient => organizationClient.ClientId == clientId, cancellationToken); .FirstOrDefaultAsync(client => client.Id == clientId, cancellationToken);
if (organizationClient is null) if (client is null)
return; return;
var validation = new HasAccessToOrganizationValidator(_role, _dbContext, _userIdentityService); var validation = new HasAccessToOrganizationValidator(_role, _dbContext, _userIdentityService);
var validationResult = validation.Validate(organizationClient.OrganizationId); var validationResult = validation.Validate(client.OrganizationId);
if (!validationResult.IsValid) if (!validationResult.IsValid)
foreach (var error in validationResult.Errors) foreach (var error in validationResult.Errors)

View File

@ -13,7 +13,6 @@ public class ClientParams
public class ClientItem public class ClientItem
{ {
public long Id { get; set; } public long Id { get; set; }
public IEnumerable<long> OrganizationIds { get; set; }
public required string Name { get; set; } public required string Name { get; set; }
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
@ -39,18 +38,14 @@ public class ClientQueryableProvider(MainDbContext dbContext, UserIdentityServic
var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken); var user = await userIdentityService.GetUserOrDefaultAsync(cancellationToken);
var queryable = dbContext.Clients
.AsNoTracking();
var result = dbContext.Clients var result = dbContext.Clients
.AsNoTracking() .AsNoTracking()
.Where(client => client.OrganizationClients.Any(organizationClient => organizationClient.OrganizationId == organizationId)) .Where(client => client.OrganizationId == organizationId)
.Where(client => .Where(client =>
client.OrganizationClients.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id))) client.Organization.OrganizationUsers.Any(organizationClient => organizationClient.Organization.OrganizationUsers.Any(organizationUser => organizationUser.UserId == user!.Id)))
.Select(client => new ClientItem .Select(client => new ClientItem
{ {
Id = client.Id, Id = client.Id,
OrganizationIds = client.OrganizationClients.Select(organizationClient => organizationClient.OrganizationId),
Name = client.Name, Name = client.Name,
CreatedAt = client.CreatedAt, CreatedAt = client.CreatedAt,
UpdatedAt = client.UpdatedAt UpdatedAt = client.UpdatedAt

View File

@ -55,7 +55,8 @@ public class ProjectQueryableProvider(MainDbContext dbContext, UserIdentityServi
} }
queryable = queryable queryable = queryable
.Where(project => project.Client.OrganizationClients.Any(organizationClient => .Where(project => project.Client.OrganizationId == organizationId)
.Where(project => project.Client.Organization.OrganizationUsers.Any(organizationClient =>
organizationClient.OrganizationId == organizationUser!.OrganizationId)); organizationClient.OrganizationId == organizationUser!.OrganizationId));
var result = queryable.Select(project => new ProjectItem var result = queryable.Select(project => new ProjectItem

View File

@ -21,16 +21,11 @@ public class ClientService(MainDbContext dbContext, UserIdentityService userIden
var client = new Dal.DbEntity.Client var client = new Dal.DbEntity.Client
{ {
Name = options.Name Name = options.Name,
}; Organization = organization
var organizationClient = new OrganizationClient
{
Organization = organization,
Client = client
}; };
organization.OrganizationClients.Add(organizationClient); organization.Clients.Add(client);
await dbContext.SaveChangesAsync(cancellationToken); await dbContext.SaveChangesAsync(cancellationToken);
} }
} }

View File

@ -7,13 +7,15 @@ public partial class Client
{ {
public long Id { get; set; } public long Id { get; set; }
public long OrganizationId { get; set; }
public string Name { get; set; } = null!; public string Name { get; set; } = null!;
public DateTime CreatedAt { get; set; } public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
public virtual ICollection<OrganizationClient> OrganizationClients { get; set; } = new List<OrganizationClient>(); public virtual Organization Organization { get; set; } = null!;
public virtual ICollection<Project> Projects { get; set; } = new List<Project>(); public virtual ICollection<Project> Projects { get; set; } = new List<Project>();
} }

View File

@ -13,9 +13,9 @@ public partial class Organization
public DateTime? UpdatedAt { get; set; } public DateTime? UpdatedAt { get; set; }
public virtual ICollection<OidcProvider> OidcProviders { get; set; } = new List<OidcProvider>(); public virtual ICollection<Client> Clients { get; set; } = new List<Client>();
public virtual ICollection<OrganizationClient> OrganizationClients { get; set; } = new List<OrganizationClient>(); public virtual ICollection<OidcProvider> OidcProviders { get; set; } = new List<OidcProvider>();
public virtual ICollection<OrganizationUser> OrganizationUsers { get; set; } = new List<OrganizationUser>(); public virtual ICollection<OrganizationUser> OrganizationUsers { get; set; } = new List<OrganizationUser>();
} }

View File

@ -1,21 +0,0 @@
using System;
using System.Collections.Generic;
namespace DigitalOps.Dal.DbEntity;
public partial class OrganizationClient
{
public long Id { get; set; }
public long OrganizationId { get; set; }
public long ClientId { get; set; }
public DateTime CreatedAt { get; set; }
public DateTime? UpdatedAt { get; set; }
public virtual Client Client { get; set; } = null!;
public virtual Organization Organization { get; set; } = null!;
}

View File

@ -1,4 +1,6 @@
using DigitalOps.Dal.DbEntity; using System;
using System.Collections.Generic;
using DigitalOps.Dal.DbEntity;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
namespace DigitalOps.Dal; namespace DigitalOps.Dal;
@ -20,8 +22,6 @@ public partial class MainDbScaffoldedContext : DbContext
public virtual DbSet<Organization> Organizations { get; set; } public virtual DbSet<Organization> Organizations { get; set; }
public virtual DbSet<OrganizationClient> OrganizationClients { get; set; }
public virtual DbSet<OrganizationUser> OrganizationUsers { get; set; } public virtual DbSet<OrganizationUser> OrganizationUsers { get; set; }
public virtual DbSet<Project> Projects { get; set; } public virtual DbSet<Project> Projects { get; set; }
@ -52,7 +52,13 @@ public partial class MainDbScaffoldedContext : DbContext
entity.Property(e => e.Name) entity.Property(e => e.Name)
.HasMaxLength(255) .HasMaxLength(255)
.HasColumnName("name"); .HasColumnName("name");
entity.Property(e => e.OrganizationId).HasColumnName("organization_id");
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
entity.HasOne(d => d.Organization).WithMany(p => p.Clients)
.HasForeignKey(d => d.OrganizationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("client_organization_id_fkey");
}); });
modelBuilder.Entity<OidcProvider>(entity => modelBuilder.Entity<OidcProvider>(entity =>
@ -96,31 +102,6 @@ public partial class MainDbScaffoldedContext : DbContext
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at"); entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
}); });
modelBuilder.Entity<OrganizationClient>(entity =>
{
entity.HasKey(e => e.Id).HasName("organization_client_pkey");
entity.ToTable("organization_client");
entity.Property(e => e.Id).HasColumnName("id");
entity.Property(e => e.ClientId).HasColumnName("client_id");
entity.Property(e => e.CreatedAt)
.HasDefaultValueSql("(CURRENT_TIMESTAMP AT TIME ZONE 'UTC'::text)")
.HasColumnName("created_at");
entity.Property(e => e.OrganizationId).HasColumnName("organization_id");
entity.Property(e => e.UpdatedAt).HasColumnName("updated_at");
entity.HasOne(d => d.Client).WithMany(p => p.OrganizationClients)
.HasForeignKey(d => d.ClientId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("organization_client_client_id_fkey");
entity.HasOne(d => d.Organization).WithMany(p => p.OrganizationClients)
.HasForeignKey(d => d.OrganizationId)
.OnDelete(DeleteBehavior.ClientSetNull)
.HasConstraintName("organization_client_organization_id_fkey");
});
modelBuilder.Entity<OrganizationUser>(entity => modelBuilder.Entity<OrganizationUser>(entity =>
{ {
entity.HasKey(e => e.Id).HasName("organization_user_pkey"); entity.HasKey(e => e.Id).HasName("organization_user_pkey");