1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-04 01:22:50 -05:00

[AC-1284] AC Team code ownership moves - Provider (#3359)

This commit is contained in:
Thomas Rittson
2023-10-27 03:38:29 +10:00
committed by GitHub
parent c8bcc32764
commit 26dd8b0e47
115 changed files with 325 additions and 252 deletions

View File

@ -0,0 +1,77 @@
using AutoMapper;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
public class ProviderOrganizationRepository :
Repository<ProviderOrganization, Models.Provider.ProviderOrganization, Guid>, IProviderOrganizationRepository
{
public ProviderOrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, context => context.ProviderOrganizations)
{ }
public async Task<ICollection<ProviderOrganization>> CreateManyAsync(IEnumerable<ProviderOrganization> providerOrganizations)
{
var entities = providerOrganizations.ToList();
if (!entities.Any())
{
return default;
}
foreach (var providerOrganization in entities)
{
providerOrganization.SetNewId();
}
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
await dbContext.AddRangeAsync(entities);
await dbContext.SaveChangesAsync();
}
return entities;
}
public async Task<ICollection<ProviderOrganizationOrganizationDetails>> GetManyDetailsByProviderAsync(Guid providerId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = new ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(providerId);
var data = await query.Run(dbContext).ToListAsync();
return data;
}
}
public async Task<ProviderOrganization> GetByOrganizationId(Guid organizationId)
{
using var scope = ServiceScopeFactory.CreateScope();
var dbContext = GetDatabaseContext(scope);
return await GetDbSet(dbContext).Where(po => po.OrganizationId == organizationId).FirstOrDefaultAsync();
}
public async Task<IEnumerable<ProviderOrganizationProviderDetails>> GetManyByUserAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = new ProviderOrganizationReadByUserIdQuery(userId);
var data = await query.Run(dbContext).ToListAsync();
return data;
}
}
public async Task<int> GetCountByOrganizationIdsAsync(IEnumerable<Guid> organizationIds)
{
var query = new ProviderOrganizationCountByOrganizationIdsQuery(organizationIds);
return await GetCountFromQuery(query);
}
}

View File

@ -0,0 +1,81 @@
using AutoMapper;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
public class ProviderRepository : Repository<Provider, Models.Provider.Provider, Guid>, IProviderRepository
{
public ProviderRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, context => context.Providers)
{ }
public override async Task DeleteAsync(Provider provider)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
await dbContext.UserBumpAccountRevisionDateByProviderIdAsync(provider.Id);
await dbContext.SaveChangesAsync();
}
await base.DeleteAsync(provider);
}
public async Task<Provider> GetByOrganizationIdAsync(Guid organizationId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from p in dbContext.Providers
join po in dbContext.ProviderOrganizations
on p.Id equals po.ProviderId
where po.OrganizationId == organizationId
select p;
return await query.FirstOrDefaultAsync();
}
}
public async Task<ICollection<Provider>> SearchAsync(string name, string userEmail, int skip, int take)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = !string.IsNullOrWhiteSpace(userEmail) ?
(from p in dbContext.Providers
join pu in dbContext.ProviderUsers
on p.Id equals pu.ProviderId
join u in dbContext.Users
on pu.UserId equals u.Id
where (string.IsNullOrWhiteSpace(name) || p.Name.Contains(name)) &&
u.Email == userEmail
orderby p.CreationDate descending
select new { p, pu, u }).Skip(skip).Take(take).Select(x => x.p) :
(from p in dbContext.Providers
where string.IsNullOrWhiteSpace(name) || p.Name.Contains(name)
orderby p.CreationDate descending
select new { p }).Skip(skip).Take(take).Select(x => x.p);
var providers = await query.ToArrayAsync();
return Mapper.Map<List<Provider>>(providers);
}
}
public async Task<ICollection<ProviderAbility>> GetManyAbilitiesAsync()
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
return await GetDbSet(dbContext)
.Select(e => new ProviderAbility
{
Enabled = e.Enabled,
Id = e.Id,
UseEvents = e.UseEvents,
}).ToListAsync();
}
}
}

View File

@ -0,0 +1,200 @@
using AutoMapper;
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Core.AdminConsole.Repositories;
using Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories;
public class ProviderUserRepository :
Repository<ProviderUser, Models.Provider.ProviderUser, Guid>, IProviderUserRepository
{
public ProviderUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.ProviderUsers)
{ }
public override async Task DeleteAsync(ProviderUser providerUser)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
await dbContext.UserBumpAccountRevisionDateByProviderUserIdAsync(providerUser.Id);
await dbContext.SaveChangesAsync();
}
await base.DeleteAsync(providerUser);
}
public async Task<int> GetCountByProviderAsync(Guid providerId, string email, bool onlyRegisteredUsers)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from pu in dbContext.ProviderUsers
join u in dbContext.Users
on pu.UserId equals u.Id into u_g
from u in u_g.DefaultIfEmpty()
where pu.ProviderId == providerId &&
((!onlyRegisteredUsers && (pu.Email == email || u.Email == email)) ||
(onlyRegisteredUsers && u.Email == email))
select new { pu, u };
return await query.CountAsync();
}
}
public async Task<ICollection<ProviderUser>> GetManyAsync(IEnumerable<Guid> ids)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = dbContext.ProviderUsers.Where(item => ids.Contains(item.Id));
return await query.ToArrayAsync();
}
}
public async Task<ICollection<ProviderUser>> GetManyByProviderAsync(Guid providerId, ProviderUserType? type = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = dbContext.ProviderUsers.Where(pu => pu.ProviderId.Equals(providerId) &&
(type != null && pu.Type.Equals(type)));
return await query.ToArrayAsync();
}
}
public async Task DeleteManyAsync(IEnumerable<Guid> providerUserIds)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
foreach (var providerUserId in providerUserIds)
{
await dbContext.UserBumpAccountRevisionDateByProviderUserIdAsync(providerUserId);
}
var entities = dbContext.ProviderUsers.Where(pu => providerUserIds.Contains(pu.Id));
dbContext.ProviderUsers.RemoveRange(entities);
await dbContext.SaveChangesAsync();
}
}
public async Task<ICollection<ProviderUser>> GetManyByUserAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from pu in dbContext.ProviderUsers
where pu.UserId == userId
select pu;
return await query.ToArrayAsync();
}
}
public async Task<ProviderUser> GetByProviderUserAsync(Guid providerId, Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from pu in dbContext.ProviderUsers
where pu.UserId == userId &&
pu.ProviderId == providerId
select pu;
return await query.FirstOrDefaultAsync();
}
}
public async Task<ICollection<ProviderUserUserDetails>> GetManyDetailsByProviderAsync(Guid providerId, ProviderUserStatusType? status)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var view = from pu in dbContext.ProviderUsers
join u in dbContext.Users
on pu.UserId equals u.Id into u_g
from u in u_g.DefaultIfEmpty()
select new { pu, u };
var data = await view
.Where(e => e.pu.ProviderId == providerId && (status == null || e.pu.Status == status))
.Select(e => new ProviderUserUserDetails
{
Id = e.pu.Id,
UserId = e.pu.UserId,
ProviderId = e.pu.ProviderId,
Name = e.u.Name,
Email = e.u.Email ?? e.pu.Email,
Status = e.pu.Status,
Type = e.pu.Type,
Permissions = e.pu.Permissions,
}).ToArrayAsync();
return data;
}
}
public async Task<ICollection<ProviderUserProviderDetails>> GetManyDetailsByUserAsync(Guid userId, ProviderUserStatusType? status = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = new ProviderUserProviderDetailsReadByUserIdStatusQuery(userId, status);
var data = await query.Run(dbContext).ToArrayAsync();
return data;
}
}
public async Task<IEnumerable<ProviderUserPublicKey>> GetManyPublicKeysByProviderUserAsync(Guid providerId, IEnumerable<Guid> Ids)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = new UserReadPublicKeysByProviderUserIdsQuery(providerId, Ids);
var data = await query.Run(dbContext).ToListAsync();
return data;
}
}
public async Task<IEnumerable<ProviderUserOrganizationDetails>> GetManyOrganizationDetailsByUserAsync(Guid userId, ProviderUserStatusType? status = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var view = new ProviderUserOrganizationDetailsViewQuery();
var query = from ou in view.Run(dbContext)
where ou.UserId == userId &&
(status == null || ou.Status == status)
select ou;
var organizationUsers = await query.ToListAsync();
return organizationUsers;
}
}
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
return await dbContext.ProviderUsers
.Where(pu => pu.Type == ProviderUserType.ProviderAdmin && pu.Status == ProviderUserStatusType.Confirmed)
.GroupBy(pu => pu.UserId)
.Select(g => new { UserId = g.Key, ConfirmedOwnerCount = g.Count() })
.Where(oc => oc.UserId == userId && oc.ConfirmedOwnerCount == 1)
.CountAsync();
}
}
public async Task<ICollection<ProviderUser>> GetManyByOrganizationAsync(Guid organizationId, ProviderUserStatusType? status = null)
{
using (var scope = ServiceScopeFactory.CreateScope())
{
var dbContext = GetDatabaseContext(scope);
var query = from pu in dbContext.ProviderUsers
join po in dbContext.ProviderOrganizations
on pu.ProviderId equals po.ProviderId
where po.OrganizationId == organizationId &&
(status == null || pu.Status == status)
select pu;
return await query.ToArrayAsync();
}
}
}

View File

@ -0,0 +1,23 @@
using Bit.Core.AdminConsole.Entities.Provider;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
public class ProviderOrganizationCountByOrganizationIdsQuery : IQuery<ProviderOrganization>
{
private readonly IEnumerable<Guid> _organizationIds;
public ProviderOrganizationCountByOrganizationIdsQuery(IEnumerable<Guid> organizationIds)
{
_organizationIds = organizationIds;
}
public IQueryable<ProviderOrganization> Run(DatabaseContext dbContext)
{
var query = from po in dbContext.ProviderOrganizations
where _organizationIds.Contains(po.OrganizationId)
select po;
return query;
}
}

View File

@ -0,0 +1,40 @@
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
public class ProviderOrganizationOrganizationDetailsReadByProviderIdQuery : IQuery<ProviderOrganizationOrganizationDetails>
{
private readonly Guid _providerId;
public ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(Guid providerId)
{
_providerId = providerId;
}
public IQueryable<ProviderOrganizationOrganizationDetails> Run(DatabaseContext dbContext)
{
var query = from po in dbContext.ProviderOrganizations
join o in dbContext.Organizations
on po.OrganizationId equals o.Id
join ou in dbContext.OrganizationUsers
on po.OrganizationId equals ou.OrganizationId
where po.ProviderId == _providerId
select new { po, o };
return query.Select(x => new ProviderOrganizationOrganizationDetails()
{
Id = x.po.Id,
ProviderId = x.po.ProviderId,
OrganizationId = x.po.OrganizationId,
OrganizationName = x.o.Name,
Key = x.po.Key,
Settings = x.po.Settings,
CreationDate = x.po.CreationDate,
RevisionDate = x.po.RevisionDate,
UserCount = x.o.OrganizationUsers.Count(ou => ou.Status == Core.Enums.OrganizationUserStatusType.Confirmed),
Seats = x.o.Seats,
Plan = x.o.Plan,
Status = x.o.Status
});
}
}

View File

@ -0,0 +1,34 @@
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
public class ProviderOrganizationReadByUserIdQuery : IQuery<ProviderOrganizationProviderDetails>
{
private readonly Guid _userId;
public ProviderOrganizationReadByUserIdQuery(Guid userId)
{
_userId = userId;
}
public IQueryable<ProviderOrganizationProviderDetails> Run(DatabaseContext dbContext)
{
var query = from po in dbContext.ProviderOrganizations
join ou in dbContext.OrganizationUsers
on po.OrganizationId equals ou.OrganizationId
join p in dbContext.Providers
on po.ProviderId equals p.Id
where ou.UserId == _userId
select new ProviderOrganizationProviderDetails
{
Id = po.Id,
OrganizationId = po.OrganizationId,
ProviderId = po.ProviderId,
ProviderName = p.Name,
ProviderType = p.Type
};
return query;
}
}

View File

@ -0,0 +1,49 @@
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
public class ProviderUserOrganizationDetailsViewQuery : IQuery<ProviderUserOrganizationDetails>
{
public IQueryable<ProviderUserOrganizationDetails> Run(DatabaseContext dbContext)
{
var query = from pu in dbContext.ProviderUsers
join po in dbContext.ProviderOrganizations on pu.ProviderId equals po.ProviderId
join o in dbContext.Organizations on po.OrganizationId equals o.Id
join p in dbContext.Providers on pu.ProviderId equals p.Id
select new { pu, po, o, p };
return query.Select(x => new ProviderUserOrganizationDetails
{
OrganizationId = x.po.OrganizationId,
UserId = x.pu.UserId,
Name = x.o.Name,
Enabled = x.o.Enabled,
UsePolicies = x.o.UsePolicies,
UseSso = x.o.UseSso,
UseKeyConnector = x.o.UseKeyConnector,
UseScim = x.o.UseScim,
UseGroups = x.o.UseGroups,
UseDirectory = x.o.UseDirectory,
UseEvents = x.o.UseEvents,
UseTotp = x.o.UseTotp,
Use2fa = x.o.Use2fa,
UseApi = x.o.UseApi,
SelfHost = x.o.SelfHost,
UsersGetPremium = x.o.UsersGetPremium,
UseCustomPermissions = x.o.UseCustomPermissions,
Seats = x.o.Seats,
MaxCollections = x.o.MaxCollections,
MaxStorageGb = x.o.MaxStorageGb,
Identifier = x.o.Identifier,
Key = x.po.Key,
Status = x.pu.Status,
Type = x.pu.Type,
PublicKey = x.o.PublicKey,
PrivateKey = x.o.PrivateKey,
ProviderId = x.p.Id,
ProviderName = x.p.Name,
PlanType = x.o.PlanType
});
}
}

View File

@ -0,0 +1,40 @@
using Bit.Core.AdminConsole.Enums.Provider;
using Bit.Core.AdminConsole.Models.Data.Provider;
using Bit.Infrastructure.EntityFramework.Repositories;
using Bit.Infrastructure.EntityFramework.Repositories.Queries;
namespace Bit.Infrastructure.EntityFramework.AdminConsole.Repositories.Queries;
public class ProviderUserProviderDetailsReadByUserIdStatusQuery : IQuery<ProviderUserProviderDetails>
{
private readonly Guid _userId;
private readonly ProviderUserStatusType? _status;
public ProviderUserProviderDetailsReadByUserIdStatusQuery(Guid userId, ProviderUserStatusType? status)
{
_userId = userId;
_status = status;
}
public IQueryable<ProviderUserProviderDetails> Run(DatabaseContext dbContext)
{
var query = from pu in dbContext.ProviderUsers
join p in dbContext.Providers
on pu.ProviderId equals p.Id into p_g
from p in p_g.DefaultIfEmpty()
where pu.UserId == _userId && p.Status != ProviderStatusType.Pending && (_status == null || pu.Status == _status)
select new { pu, p };
return query.Select(x => new ProviderUserProviderDetails()
{
UserId = x.pu.UserId,
ProviderId = x.pu.ProviderId,
Name = x.p.Name,
Key = x.pu.Key,
Status = x.pu.Status,
Type = x.pu.Type,
Enabled = x.p.Enabled,
Permissions = x.pu.Permissions,
UseEvents = x.p.UseEvents,
ProviderStatus = x.p.Status,
});
}
}