mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 17:12:49 -05:00
Revert filescoped (#2227)
* Revert "Add git blame entry (#2226)" This reverts commit239286737d
. * Revert "Turn on file scoped namespaces (#2225)" This reverts commit34fb4cca2a
.
This commit is contained in:
@ -10,63 +10,80 @@ using Microsoft.Extensions.DependencyInjection;
|
||||
using Cipher = Bit.Core.Entities.Cipher;
|
||||
using User = Bit.Core.Entities.User;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public abstract class BaseEntityFrameworkRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
protected BulkCopyOptions DefaultBulkCopyOptions { get; set; } = new BulkCopyOptions
|
||||
public abstract class BaseEntityFrameworkRepository
|
||||
{
|
||||
KeepIdentity = true,
|
||||
BulkCopyType = BulkCopyType.MultipleRows,
|
||||
};
|
||||
|
||||
public BaseEntityFrameworkRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
{
|
||||
ServiceScopeFactory = serviceScopeFactory;
|
||||
Mapper = mapper;
|
||||
}
|
||||
|
||||
protected IServiceScopeFactory ServiceScopeFactory { get; private set; }
|
||||
protected IMapper Mapper { get; private set; }
|
||||
|
||||
public DatabaseContext GetDatabaseContext(IServiceScope serviceScope)
|
||||
{
|
||||
return serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
}
|
||||
|
||||
public void ClearChangeTracking()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected BulkCopyOptions DefaultBulkCopyOptions { get; set; } = new BulkCopyOptions
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
dbContext.ChangeTracker.Clear();
|
||||
KeepIdentity = true,
|
||||
BulkCopyType = BulkCopyType.MultipleRows,
|
||||
};
|
||||
|
||||
public BaseEntityFrameworkRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
{
|
||||
ServiceScopeFactory = serviceScopeFactory;
|
||||
Mapper = mapper;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountFromQuery<T>(IQuery<T> query)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected IServiceScopeFactory ServiceScopeFactory { get; private set; }
|
||||
protected IMapper Mapper { get; private set; }
|
||||
|
||||
public DatabaseContext GetDatabaseContext(IServiceScope serviceScope)
|
||||
{
|
||||
return await query.Run(GetDatabaseContext(scope)).CountAsync();
|
||||
return serviceScope.ServiceProvider.GetRequiredService<DatabaseContext>();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCipherId(Cipher cipher)
|
||||
{
|
||||
var list = new List<Cipher> { cipher };
|
||||
await UserBumpAccountRevisionDateByCipherId(list);
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCipherId(IEnumerable<Cipher> ciphers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public void ClearChangeTracking()
|
||||
{
|
||||
foreach (var cipher in ciphers)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new UserBumpAccountRevisionDateByCipherIdQuery(cipher);
|
||||
var users = query.Run(dbContext);
|
||||
dbContext.ChangeTracker.Clear();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountFromQuery<T>(IQuery<T> query)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
return await query.Run(GetDatabaseContext(scope)).CountAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCipherId(Cipher cipher)
|
||||
{
|
||||
var list = new List<Cipher> { cipher };
|
||||
await UserBumpAccountRevisionDateByCipherId(list);
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCipherId(IEnumerable<Cipher> ciphers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
foreach (var cipher in ciphers)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new UserBumpAccountRevisionDateByCipherIdQuery(cipher);
|
||||
var users = query.Run(dbContext);
|
||||
|
||||
await users.ForEachAsync(e =>
|
||||
{
|
||||
dbContext.Attach(e);
|
||||
e.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByOrganizationId(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new UserBumpAccountRevisionDateByOrganizationIdQuery(organizationId);
|
||||
var users = query.Run(dbContext);
|
||||
await users.ForEachAsync(e =>
|
||||
{
|
||||
dbContext.Attach(e);
|
||||
@ -75,191 +92,175 @@ public abstract class BaseEntityFrameworkRepository
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByOrganizationId(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected async Task UserBumpAccountRevisionDate(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new UserBumpAccountRevisionDateByOrganizationIdQuery(organizationId);
|
||||
var users = query.Run(dbContext);
|
||||
await users.ForEachAsync(e =>
|
||||
{
|
||||
dbContext.Attach(e);
|
||||
e.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpManyAccountRevisionDates(new[] { userId });
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDate(Guid userId)
|
||||
{
|
||||
await UserBumpManyAccountRevisionDates(new[] { userId });
|
||||
}
|
||||
|
||||
protected async Task UserBumpManyAccountRevisionDates(ICollection<Guid> userIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected async Task UserBumpManyAccountRevisionDates(ICollection<Guid> userIds)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = dbContext.Users.Where(u => userIds.Contains(u.Id));
|
||||
await users.ForEachAsync(u =>
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task OrganizationUpdateStorage(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var attachments = await dbContext.Ciphers
|
||||
.Where(e => e.UserId == null &&
|
||||
e.OrganizationId == organizationId &&
|
||||
!string.IsNullOrWhiteSpace(e.Attachments))
|
||||
.Select(e => e.Attachments)
|
||||
.ToListAsync();
|
||||
var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray()
|
||||
.Sum(p => p.GetProperty("Size").GetInt64()) ?? 0);
|
||||
var organization = new Organization
|
||||
{
|
||||
Id = organizationId,
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
Storage = storage,
|
||||
};
|
||||
dbContext.Organizations.Attach(organization);
|
||||
var entry = dbContext.Entry(organization);
|
||||
entry.Property(e => e.RevisionDate).IsModified = true;
|
||||
entry.Property(e => e.Storage).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserUpdateStorage(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var attachments = await dbContext.Ciphers
|
||||
.Where(e => e.UserId.HasValue &&
|
||||
e.UserId.Value == userId &&
|
||||
e.OrganizationId == null &&
|
||||
!string.IsNullOrWhiteSpace(e.Attachments))
|
||||
.Select(e => e.Attachments)
|
||||
.ToListAsync();
|
||||
var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray()
|
||||
.Sum(p => p.GetProperty("Size").GetInt64()) ?? 0);
|
||||
var user = new Models.User
|
||||
{
|
||||
Id = userId,
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
Storage = storage,
|
||||
};
|
||||
dbContext.Users.Attach(user);
|
||||
var entry = dbContext.Entry(user);
|
||||
entry.Property(e => e.RevisionDate).IsModified = true;
|
||||
entry.Property(e => e.Storage).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserUpdateKeys(User user)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await dbContext.Users.FindAsync(user.Id);
|
||||
if (entity == null)
|
||||
{
|
||||
return;
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = dbContext.Users.Where(u => userIds.Contains(u.Id));
|
||||
await users.ForEachAsync(u =>
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
entity.SecurityStamp = user.SecurityStamp;
|
||||
entity.Key = user.Key;
|
||||
entity.PrivateKey = user.PrivateKey;
|
||||
entity.RevisionDate = DateTime.UtcNow;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCollectionId(Guid collectionId, Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected async Task OrganizationUpdateStorage(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId.Equals(collectionId)
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == default(Guid) && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == collectionId &&
|
||||
(ou.OrganizationId == organizationId && ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(cu.CollectionId != default(Guid) || cg.CollectionId != default(Guid) || ou.AccessAll || g.AccessAll))
|
||||
select new { u, ou, cu, gu, g, cg };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var attachments = await dbContext.Ciphers
|
||||
.Where(e => e.UserId == null &&
|
||||
e.OrganizationId == organizationId &&
|
||||
!string.IsNullOrWhiteSpace(e.Attachments))
|
||||
.Select(e => e.Attachments)
|
||||
.ToListAsync();
|
||||
var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray()
|
||||
.Sum(p => p.GetProperty("Size").GetInt64()) ?? 0);
|
||||
var organization = new Organization
|
||||
{
|
||||
Id = organizationId,
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
Storage = storage,
|
||||
};
|
||||
dbContext.Organizations.Attach(organization);
|
||||
var entry = dbContext.Entry(organization);
|
||||
entry.Property(e => e.RevisionDate).IsModified = true;
|
||||
entry.Property(e => e.Storage).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByOrganizationUserId(Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected async Task UserUpdateStorage(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
where ou.Id.Equals(organizationUserId) && ou.Status.Equals(OrganizationUserStatusType.Confirmed)
|
||||
select new { u, ou };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.AccountRevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var attachments = await dbContext.Ciphers
|
||||
.Where(e => e.UserId.HasValue &&
|
||||
e.UserId.Value == userId &&
|
||||
e.OrganizationId == null &&
|
||||
!string.IsNullOrWhiteSpace(e.Attachments))
|
||||
.Select(e => e.Attachments)
|
||||
.ToListAsync();
|
||||
var storage = attachments.Sum(e => JsonDocument.Parse(e)?.RootElement.EnumerateArray()
|
||||
.Sum(p => p.GetProperty("Size").GetInt64()) ?? 0);
|
||||
var user = new Models.User
|
||||
{
|
||||
Id = userId,
|
||||
RevisionDate = DateTime.UtcNow,
|
||||
Storage = storage,
|
||||
};
|
||||
dbContext.Users.Attach(user);
|
||||
var entry = dbContext.Entry(user);
|
||||
entry.Property(e => e.RevisionDate).IsModified = true;
|
||||
entry.Property(e => e.Storage).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByProviderUserIds(ICollection<Guid> providerUserIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
protected async Task UserUpdateKeys(User user)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from pu in dbContext.ProviderUsers
|
||||
join u in dbContext.Users
|
||||
on pu.UserId equals u.Id
|
||||
where pu.Status.Equals(ProviderUserStatusType.Confirmed) &&
|
||||
providerUserIds.Contains(pu.Id)
|
||||
select new { pu, u };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.AccountRevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await dbContext.Users.FindAsync(user.Id);
|
||||
if (entity == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
entity.SecurityStamp = user.SecurityStamp;
|
||||
entity.Key = user.Key;
|
||||
entity.PrivateKey = user.PrivateKey;
|
||||
entity.RevisionDate = DateTime.UtcNow;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByCollectionId(Guid collectionId, Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId.Equals(collectionId)
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == default(Guid) && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == collectionId &&
|
||||
(ou.OrganizationId == organizationId && ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(cu.CollectionId != default(Guid) || cg.CollectionId != default(Guid) || ou.AccessAll || g.AccessAll))
|
||||
select new { u, ou, cu, gu, g, cg };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.RevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByOrganizationUserId(Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
where ou.Id.Equals(organizationUserId) && ou.Status.Equals(OrganizationUserStatusType.Confirmed)
|
||||
select new { u, ou };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.AccountRevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
protected async Task UserBumpAccountRevisionDateByProviderUserIds(ICollection<Guid> providerUserIds)
|
||||
{
|
||||
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
|
||||
where pu.Status.Equals(ProviderUserStatusType.Confirmed) &&
|
||||
providerUserIds.Contains(pu.Id)
|
||||
select new { pu, u };
|
||||
var users = query.Select(x => x.u);
|
||||
await users.ForEachAsync(u =>
|
||||
{
|
||||
dbContext.Attach(u);
|
||||
u.AccountRevisionDate = DateTime.UtcNow;
|
||||
});
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
File diff suppressed because it is too large
Load Diff
@ -6,232 +6,233 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using CollectionCipher = Bit.Core.Entities.CollectionCipher;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollectionCipherRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public CollectionCipherRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task<CollectionCipher> CreateAsync(CollectionCipher obj)
|
||||
public class CollectionCipherRepository : BaseEntityFrameworkRepository, ICollectionCipherRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public CollectionCipherRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task<CollectionCipher> CreateAsync(CollectionCipher obj)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<Models.CollectionCipher>(obj);
|
||||
dbContext.Add(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
var organizationId = (await dbContext.Ciphers.FirstOrDefaultAsync(c => c.Id.Equals(obj.CipherId))).OrganizationId;
|
||||
if (organizationId.HasValue)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
await UserBumpAccountRevisionDateByCollectionId(obj.CollectionId, organizationId.Value);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await (from cc in dbContext.CollectionCiphers
|
||||
join c in dbContext.Collections
|
||||
on cc.CollectionId equals c.Id
|
||||
where c.OrganizationId == organizationId
|
||||
select cc).ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await new CollectionCipherReadByUserIdQuery(userId)
|
||||
.Run(dbContext)
|
||||
.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByUserIdCipherIdAsync(Guid userId, Guid cipherId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await new CollectionCipherReadByUserIdCipherIdQuery(userId, cipherId)
|
||||
.Run(dbContext)
|
||||
.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsAsync(Guid cipherId, Guid userId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizationId = (await dbContext.Ciphers.FindAsync(cipherId)).OrganizationId;
|
||||
var availableCollectionsCte = from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId == c.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed && (
|
||||
ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
|
||||
select new { c, o, cu, gu, g, cg };
|
||||
var target = from cc in dbContext.CollectionCiphers
|
||||
where cc.CipherId == cipherId
|
||||
select new { cc.CollectionId, cc.CipherId };
|
||||
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
|
||||
var merge1 = from t in target
|
||||
join s in source
|
||||
on t.CollectionId equals s.CollectionId into s_g
|
||||
from s in s_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var merge2 = from s in source
|
||||
join t in target
|
||||
on s.CollectionId equals t.CollectionId into t_g
|
||||
from t in t_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var union = merge1.Union(merge2).Distinct();
|
||||
var insert = union
|
||||
.Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<Models.CollectionCipher>(obj);
|
||||
dbContext.Add(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
var organizationId = (await dbContext.Ciphers.FirstOrDefaultAsync(c => c.Id.Equals(obj.CipherId))).OrganizationId;
|
||||
if (organizationId.HasValue)
|
||||
{
|
||||
CollectionId = x.s.CollectionId,
|
||||
CipherId = x.s.CipherId,
|
||||
});
|
||||
var delete = union
|
||||
.Where(x => x.s == null && x.t.CipherId == cipherId && collectionIds.Contains(x.t.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.t.CollectionId,
|
||||
CipherId = x.t.CipherId,
|
||||
});
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
if (organizationId.HasValue)
|
||||
{
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId.Value);
|
||||
await UserBumpAccountRevisionDateByCollectionId(obj.CollectionId, organizationId.Value);
|
||||
}
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsForAdminAsync(Guid cipherId, Guid organizationId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availableCollectionsCte = from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationId
|
||||
select c;
|
||||
var target = from cc in dbContext.CollectionCiphers
|
||||
where cc.CipherId == cipherId
|
||||
select new { cc.CollectionId, cc.CipherId };
|
||||
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
|
||||
var merge1 = from t in target
|
||||
join s in source
|
||||
on t.CollectionId equals s.CollectionId into s_g
|
||||
from s in s_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var merge2 = from s in source
|
||||
join t in target
|
||||
on s.CollectionId equals t.CollectionId into t_g
|
||||
from t in t_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var union = merge1.Union(merge2).Distinct();
|
||||
var insert = union
|
||||
.Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.s.CollectionId,
|
||||
CipherId = x.s.CipherId,
|
||||
});
|
||||
var delete = union
|
||||
.Where(x => x.s == null && x.t.CipherId == cipherId)
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.t.CollectionId,
|
||||
CipherId = x.t.CipherId,
|
||||
});
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsForCiphersAsync(IEnumerable<Guid> cipherIds, Guid userId, Guid organizationId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId == c.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
|
||||
select new { c, o, ou, cu, gu, g, cg };
|
||||
var count = await availibleCollections.CountAsync();
|
||||
if (await availibleCollections.CountAsync() < 1)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
return;
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await (from cc in dbContext.CollectionCiphers
|
||||
join c in dbContext.Collections
|
||||
on cc.CollectionId equals c.Id
|
||||
where c.OrganizationId == organizationId
|
||||
select cc).ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
var insertData = from collectionId in collectionIds
|
||||
from cipherId in cipherIds
|
||||
where availibleCollections.Select(x => x.c.Id).Contains(collectionId)
|
||||
select new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = collectionId,
|
||||
CipherId = cipherId,
|
||||
};
|
||||
await dbContext.AddRangeAsync(insertData);
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId);
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await new CollectionCipherReadByUserIdQuery(userId)
|
||||
.Run(dbContext)
|
||||
.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionCipher>> GetManyByUserIdCipherIdAsync(Guid userId, Guid cipherId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await new CollectionCipherReadByUserIdCipherIdQuery(userId, cipherId)
|
||||
.Run(dbContext)
|
||||
.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsAsync(Guid cipherId, Guid userId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizationId = (await dbContext.Ciphers.FindAsync(cipherId)).OrganizationId;
|
||||
var availableCollectionsCte = from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId == c.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed && (
|
||||
ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
|
||||
select new { c, o, cu, gu, g, cg };
|
||||
var target = from cc in dbContext.CollectionCiphers
|
||||
where cc.CipherId == cipherId
|
||||
select new { cc.CollectionId, cc.CipherId };
|
||||
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
|
||||
var merge1 = from t in target
|
||||
join s in source
|
||||
on t.CollectionId equals s.CollectionId into s_g
|
||||
from s in s_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var merge2 = from s in source
|
||||
join t in target
|
||||
on s.CollectionId equals t.CollectionId into t_g
|
||||
from t in t_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var union = merge1.Union(merge2).Distinct();
|
||||
var insert = union
|
||||
.Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.s.CollectionId,
|
||||
CipherId = x.s.CipherId,
|
||||
});
|
||||
var delete = union
|
||||
.Where(x => x.s == null && x.t.CipherId == cipherId && collectionIds.Contains(x.t.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.t.CollectionId,
|
||||
CipherId = x.t.CipherId,
|
||||
});
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
|
||||
if (organizationId.HasValue)
|
||||
{
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsForAdminAsync(Guid cipherId, Guid organizationId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availableCollectionsCte = from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationId
|
||||
select c;
|
||||
var target = from cc in dbContext.CollectionCiphers
|
||||
where cc.CipherId == cipherId
|
||||
select new { cc.CollectionId, cc.CipherId };
|
||||
var source = collectionIds.Select(x => new { CollectionId = x, CipherId = cipherId });
|
||||
var merge1 = from t in target
|
||||
join s in source
|
||||
on t.CollectionId equals s.CollectionId into s_g
|
||||
from s in s_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var merge2 = from s in source
|
||||
join t in target
|
||||
on s.CollectionId equals t.CollectionId into t_g
|
||||
from t in t_g.DefaultIfEmpty()
|
||||
where t.CipherId == s.CipherId
|
||||
select new { t, s };
|
||||
var union = merge1.Union(merge2).Distinct();
|
||||
var insert = union
|
||||
.Where(x => x.t == null && collectionIds.Contains(x.s.CollectionId))
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.s.CollectionId,
|
||||
CipherId = x.s.CipherId,
|
||||
});
|
||||
var delete = union
|
||||
.Where(x => x.s == null && x.t.CipherId == cipherId)
|
||||
.Select(x => new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = x.t.CollectionId,
|
||||
CipherId = x.t.CipherId,
|
||||
});
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateCollectionsForCiphersAsync(IEnumerable<Guid> cipherIds, Guid userId, Guid organizationId, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.CollectionId == c.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
(o.Id == organizationId && o.Enabled && ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly))
|
||||
select new { c, o, ou, cu, gu, g, cg };
|
||||
var count = await availibleCollections.CountAsync();
|
||||
if (await availibleCollections.CountAsync() < 1)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var insertData = from collectionId in collectionIds
|
||||
from cipherId in cipherIds
|
||||
where availibleCollections.Select(x => x.c.Id).Contains(collectionId)
|
||||
select new Models.CollectionCipher
|
||||
{
|
||||
CollectionId = collectionId,
|
||||
CipherId = cipherId,
|
||||
};
|
||||
await dbContext.AddRangeAsync(insertData);
|
||||
await UserBumpAccountRevisionDateByOrganizationId(organizationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,244 +6,245 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class CollectionRepository : Repository<Core.Entities.Collection, Collection, Guid>, ICollectionRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public CollectionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Collections)
|
||||
{ }
|
||||
|
||||
public override async Task<Core.Entities.Collection> CreateAsync(Core.Entities.Collection obj)
|
||||
public class CollectionRepository : Repository<Core.Entities.Collection, Collection, Guid>, ICollectionRepository
|
||||
{
|
||||
await base.CreateAsync(obj);
|
||||
await UserBumpAccountRevisionDateByCollectionId(obj.Id, obj.OrganizationId);
|
||||
return obj;
|
||||
}
|
||||
public CollectionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Collections)
|
||||
{ }
|
||||
|
||||
public async Task CreateAsync(Core.Entities.Collection obj, IEnumerable<SelectionReadOnly> groups)
|
||||
{
|
||||
await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public override async Task<Core.Entities.Collection> CreateAsync(Core.Entities.Collection obj)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleGroups = await (from g in dbContext.Groups
|
||||
where g.OrganizationId == obj.OrganizationId
|
||||
select g.Id).ToListAsync();
|
||||
var collectionGroups = groups
|
||||
.Where(g => availibleGroups.Contains(g.Id))
|
||||
.Select(g => new CollectionGroup
|
||||
{
|
||||
CollectionId = obj.Id,
|
||||
GroupId = g.Id,
|
||||
ReadOnly = g.ReadOnly,
|
||||
HidePasswords = g.HidePasswords,
|
||||
});
|
||||
await dbContext.AddRangeAsync(collectionGroups);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(obj.OrganizationId);
|
||||
await base.CreateAsync(obj);
|
||||
await UserBumpAccountRevisionDateByCollectionId(obj.Id, obj.OrganizationId);
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid collectionId, Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task CreateAsync(Core.Entities.Collection obj, IEnumerable<SelectionReadOnly> groups)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cu in dbContext.CollectionUsers
|
||||
where cu.CollectionId == collectionId &&
|
||||
cu.OrganizationUserId == organizationUserId
|
||||
select cu;
|
||||
dbContext.RemoveRange(await query.ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationUserId(organizationUserId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<CollectionDetails> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return (await GetManyByUserIdAsync(userId)).FirstOrDefault(c => c.Id == id);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<Core.Entities.Collection, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id)
|
||||
{
|
||||
var collection = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var collectionGroups = await (from cg in dbContext.CollectionGroups
|
||||
where cg.CollectionId == id
|
||||
select cg).ToListAsync();
|
||||
var selectionReadOnlys = collectionGroups.Select(cg => new SelectionReadOnly
|
||||
await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Id = cg.GroupId,
|
||||
ReadOnly = cg.ReadOnly,
|
||||
HidePasswords = cg.HidePasswords,
|
||||
}).ToList();
|
||||
return new Tuple<Core.Entities.Collection, ICollection<SelectionReadOnly>>(collection, selectionReadOnlys);
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleGroups = await (from g in dbContext.Groups
|
||||
where g.OrganizationId == obj.OrganizationId
|
||||
select g.Id).ToListAsync();
|
||||
var collectionGroups = groups
|
||||
.Where(g => availibleGroups.Contains(g.Id))
|
||||
.Select(g => new CollectionGroup
|
||||
{
|
||||
CollectionId = obj.Id,
|
||||
GroupId = g.Id,
|
||||
ReadOnly = g.ReadOnly,
|
||||
HidePasswords = g.HidePasswords,
|
||||
});
|
||||
await dbContext.AddRangeAsync(collectionGroups);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(obj.OrganizationId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<CollectionDetails, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id, Guid userId)
|
||||
{
|
||||
var collection = await GetByIdAsync(id, userId);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteUserAsync(Guid collectionId, Guid organizationUserId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cg in dbContext.CollectionGroups
|
||||
where cg.CollectionId.Equals(id)
|
||||
select new SelectionReadOnly
|
||||
{
|
||||
Id = cg.GroupId,
|
||||
ReadOnly = cg.ReadOnly,
|
||||
HidePasswords = cg.HidePasswords,
|
||||
};
|
||||
var configurations = await query.ToArrayAsync();
|
||||
return new Tuple<CollectionDetails, ICollection<SelectionReadOnly>>(collection, configurations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var query = new CollectionReadCountByOrganizationIdQuery(organizationId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Collection>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationId
|
||||
select c;
|
||||
var collections = await query.ToArrayAsync();
|
||||
return collections;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionDetails>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return (await new UserCollectionDetailsQuery(userId).Run(dbContext).ToListAsync())
|
||||
.GroupBy(c => c.Id)
|
||||
.Select(g => new CollectionDetails
|
||||
{
|
||||
Id = g.Key,
|
||||
OrganizationId = g.FirstOrDefault().OrganizationId,
|
||||
Name = g.FirstOrDefault().Name,
|
||||
ExternalId = g.FirstOrDefault().ExternalId,
|
||||
CreationDate = g.FirstOrDefault().CreationDate,
|
||||
RevisionDate = g.FirstOrDefault().RevisionDate,
|
||||
ReadOnly = g.Min(c => c.ReadOnly),
|
||||
HidePasswords = g.Min(c => c.HidePasswords)
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<SelectionReadOnly>> GetManyUsersByIdAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cu in dbContext.CollectionUsers
|
||||
where cu.CollectionId == id
|
||||
select cu;
|
||||
var collectionUsers = await query.ToListAsync();
|
||||
return collectionUsers.Select(cu => new SelectionReadOnly
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Id = cu.OrganizationUserId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
}).ToArray();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cu in dbContext.CollectionUsers
|
||||
where cu.CollectionId == collectionId &&
|
||||
cu.OrganizationUserId == organizationUserId
|
||||
select cu;
|
||||
dbContext.RemoveRange(await query.ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationUserId(organizationUserId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Core.Entities.Collection collection, IEnumerable<SelectionReadOnly> groups)
|
||||
{
|
||||
await base.ReplaceAsync(collection);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<CollectionDetails> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var groupsInOrg = dbContext.Groups.Where(g => g.OrganizationId == collection.OrganizationId);
|
||||
var modifiedGroupEntities = dbContext.Groups.Where(x => groups.Select(x => x.Id).Contains(x.Id));
|
||||
var target = (from cg in dbContext.CollectionGroups
|
||||
join g in modifiedGroupEntities
|
||||
on cg.CollectionId equals collection.Id into s_g
|
||||
from g in s_g.DefaultIfEmpty()
|
||||
where g == null || cg.GroupId == g.Id
|
||||
select new { cg, g }).AsNoTracking();
|
||||
var source = (from g in modifiedGroupEntities
|
||||
from cg in dbContext.CollectionGroups
|
||||
.Where(cg => cg.CollectionId == collection.Id && cg.GroupId == g.Id).DefaultIfEmpty()
|
||||
select new { cg, g }).AsNoTracking();
|
||||
var union = await target
|
||||
.Union(source)
|
||||
.Where(x =>
|
||||
x.cg == null ||
|
||||
((x.g == null || x.g.Id == x.cg.GroupId) &&
|
||||
(x.cg.CollectionId == collection.Id)))
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
var insert = union.Where(x => x.cg == null && groupsInOrg.Any(c => x.g.Id == c.Id))
|
||||
.Select(x => new CollectionGroup
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return (await GetManyByUserIdAsync(userId)).FirstOrDefault(c => c.Id == id);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<Core.Entities.Collection, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id)
|
||||
{
|
||||
var collection = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var collectionGroups = await (from cg in dbContext.CollectionGroups
|
||||
where cg.CollectionId == id
|
||||
select cg).ToListAsync();
|
||||
var selectionReadOnlys = collectionGroups.Select(cg => new SelectionReadOnly
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.g.Id,
|
||||
ReadOnly = groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly,
|
||||
HidePasswords = groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords,
|
||||
Id = cg.GroupId,
|
||||
ReadOnly = cg.ReadOnly,
|
||||
HidePasswords = cg.HidePasswords,
|
||||
}).ToList();
|
||||
var update = union
|
||||
.Where(
|
||||
x => x.g != null &&
|
||||
x.cg != null &&
|
||||
(x.cg.ReadOnly != groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly ||
|
||||
x.cg.HidePasswords != groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords)
|
||||
)
|
||||
.Select(x => new CollectionGroup
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.g.Id,
|
||||
ReadOnly = groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly,
|
||||
HidePasswords = groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords,
|
||||
});
|
||||
var delete = union
|
||||
.Where(
|
||||
x => x.g == null &&
|
||||
x.cg.CollectionId == collection.Id
|
||||
)
|
||||
.Select(x => new CollectionGroup
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.cg.GroupId,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.UpdateRange(update);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByCollectionId(collection.Id, collection.OrganizationId);
|
||||
return new Tuple<Core.Entities.Collection, ICollection<SelectionReadOnly>>(collection, selectionReadOnlys);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateUsersAsync(Guid id, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Tuple<CollectionDetails, ICollection<SelectionReadOnly>>> GetByIdWithGroupsAsync(Guid id, Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var procedure = new CollectionUserUpdateUsersQuery(id, users);
|
||||
var updateData = await procedure.Update.BuildInMemory(dbContext);
|
||||
dbContext.UpdateRange(updateData);
|
||||
var insertData = await procedure.Insert.BuildInMemory(dbContext);
|
||||
await dbContext.AddRangeAsync(insertData);
|
||||
dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync());
|
||||
var collection = await GetByIdAsync(id, userId);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cg in dbContext.CollectionGroups
|
||||
where cg.CollectionId.Equals(id)
|
||||
select new SelectionReadOnly
|
||||
{
|
||||
Id = cg.GroupId,
|
||||
ReadOnly = cg.ReadOnly,
|
||||
HidePasswords = cg.HidePasswords,
|
||||
};
|
||||
var configurations = await query.ToArrayAsync();
|
||||
return new Tuple<CollectionDetails, ICollection<SelectionReadOnly>>(collection, configurations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var query = new CollectionReadCountByOrganizationIdQuery(organizationId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Collection>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationId
|
||||
select c;
|
||||
var collections = await query.ToArrayAsync();
|
||||
return collections;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionDetails>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return (await new UserCollectionDetailsQuery(userId).Run(dbContext).ToListAsync())
|
||||
.GroupBy(c => c.Id)
|
||||
.Select(g => new CollectionDetails
|
||||
{
|
||||
Id = g.Key,
|
||||
OrganizationId = g.FirstOrDefault().OrganizationId,
|
||||
Name = g.FirstOrDefault().Name,
|
||||
ExternalId = g.FirstOrDefault().ExternalId,
|
||||
CreationDate = g.FirstOrDefault().CreationDate,
|
||||
RevisionDate = g.FirstOrDefault().RevisionDate,
|
||||
ReadOnly = g.Min(c => c.ReadOnly),
|
||||
HidePasswords = g.Min(c => c.HidePasswords)
|
||||
}).ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<SelectionReadOnly>> GetManyUsersByIdAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from cu in dbContext.CollectionUsers
|
||||
where cu.CollectionId == id
|
||||
select cu;
|
||||
var collectionUsers = await query.ToListAsync();
|
||||
return collectionUsers.Select(cu => new SelectionReadOnly
|
||||
{
|
||||
Id = cu.OrganizationUserId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
}).ToArray();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Core.Entities.Collection collection, IEnumerable<SelectionReadOnly> groups)
|
||||
{
|
||||
await base.ReplaceAsync(collection);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var groupsInOrg = dbContext.Groups.Where(g => g.OrganizationId == collection.OrganizationId);
|
||||
var modifiedGroupEntities = dbContext.Groups.Where(x => groups.Select(x => x.Id).Contains(x.Id));
|
||||
var target = (from cg in dbContext.CollectionGroups
|
||||
join g in modifiedGroupEntities
|
||||
on cg.CollectionId equals collection.Id into s_g
|
||||
from g in s_g.DefaultIfEmpty()
|
||||
where g == null || cg.GroupId == g.Id
|
||||
select new { cg, g }).AsNoTracking();
|
||||
var source = (from g in modifiedGroupEntities
|
||||
from cg in dbContext.CollectionGroups
|
||||
.Where(cg => cg.CollectionId == collection.Id && cg.GroupId == g.Id).DefaultIfEmpty()
|
||||
select new { cg, g }).AsNoTracking();
|
||||
var union = await target
|
||||
.Union(source)
|
||||
.Where(x =>
|
||||
x.cg == null ||
|
||||
((x.g == null || x.g.Id == x.cg.GroupId) &&
|
||||
(x.cg.CollectionId == collection.Id)))
|
||||
.AsNoTracking()
|
||||
.ToListAsync();
|
||||
var insert = union.Where(x => x.cg == null && groupsInOrg.Any(c => x.g.Id == c.Id))
|
||||
.Select(x => new CollectionGroup
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.g.Id,
|
||||
ReadOnly = groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly,
|
||||
HidePasswords = groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords,
|
||||
}).ToList();
|
||||
var update = union
|
||||
.Where(
|
||||
x => x.g != null &&
|
||||
x.cg != null &&
|
||||
(x.cg.ReadOnly != groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly ||
|
||||
x.cg.HidePasswords != groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords)
|
||||
)
|
||||
.Select(x => new CollectionGroup
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.g.Id,
|
||||
ReadOnly = groups.FirstOrDefault(g => g.Id == x.g.Id).ReadOnly,
|
||||
HidePasswords = groups.FirstOrDefault(g => g.Id == x.g.Id).HidePasswords,
|
||||
});
|
||||
var delete = union
|
||||
.Where(
|
||||
x => x.g == null &&
|
||||
x.cg.CollectionId == collection.Id
|
||||
)
|
||||
.Select(x => new CollectionGroup
|
||||
{
|
||||
CollectionId = collection.Id,
|
||||
GroupId = x.cg.GroupId,
|
||||
})
|
||||
.ToList();
|
||||
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
dbContext.UpdateRange(update);
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByCollectionId(collection.Id, collection.OrganizationId);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateUsersAsync(Guid id, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var procedure = new CollectionUserUpdateUsersQuery(id, users);
|
||||
var updateData = await procedure.Update.BuildInMemory(dbContext);
|
||||
dbContext.UpdateRange(updateData);
|
||||
var insertData = await procedure.Insert.BuildInMemory(dbContext);
|
||||
await dbContext.AddRangeAsync(insertData);
|
||||
dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,139 +1,140 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class DatabaseContext : DbContext
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public const string postgresIndetermanisticCollation = "postgresIndetermanisticCollation";
|
||||
|
||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||
: base(options)
|
||||
{ }
|
||||
|
||||
public DbSet<Cipher> Ciphers { get; set; }
|
||||
public DbSet<Collection> Collections { get; set; }
|
||||
public DbSet<CollectionCipher> CollectionCiphers { get; set; }
|
||||
public DbSet<CollectionGroup> CollectionGroups { get; set; }
|
||||
public DbSet<CollectionUser> CollectionUsers { get; set; }
|
||||
public DbSet<Device> Devices { get; set; }
|
||||
public DbSet<EmergencyAccess> EmergencyAccesses { get; set; }
|
||||
public DbSet<Event> Events { get; set; }
|
||||
public DbSet<Folder> Folders { get; set; }
|
||||
public DbSet<Grant> Grants { get; set; }
|
||||
public DbSet<Group> Groups { get; set; }
|
||||
public DbSet<GroupUser> GroupUsers { get; set; }
|
||||
public DbSet<Installation> Installations { get; set; }
|
||||
public DbSet<Organization> Organizations { get; set; }
|
||||
public DbSet<OrganizationApiKey> OrganizationApiKeys { get; set; }
|
||||
public DbSet<OrganizationSponsorship> OrganizationSponsorships { get; set; }
|
||||
public DbSet<OrganizationConnection> OrganizationConnections { get; set; }
|
||||
public DbSet<OrganizationUser> OrganizationUsers { get; set; }
|
||||
public DbSet<Policy> Policies { get; set; }
|
||||
public DbSet<Provider> Providers { get; set; }
|
||||
public DbSet<ProviderUser> ProviderUsers { get; set; }
|
||||
public DbSet<ProviderOrganization> ProviderOrganizations { get; set; }
|
||||
public DbSet<Send> Sends { get; set; }
|
||||
public DbSet<SsoConfig> SsoConfigs { get; set; }
|
||||
public DbSet<SsoUser> SsoUsers { get; set; }
|
||||
public DbSet<TaxRate> TaxRates { get; set; }
|
||||
public DbSet<Transaction> Transactions { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
public class DatabaseContext : DbContext
|
||||
{
|
||||
var eCipher = builder.Entity<Cipher>();
|
||||
var eCollection = builder.Entity<Collection>();
|
||||
var eCollectionCipher = builder.Entity<CollectionCipher>();
|
||||
var eCollectionUser = builder.Entity<CollectionUser>();
|
||||
var eCollectionGroup = builder.Entity<CollectionGroup>();
|
||||
var eDevice = builder.Entity<Device>();
|
||||
var eEmergencyAccess = builder.Entity<EmergencyAccess>();
|
||||
var eEvent = builder.Entity<Event>();
|
||||
var eFolder = builder.Entity<Folder>();
|
||||
var eGrant = builder.Entity<Grant>();
|
||||
var eGroup = builder.Entity<Group>();
|
||||
var eGroupUser = builder.Entity<GroupUser>();
|
||||
var eInstallation = builder.Entity<Installation>();
|
||||
var eOrganization = builder.Entity<Organization>();
|
||||
var eOrganizationSponsorship = builder.Entity<OrganizationSponsorship>();
|
||||
var eOrganizationUser = builder.Entity<OrganizationUser>();
|
||||
var ePolicy = builder.Entity<Policy>();
|
||||
var eProvider = builder.Entity<Provider>();
|
||||
var eProviderUser = builder.Entity<ProviderUser>();
|
||||
var eProviderOrganization = builder.Entity<ProviderOrganization>();
|
||||
var eSend = builder.Entity<Send>();
|
||||
var eSsoConfig = builder.Entity<SsoConfig>();
|
||||
var eSsoUser = builder.Entity<SsoUser>();
|
||||
var eTaxRate = builder.Entity<TaxRate>();
|
||||
var eTransaction = builder.Entity<Transaction>();
|
||||
var eUser = builder.Entity<User>();
|
||||
var eOrganizationApiKey = builder.Entity<OrganizationApiKey>();
|
||||
var eOrganizationConnection = builder.Entity<OrganizationConnection>();
|
||||
public const string postgresIndetermanisticCollation = "postgresIndetermanisticCollation";
|
||||
|
||||
eCipher.Property(c => c.Id).ValueGeneratedNever();
|
||||
eCollection.Property(c => c.Id).ValueGeneratedNever();
|
||||
eEmergencyAccess.Property(c => c.Id).ValueGeneratedNever();
|
||||
eEvent.Property(c => c.Id).ValueGeneratedNever();
|
||||
eFolder.Property(c => c.Id).ValueGeneratedNever();
|
||||
eGroup.Property(c => c.Id).ValueGeneratedNever();
|
||||
eInstallation.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganization.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationSponsorship.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
ePolicy.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProvider.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProviderUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProviderOrganization.Property(c => c.Id).ValueGeneratedNever();
|
||||
eSend.Property(c => c.Id).ValueGeneratedNever();
|
||||
eTransaction.Property(c => c.Id).ValueGeneratedNever();
|
||||
eUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationApiKey.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationConnection.Property(c => c.Id).ValueGeneratedNever();
|
||||
public DatabaseContext(DbContextOptions<DatabaseContext> options)
|
||||
: base(options)
|
||||
{ }
|
||||
|
||||
eCollectionCipher.HasKey(cc => new { cc.CollectionId, cc.CipherId });
|
||||
eCollectionUser.HasKey(cu => new { cu.CollectionId, cu.OrganizationUserId });
|
||||
eCollectionGroup.HasKey(cg => new { cg.CollectionId, cg.GroupId });
|
||||
eGrant.HasKey(x => x.Key);
|
||||
eGroupUser.HasKey(gu => new { gu.GroupId, gu.OrganizationUserId });
|
||||
public DbSet<Cipher> Ciphers { get; set; }
|
||||
public DbSet<Collection> Collections { get; set; }
|
||||
public DbSet<CollectionCipher> CollectionCiphers { get; set; }
|
||||
public DbSet<CollectionGroup> CollectionGroups { get; set; }
|
||||
public DbSet<CollectionUser> CollectionUsers { get; set; }
|
||||
public DbSet<Device> Devices { get; set; }
|
||||
public DbSet<EmergencyAccess> EmergencyAccesses { get; set; }
|
||||
public DbSet<Event> Events { get; set; }
|
||||
public DbSet<Folder> Folders { get; set; }
|
||||
public DbSet<Grant> Grants { get; set; }
|
||||
public DbSet<Group> Groups { get; set; }
|
||||
public DbSet<GroupUser> GroupUsers { get; set; }
|
||||
public DbSet<Installation> Installations { get; set; }
|
||||
public DbSet<Organization> Organizations { get; set; }
|
||||
public DbSet<OrganizationApiKey> OrganizationApiKeys { get; set; }
|
||||
public DbSet<OrganizationSponsorship> OrganizationSponsorships { get; set; }
|
||||
public DbSet<OrganizationConnection> OrganizationConnections { get; set; }
|
||||
public DbSet<OrganizationUser> OrganizationUsers { get; set; }
|
||||
public DbSet<Policy> Policies { get; set; }
|
||||
public DbSet<Provider> Providers { get; set; }
|
||||
public DbSet<ProviderUser> ProviderUsers { get; set; }
|
||||
public DbSet<ProviderOrganization> ProviderOrganizations { get; set; }
|
||||
public DbSet<Send> Sends { get; set; }
|
||||
public DbSet<SsoConfig> SsoConfigs { get; set; }
|
||||
public DbSet<SsoUser> SsoUsers { get; set; }
|
||||
public DbSet<TaxRate> TaxRates { get; set; }
|
||||
public DbSet<Transaction> Transactions { get; set; }
|
||||
public DbSet<User> Users { get; set; }
|
||||
|
||||
|
||||
if (Database.IsNpgsql())
|
||||
protected override void OnModelCreating(ModelBuilder builder)
|
||||
{
|
||||
// the postgres provider doesn't currently support database level non-deterministic collations.
|
||||
// see https://www.npgsql.org/efcore/misc/collations-and-case-sensitivity.html#database-collation
|
||||
builder.HasCollation(postgresIndetermanisticCollation, locale: "en-u-ks-primary", provider: "icu", deterministic: false);
|
||||
eUser.Property(e => e.Email).UseCollation(postgresIndetermanisticCollation);
|
||||
eSsoUser.Property(e => e.ExternalId).UseCollation(postgresIndetermanisticCollation);
|
||||
eOrganization.Property(e => e.Identifier).UseCollation(postgresIndetermanisticCollation);
|
||||
//
|
||||
}
|
||||
var eCipher = builder.Entity<Cipher>();
|
||||
var eCollection = builder.Entity<Collection>();
|
||||
var eCollectionCipher = builder.Entity<CollectionCipher>();
|
||||
var eCollectionUser = builder.Entity<CollectionUser>();
|
||||
var eCollectionGroup = builder.Entity<CollectionGroup>();
|
||||
var eDevice = builder.Entity<Device>();
|
||||
var eEmergencyAccess = builder.Entity<EmergencyAccess>();
|
||||
var eEvent = builder.Entity<Event>();
|
||||
var eFolder = builder.Entity<Folder>();
|
||||
var eGrant = builder.Entity<Grant>();
|
||||
var eGroup = builder.Entity<Group>();
|
||||
var eGroupUser = builder.Entity<GroupUser>();
|
||||
var eInstallation = builder.Entity<Installation>();
|
||||
var eOrganization = builder.Entity<Organization>();
|
||||
var eOrganizationSponsorship = builder.Entity<OrganizationSponsorship>();
|
||||
var eOrganizationUser = builder.Entity<OrganizationUser>();
|
||||
var ePolicy = builder.Entity<Policy>();
|
||||
var eProvider = builder.Entity<Provider>();
|
||||
var eProviderUser = builder.Entity<ProviderUser>();
|
||||
var eProviderOrganization = builder.Entity<ProviderOrganization>();
|
||||
var eSend = builder.Entity<Send>();
|
||||
var eSsoConfig = builder.Entity<SsoConfig>();
|
||||
var eSsoUser = builder.Entity<SsoUser>();
|
||||
var eTaxRate = builder.Entity<TaxRate>();
|
||||
var eTransaction = builder.Entity<Transaction>();
|
||||
var eUser = builder.Entity<User>();
|
||||
var eOrganizationApiKey = builder.Entity<OrganizationApiKey>();
|
||||
var eOrganizationConnection = builder.Entity<OrganizationConnection>();
|
||||
|
||||
eCipher.ToTable(nameof(Cipher));
|
||||
eCollection.ToTable(nameof(Collection));
|
||||
eCollectionCipher.ToTable(nameof(CollectionCipher));
|
||||
eDevice.ToTable(nameof(Device));
|
||||
eEmergencyAccess.ToTable(nameof(EmergencyAccess));
|
||||
eEvent.ToTable(nameof(Event));
|
||||
eFolder.ToTable(nameof(Folder));
|
||||
eGrant.ToTable(nameof(Grant));
|
||||
eGroup.ToTable(nameof(Group));
|
||||
eGroupUser.ToTable(nameof(GroupUser));
|
||||
eInstallation.ToTable(nameof(Installation));
|
||||
eOrganization.ToTable(nameof(Organization));
|
||||
eOrganizationSponsorship.ToTable(nameof(OrganizationSponsorship));
|
||||
eOrganizationUser.ToTable(nameof(OrganizationUser));
|
||||
ePolicy.ToTable(nameof(Policy));
|
||||
eProvider.ToTable(nameof(Provider));
|
||||
eProviderUser.ToTable(nameof(ProviderUser));
|
||||
eProviderOrganization.ToTable(nameof(ProviderOrganization));
|
||||
eSend.ToTable(nameof(Send));
|
||||
eSsoConfig.ToTable(nameof(SsoConfig));
|
||||
eSsoUser.ToTable(nameof(SsoUser));
|
||||
eTaxRate.ToTable(nameof(TaxRate));
|
||||
eTransaction.ToTable(nameof(Transaction));
|
||||
eUser.ToTable(nameof(User));
|
||||
eOrganizationApiKey.ToTable(nameof(OrganizationApiKey));
|
||||
eOrganizationConnection.ToTable(nameof(OrganizationConnection));
|
||||
eCipher.Property(c => c.Id).ValueGeneratedNever();
|
||||
eCollection.Property(c => c.Id).ValueGeneratedNever();
|
||||
eEmergencyAccess.Property(c => c.Id).ValueGeneratedNever();
|
||||
eEvent.Property(c => c.Id).ValueGeneratedNever();
|
||||
eFolder.Property(c => c.Id).ValueGeneratedNever();
|
||||
eGroup.Property(c => c.Id).ValueGeneratedNever();
|
||||
eInstallation.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganization.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationSponsorship.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
ePolicy.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProvider.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProviderUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
eProviderOrganization.Property(c => c.Id).ValueGeneratedNever();
|
||||
eSend.Property(c => c.Id).ValueGeneratedNever();
|
||||
eTransaction.Property(c => c.Id).ValueGeneratedNever();
|
||||
eUser.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationApiKey.Property(c => c.Id).ValueGeneratedNever();
|
||||
eOrganizationConnection.Property(c => c.Id).ValueGeneratedNever();
|
||||
|
||||
eCollectionCipher.HasKey(cc => new { cc.CollectionId, cc.CipherId });
|
||||
eCollectionUser.HasKey(cu => new { cu.CollectionId, cu.OrganizationUserId });
|
||||
eCollectionGroup.HasKey(cg => new { cg.CollectionId, cg.GroupId });
|
||||
eGrant.HasKey(x => x.Key);
|
||||
eGroupUser.HasKey(gu => new { gu.GroupId, gu.OrganizationUserId });
|
||||
|
||||
|
||||
if (Database.IsNpgsql())
|
||||
{
|
||||
// the postgres provider doesn't currently support database level non-deterministic collations.
|
||||
// see https://www.npgsql.org/efcore/misc/collations-and-case-sensitivity.html#database-collation
|
||||
builder.HasCollation(postgresIndetermanisticCollation, locale: "en-u-ks-primary", provider: "icu", deterministic: false);
|
||||
eUser.Property(e => e.Email).UseCollation(postgresIndetermanisticCollation);
|
||||
eSsoUser.Property(e => e.ExternalId).UseCollation(postgresIndetermanisticCollation);
|
||||
eOrganization.Property(e => e.Identifier).UseCollation(postgresIndetermanisticCollation);
|
||||
//
|
||||
}
|
||||
|
||||
eCipher.ToTable(nameof(Cipher));
|
||||
eCollection.ToTable(nameof(Collection));
|
||||
eCollectionCipher.ToTable(nameof(CollectionCipher));
|
||||
eDevice.ToTable(nameof(Device));
|
||||
eEmergencyAccess.ToTable(nameof(EmergencyAccess));
|
||||
eEvent.ToTable(nameof(Event));
|
||||
eFolder.ToTable(nameof(Folder));
|
||||
eGrant.ToTable(nameof(Grant));
|
||||
eGroup.ToTable(nameof(Group));
|
||||
eGroupUser.ToTable(nameof(GroupUser));
|
||||
eInstallation.ToTable(nameof(Installation));
|
||||
eOrganization.ToTable(nameof(Organization));
|
||||
eOrganizationSponsorship.ToTable(nameof(OrganizationSponsorship));
|
||||
eOrganizationUser.ToTable(nameof(OrganizationUser));
|
||||
ePolicy.ToTable(nameof(Policy));
|
||||
eProvider.ToTable(nameof(Provider));
|
||||
eProviderUser.ToTable(nameof(ProviderUser));
|
||||
eProviderOrganization.ToTable(nameof(ProviderOrganization));
|
||||
eSend.ToTable(nameof(Send));
|
||||
eSsoConfig.ToTable(nameof(SsoConfig));
|
||||
eSsoUser.ToTable(nameof(SsoUser));
|
||||
eTaxRate.ToTable(nameof(TaxRate));
|
||||
eTransaction.ToTable(nameof(Transaction));
|
||||
eUser.ToTable(nameof(User));
|
||||
eOrganizationApiKey.ToTable(nameof(OrganizationApiKey));
|
||||
eOrganizationConnection.ToTable(nameof(OrganizationConnection));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,67 +4,68 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class DeviceRepository : Repository<Core.Entities.Device, Device, Guid>, IDeviceRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public DeviceRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Devices)
|
||||
{ }
|
||||
|
||||
public async Task ClearPushTokenAsync(Guid id)
|
||||
public class DeviceRepository : Repository<Core.Entities.Device, Device, Guid>, IDeviceRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Id == id);
|
||||
dbContext.AttachRange(query);
|
||||
await query.ForEachAsync(x => x.PushToken = null);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
public DeviceRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Devices)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Device> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
var device = await base.GetByIdAsync(id);
|
||||
if (device == null || device.UserId != userId)
|
||||
public async Task ClearPushTokenAsync(Guid id)
|
||||
{
|
||||
return null;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Id == id);
|
||||
dbContext.AttachRange(query);
|
||||
await query.ForEachAsync(x => x.PushToken = null);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
return Mapper.Map<Core.Entities.Device>(device);
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.Device> GetByIdentifierAsync(string identifier)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.Device> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Identifier == identifier);
|
||||
var device = await query.FirstOrDefaultAsync();
|
||||
var device = await base.GetByIdAsync(id);
|
||||
if (device == null || device.UserId != userId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return Mapper.Map<Core.Entities.Device>(device);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.Device> GetByIdentifierAsync(string identifier, Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.Device> GetByIdentifierAsync(string identifier)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Identifier == identifier && d.UserId == userId);
|
||||
var device = await query.FirstOrDefaultAsync();
|
||||
return Mapper.Map<Core.Entities.Device>(device);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Identifier == identifier);
|
||||
var device = await query.FirstOrDefaultAsync();
|
||||
return Mapper.Map<Core.Entities.Device>(device);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Device>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.Device> GetByIdentifierAsync(string identifier, Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.UserId == userId);
|
||||
var devices = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Device>>(devices);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.Identifier == identifier && d.UserId == userId);
|
||||
var device = await query.FirstOrDefaultAsync();
|
||||
return Mapper.Map<Core.Entities.Device>(device);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Device>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.Devices.Where(d => d.UserId == userId);
|
||||
var devices = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Device>>(devices);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,101 +7,102 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class EmergencyAccessRepository : Repository<Core.Entities.EmergencyAccess, EmergencyAccess, Guid>, IEmergencyAccessRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public EmergencyAccessRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.EmergencyAccesses)
|
||||
{ }
|
||||
|
||||
public async Task<int> GetCountByGrantorIdEmailAsync(Guid grantorId, string email, bool onlyRegisteredUsers)
|
||||
public class EmergencyAccessRepository : Repository<Core.Entities.EmergencyAccess, EmergencyAccess, Guid>, IEmergencyAccessRepository
|
||||
{
|
||||
var query = new EmergencyAccessReadCountByGrantorIdEmailQuery(grantorId, email, onlyRegisteredUsers);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
public EmergencyAccessRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.EmergencyAccesses)
|
||||
{ }
|
||||
|
||||
public async Task<EmergencyAccessDetails> GetDetailsByIdGrantorIdAsync(Guid id, Guid grantorId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<int> GetCountByGrantorIdEmailAsync(Guid grantorId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Id == id &&
|
||||
ea.GrantorId == grantorId
|
||||
);
|
||||
return await query.FirstOrDefaultAsync();
|
||||
var query = new EmergencyAccessReadCountByGrantorIdEmailQuery(grantorId, email, onlyRegisteredUsers);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetExpiredRecoveriesAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<EmergencyAccessDetails> GetDetailsByIdGrantorIdAsync(Guid id, Guid grantorId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Status == EmergencyAccessStatusType.RecoveryInitiated
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetManyDetailsByGranteeIdAsync(Guid granteeId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.GranteeId == granteeId
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetManyDetailsByGrantorIdAsync(Guid grantorId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.GrantorId == grantorId
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessNotify>> GetManyToNotifyAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Status == EmergencyAccessStatusType.RecoveryInitiated
|
||||
);
|
||||
var notifies = await query.Select(ea => new EmergencyAccessNotify
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Id = ea.Id,
|
||||
GrantorId = ea.GrantorId,
|
||||
GranteeId = ea.GranteeId,
|
||||
Email = ea.Email,
|
||||
KeyEncrypted = ea.KeyEncrypted,
|
||||
Type = ea.Type,
|
||||
Status = ea.Status,
|
||||
WaitTimeDays = ea.WaitTimeDays,
|
||||
RecoveryInitiatedDate = ea.RecoveryInitiatedDate,
|
||||
LastNotificationDate = ea.LastNotificationDate,
|
||||
CreationDate = ea.CreationDate,
|
||||
RevisionDate = ea.RevisionDate,
|
||||
GranteeName = ea.GranteeName,
|
||||
GranteeEmail = ea.GranteeEmail,
|
||||
GrantorEmail = ea.GrantorEmail,
|
||||
}).ToListAsync();
|
||||
return notifies;
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Id == id &&
|
||||
ea.GrantorId == grantorId
|
||||
);
|
||||
return await query.FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetExpiredRecoveriesAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Status == EmergencyAccessStatusType.RecoveryInitiated
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetManyDetailsByGranteeIdAsync(Guid granteeId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.GranteeId == granteeId
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessDetails>> GetManyDetailsByGrantorIdAsync(Guid grantorId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.GrantorId == grantorId
|
||||
);
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<EmergencyAccessNotify>> GetManyToNotifyAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new EmergencyAccessDetailsViewQuery();
|
||||
var query = view.Run(dbContext).Where(ea =>
|
||||
ea.Status == EmergencyAccessStatusType.RecoveryInitiated
|
||||
);
|
||||
var notifies = await query.Select(ea => new EmergencyAccessNotify
|
||||
{
|
||||
Id = ea.Id,
|
||||
GrantorId = ea.GrantorId,
|
||||
GranteeId = ea.GranteeId,
|
||||
Email = ea.Email,
|
||||
KeyEncrypted = ea.KeyEncrypted,
|
||||
Type = ea.Type,
|
||||
Status = ea.Status,
|
||||
WaitTimeDays = ea.WaitTimeDays,
|
||||
RecoveryInitiatedDate = ea.RecoveryInitiatedDate,
|
||||
LastNotificationDate = ea.LastNotificationDate,
|
||||
CreationDate = ea.CreationDate,
|
||||
RevisionDate = ea.RevisionDate,
|
||||
GranteeName = ea.GranteeName,
|
||||
GranteeEmail = ea.GranteeEmail,
|
||||
GrantorEmail = ea.GrantorEmail,
|
||||
}).ToListAsync();
|
||||
return notifies;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -8,195 +8,196 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Cipher = Bit.Core.Entities.Cipher;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class EventRepository : Repository<Core.Entities.Event, Event, Guid>, IEventRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public EventRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Events)
|
||||
{ }
|
||||
|
||||
public async Task CreateAsync(IEvent e)
|
||||
public class EventRepository : Repository<Core.Entities.Event, Event, Guid>, IEventRepository
|
||||
{
|
||||
if (e is not Core.Entities.Event ev)
|
||||
{
|
||||
ev = new Core.Entities.Event(e);
|
||||
}
|
||||
public EventRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Events)
|
||||
{ }
|
||||
|
||||
await base.CreateAsync(ev);
|
||||
}
|
||||
|
||||
public async Task CreateManyAsync(IEnumerable<IEvent> entities)
|
||||
{
|
||||
if (!entities?.Any() ?? true)
|
||||
public async Task CreateAsync(IEvent e)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!entities.Skip(1).Any())
|
||||
{
|
||||
await CreateAsync(entities.First());
|
||||
return;
|
||||
}
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var tableEvents = entities.Select(e => e as Core.Entities.Event ?? new Core.Entities.Event(e));
|
||||
var entityEvents = Mapper.Map<List<Event>>(tableEvents);
|
||||
entityEvents.ForEach(e => e.SetNewId());
|
||||
await dbContext.BulkCopyAsync(entityEvents);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByCipherAsync(Cipher cipher, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByCipherIdQuery(cipher, startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
if (e is not Core.Entities.Event ev)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
ev = new Core.Entities.Event(e);
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
|
||||
await base.CreateAsync(ev);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByOrganizationActingUserAsync(Guid organizationId, Guid actingUserId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
public async Task CreateManyAsync(IEnumerable<IEvent> entities)
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByOrganizationIdActingUserIdQuery(organizationId, actingUserId,
|
||||
startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
if (!entities?.Any() ?? true)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
return;
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByProviderAsync(Guid providerId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByProviderIdQuery(providerId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
if (!entities.Skip(1).Any())
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
await CreateAsync(entities.First());
|
||||
return;
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByProviderActingUserAsync(Guid providerId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByProviderIdActingUserIdQuery(providerId, actingUserId,
|
||||
startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var tableEvents = entities.Select(e => e as Core.Entities.Event ?? new Core.Entities.Event(e));
|
||||
var entityEvents = Mapper.Map<List<Event>>(tableEvents);
|
||||
entityEvents.ForEach(e => e.SetNewId());
|
||||
await dbContext.BulkCopyAsync(entityEvents);
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByOrganizationAsync(Guid organizationId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
public async Task<PagedResult<IEvent>> GetManyByCipherAsync(Cipher cipher, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByOrganizationIdQuery(organizationId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByUserAsync(Guid userId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByUserIdQuery(userId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByCipherIdQuery(cipher, startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByOrganizationActingUserAsync(Guid organizationId, Guid actingUserId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByOrganizationIdActingUserIdQuery(organizationId, actingUserId,
|
||||
startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByProviderAsync(Guid providerId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByProviderIdQuery(providerId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByProviderActingUserAsync(Guid providerId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByProviderIdActingUserIdQuery(providerId, actingUserId,
|
||||
startDate, endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByOrganizationAsync(Guid organizationId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByOrganizationIdQuery(organizationId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<PagedResult<IEvent>> GetManyByUserAsync(Guid userId, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
DateTime? beforeDate = null;
|
||||
if (!string.IsNullOrWhiteSpace(pageOptions.ContinuationToken) &&
|
||||
long.TryParse(pageOptions.ContinuationToken, out var binaryDate))
|
||||
{
|
||||
beforeDate = DateTime.SpecifyKind(DateTime.FromBinary(binaryDate), DateTimeKind.Utc);
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new EventReadPageByUserIdQuery(userId, startDate,
|
||||
endDate, beforeDate, pageOptions);
|
||||
var events = await query.Run(dbContext).ToListAsync();
|
||||
|
||||
var result = new PagedResult<IEvent>();
|
||||
if (events.Any() && events.Count >= pageOptions.PageSize)
|
||||
{
|
||||
result.ContinuationToken = events.Last().Date.ToBinary().ToString();
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
result.Data.AddRange(events);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,35 +4,36 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class FolderRepository : Repository<Core.Entities.Folder, Folder, Guid>, IFolderRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public FolderRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Folders)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Folder> GetByIdAsync(Guid id, Guid userId)
|
||||
public class FolderRepository : Repository<Core.Entities.Folder, Folder, Guid>, IFolderRepository
|
||||
{
|
||||
var folder = await base.GetByIdAsync(id);
|
||||
if (folder == null || folder.UserId != userId)
|
||||
public FolderRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Folders)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Folder> GetByIdAsync(Guid id, Guid userId)
|
||||
{
|
||||
return null;
|
||||
var folder = await base.GetByIdAsync(id);
|
||||
if (folder == null || folder.UserId != userId)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
return folder;
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Folder>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Folder>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from f in dbContext.Folders
|
||||
where f.UserId == userId
|
||||
select f;
|
||||
var folders = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Folder>>(folders);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from f in dbContext.Folders
|
||||
where f.UserId == userId
|
||||
select f;
|
||||
var folders = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Folder>>(folders);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,91 +4,92 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class GrantRepository : BaseEntityFrameworkRepository, IGrantRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public GrantRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task DeleteByKeyAsync(string key)
|
||||
public class GrantRepository : BaseEntityFrameworkRepository, IGrantRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.Key == key
|
||||
select g;
|
||||
dbContext.Remove(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
public GrantRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task DeleteManyAsync(string subjectId, string sessionId, string clientId, string type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteByKeyAsync(string key)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.SubjectId == subjectId &&
|
||||
g.ClientId == clientId &&
|
||||
g.SessionId == sessionId &&
|
||||
g.Type == type
|
||||
select g;
|
||||
dbContext.Remove(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.Grant> GetByKeyAsync(string key)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.Key == key
|
||||
select g;
|
||||
var grant = await query.FirstOrDefaultAsync();
|
||||
return grant;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Grant>> GetManyAsync(string subjectId, string sessionId, string clientId, string type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.SubjectId == subjectId &&
|
||||
g.ClientId == clientId &&
|
||||
g.SessionId == sessionId &&
|
||||
g.Type == type
|
||||
select g;
|
||||
var grants = await query.ToListAsync();
|
||||
return (ICollection<Core.Entities.Grant>)grants;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Core.Entities.Grant obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var existingGrant = await (from g in dbContext.Grants
|
||||
where g.Key == obj.Key
|
||||
select g).FirstOrDefaultAsync();
|
||||
if (existingGrant != null)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Entry(existingGrant).CurrentValues.SetValues(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = Mapper.Map<Grant>(obj);
|
||||
await dbContext.AddAsync(entity);
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.Key == key
|
||||
select g;
|
||||
dbContext.Remove(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteManyAsync(string subjectId, string sessionId, string clientId, string type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.SubjectId == subjectId &&
|
||||
g.ClientId == clientId &&
|
||||
g.SessionId == sessionId &&
|
||||
g.Type == type
|
||||
select g;
|
||||
dbContext.Remove(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.Grant> GetByKeyAsync(string key)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.Key == key
|
||||
select g;
|
||||
var grant = await query.FirstOrDefaultAsync();
|
||||
return grant;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Grant>> GetManyAsync(string subjectId, string sessionId, string clientId, string type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.SubjectId == subjectId &&
|
||||
g.ClientId == clientId &&
|
||||
g.SessionId == sessionId &&
|
||||
g.Type == type
|
||||
select g;
|
||||
var grants = await query.ToListAsync();
|
||||
return (ICollection<Core.Entities.Grant>)grants;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task SaveAsync(Core.Entities.Grant obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var existingGrant = await (from g in dbContext.Grants
|
||||
where g.Key == obj.Key
|
||||
select g).FirstOrDefaultAsync();
|
||||
if (existingGrant != null)
|
||||
{
|
||||
dbContext.Entry(existingGrant).CurrentValues.SetValues(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
var entity = Mapper.Map<Grant>(obj);
|
||||
await dbContext.AddAsync(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,163 +5,164 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class GroupRepository : Repository<Core.Entities.Group, Group, Guid>, IGroupRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public GroupRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Groups)
|
||||
{ }
|
||||
|
||||
public async Task CreateAsync(Core.Entities.Group obj, IEnumerable<SelectionReadOnly> collections)
|
||||
public class GroupRepository : Repository<Core.Entities.Group, Group, Guid>, IGroupRepository
|
||||
{
|
||||
var grp = await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public GroupRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Groups)
|
||||
{ }
|
||||
|
||||
public async Task CreateAsync(Core.Entities.Group obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = await (
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == grp.OrganizationId
|
||||
select c).ToListAsync();
|
||||
var filteredCollections = collections.Where(c => availibleCollections.Any(a => c.Id == a.Id));
|
||||
var collectionGroups = filteredCollections.Select(y => new CollectionGroup
|
||||
var grp = await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
CollectionId = y.Id,
|
||||
GroupId = grp.Id,
|
||||
ReadOnly = y.ReadOnly,
|
||||
HidePasswords = y.HidePasswords,
|
||||
});
|
||||
await dbContext.CollectionGroups.AddRangeAsync(collectionGroups);
|
||||
await dbContext.SaveChangesAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = await (
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == grp.OrganizationId
|
||||
select c).ToListAsync();
|
||||
var filteredCollections = collections.Where(c => availibleCollections.Any(a => c.Id == a.Id));
|
||||
var collectionGroups = filteredCollections.Select(y => new CollectionGroup
|
||||
{
|
||||
CollectionId = y.Id,
|
||||
GroupId = grp.Id,
|
||||
ReadOnly = y.ReadOnly,
|
||||
HidePasswords = y.HidePasswords,
|
||||
});
|
||||
await dbContext.CollectionGroups.AddRangeAsync(collectionGroups);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid groupId, Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteUserAsync(Guid groupId, Guid organizationUserId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == groupId &&
|
||||
gu.OrganizationUserId == organizationUserId
|
||||
select gu;
|
||||
dbContext.RemoveRange(await query.ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<Core.Entities.Group, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var grp = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = await (
|
||||
from cg in dbContext.CollectionGroups
|
||||
where cg.GroupId == id
|
||||
select cg).ToListAsync();
|
||||
var collections = query.Select(c => new SelectionReadOnly
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Id = c.CollectionId,
|
||||
ReadOnly = c.ReadOnly,
|
||||
HidePasswords = c.HidePasswords,
|
||||
}).ToList();
|
||||
return new Tuple<Core.Entities.Group, ICollection<SelectionReadOnly>>(
|
||||
grp, collections);
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == groupId &&
|
||||
gu.OrganizationUserId == organizationUserId
|
||||
select gu;
|
||||
dbContext.RemoveRange(await query.ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Group>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Tuple<Core.Entities.Group, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await (
|
||||
from g in dbContext.Groups
|
||||
where g.OrganizationId == organizationId
|
||||
select g).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Group>>(data);
|
||||
var grp = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = await (
|
||||
from cg in dbContext.CollectionGroups
|
||||
where cg.GroupId == id
|
||||
select cg).ToListAsync();
|
||||
var collections = query.Select(c => new SelectionReadOnly
|
||||
{
|
||||
Id = c.CollectionId,
|
||||
ReadOnly = c.ReadOnly,
|
||||
HidePasswords = c.HidePasswords,
|
||||
}).ToList();
|
||||
return new Tuple<Core.Entities.Group, ICollection<SelectionReadOnly>>(
|
||||
grp, collections);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.GroupUser>> GetManyGroupUsersByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Group>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id
|
||||
where g.OrganizationId == organizationId
|
||||
select gu;
|
||||
var groupUsers = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.GroupUser>>(groupUsers);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var data = await (
|
||||
from g in dbContext.Groups
|
||||
where g.OrganizationId == organizationId
|
||||
select g).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Group>>(data);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Guid>> GetManyIdsByUserIdAsync(Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.GroupUser>> GetManyGroupUsersByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
where gu.OrganizationUserId == organizationUserId
|
||||
select gu;
|
||||
var groupIds = await query.Select(x => x.GroupId).ToListAsync();
|
||||
return groupIds;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id
|
||||
where g.OrganizationId == organizationId
|
||||
select gu;
|
||||
var groupUsers = await query.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.GroupUser>>(groupUsers);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Guid>> GetManyUserIdsByIdAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Guid>> GetManyIdsByUserIdAsync(Guid organizationUserId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == id
|
||||
select gu;
|
||||
var groupIds = await query.Select(x => x.OrganizationUserId).ToListAsync();
|
||||
return groupIds;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
where gu.OrganizationUserId == organizationUserId
|
||||
select gu;
|
||||
var groupIds = await query.Select(x => x.GroupId).ToListAsync();
|
||||
return groupIds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Core.Entities.Group obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
await base.ReplaceAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Guid>> GetManyUserIdsByIdAsync(Guid id)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
await UserBumpAccountRevisionDateByOrganizationId(obj.OrganizationId);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query =
|
||||
from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == id
|
||||
select gu;
|
||||
var groupIds = await query.Select(x => x.OrganizationUserId).ToListAsync();
|
||||
return groupIds;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateUsersAsync(Guid groupId, IEnumerable<Guid> organizationUserIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task ReplaceAsync(Core.Entities.Group obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgId = (await dbContext.Groups.FindAsync(groupId)).OrganizationId;
|
||||
var insert = from ou in dbContext.OrganizationUsers
|
||||
where organizationUserIds.Contains(ou.Id) &&
|
||||
ou.OrganizationId == orgId &&
|
||||
!dbContext.GroupUsers.Any(gu => gu.GroupId == groupId && ou.Id == gu.OrganizationUserId)
|
||||
select new GroupUser
|
||||
{
|
||||
GroupId = groupId,
|
||||
OrganizationUserId = ou.Id,
|
||||
};
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
await base.ReplaceAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
await UserBumpAccountRevisionDateByOrganizationId(obj.OrganizationId);
|
||||
}
|
||||
}
|
||||
|
||||
var delete = from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == groupId &&
|
||||
!organizationUserIds.Contains(gu.OrganizationUserId)
|
||||
select gu;
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(orgId);
|
||||
public async Task UpdateUsersAsync(Guid groupId, IEnumerable<Guid> organizationUserIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgId = (await dbContext.Groups.FindAsync(groupId)).OrganizationId;
|
||||
var insert = from ou in dbContext.OrganizationUsers
|
||||
where organizationUserIds.Contains(ou.Id) &&
|
||||
ou.OrganizationId == orgId &&
|
||||
!dbContext.GroupUsers.Any(gu => gu.GroupId == groupId && ou.Id == gu.OrganizationUserId)
|
||||
select new GroupUser
|
||||
{
|
||||
GroupId = groupId,
|
||||
OrganizationUserId = ou.Id,
|
||||
};
|
||||
await dbContext.AddRangeAsync(insert);
|
||||
|
||||
var delete = from gu in dbContext.GroupUsers
|
||||
where gu.GroupId == groupId &&
|
||||
!organizationUserIds.Contains(gu.OrganizationUserId)
|
||||
select gu;
|
||||
dbContext.RemoveRange(delete);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpAccountRevisionDateByOrganizationId(orgId);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,11 +3,12 @@ using Bit.Core.Repositories;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class InstallationRepository : Repository<Core.Entities.Installation, Installation, Guid>, IInstallationRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public InstallationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Installations)
|
||||
{ }
|
||||
public class InstallationRepository : Repository<Core.Entities.Installation, Installation, Guid>, IInstallationRepository
|
||||
{
|
||||
public InstallationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Installations)
|
||||
{ }
|
||||
}
|
||||
}
|
||||
|
@ -2,52 +2,53 @@
|
||||
using Bit.Core.Repositories;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class MaintenanceRepository : BaseEntityFrameworkRepository, IMaintenanceRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public MaintenanceRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task DeleteExpiredGrantsAsync()
|
||||
public class MaintenanceRepository : BaseEntityFrameworkRepository, IMaintenanceRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public MaintenanceRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{ }
|
||||
|
||||
public async Task DeleteExpiredGrantsAsync()
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.ExpirationDate < DateTime.UtcNow
|
||||
select g;
|
||||
dbContext.RemoveRange(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from g in dbContext.Grants
|
||||
where g.ExpirationDate < DateTime.UtcNow
|
||||
select g;
|
||||
dbContext.RemoveRange(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteExpiredSponsorshipsAsync(DateTime validUntilBeforeDate)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteExpiredSponsorshipsAsync(DateTime validUntilBeforeDate)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from s in dbContext.OrganizationSponsorships
|
||||
where s.ValidUntil < validUntilBeforeDate
|
||||
select s;
|
||||
dbContext.RemoveRange(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from s in dbContext.OrganizationSponsorships
|
||||
where s.ValidUntil < validUntilBeforeDate
|
||||
select s;
|
||||
dbContext.RemoveRange(query);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Task DisableCipherAutoStatsAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public Task DisableCipherAutoStatsAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task RebuildIndexesAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
public Task RebuildIndexesAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task UpdateStatisticsAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
public Task UpdateStatisticsAsync()
|
||||
{
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,25 +5,26 @@ using Bit.Core.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class OrganizationApiKeyRepository : Repository<OrganizationApiKey, Models.OrganizationApiKey, Guid>, IOrganizationApiKeyRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public OrganizationApiKeyRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, db => db.OrganizationApiKeys)
|
||||
public class OrganizationApiKeyRepository : Repository<OrganizationApiKey, Models.OrganizationApiKey, Guid>, IOrganizationApiKeyRepository
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrganizationApiKey>> GetManyByOrganizationIdTypeAsync(Guid organizationId, OrganizationApiKeyType? type = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public OrganizationApiKeyRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, db => db.OrganizationApiKeys)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var apiKeys = await dbContext.OrganizationApiKeys
|
||||
.Where(o => o.OrganizationId == organizationId && (type == null || o.Type == type))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationApiKey>>(apiKeys);
|
||||
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrganizationApiKey>> GetManyByOrganizationIdTypeAsync(Guid organizationId, OrganizationApiKeyType? type = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var apiKeys = await dbContext.OrganizationApiKeys
|
||||
.Where(o => o.OrganizationId == organizationId && (type == null || o.Type == type))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationApiKey>>(apiKeys);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,37 +5,38 @@ using Bit.Core.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class OrganizationConnectionRepository : Repository<OrganizationConnection, Models.OrganizationConnection, Guid>, IOrganizationConnectionRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public OrganizationConnectionRepository(IServiceScopeFactory serviceScopeFactory,
|
||||
IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.OrganizationConnections)
|
||||
public class OrganizationConnectionRepository : Repository<OrganizationConnection, Models.OrganizationConnection, Guid>, IOrganizationConnectionRepository
|
||||
{
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationConnection>> GetByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public OrganizationConnectionRepository(IServiceScopeFactory serviceScopeFactory,
|
||||
IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.OrganizationConnections)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var connections = await dbContext.OrganizationConnections
|
||||
.Where(oc => oc.OrganizationId == organizationId && oc.Type == type)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationConnection>>(connections);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationConnection>> GetEnabledByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<OrganizationConnection>> GetByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var connections = await dbContext.OrganizationConnections
|
||||
.Where(oc => oc.OrganizationId == organizationId && oc.Type == type && oc.Enabled)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationConnection>>(connections);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var connections = await dbContext.OrganizationConnections
|
||||
.Where(oc => oc.OrganizationId == organizationId && oc.Type == type)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationConnection>>(connections);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationConnection>> GetEnabledByOrganizationIdTypeAsync(Guid organizationId, OrganizationConnectionType type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var connections = await dbContext.OrganizationConnections
|
||||
.Where(oc => oc.OrganizationId == organizationId && oc.Type == type && oc.Enabled)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<OrganizationConnection>>(connections);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,104 +5,105 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class OrganizationRepository : Repository<Core.Entities.Organization, Organization, Guid>, IOrganizationRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public OrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Organizations)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Organization> GetByIdentifierAsync(string identifier)
|
||||
public class OrganizationRepository : Repository<Core.Entities.Organization, Organization, Guid>, IOrganizationRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organization = await GetDbSet(dbContext).Where(e => e.Identifier == identifier)
|
||||
.FirstOrDefaultAsync();
|
||||
return organization;
|
||||
}
|
||||
}
|
||||
public OrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Organizations)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<Core.Entities.Organization>> GetManyByEnabledAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.Organization> GetByIdentifierAsync(string identifier)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext).Where(e => e.Enabled).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Organization>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext)
|
||||
.Select(e => e.OrganizationUsers
|
||||
.Where(ou => ou.UserId == userId)
|
||||
.Select(ou => ou.Organization))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Organization>> SearchAsync(string name, string userEmail,
|
||||
bool? paid, int skip, int take)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext)
|
||||
.Where(e => name == null || e.Name.Contains(name))
|
||||
.Where(e => userEmail == null || e.OrganizationUsers.Any(u => u.Email == userEmail))
|
||||
.Where(e => paid == null ||
|
||||
(paid == true && !string.IsNullOrWhiteSpace(e.GatewaySubscriptionId)) ||
|
||||
(paid == false && e.GatewaySubscriptionId == null))
|
||||
.OrderBy(e => e.CreationDate)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext)
|
||||
.Select(e => new OrganizationAbility
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Enabled = e.Enabled,
|
||||
Id = e.Id,
|
||||
Use2fa = e.Use2fa,
|
||||
UseEvents = e.UseEvents,
|
||||
UsersGetPremium = e.UsersGetPremium,
|
||||
Using2fa = e.Use2fa && e.TwoFactorProviders != null,
|
||||
UseSso = e.UseSso,
|
||||
UseKeyConnector = e.UseKeyConnector,
|
||||
UseResetPassword = e.UseResetPassword,
|
||||
UseScim = e.UseScim,
|
||||
}).ToListAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organization = await GetDbSet(dbContext).Where(e => e.Identifier == identifier)
|
||||
.FirstOrDefaultAsync();
|
||||
return organization;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
{
|
||||
await OrganizationUpdateStorage(id);
|
||||
}
|
||||
|
||||
public override async Task DeleteAsync(Core.Entities.Organization organization)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Organization>> GetManyByEnabledAsync()
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgEntity = await dbContext.FindAsync<Organization>(organization.Id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext).Where(e => e.Enabled).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.Remove(orgEntity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
public async Task<ICollection<Core.Entities.Organization>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext)
|
||||
.Select(e => e.OrganizationUsers
|
||||
.Where(ou => ou.UserId == userId)
|
||||
.Select(ou => ou.Organization))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Organization>> SearchAsync(string name, string userEmail,
|
||||
bool? paid, int skip, int take)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var organizations = await GetDbSet(dbContext)
|
||||
.Where(e => name == null || e.Name.Contains(name))
|
||||
.Where(e => userEmail == null || e.OrganizationUsers.Any(u => u.Email == userEmail))
|
||||
.Where(e => paid == null ||
|
||||
(paid == true && !string.IsNullOrWhiteSpace(e.GatewaySubscriptionId)) ||
|
||||
(paid == false && e.GatewaySubscriptionId == null))
|
||||
.OrderBy(e => e.CreationDate)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Organization>>(organizations);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationAbility>> GetManyAbilitiesAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext)
|
||||
.Select(e => new OrganizationAbility
|
||||
{
|
||||
Enabled = e.Enabled,
|
||||
Id = e.Id,
|
||||
Use2fa = e.Use2fa,
|
||||
UseEvents = e.UseEvents,
|
||||
UsersGetPremium = e.UsersGetPremium,
|
||||
Using2fa = e.Use2fa && e.TwoFactorProviders != null,
|
||||
UseSso = e.UseSso,
|
||||
UseKeyConnector = e.UseKeyConnector,
|
||||
UseResetPassword = e.UseResetPassword,
|
||||
UseScim = e.UseScim,
|
||||
}).ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
{
|
||||
await OrganizationUpdateStorage(id);
|
||||
}
|
||||
|
||||
public override async Task DeleteAsync(Core.Entities.Organization organization)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgEntity = await dbContext.FindAsync<Organization>(organization.Id);
|
||||
|
||||
dbContext.Remove(orgEntity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,137 +4,138 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class OrganizationSponsorshipRepository : Repository<Core.Entities.OrganizationSponsorship, OrganizationSponsorship, Guid>, IOrganizationSponsorshipRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public OrganizationSponsorshipRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationSponsorships)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<Guid>> CreateManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
public class OrganizationSponsorshipRepository : Repository<Core.Entities.OrganizationSponsorship, OrganizationSponsorship, Guid>, IOrganizationSponsorshipRepository
|
||||
{
|
||||
if (!organizationSponsorships.Any())
|
||||
{
|
||||
return new List<Guid>();
|
||||
}
|
||||
public OrganizationSponsorshipRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationSponsorships)
|
||||
{ }
|
||||
|
||||
foreach (var organizationSponsorship in organizationSponsorships)
|
||||
public async Task<ICollection<Guid>> CreateManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
{
|
||||
organizationSponsorship.SetNewId();
|
||||
}
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = Mapper.Map<List<OrganizationUser>>(organizationSponsorships);
|
||||
await dbContext.AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return organizationSponsorships.Select(u => u.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task ReplaceManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
dbContext.UpdateRange(organizationSponsorships);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpsertManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
{
|
||||
var createSponsorships = new List<Core.Entities.OrganizationSponsorship>();
|
||||
var replaceSponsorships = new List<Core.Entities.OrganizationSponsorship>();
|
||||
foreach (var organizationSponsorship in organizationSponsorships)
|
||||
{
|
||||
if (organizationSponsorship.Id.Equals(default))
|
||||
if (!organizationSponsorships.Any())
|
||||
{
|
||||
createSponsorships.Add(organizationSponsorship);
|
||||
return new List<Guid>();
|
||||
}
|
||||
else
|
||||
|
||||
foreach (var organizationSponsorship in organizationSponsorships)
|
||||
{
|
||||
replaceSponsorships.Add(organizationSponsorship);
|
||||
organizationSponsorship.SetNewId();
|
||||
}
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = Mapper.Map<List<OrganizationUser>>(organizationSponsorships);
|
||||
await dbContext.AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return organizationSponsorships.Select(u => u.Id).ToList();
|
||||
}
|
||||
|
||||
public async Task ReplaceManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
dbContext.UpdateRange(organizationSponsorships);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
await CreateManyAsync(createSponsorships);
|
||||
await ReplaceManyAsync(replaceSponsorships);
|
||||
}
|
||||
|
||||
public async Task DeleteManyAsync(IEnumerable<Guid> organizationSponsorshipIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task UpsertManyAsync(IEnumerable<Core.Entities.OrganizationSponsorship> organizationSponsorships)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = await dbContext.OrganizationSponsorships
|
||||
.Where(os => organizationSponsorshipIds.Contains(os.Id))
|
||||
.ToListAsync();
|
||||
var createSponsorships = new List<Core.Entities.OrganizationSponsorship>();
|
||||
var replaceSponsorships = new List<Core.Entities.OrganizationSponsorship>();
|
||||
foreach (var organizationSponsorship in organizationSponsorships)
|
||||
{
|
||||
if (organizationSponsorship.Id.Equals(default))
|
||||
{
|
||||
createSponsorships.Add(organizationSponsorship);
|
||||
}
|
||||
else
|
||||
{
|
||||
replaceSponsorships.Add(organizationSponsorship);
|
||||
}
|
||||
}
|
||||
|
||||
dbContext.OrganizationSponsorships.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await CreateManyAsync(createSponsorships);
|
||||
await ReplaceManyAsync(replaceSponsorships);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetByOfferedToEmailAsync(string email)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteManyAsync(IEnumerable<Guid> organizationSponsorshipIds)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.OfferedToEmail == email)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = await dbContext.OrganizationSponsorships
|
||||
.Where(os => organizationSponsorshipIds.Contains(os.Id))
|
||||
.ToListAsync();
|
||||
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetBySponsoredOrganizationIdAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
dbContext.OrganizationSponsorships.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetByOfferedToEmailAsync(string email)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoredOrganizationId == sponsoredOrganizationId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.OfferedToEmail == email)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetBySponsoringOrganizationUserIdAsync(Guid sponsoringOrganizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetBySponsoredOrganizationIdAsync(Guid sponsoredOrganizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoringOrganizationUserId == sponsoringOrganizationUserId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoredOrganizationId == sponsoredOrganizationId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DateTime?> GetLatestSyncDateBySponsoringOrganizationIdAsync(Guid sponsoringOrganizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.OrganizationSponsorship> GetBySponsoringOrganizationUserIdAsync(Guid sponsoringOrganizationUserId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.SponsoringOrganizationId == sponsoringOrganizationId && e.LastSyncDate != null)
|
||||
.OrderByDescending(e => e.LastSyncDate)
|
||||
.Select(e => e.LastSyncDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgSponsorship = await GetDbSet(dbContext).Where(e => e.SponsoringOrganizationUserId == sponsoringOrganizationUserId)
|
||||
.FirstOrDefaultAsync();
|
||||
return orgSponsorship;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationSponsorship>> GetManyBySponsoringOrganizationAsync(Guid sponsoringOrganizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<DateTime?> GetLatestSyncDateBySponsoringOrganizationIdAsync(Guid sponsoringOrganizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from os in dbContext.OrganizationSponsorships
|
||||
where os.SponsoringOrganizationId == sponsoringOrganizationId
|
||||
select os;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationSponsorship>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.SponsoringOrganizationId == sponsoringOrganizationId && e.LastSyncDate != null)
|
||||
.OrderByDescending(e => e.LastSyncDate)
|
||||
.Select(e => e.LastSyncDate)
|
||||
.FirstOrDefaultAsync();
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationSponsorship>> GetManyBySponsoringOrganizationAsync(Guid sponsoringOrganizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from os in dbContext.OrganizationSponsorships
|
||||
where os.SponsoringOrganizationId == sponsoringOrganizationId
|
||||
select os;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationSponsorship>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -8,455 +8,456 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class OrganizationUserRepository : Repository<Core.Entities.OrganizationUser, OrganizationUser, Guid>, IOrganizationUserRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public OrganizationUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationUsers)
|
||||
{ }
|
||||
|
||||
public async Task<Guid> CreateAsync(Core.Entities.OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
public class OrganizationUserRepository : Repository<Core.Entities.OrganizationUser, OrganizationUser, Guid>, IOrganizationUserRepository
|
||||
{
|
||||
var organizationUser = await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public OrganizationUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.OrganizationUsers)
|
||||
{ }
|
||||
|
||||
public async Task<Guid> CreateAsync(Core.Entities.OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = await (
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationUser.OrganizationId
|
||||
select c).ToListAsync();
|
||||
var filteredCollections = collections.Where(c => availibleCollections.Any(a => c.Id == a.Id));
|
||||
var collectionUsers = filteredCollections.Select(y => new CollectionUser
|
||||
var organizationUser = await base.CreateAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
CollectionId = y.Id,
|
||||
OrganizationUserId = organizationUser.Id,
|
||||
ReadOnly = y.ReadOnly,
|
||||
HidePasswords = y.HidePasswords,
|
||||
});
|
||||
await dbContext.CollectionUsers.AddRangeAsync(collectionUsers);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return organizationUser.Id;
|
||||
}
|
||||
|
||||
public async Task<ICollection<Guid>> CreateManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
if (!organizationUsers.Any())
|
||||
{
|
||||
return new List<Guid>();
|
||||
}
|
||||
|
||||
foreach (var organizationUser in organizationUsers)
|
||||
{
|
||||
organizationUser.SetNewId();
|
||||
}
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = Mapper.Map<List<OrganizationUser>>(organizationUsers);
|
||||
await dbContext.AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return organizationUsers.Select(u => u.Id).ToList();
|
||||
}
|
||||
|
||||
public override async Task DeleteAsync(Core.Entities.OrganizationUser organizationUser) => await DeleteAsync(organizationUser.Id);
|
||||
public async Task DeleteAsync(Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await dbContext.FindAsync<OrganizationUser>(organizationUserId);
|
||||
|
||||
dbContext.Remove(orgUser);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteManyAsync(IEnumerable<Guid> organizationUserIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = await dbContext.OrganizationUsers
|
||||
.Where(ou => organizationUserIds.Contains(ou.Id))
|
||||
.ToListAsync();
|
||||
|
||||
dbContext.OrganizationUsers.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<Core.Entities.OrganizationUser, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var organizationUser = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = await (
|
||||
from ou in dbContext.OrganizationUsers
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId
|
||||
where !ou.AccessAll &&
|
||||
ou.Id == id
|
||||
select cu).ToListAsync();
|
||||
var collections = query.Select(cu => new SelectionReadOnly
|
||||
{
|
||||
Id = cu.CollectionId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
});
|
||||
return new Tuple<Core.Entities.OrganizationUser, ICollection<SelectionReadOnly>>(
|
||||
organizationUser, collections.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationUser> GetByOrganizationAsync(Guid organizationId, Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(e => e.OrganizationId == organizationId && e.UserId == userId);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationUser> GetByOrganizationEmailAsync(Guid organizationId, string email)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(ou => ou.OrganizationId == organizationId &&
|
||||
!string.IsNullOrWhiteSpace(ou.Email) &&
|
||||
ou.Email == email);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByFreeOrganizationAdminUserAsync(Guid userId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByFreeOrganizationAdminUserQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOnlyOwnerQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationAsync(Guid organizationId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOrganizationIdEmailQuery(organizationId, email, onlyRegisteredUsers);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOrganizationIdQuery(organizationId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<OrganizationUserUserDetails> GetDetailsByIdAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserUserDetailsViewQuery();
|
||||
var entity = await view.Run(dbContext).FirstOrDefaultAsync(ou => ou.Id == id);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<OrganizationUserUserDetails, ICollection<SelectionReadOnly>>> GetDetailsByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var organizationUserUserDetails = await GetDetailsByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join cu in dbContext.CollectionUsers on ou.Id equals cu.OrganizationUserId
|
||||
where !ou.AccessAll && ou.Id == id
|
||||
select cu;
|
||||
var collections = await query.Select(cu => new SelectionReadOnly
|
||||
{
|
||||
Id = cu.CollectionId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
}).ToListAsync();
|
||||
return new Tuple<OrganizationUserUserDetails, ICollection<SelectionReadOnly>>(organizationUserUserDetails, collections);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationUserOrganizationDetails> GetDetailsByUserAsync(Guid userId, Guid organizationId, OrganizationUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserOrganizationDetailsViewQuery();
|
||||
var t = await (view.Run(dbContext)).ToArrayAsync();
|
||||
var entity = await view.Run(dbContext)
|
||||
.FirstOrDefaultAsync(o => o.UserId == userId &&
|
||||
o.OrganizationId == organizationId &&
|
||||
(status == null || o.Status == status));
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyAsync(IEnumerable<Guid> Ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where Ids.Contains(ou.Id)
|
||||
select ou;
|
||||
var data = await query.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByManyUsersAsync(IEnumerable<Guid> userIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where userIds.Contains(ou.Id)
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByOrganizationAsync(Guid organizationId, OrganizationUserType? type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.OrganizationId == organizationId &&
|
||||
(type == null || ou.Type == type)
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByUserAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.UserId == userId
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationUserUserDetails>> GetManyDetailsByOrganizationAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserUserDetailsViewQuery();
|
||||
var query = from ou in view.Run(dbContext)
|
||||
where ou.OrganizationId == organizationId
|
||||
select ou;
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationUserOrganizationDetails>> GetManyDetailsByUserAsync(Guid userId,
|
||||
OrganizationUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserOrganizationDetailsViewQuery();
|
||||
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<IEnumerable<OrganizationUserPublicKey>> GetManyPublicKeysByOrganizationUserAsync(Guid organizationId, IEnumerable<Guid> Ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where Ids.Contains(ou.Id) && ou.Status == OrganizationUserStatusType.Accepted
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id
|
||||
where ou.OrganizationId == organizationId
|
||||
select new { ou, u };
|
||||
var data = await query
|
||||
.Select(x => new OrganizationUserPublicKey()
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var availibleCollections = await (
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == organizationUser.OrganizationId
|
||||
select c).ToListAsync();
|
||||
var filteredCollections = collections.Where(c => availibleCollections.Any(a => c.Id == a.Id));
|
||||
var collectionUsers = filteredCollections.Select(y => new CollectionUser
|
||||
{
|
||||
Id = x.ou.Id,
|
||||
PublicKey = x.u.PublicKey,
|
||||
}).ToListAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Core.Entities.OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
await base.ReplaceAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var procedure = new OrganizationUserUpdateWithCollectionsQuery(obj, collections);
|
||||
|
||||
var update = procedure.Update.Run(dbContext);
|
||||
dbContext.UpdateRange(await update.ToListAsync());
|
||||
|
||||
var insert = procedure.Insert.Run(dbContext);
|
||||
await dbContext.AddRangeAsync(await insert.ToListAsync());
|
||||
|
||||
dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
dbContext.UpdateRange(organizationUsers);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpManyAccountRevisionDates(organizationUsers
|
||||
.Where(ou => ou.UserId.HasValue)
|
||||
.Select(ou => ou.UserId.Value).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<string>> SelectKnownEmailsAsync(Guid organizationId, IEnumerable<string> emails, bool onlyRegisteredUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var usersQuery = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id into u_g
|
||||
from u in u_g
|
||||
where ou.OrganizationId == organizationId
|
||||
select new { ou, u };
|
||||
var ouu = await usersQuery.ToListAsync();
|
||||
var ouEmails = ouu.Select(x => x.ou.Email);
|
||||
var uEmails = ouu.Select(x => x.u.Email);
|
||||
var knownEmails = from e in emails
|
||||
where (ouEmails.Contains(e) || uEmails.Contains(e)) &&
|
||||
(!onlyRegisteredUsers && (uEmails.Contains(e) || ouEmails.Contains(e))) ||
|
||||
(onlyRegisteredUsers && uEmails.Contains(e))
|
||||
select e;
|
||||
return knownEmails.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateGroupsAsync(Guid orgUserId, IEnumerable<Guid> groupIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var procedure = new GroupUserUpdateGroupsQuery(orgUserId, groupIds);
|
||||
|
||||
var insert = procedure.Insert.Run(dbContext);
|
||||
var data = await insert.ToListAsync();
|
||||
await dbContext.AddRangeAsync(data);
|
||||
|
||||
var delete = procedure.Delete.Run(dbContext);
|
||||
var deleteData = await delete.ToListAsync();
|
||||
dbContext.RemoveRange(deleteData);
|
||||
await UserBumpAccountRevisionDateByOrganizationUserId(orgUserId);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpsertManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
var createUsers = new List<Core.Entities.OrganizationUser>();
|
||||
var replaceUsers = new List<Core.Entities.OrganizationUser>();
|
||||
foreach (var organizationUser in organizationUsers)
|
||||
{
|
||||
if (organizationUser.Id.Equals(default))
|
||||
{
|
||||
createUsers.Add(organizationUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
replaceUsers.Add(organizationUser);
|
||||
}
|
||||
}
|
||||
|
||||
await CreateManyAsync(createUsers);
|
||||
await ReplaceManyAsync(replaceUsers);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrganizationUserUserDetails>> GetManyByMinimumRoleAsync(Guid organizationId, OrganizationUserType minRole)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.OrganizationUsers
|
||||
.Include(e => e.User)
|
||||
.Where(e => e.OrganizationId.Equals(organizationId) &&
|
||||
e.Type <= minRole &&
|
||||
e.Status == OrganizationUserStatusType.Confirmed)
|
||||
.Select(e => new OrganizationUserUserDetails()
|
||||
{
|
||||
Id = e.Id,
|
||||
Email = e.Email ?? e.User.Email
|
||||
CollectionId = y.Id,
|
||||
OrganizationUserId = organizationUser.Id,
|
||||
ReadOnly = y.ReadOnly,
|
||||
HidePasswords = y.HidePasswords,
|
||||
});
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RevokeAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await GetDbSet(dbContext).FindAsync(id);
|
||||
if (orgUser != null)
|
||||
{
|
||||
dbContext.Update(orgUser);
|
||||
orgUser.Status = OrganizationUserStatusType.Revoked;
|
||||
await dbContext.CollectionUsers.AddRangeAsync(collectionUsers);
|
||||
await dbContext.SaveChangesAsync();
|
||||
if (orgUser.UserId.HasValue)
|
||||
}
|
||||
|
||||
return organizationUser.Id;
|
||||
}
|
||||
|
||||
public async Task<ICollection<Guid>> CreateManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
if (!organizationUsers.Any())
|
||||
{
|
||||
return new List<Guid>();
|
||||
}
|
||||
|
||||
foreach (var organizationUser in organizationUsers)
|
||||
{
|
||||
organizationUser.SetNewId();
|
||||
}
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = Mapper.Map<List<OrganizationUser>>(organizationUsers);
|
||||
await dbContext.AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
|
||||
return organizationUsers.Select(u => u.Id).ToList();
|
||||
}
|
||||
|
||||
public override async Task DeleteAsync(Core.Entities.OrganizationUser organizationUser) => await DeleteAsync(organizationUser.Id);
|
||||
public async Task DeleteAsync(Guid organizationUserId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await dbContext.FindAsync<OrganizationUser>(organizationUserId);
|
||||
|
||||
dbContext.Remove(orgUser);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteManyAsync(IEnumerable<Guid> organizationUserIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entities = await dbContext.OrganizationUsers
|
||||
.Where(ou => organizationUserIds.Contains(ou.Id))
|
||||
.ToListAsync();
|
||||
|
||||
dbContext.OrganizationUsers.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<Core.Entities.OrganizationUser, ICollection<SelectionReadOnly>>> GetByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var organizationUser = await base.GetByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = await (
|
||||
from ou in dbContext.OrganizationUsers
|
||||
join cu in dbContext.CollectionUsers
|
||||
on ou.Id equals cu.OrganizationUserId
|
||||
where !ou.AccessAll &&
|
||||
ou.Id == id
|
||||
select cu).ToListAsync();
|
||||
var collections = query.Select(cu => new SelectionReadOnly
|
||||
{
|
||||
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
|
||||
Id = cu.CollectionId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
});
|
||||
return new Tuple<Core.Entities.OrganizationUser, ICollection<SelectionReadOnly>>(
|
||||
organizationUser, collections.ToList());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationUser> GetByOrganizationAsync(Guid organizationId, Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(e => e.OrganizationId == organizationId && e.UserId == userId);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.OrganizationUser> GetByOrganizationEmailAsync(Guid organizationId, string email)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(ou => ou.OrganizationId == organizationId &&
|
||||
!string.IsNullOrWhiteSpace(ou.Email) &&
|
||||
ou.Email == email);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByFreeOrganizationAdminUserAsync(Guid userId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByFreeOrganizationAdminUserQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOnlyOwnerQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationAsync(Guid organizationId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOrganizationIdEmailQuery(organizationId, email, onlyRegisteredUsers);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var query = new OrganizationUserReadCountByOrganizationIdQuery(organizationId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
|
||||
public async Task<OrganizationUserUserDetails> GetDetailsByIdAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserUserDetailsViewQuery();
|
||||
var entity = await view.Run(dbContext).FirstOrDefaultAsync(ou => ou.Id == id);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Tuple<OrganizationUserUserDetails, ICollection<SelectionReadOnly>>> GetDetailsByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
var organizationUserUserDetails = await GetDetailsByIdAsync(id);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join cu in dbContext.CollectionUsers on ou.Id equals cu.OrganizationUserId
|
||||
where !ou.AccessAll && ou.Id == id
|
||||
select cu;
|
||||
var collections = await query.Select(cu => new SelectionReadOnly
|
||||
{
|
||||
Id = cu.CollectionId,
|
||||
ReadOnly = cu.ReadOnly,
|
||||
HidePasswords = cu.HidePasswords,
|
||||
}).ToListAsync();
|
||||
return new Tuple<OrganizationUserUserDetails, ICollection<SelectionReadOnly>>(organizationUserUserDetails, collections);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationUserOrganizationDetails> GetDetailsByUserAsync(Guid userId, Guid organizationId, OrganizationUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserOrganizationDetailsViewQuery();
|
||||
var t = await (view.Run(dbContext)).ToArrayAsync();
|
||||
var entity = await view.Run(dbContext)
|
||||
.FirstOrDefaultAsync(o => o.UserId == userId &&
|
||||
o.OrganizationId == organizationId &&
|
||||
(status == null || o.Status == status));
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyAsync(IEnumerable<Guid> Ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where Ids.Contains(ou.Id)
|
||||
select ou;
|
||||
var data = await query.ToArrayAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByManyUsersAsync(IEnumerable<Guid> userIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where userIds.Contains(ou.Id)
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByOrganizationAsync(Guid organizationId, OrganizationUserType? type)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.OrganizationId == organizationId &&
|
||||
(type == null || ou.Type == type)
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.OrganizationUser>> GetManyByUserAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.UserId == userId
|
||||
select ou;
|
||||
return Mapper.Map<List<Core.Entities.OrganizationUser>>(await query.ToListAsync());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationUserUserDetails>> GetManyDetailsByOrganizationAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserUserDetailsViewQuery();
|
||||
var query = from ou in view.Run(dbContext)
|
||||
where ou.OrganizationId == organizationId
|
||||
select ou;
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationUserOrganizationDetails>> GetManyDetailsByUserAsync(Guid userId,
|
||||
OrganizationUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var view = new OrganizationUserOrganizationDetailsViewQuery();
|
||||
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<IEnumerable<OrganizationUserPublicKey>> GetManyPublicKeysByOrganizationUserAsync(Guid organizationId, IEnumerable<Guid> Ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where Ids.Contains(ou.Id) && ou.Status == OrganizationUserStatusType.Accepted
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id
|
||||
where ou.OrganizationId == organizationId
|
||||
select new { ou, u };
|
||||
var data = await query
|
||||
.Select(x => new OrganizationUserPublicKey()
|
||||
{
|
||||
Id = x.ou.Id,
|
||||
PublicKey = x.u.PublicKey,
|
||||
}).ToListAsync();
|
||||
return data;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Core.Entities.OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
await base.ReplaceAsync(obj);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var procedure = new OrganizationUserUpdateWithCollectionsQuery(obj, collections);
|
||||
|
||||
var update = procedure.Update.Run(dbContext);
|
||||
dbContext.UpdateRange(await update.ToListAsync());
|
||||
|
||||
var insert = procedure.Insert.Run(dbContext);
|
||||
await dbContext.AddRangeAsync(await insert.ToListAsync());
|
||||
|
||||
dbContext.RemoveRange(await procedure.Delete.Run(dbContext).ToListAsync());
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
dbContext.UpdateRange(organizationUsers);
|
||||
await dbContext.SaveChangesAsync();
|
||||
await UserBumpManyAccountRevisionDates(organizationUsers
|
||||
.Where(ou => ou.UserId.HasValue)
|
||||
.Select(ou => ou.UserId.Value).ToArray());
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<string>> SelectKnownEmailsAsync(Guid organizationId, IEnumerable<string> emails, bool onlyRegisteredUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var usersQuery = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id into u_g
|
||||
from u in u_g
|
||||
where ou.OrganizationId == organizationId
|
||||
select new { ou, u };
|
||||
var ouu = await usersQuery.ToListAsync();
|
||||
var ouEmails = ouu.Select(x => x.ou.Email);
|
||||
var uEmails = ouu.Select(x => x.u.Email);
|
||||
var knownEmails = from e in emails
|
||||
where (ouEmails.Contains(e) || uEmails.Contains(e)) &&
|
||||
(!onlyRegisteredUsers && (uEmails.Contains(e) || ouEmails.Contains(e))) ||
|
||||
(onlyRegisteredUsers && uEmails.Contains(e))
|
||||
select e;
|
||||
return knownEmails.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateGroupsAsync(Guid orgUserId, IEnumerable<Guid> groupIds)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var procedure = new GroupUserUpdateGroupsQuery(orgUserId, groupIds);
|
||||
|
||||
var insert = procedure.Insert.Run(dbContext);
|
||||
var data = await insert.ToListAsync();
|
||||
await dbContext.AddRangeAsync(data);
|
||||
|
||||
var delete = procedure.Delete.Run(dbContext);
|
||||
var deleteData = await delete.ToListAsync();
|
||||
dbContext.RemoveRange(deleteData);
|
||||
await UserBumpAccountRevisionDateByOrganizationUserId(orgUserId);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpsertManyAsync(IEnumerable<Core.Entities.OrganizationUser> organizationUsers)
|
||||
{
|
||||
var createUsers = new List<Core.Entities.OrganizationUser>();
|
||||
var replaceUsers = new List<Core.Entities.OrganizationUser>();
|
||||
foreach (var organizationUser in organizationUsers)
|
||||
{
|
||||
if (organizationUser.Id.Equals(default))
|
||||
{
|
||||
createUsers.Add(organizationUser);
|
||||
}
|
||||
else
|
||||
{
|
||||
replaceUsers.Add(organizationUser);
|
||||
}
|
||||
}
|
||||
|
||||
await CreateManyAsync(createUsers);
|
||||
await ReplaceManyAsync(replaceUsers);
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<OrganizationUserUserDetails>> GetManyByMinimumRoleAsync(Guid organizationId, OrganizationUserType minRole)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = dbContext.OrganizationUsers
|
||||
.Include(e => e.User)
|
||||
.Where(e => e.OrganizationId.Equals(organizationId) &&
|
||||
e.Type <= minRole &&
|
||||
e.Status == OrganizationUserStatusType.Confirmed)
|
||||
.Select(e => new OrganizationUserUserDetails()
|
||||
{
|
||||
Id = e.Id,
|
||||
Email = e.Email ?? e.User.Email
|
||||
});
|
||||
return await query.ToListAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RevokeAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await GetDbSet(dbContext).FindAsync(id);
|
||||
if (orgUser != null)
|
||||
{
|
||||
dbContext.Update(orgUser);
|
||||
orgUser.Status = OrganizationUserStatusType.Revoked;
|
||||
await dbContext.SaveChangesAsync();
|
||||
if (orgUser.UserId.HasValue)
|
||||
{
|
||||
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task RestoreAsync(Guid id, OrganizationUserStatusType status)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task RestoreAsync(Guid id, OrganizationUserStatusType status)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await GetDbSet(dbContext).FindAsync(id);
|
||||
if (orgUser != null)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
dbContext.Update(orgUser);
|
||||
orgUser.Status = status;
|
||||
await dbContext.SaveChangesAsync();
|
||||
if (orgUser.UserId.HasValue)
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var orgUser = await GetDbSet(dbContext).FindAsync(id);
|
||||
if (orgUser != null)
|
||||
{
|
||||
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
|
||||
dbContext.Update(orgUser);
|
||||
orgUser.Status = status;
|
||||
await dbContext.SaveChangesAsync();
|
||||
if (orgUser.UserId.HasValue)
|
||||
{
|
||||
await UserBumpAccountRevisionDate(orgUser.UserId.Value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,71 +6,72 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class PolicyRepository : Repository<Core.Entities.Policy, Policy, Guid>, IPolicyRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public PolicyRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Policies)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
||||
public class PolicyRepository : Repository<Core.Entities.Policy, Policy, Guid>, IPolicyRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public PolicyRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Policies)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Policy> GetByOrganizationIdTypeAsync(Guid organizationId, PolicyType type)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Policies
|
||||
.FirstOrDefaultAsync(p => p.OrganizationId == organizationId && p.Type == type);
|
||||
return Mapper.Map<Core.Entities.Policy>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Policies
|
||||
.FirstOrDefaultAsync(p => p.OrganizationId == organizationId && p.Type == type);
|
||||
return Mapper.Map<Core.Entities.Policy>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Policies
|
||||
.Where(p => p.OrganizationId == organizationId)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Policies
|
||||
.Where(p => p.OrganizationId == organizationId)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var query = new PolicyReadByUserIdQuery(userId);
|
||||
var results = await query.Run(dbContext).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
var query = new PolicyReadByUserIdQuery(userId);
|
||||
var results = await query.Run(dbContext).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
||||
OrganizationUserStatusType minStatus)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Policy>> GetManyByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
||||
OrganizationUserStatusType minStatus)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var query = new PolicyReadByTypeApplicableToUserQuery(userId, policyType, minStatus);
|
||||
var results = await query.Run(dbContext).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
var query = new PolicyReadByTypeApplicableToUserQuery(userId, policyType, minStatus);
|
||||
var results = await query.Run(dbContext).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Policy>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
||||
OrganizationUserStatusType minStatus)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<int> GetCountByTypeApplicableToUserIdAsync(Guid userId, PolicyType policyType,
|
||||
OrganizationUserStatusType minStatus)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
|
||||
var query = new PolicyReadByTypeApplicableToUserQuery(userId, policyType, minStatus);
|
||||
return await GetCountFromQuery(query);
|
||||
var query = new PolicyReadByTypeApplicableToUserQuery(userId, policyType, minStatus);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -6,30 +6,31 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class ProviderOrganizationRepository :
|
||||
Repository<ProviderOrganization, Models.ProviderOrganization, Guid>, IProviderOrganizationRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public ProviderOrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.ProviderOrganizations)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<ProviderOrganizationOrganizationDetails>> GetManyDetailsByProviderAsync(Guid providerId)
|
||||
public class ProviderOrganizationRepository :
|
||||
Repository<ProviderOrganization, Models.ProviderOrganization, Guid>, IProviderOrganizationRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public ProviderOrganizationRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.ProviderOrganizations)
|
||||
{ }
|
||||
|
||||
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);
|
||||
var query = new ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(providerId);
|
||||
var data = await query.Run(dbContext).ToListAsync();
|
||||
return data;
|
||||
return await GetDbSet(dbContext).Where(po => po.OrganizationId == organizationId).FirstOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
@ -5,51 +5,52 @@ using Bit.Core.Repositories;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class ProviderRepository : Repository<Provider, Models.Provider, Guid>, IProviderRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
|
||||
public ProviderRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.Providers)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<Provider>> SearchAsync(string name, string userEmail, int skip, int take)
|
||||
public class ProviderRepository : Repository<Provider, Models.Provider, Guid>, IProviderRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
|
||||
public ProviderRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, context => context.Providers)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<Provider>> SearchAsync(string name, string userEmail, int skip, int take)
|
||||
{
|
||||
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);
|
||||
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())
|
||||
public async Task<ICollection<ProviderAbility>> GetManyAbilitiesAsync()
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext)
|
||||
.Select(e => new ProviderAbility
|
||||
{
|
||||
Enabled = e.Enabled,
|
||||
Id = e.Id,
|
||||
UseEvents = e.UseEvents,
|
||||
}).ToListAsync();
|
||||
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();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,153 +7,154 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class ProviderUserRepository :
|
||||
Repository<ProviderUser, Models.ProviderUser, Guid>, IProviderUserRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public ProviderUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.ProviderUsers)
|
||||
{ }
|
||||
public class ProviderUserRepository :
|
||||
Repository<ProviderUser, Models.ProviderUser, Guid>, IProviderUserRepository
|
||||
{
|
||||
public ProviderUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.ProviderUsers)
|
||||
{ }
|
||||
|
||||
public async Task<int> GetCountByProviderAsync(Guid providerId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<int> GetCountByProviderAsync(Guid providerId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
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);
|
||||
await UserBumpAccountRevisionDateByProviderUserIds(providerUserIds.ToArray());
|
||||
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)
|
||||
{
|
||||
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).Select(e => new ProviderUserUserDetails
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
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;
|
||||
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<ProviderUserProviderDetails>> GetManyDetailsByUserAsync(Guid userId, ProviderUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<ProviderUser>> GetManyAsync(IEnumerable<Guid> ids)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new ProviderUserProviderDetailsReadByUserIdStatusQuery(userId, status);
|
||||
var data = await query.Run(dbContext).ToArrayAsync();
|
||||
return data;
|
||||
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<IEnumerable<ProviderUserPublicKey>> GetManyPublicKeysByProviderUserAsync(Guid providerId, IEnumerable<Guid> Ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<ProviderUser>> GetManyByProviderAsync(Guid providerId, ProviderUserType? type = null)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var query = new UserReadPublicKeysByProviderUserIdsQuery(providerId, Ids);
|
||||
var data = await query.Run(dbContext).ToListAsync();
|
||||
return data;
|
||||
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<IEnumerable<ProviderUserOrganizationDetails>> GetManyOrganizationDetailsByUserAsync(Guid userId, ProviderUserStatusType? status = null)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task DeleteManyAsync(IEnumerable<Guid> providerUserIds)
|
||||
{
|
||||
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;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
await UserBumpAccountRevisionDateByProviderUserIds(providerUserIds.ToArray());
|
||||
var entities = dbContext.ProviderUsers.Where(pu => providerUserIds.Contains(pu.Id));
|
||||
dbContext.ProviderUsers.RemoveRange(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<int> GetCountByOnlyOwnerAsync(Guid userId)
|
||||
{
|
||||
var query = new ProviderUserReadCountByOnlyOwnerQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
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)
|
||||
{
|
||||
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).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)
|
||||
{
|
||||
var query = new ProviderUserReadCountByOnlyOwnerQuery(userId);
|
||||
return await GetCountFromQuery(query);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +1,37 @@
|
||||
using Bit.Core.Utilities;
|
||||
using Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CipherDetailsQuery : IQuery<CipherDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid? _userId;
|
||||
private readonly bool _ignoreFolders;
|
||||
public CipherDetailsQuery(Guid? userId, bool ignoreFolders = false)
|
||||
public class CipherDetailsQuery : IQuery<CipherDetails>
|
||||
{
|
||||
_userId = userId;
|
||||
_ignoreFolders = ignoreFolders;
|
||||
}
|
||||
public virtual IQueryable<CipherDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
select new CipherDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
Favorite = _userId.HasValue && c.Favorites != null && c.Favorites.Contains($"\"{_userId}\":true"),
|
||||
FolderId = (_ignoreFolders || !_userId.HasValue || c.Folders == null || !c.Folders.Contains(_userId.Value.ToString())) ?
|
||||
null :
|
||||
CoreHelpers.LoadClassFromJsonData<Dictionary<Guid, Guid>>(c.Folders)[_userId.Value],
|
||||
};
|
||||
return query;
|
||||
private readonly Guid? _userId;
|
||||
private readonly bool _ignoreFolders;
|
||||
public CipherDetailsQuery(Guid? userId, bool ignoreFolders = false)
|
||||
{
|
||||
_userId = userId;
|
||||
_ignoreFolders = ignoreFolders;
|
||||
}
|
||||
public virtual IQueryable<CipherDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
select new CipherDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
Favorite = _userId.HasValue && c.Favorites != null && c.Favorites.Contains($"\"{_userId}\":true"),
|
||||
FolderId = (_ignoreFolders || !_userId.HasValue || c.Folders == null || !c.Folders.Contains(_userId.Value.ToString())) ?
|
||||
null :
|
||||
CoreHelpers.LoadClassFromJsonData<Dictionary<Guid, Guid>>(c.Folders)[_userId.Value],
|
||||
};
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,39 @@
|
||||
using Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CipherOrganizationDetailsReadByIdQuery : IQuery<CipherOrganizationDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public CipherOrganizationDetailsReadByIdQuery(Guid cipherId)
|
||||
public class CipherOrganizationDetailsReadByIdQuery : IQuery<CipherOrganizationDetails>
|
||||
{
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public virtual IQueryable<CipherOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where c.Id == _cipherId
|
||||
select new CipherOrganizationDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Favorites = c.Favorites,
|
||||
Folders = c.Folders,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
OrganizationUseTotp = o.UseTotp,
|
||||
};
|
||||
return query;
|
||||
public CipherOrganizationDetailsReadByIdQuery(Guid cipherId)
|
||||
{
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
|
||||
public virtual IQueryable<CipherOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where c.Id == _cipherId
|
||||
select new CipherOrganizationDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Favorites = c.Favorites,
|
||||
Folders = c.Folders,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
OrganizationUseTotp = o.UseTotp,
|
||||
};
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,38 @@
|
||||
using Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CipherOrganizationDetailsReadByOrgizationIdQuery : IQuery<CipherOrganizationDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
public class CipherOrganizationDetailsReadByOrgizationIdQuery : IQuery<CipherOrganizationDetails>
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public CipherOrganizationDetailsReadByOrgizationIdQuery(Guid organizationId)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
public virtual IQueryable<CipherOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where c.OrganizationId == _organizationId
|
||||
select new CipherOrganizationDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Favorites = c.Favorites,
|
||||
Folders = c.Folders,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
OrganizationUseTotp = o.UseTotp,
|
||||
};
|
||||
return query;
|
||||
public CipherOrganizationDetailsReadByOrgizationIdQuery(Guid organizationId)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
public virtual IQueryable<CipherOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where c.OrganizationId == _organizationId
|
||||
select new CipherOrganizationDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Favorites = c.Favorites,
|
||||
Folders = c.Folders,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
OrganizationUseTotp = o.UseTotp,
|
||||
};
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,55 +1,56 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CipherReadCanEditByIdUserIdQuery : IQuery<Cipher>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public CipherReadCanEditByIdUserIdQuery(Guid userId, Guid cipherId)
|
||||
public class CipherReadCanEditByIdUserIdQuery : IQuery<Cipher>
|
||||
{
|
||||
_userId = userId;
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public virtual IQueryable<Cipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId into ou_g
|
||||
from ou in ou_g.DefaultIfEmpty()
|
||||
where ou.UserId == _userId
|
||||
join cc in dbContext.CollectionCiphers
|
||||
on c.Id equals cc.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue && !ou.AccessAll
|
||||
join cu in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.Id == cu.OrganizationUserId
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue && cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == cc.CollectionId &&
|
||||
(c.Id == _cipherId &&
|
||||
(c.UserId == _userId ||
|
||||
(!c.UserId.HasValue && ou.Status == OrganizationUserStatusType.Confirmed && o.Enabled &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)))) &&
|
||||
(c.UserId.HasValue || ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
|
||||
select c;
|
||||
return query;
|
||||
public CipherReadCanEditByIdUserIdQuery(Guid userId, Guid cipherId)
|
||||
{
|
||||
_userId = userId;
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
|
||||
public virtual IQueryable<Cipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id into o_g
|
||||
from o in o_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId into ou_g
|
||||
from ou in ou_g.DefaultIfEmpty()
|
||||
where ou.UserId == _userId
|
||||
join cc in dbContext.CollectionCiphers
|
||||
on c.Id equals cc.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue && !ou.AccessAll
|
||||
join cu in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.Id == cu.OrganizationUserId
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where !c.UserId.HasValue && cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == cc.CollectionId &&
|
||||
(c.Id == _cipherId &&
|
||||
(c.UserId == _userId ||
|
||||
(!c.UserId.HasValue && ou.Status == OrganizationUserStatusType.Confirmed && o.Enabled &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)))) &&
|
||||
(c.UserId.HasValue || ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
|
||||
select c;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,64 +2,65 @@
|
||||
using Bit.Core.Enums;
|
||||
using CollectionCipher = Bit.Infrastructure.EntityFramework.Models.CollectionCipher;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CipherUpdateCollectionsQuery : IQuery<CollectionCipher>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Cipher _cipher;
|
||||
private readonly IEnumerable<Guid> _collectionIds;
|
||||
|
||||
public CipherUpdateCollectionsQuery(Cipher cipher, IEnumerable<Guid> collectionIds)
|
||||
public class CipherUpdateCollectionsQuery : IQuery<CollectionCipher>
|
||||
{
|
||||
_cipher = cipher;
|
||||
_collectionIds = collectionIds;
|
||||
}
|
||||
private readonly Cipher _cipher;
|
||||
private readonly IEnumerable<Guid> _collectionIds;
|
||||
|
||||
public virtual IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
if (!_cipher.OrganizationId.HasValue || !_collectionIds.Any())
|
||||
public CipherUpdateCollectionsQuery(Cipher cipher, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
return null;
|
||||
_cipher = cipher;
|
||||
_collectionIds = collectionIds;
|
||||
}
|
||||
|
||||
var availibleCollections = !_cipher.UserId.HasValue ?
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == _cipher.OrganizationId
|
||||
select c.Id :
|
||||
from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == _cipher.UserId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on c.Id equals cg.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && gu.GroupId == cg.GroupId &&
|
||||
o.Id == _cipher.OrganizationId &&
|
||||
o.Enabled &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
|
||||
select c.Id;
|
||||
|
||||
if (!availibleCollections.Any())
|
||||
public virtual IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
if (!_cipher.OrganizationId.HasValue || !_collectionIds.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var query = from c in availibleCollections
|
||||
select new CollectionCipher { CollectionId = c, CipherId = _cipher.Id };
|
||||
return query;
|
||||
var availibleCollections = !_cipher.UserId.HasValue ?
|
||||
from c in dbContext.Collections
|
||||
where c.OrganizationId == _cipher.OrganizationId
|
||||
select c.Id :
|
||||
from c in dbContext.Collections
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on o.Id equals ou.OrganizationId
|
||||
where ou.UserId == _cipher.UserId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where !ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on c.Id equals cg.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && gu.GroupId == cg.GroupId &&
|
||||
o.Id == _cipher.OrganizationId &&
|
||||
o.Enabled &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || !cu.ReadOnly || g.AccessAll || !cg.ReadOnly)
|
||||
select c.Id;
|
||||
|
||||
if (!availibleCollections.Any())
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var query = from c in availibleCollections
|
||||
select new CollectionCipher { CollectionId = c, CipherId = _cipher.Id };
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,19 +1,20 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CollectionCipherReadByUserIdCipherIdQuery : CollectionCipherReadByUserIdQuery
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public CollectionCipherReadByUserIdCipherIdQuery(Guid userId, Guid cipherId) : base(userId)
|
||||
public class CollectionCipherReadByUserIdCipherIdQuery : CollectionCipherReadByUserIdQuery
|
||||
{
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
private readonly Guid _cipherId;
|
||||
|
||||
public override IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = base.Run(dbContext);
|
||||
return query.Where(x => x.CipherId == _cipherId);
|
||||
public CollectionCipherReadByUserIdCipherIdQuery(Guid userId, Guid cipherId) : base(userId)
|
||||
{
|
||||
_cipherId = cipherId;
|
||||
}
|
||||
|
||||
public override IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = base.Run(dbContext);
|
||||
return query.Where(x => x.CipherId == _cipherId);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,43 +1,44 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CollectionCipherReadByUserIdQuery : IQuery<CollectionCipher>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public CollectionCipherReadByUserIdQuery(Guid userId)
|
||||
public class CollectionCipherReadByUserIdQuery : IQuery<CollectionCipher>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
|
||||
public virtual IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from cc in dbContext.CollectionCiphers
|
||||
join c in dbContext.Collections
|
||||
on cc.CollectionId equals c.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
where ou.UserId == _userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g
|
||||
where ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g
|
||||
join cg in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals cg.CollectionId into cg_g
|
||||
from cg in cg_g
|
||||
where g.AccessAll && cg.GroupId == gu.GroupId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
|
||||
select cc;
|
||||
return query;
|
||||
public CollectionCipherReadByUserIdQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
public virtual IQueryable<CollectionCipher> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from cc in dbContext.CollectionCiphers
|
||||
join c in dbContext.Collections
|
||||
on cc.CollectionId equals c.Id
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
where ou.UserId == _userId
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g
|
||||
where ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g
|
||||
join cg in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals cg.CollectionId into cg_g
|
||||
from cg in cg_g
|
||||
where g.AccessAll && cg.GroupId == gu.GroupId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
|
||||
select cc;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,22 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CollectionReadCountByOrganizationIdQuery : IQuery<Collection>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public CollectionReadCountByOrganizationIdQuery(Guid organizationId)
|
||||
public class CollectionReadCountByOrganizationIdQuery : IQuery<Collection>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public IQueryable<Collection> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Collections
|
||||
where c.OrganizationId == _organizationId
|
||||
select c;
|
||||
return query;
|
||||
public CollectionReadCountByOrganizationIdQuery(Guid organizationId)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
|
||||
public IQueryable<Collection> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Collections
|
||||
where c.OrganizationId == _organizationId
|
||||
select c;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,115 +2,116 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class CollectionUserUpdateUsersQuery
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public readonly CollectionUserUpdateUsersInsertQuery Insert;
|
||||
public readonly CollectionUserUpdateUsersUpdateQuery Update;
|
||||
public readonly CollectionUserUpdateUsersDeleteQuery Delete;
|
||||
|
||||
public CollectionUserUpdateUsersQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
public class CollectionUserUpdateUsersQuery
|
||||
{
|
||||
Insert = new CollectionUserUpdateUsersInsertQuery(collectionId, users);
|
||||
Update = new CollectionUserUpdateUsersUpdateQuery(collectionId, users);
|
||||
Delete = new CollectionUserUpdateUsersDeleteQuery(collectionId, users);
|
||||
}
|
||||
}
|
||||
public readonly CollectionUserUpdateUsersInsertQuery Insert;
|
||||
public readonly CollectionUserUpdateUsersUpdateQuery Update;
|
||||
public readonly CollectionUserUpdateUsersDeleteQuery Delete;
|
||||
|
||||
public class CollectionUserUpdateUsersInsertQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public CollectionUserUpdateUsersInsertQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
}
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var organizationUserIds = _users.Select(u => u.Id);
|
||||
var insertQuery = from ou in dbContext.OrganizationUsers
|
||||
where
|
||||
organizationUserIds.Contains(ou.Id) &&
|
||||
ou.OrganizationId == orgId &&
|
||||
!dbContext.CollectionUsers.Any(
|
||||
x => x.CollectionId != _collectionId && x.OrganizationUserId == ou.Id)
|
||||
select ou;
|
||||
return insertQuery;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CollectionUser>> BuildInMemory(DatabaseContext dbContext)
|
||||
{
|
||||
var data = await Run(dbContext).ToListAsync();
|
||||
var collectionUsers = data.Select(x => new CollectionUser()
|
||||
public CollectionUserUpdateUsersQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
CollectionId = _collectionId,
|
||||
OrganizationUserId = x.Id,
|
||||
ReadOnly = _users.FirstOrDefault(u => u.Id.Equals(x.Id)).ReadOnly,
|
||||
HidePasswords = _users.FirstOrDefault(u => u.Id.Equals(x.Id)).HidePasswords,
|
||||
});
|
||||
return collectionUsers;
|
||||
}
|
||||
}
|
||||
|
||||
public class CollectionUserUpdateUsersUpdateQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public CollectionUserUpdateUsersUpdateQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
Insert = new CollectionUserUpdateUsersInsertQuery(collectionId, users);
|
||||
Update = new CollectionUserUpdateUsersUpdateQuery(collectionId, users);
|
||||
Delete = new CollectionUserUpdateUsersDeleteQuery(collectionId, users);
|
||||
}
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
public class CollectionUserUpdateUsersInsertQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var ids = _users.Select(x => x.Id);
|
||||
var updateQuery = from target in dbContext.CollectionUsers
|
||||
where target.CollectionId == _collectionId &&
|
||||
ids.Contains(target.OrganizationUserId)
|
||||
select target;
|
||||
return updateQuery;
|
||||
}
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public async Task<IEnumerable<CollectionUser>> BuildInMemory(DatabaseContext dbContext)
|
||||
{
|
||||
var data = await Run(dbContext).ToListAsync();
|
||||
var collectionUsers = data.Select(x => new CollectionUser
|
||||
public CollectionUserUpdateUsersInsertQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
CollectionId = _collectionId,
|
||||
OrganizationUserId = x.OrganizationUserId,
|
||||
ReadOnly = _users.FirstOrDefault(u => u.Id.Equals(x.OrganizationUserId)).ReadOnly,
|
||||
HidePasswords = _users.FirstOrDefault(u => u.Id.Equals(x.OrganizationUserId)).HidePasswords,
|
||||
});
|
||||
return collectionUsers;
|
||||
}
|
||||
}
|
||||
|
||||
public class CollectionUserUpdateUsersDeleteQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public CollectionUserUpdateUsersDeleteQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var deleteQuery = from cu in dbContext.CollectionUsers
|
||||
where !dbContext.Users.Any(
|
||||
u => u.Id == cu.OrganizationUserId)
|
||||
select cu;
|
||||
return deleteQuery;
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
}
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var organizationUserIds = _users.Select(u => u.Id);
|
||||
var insertQuery = from ou in dbContext.OrganizationUsers
|
||||
where
|
||||
organizationUserIds.Contains(ou.Id) &&
|
||||
ou.OrganizationId == orgId &&
|
||||
!dbContext.CollectionUsers.Any(
|
||||
x => x.CollectionId != _collectionId && x.OrganizationUserId == ou.Id)
|
||||
select ou;
|
||||
return insertQuery;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CollectionUser>> BuildInMemory(DatabaseContext dbContext)
|
||||
{
|
||||
var data = await Run(dbContext).ToListAsync();
|
||||
var collectionUsers = data.Select(x => new CollectionUser()
|
||||
{
|
||||
CollectionId = _collectionId,
|
||||
OrganizationUserId = x.Id,
|
||||
ReadOnly = _users.FirstOrDefault(u => u.Id.Equals(x.Id)).ReadOnly,
|
||||
HidePasswords = _users.FirstOrDefault(u => u.Id.Equals(x.Id)).HidePasswords,
|
||||
});
|
||||
return collectionUsers;
|
||||
}
|
||||
}
|
||||
|
||||
public class CollectionUserUpdateUsersUpdateQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public CollectionUserUpdateUsersUpdateQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var ids = _users.Select(x => x.Id);
|
||||
var updateQuery = from target in dbContext.CollectionUsers
|
||||
where target.CollectionId == _collectionId &&
|
||||
ids.Contains(target.OrganizationUserId)
|
||||
select target;
|
||||
return updateQuery;
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<CollectionUser>> BuildInMemory(DatabaseContext dbContext)
|
||||
{
|
||||
var data = await Run(dbContext).ToListAsync();
|
||||
var collectionUsers = data.Select(x => new CollectionUser
|
||||
{
|
||||
CollectionId = _collectionId,
|
||||
OrganizationUserId = x.OrganizationUserId,
|
||||
ReadOnly = _users.FirstOrDefault(u => u.Id.Equals(x.OrganizationUserId)).ReadOnly,
|
||||
HidePasswords = _users.FirstOrDefault(u => u.Id.Equals(x.OrganizationUserId)).HidePasswords,
|
||||
});
|
||||
return collectionUsers;
|
||||
}
|
||||
}
|
||||
|
||||
public class CollectionUserUpdateUsersDeleteQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly Guid _collectionId;
|
||||
private readonly IEnumerable<SelectionReadOnly> _users;
|
||||
|
||||
public CollectionUserUpdateUsersDeleteQuery(Guid collectionId, IEnumerable<SelectionReadOnly> users)
|
||||
{
|
||||
_collectionId = collectionId;
|
||||
_users = users;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgId = dbContext.Collections.FirstOrDefault(c => c.Id == _collectionId)?.OrganizationId;
|
||||
var deleteQuery = from cu in dbContext.CollectionUsers
|
||||
where !dbContext.Users.Any(
|
||||
u => u.Id == cu.OrganizationUserId)
|
||||
select cu;
|
||||
return deleteQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,38 @@
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EmergencyAccessDetailsViewQuery : IQuery<EmergencyAccessDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public IQueryable<EmergencyAccessDetails> Run(DatabaseContext dbContext)
|
||||
public class EmergencyAccessDetailsViewQuery : IQuery<EmergencyAccessDetails>
|
||||
{
|
||||
var query = from ea in dbContext.EmergencyAccesses
|
||||
join grantee in dbContext.Users
|
||||
on ea.GranteeId equals grantee.Id into grantee_g
|
||||
from grantee in grantee_g.DefaultIfEmpty()
|
||||
join grantor in dbContext.Users
|
||||
on ea.GrantorId equals grantor.Id into grantor_g
|
||||
from grantor in grantor_g.DefaultIfEmpty()
|
||||
select new { ea, grantee, grantor };
|
||||
return query.Select(x => new EmergencyAccessDetails
|
||||
public IQueryable<EmergencyAccessDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
Id = x.ea.Id,
|
||||
GrantorId = x.ea.GrantorId,
|
||||
GranteeId = x.ea.GranteeId,
|
||||
Email = x.ea.Email,
|
||||
KeyEncrypted = x.ea.KeyEncrypted,
|
||||
Type = x.ea.Type,
|
||||
Status = x.ea.Status,
|
||||
WaitTimeDays = x.ea.WaitTimeDays,
|
||||
RecoveryInitiatedDate = x.ea.RecoveryInitiatedDate,
|
||||
LastNotificationDate = x.ea.LastNotificationDate,
|
||||
CreationDate = x.ea.CreationDate,
|
||||
RevisionDate = x.ea.RevisionDate,
|
||||
GranteeName = x.grantee.Name,
|
||||
GranteeEmail = x.grantee.Email,
|
||||
GrantorName = x.grantor.Name,
|
||||
GrantorEmail = x.grantor.Email,
|
||||
});
|
||||
var query = from ea in dbContext.EmergencyAccesses
|
||||
join grantee in dbContext.Users
|
||||
on ea.GranteeId equals grantee.Id into grantee_g
|
||||
from grantee in grantee_g.DefaultIfEmpty()
|
||||
join grantor in dbContext.Users
|
||||
on ea.GrantorId equals grantor.Id into grantor_g
|
||||
from grantor in grantor_g.DefaultIfEmpty()
|
||||
select new { ea, grantee, grantor };
|
||||
return query.Select(x => new EmergencyAccessDetails
|
||||
{
|
||||
Id = x.ea.Id,
|
||||
GrantorId = x.ea.GrantorId,
|
||||
GranteeId = x.ea.GranteeId,
|
||||
Email = x.ea.Email,
|
||||
KeyEncrypted = x.ea.KeyEncrypted,
|
||||
Type = x.ea.Type,
|
||||
Status = x.ea.Status,
|
||||
WaitTimeDays = x.ea.WaitTimeDays,
|
||||
RecoveryInitiatedDate = x.ea.RecoveryInitiatedDate,
|
||||
LastNotificationDate = x.ea.LastNotificationDate,
|
||||
CreationDate = x.ea.CreationDate,
|
||||
RevisionDate = x.ea.RevisionDate,
|
||||
GranteeName = x.grantee.Name,
|
||||
GranteeEmail = x.grantee.Email,
|
||||
GrantorName = x.grantor.Name,
|
||||
GrantorEmail = x.grantor.Email,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,31 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EmergencyAccessReadCountByGrantorIdEmailQuery : IQuery<EmergencyAccess>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _grantorId;
|
||||
private readonly string _email;
|
||||
private readonly bool _onlyRegisteredUsers;
|
||||
|
||||
public EmergencyAccessReadCountByGrantorIdEmailQuery(Guid grantorId, string email, bool onlyRegisteredUsers)
|
||||
public class EmergencyAccessReadCountByGrantorIdEmailQuery : IQuery<EmergencyAccess>
|
||||
{
|
||||
_grantorId = grantorId;
|
||||
_email = email;
|
||||
_onlyRegisteredUsers = onlyRegisteredUsers;
|
||||
}
|
||||
private readonly Guid _grantorId;
|
||||
private readonly string _email;
|
||||
private readonly bool _onlyRegisteredUsers;
|
||||
|
||||
public IQueryable<EmergencyAccess> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ea in dbContext.EmergencyAccesses
|
||||
join u in dbContext.Users
|
||||
on ea.GranteeId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
where ea.GrantorId == _grantorId &&
|
||||
((!_onlyRegisteredUsers && (ea.Email == _email || u.Email == _email))
|
||||
|| (_onlyRegisteredUsers && u.Email == _email))
|
||||
select ea;
|
||||
return query;
|
||||
public EmergencyAccessReadCountByGrantorIdEmailQuery(Guid grantorId, string email, bool onlyRegisteredUsers)
|
||||
{
|
||||
_grantorId = grantorId;
|
||||
_email = email;
|
||||
_onlyRegisteredUsers = onlyRegisteredUsers;
|
||||
}
|
||||
|
||||
public IQueryable<EmergencyAccess> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ea in dbContext.EmergencyAccesses
|
||||
join u in dbContext.Users
|
||||
on ea.GranteeId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
where ea.GrantorId == _grantorId &&
|
||||
((!_onlyRegisteredUsers && (ea.Email == _email || u.Email == _email))
|
||||
|| (_onlyRegisteredUsers && u.Email == _email))
|
||||
select ea;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,46 +2,47 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Event = Bit.Infrastructure.EntityFramework.Models.Event;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByCipherIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Cipher _cipher;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByCipherIdQuery(Cipher cipher, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
public class EventReadPageByCipherIdQuery : IQuery<Event>
|
||||
{
|
||||
_cipher = cipher;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = null;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Cipher _cipher;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByCipherIdQuery(Cipher cipher, DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_cipher = cipher;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
public EventReadPageByCipherIdQuery(Cipher cipher, DateTime startDate, DateTime endDate, PageOptions pageOptions)
|
||||
{
|
||||
_cipher = cipher;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = null;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
((!_cipher.OrganizationId.HasValue && !e.OrganizationId.HasValue) ||
|
||||
(_cipher.OrganizationId.HasValue && _cipher.OrganizationId == e.OrganizationId)) &&
|
||||
((!_cipher.UserId.HasValue && !e.UserId.HasValue) ||
|
||||
(_cipher.UserId.HasValue && _cipher.UserId == e.UserId)) &&
|
||||
_cipher.Id == e.CipherId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByCipherIdQuery(Cipher cipher, DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_cipher = cipher;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
((!_cipher.OrganizationId.HasValue && !e.OrganizationId.HasValue) ||
|
||||
(_cipher.OrganizationId.HasValue && _cipher.OrganizationId == e.OrganizationId)) &&
|
||||
((!_cipher.UserId.HasValue && !e.UserId.HasValue) ||
|
||||
(_cipher.UserId.HasValue && _cipher.UserId == e.UserId)) &&
|
||||
_cipher.Id == e.CipherId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,39 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByOrganizationIdActingUserIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
private readonly Guid _actingUserId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByOrganizationIdActingUserIdQuery(Guid organizationId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
public class EventReadPageByOrganizationIdActingUserIdQuery : IQuery<Event>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_actingUserId = actingUserId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
private readonly Guid _actingUserId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.OrganizationId == _organizationId &&
|
||||
e.ActingUserId == _actingUserId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByOrganizationIdActingUserIdQuery(Guid organizationId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_actingUserId = actingUserId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.OrganizationId == _organizationId &&
|
||||
e.ActingUserId == _actingUserId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,36 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByOrganizationIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByOrganizationIdQuery(Guid organizationId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
public class EventReadPageByOrganizationIdQuery : IQuery<Event>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.OrganizationId == _organizationId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByOrganizationIdQuery(Guid organizationId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.OrganizationId == _organizationId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,39 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByProviderIdActingUserIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _providerId;
|
||||
private readonly Guid _actingUserId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByProviderIdActingUserIdQuery(Guid providerId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
public class EventReadPageByProviderIdActingUserIdQuery : IQuery<Event>
|
||||
{
|
||||
_providerId = providerId;
|
||||
_actingUserId = actingUserId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Guid _providerId;
|
||||
private readonly Guid _actingUserId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.ProviderId == _providerId &&
|
||||
e.ActingUserId == _actingUserId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByProviderIdActingUserIdQuery(Guid providerId, Guid actingUserId,
|
||||
DateTime startDate, DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_providerId = providerId;
|
||||
_actingUserId = actingUserId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.ProviderId == _providerId &&
|
||||
e.ActingUserId == _actingUserId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,35 +1,36 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByProviderIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _providerId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByProviderIdQuery(Guid providerId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
public class EventReadPageByProviderIdQuery : IQuery<Event>
|
||||
{
|
||||
_providerId = providerId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Guid _providerId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.ProviderId == _providerId && e.OrganizationId == null
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByProviderIdQuery(Guid providerId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_providerId = providerId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
e.ProviderId == _providerId && e.OrganizationId == null
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +1,37 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class EventReadPageByUserIdQuery : IQuery<Event>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public EventReadPageByUserIdQuery(Guid userId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
public class EventReadPageByUserIdQuery : IQuery<Event>
|
||||
{
|
||||
_userId = userId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
private readonly DateTime _startDate;
|
||||
private readonly DateTime _endDate;
|
||||
private readonly DateTime? _beforeDate;
|
||||
private readonly PageOptions _pageOptions;
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
!e.OrganizationId.HasValue &&
|
||||
e.ActingUserId == _userId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
public EventReadPageByUserIdQuery(Guid userId, DateTime startDate,
|
||||
DateTime endDate, DateTime? beforeDate, PageOptions pageOptions)
|
||||
{
|
||||
_userId = userId;
|
||||
_startDate = startDate;
|
||||
_endDate = endDate;
|
||||
_beforeDate = beforeDate;
|
||||
_pageOptions = pageOptions;
|
||||
}
|
||||
|
||||
public IQueryable<Event> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var q = from e in dbContext.Events
|
||||
where e.Date >= _startDate &&
|
||||
(_beforeDate != null || e.Date <= _endDate) &&
|
||||
(_beforeDate == null || e.Date < _beforeDate.Value) &&
|
||||
!e.OrganizationId.HasValue &&
|
||||
e.ActingUserId == _userId
|
||||
orderby e.Date descending
|
||||
select e;
|
||||
return q.Skip(0).Take(_pageOptions.PageSize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,68 +1,69 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class GroupUserUpdateGroupsQuery
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public readonly GroupUserUpdateGroupsInsertQuery Insert;
|
||||
public readonly GroupUserUpdateGroupsDeleteQuery Delete;
|
||||
|
||||
public GroupUserUpdateGroupsQuery(Guid organizationUserId, IEnumerable<Guid> groupIds)
|
||||
public class GroupUserUpdateGroupsQuery
|
||||
{
|
||||
Insert = new GroupUserUpdateGroupsInsertQuery(organizationUserId, groupIds);
|
||||
Delete = new GroupUserUpdateGroupsDeleteQuery(organizationUserId, groupIds);
|
||||
}
|
||||
}
|
||||
public readonly GroupUserUpdateGroupsInsertQuery Insert;
|
||||
public readonly GroupUserUpdateGroupsDeleteQuery Delete;
|
||||
|
||||
public class GroupUserUpdateGroupsInsertQuery : IQuery<GroupUser>
|
||||
{
|
||||
private readonly Guid _organizationUserId;
|
||||
private readonly IEnumerable<Guid> _groupIds;
|
||||
|
||||
public GroupUserUpdateGroupsInsertQuery(Guid organizationUserId, IEnumerable<Guid> collections)
|
||||
{
|
||||
_organizationUserId = organizationUserId;
|
||||
_groupIds = collections;
|
||||
}
|
||||
|
||||
public IQueryable<GroupUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgUser = from ou in dbContext.OrganizationUsers
|
||||
where ou.Id == _organizationUserId
|
||||
select ou;
|
||||
var groupIdEntities = dbContext.Groups.Where(x => _groupIds.Contains(x.Id));
|
||||
var query = from g in dbContext.Groups
|
||||
join ou in orgUser
|
||||
on g.OrganizationId equals ou.OrganizationId
|
||||
join gie in groupIdEntities
|
||||
on g.Id equals gie.Id
|
||||
where !dbContext.GroupUsers.Any(gu => _groupIds.Contains(gu.GroupId) && gu.OrganizationUserId == _organizationUserId)
|
||||
select g;
|
||||
return query.Select(x => new GroupUser
|
||||
public GroupUserUpdateGroupsQuery(Guid organizationUserId, IEnumerable<Guid> groupIds)
|
||||
{
|
||||
GroupId = x.Id,
|
||||
OrganizationUserId = _organizationUserId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupUserUpdateGroupsDeleteQuery : IQuery<GroupUser>
|
||||
{
|
||||
private readonly Guid _organizationUserId;
|
||||
private readonly IEnumerable<Guid> _groupIds;
|
||||
|
||||
public GroupUserUpdateGroupsDeleteQuery(Guid organizationUserId, IEnumerable<Guid> groupIds)
|
||||
{
|
||||
_organizationUserId = organizationUserId;
|
||||
_groupIds = groupIds;
|
||||
}
|
||||
|
||||
public IQueryable<GroupUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var deleteQuery = from gu in dbContext.GroupUsers
|
||||
where gu.OrganizationUserId == _organizationUserId &&
|
||||
!_groupIds.Any(x => gu.GroupId == x)
|
||||
select gu;
|
||||
return deleteQuery;
|
||||
Insert = new GroupUserUpdateGroupsInsertQuery(organizationUserId, groupIds);
|
||||
Delete = new GroupUserUpdateGroupsDeleteQuery(organizationUserId, groupIds);
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupUserUpdateGroupsInsertQuery : IQuery<GroupUser>
|
||||
{
|
||||
private readonly Guid _organizationUserId;
|
||||
private readonly IEnumerable<Guid> _groupIds;
|
||||
|
||||
public GroupUserUpdateGroupsInsertQuery(Guid organizationUserId, IEnumerable<Guid> collections)
|
||||
{
|
||||
_organizationUserId = organizationUserId;
|
||||
_groupIds = collections;
|
||||
}
|
||||
|
||||
public IQueryable<GroupUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var orgUser = from ou in dbContext.OrganizationUsers
|
||||
where ou.Id == _organizationUserId
|
||||
select ou;
|
||||
var groupIdEntities = dbContext.Groups.Where(x => _groupIds.Contains(x.Id));
|
||||
var query = from g in dbContext.Groups
|
||||
join ou in orgUser
|
||||
on g.OrganizationId equals ou.OrganizationId
|
||||
join gie in groupIdEntities
|
||||
on g.Id equals gie.Id
|
||||
where !dbContext.GroupUsers.Any(gu => _groupIds.Contains(gu.GroupId) && gu.OrganizationUserId == _organizationUserId)
|
||||
select g;
|
||||
return query.Select(x => new GroupUser
|
||||
{
|
||||
GroupId = x.Id,
|
||||
OrganizationUserId = _organizationUserId,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupUserUpdateGroupsDeleteQuery : IQuery<GroupUser>
|
||||
{
|
||||
private readonly Guid _organizationUserId;
|
||||
private readonly IEnumerable<Guid> _groupIds;
|
||||
|
||||
public GroupUserUpdateGroupsDeleteQuery(Guid organizationUserId, IEnumerable<Guid> groupIds)
|
||||
{
|
||||
_organizationUserId = organizationUserId;
|
||||
_groupIds = groupIds;
|
||||
}
|
||||
|
||||
public IQueryable<GroupUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var deleteQuery = from gu in dbContext.GroupUsers
|
||||
where gu.OrganizationUserId == _organizationUserId &&
|
||||
!_groupIds.Any(x => gu.GroupId == x)
|
||||
select gu;
|
||||
return deleteQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public interface IQuery<TOut>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
IQueryable<TOut> Run(DatabaseContext dbContext);
|
||||
public interface IQuery<TOut>
|
||||
{
|
||||
IQueryable<TOut> Run(DatabaseContext dbContext);
|
||||
}
|
||||
}
|
||||
|
@ -1,64 +1,65 @@
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserOrganizationDetailsViewQuery : IQuery<OrganizationUserOrganizationDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public IQueryable<OrganizationUserOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
public class OrganizationUserOrganizationDetailsViewQuery : IQuery<OrganizationUserOrganizationDetails>
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join o in dbContext.Organizations on ou.OrganizationId equals o.Id
|
||||
join su in dbContext.SsoUsers on ou.UserId equals su.UserId into su_g
|
||||
from su in su_g.DefaultIfEmpty()
|
||||
join po in dbContext.ProviderOrganizations on o.Id equals po.OrganizationId into po_g
|
||||
from po in po_g.DefaultIfEmpty()
|
||||
join p in dbContext.Providers on po.ProviderId equals p.Id into p_g
|
||||
from p in p_g.DefaultIfEmpty()
|
||||
join os in dbContext.OrganizationSponsorships on ou.Id equals os.SponsoringOrganizationUserId into os_g
|
||||
from os in os_g.DefaultIfEmpty()
|
||||
join ss in dbContext.SsoConfigs on ou.OrganizationId equals ss.OrganizationId into ss_g
|
||||
from ss in ss_g.DefaultIfEmpty()
|
||||
where ((su == null || !su.OrganizationId.HasValue) || su.OrganizationId == ou.OrganizationId)
|
||||
select new { ou, o, su, p, ss, os };
|
||||
|
||||
return query.Select(x => new OrganizationUserOrganizationDetails
|
||||
public IQueryable<OrganizationUserOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
OrganizationId = x.ou.OrganizationId,
|
||||
UserId = x.ou.UserId,
|
||||
Name = x.o.Name,
|
||||
Enabled = x.o.Enabled,
|
||||
PlanType = x.o.PlanType,
|
||||
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,
|
||||
Seats = x.o.Seats,
|
||||
MaxCollections = x.o.MaxCollections,
|
||||
MaxStorageGb = x.o.MaxStorageGb,
|
||||
Identifier = x.o.Identifier,
|
||||
Key = x.ou.Key,
|
||||
ResetPasswordKey = x.ou.ResetPasswordKey,
|
||||
Status = x.ou.Status,
|
||||
Type = x.ou.Type,
|
||||
SsoExternalId = x.su.ExternalId,
|
||||
Permissions = x.ou.Permissions,
|
||||
PublicKey = x.o.PublicKey,
|
||||
PrivateKey = x.o.PrivateKey,
|
||||
ProviderId = x.p.Id,
|
||||
ProviderName = x.p.Name,
|
||||
SsoConfig = x.ss.Data,
|
||||
FamilySponsorshipFriendlyName = x.os.FriendlyName,
|
||||
FamilySponsorshipLastSyncDate = x.os.LastSyncDate,
|
||||
FamilySponsorshipToDelete = x.os.ToDelete,
|
||||
FamilySponsorshipValidUntil = x.os.ValidUntil
|
||||
});
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join o in dbContext.Organizations on ou.OrganizationId equals o.Id
|
||||
join su in dbContext.SsoUsers on ou.UserId equals su.UserId into su_g
|
||||
from su in su_g.DefaultIfEmpty()
|
||||
join po in dbContext.ProviderOrganizations on o.Id equals po.OrganizationId into po_g
|
||||
from po in po_g.DefaultIfEmpty()
|
||||
join p in dbContext.Providers on po.ProviderId equals p.Id into p_g
|
||||
from p in p_g.DefaultIfEmpty()
|
||||
join os in dbContext.OrganizationSponsorships on ou.Id equals os.SponsoringOrganizationUserId into os_g
|
||||
from os in os_g.DefaultIfEmpty()
|
||||
join ss in dbContext.SsoConfigs on ou.OrganizationId equals ss.OrganizationId into ss_g
|
||||
from ss in ss_g.DefaultIfEmpty()
|
||||
where ((su == null || !su.OrganizationId.HasValue) || su.OrganizationId == ou.OrganizationId)
|
||||
select new { ou, o, su, p, ss, os };
|
||||
|
||||
return query.Select(x => new OrganizationUserOrganizationDetails
|
||||
{
|
||||
OrganizationId = x.ou.OrganizationId,
|
||||
UserId = x.ou.UserId,
|
||||
Name = x.o.Name,
|
||||
Enabled = x.o.Enabled,
|
||||
PlanType = x.o.PlanType,
|
||||
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,
|
||||
Seats = x.o.Seats,
|
||||
MaxCollections = x.o.MaxCollections,
|
||||
MaxStorageGb = x.o.MaxStorageGb,
|
||||
Identifier = x.o.Identifier,
|
||||
Key = x.ou.Key,
|
||||
ResetPasswordKey = x.ou.ResetPasswordKey,
|
||||
Status = x.ou.Status,
|
||||
Type = x.ou.Type,
|
||||
SsoExternalId = x.su.ExternalId,
|
||||
Permissions = x.ou.Permissions,
|
||||
PublicKey = x.o.PublicKey,
|
||||
PrivateKey = x.o.PrivateKey,
|
||||
ProviderId = x.p.Id,
|
||||
ProviderName = x.p.Name,
|
||||
SsoConfig = x.ss.Data,
|
||||
FamilySponsorshipFriendlyName = x.os.FriendlyName,
|
||||
FamilySponsorshipLastSyncDate = x.os.LastSyncDate,
|
||||
FamilySponsorshipToDelete = x.os.ToDelete,
|
||||
FamilySponsorshipValidUntil = x.os.ValidUntil
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,28 +1,29 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserReadCountByFreeOrganizationAdminUserQuery : IQuery<OrganizationUser>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public OrganizationUserReadCountByFreeOrganizationAdminUserQuery(Guid userId)
|
||||
public class OrganizationUserReadCountByFreeOrganizationAdminUserQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join o in dbContext.Organizations
|
||||
on ou.OrganizationId equals o.Id
|
||||
where ou.UserId == _userId &&
|
||||
(ou.Type == OrganizationUserType.Owner || ou.Type == OrganizationUserType.Admin) &&
|
||||
o.PlanType == PlanType.Free &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
select ou;
|
||||
public OrganizationUserReadCountByFreeOrganizationAdminUserQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
return query;
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join o in dbContext.Organizations
|
||||
on ou.OrganizationId equals o.Id
|
||||
where ou.UserId == _userId &&
|
||||
(ou.Type == OrganizationUserType.Owner || ou.Type == OrganizationUserType.Admin) &&
|
||||
o.PlanType == PlanType.Free &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
select ou;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +1,37 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserReadCountByOnlyOwnerQuery : IQuery<OrganizationUser>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public OrganizationUserReadCountByOnlyOwnerQuery(Guid userId)
|
||||
public class OrganizationUserReadCountByOnlyOwnerQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var owners = from ou in dbContext.OrganizationUsers
|
||||
where ou.Type == OrganizationUserType.Owner &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
group ou by ou.OrganizationId into g
|
||||
select new
|
||||
{
|
||||
OrgUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(),
|
||||
ConfirmedOwnerCount = g.Count(),
|
||||
};
|
||||
public OrganizationUserReadCountByOnlyOwnerQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
var query = from owner in owners
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on owner.OrgUser.Id equals ou.Id
|
||||
where owner.OrgUser.UserId == _userId &&
|
||||
owner.ConfirmedOwnerCount == 1
|
||||
select ou;
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var owners = from ou in dbContext.OrganizationUsers
|
||||
where ou.Type == OrganizationUserType.Owner &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
group ou by ou.OrganizationId into g
|
||||
select new
|
||||
{
|
||||
OrgUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(),
|
||||
ConfirmedOwnerCount = g.Count(),
|
||||
};
|
||||
|
||||
return query;
|
||||
var query = from owner in owners
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on owner.OrgUser.Id equals ou.Id
|
||||
where owner.OrgUser.UserId == _userId &&
|
||||
owner.ConfirmedOwnerCount == 1
|
||||
select ou;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,30 +1,31 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserReadCountByOrganizationIdEmailQuery : IQuery<OrganizationUser>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
private readonly string _email;
|
||||
private readonly bool _onlyUsers;
|
||||
|
||||
public OrganizationUserReadCountByOrganizationIdEmailQuery(Guid organizationId, string email, bool onlyUsers)
|
||||
public class OrganizationUserReadCountByOrganizationIdEmailQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_email = email;
|
||||
_onlyUsers = onlyUsers;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
private readonly string _email;
|
||||
private readonly bool _onlyUsers;
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
where ou.OrganizationId == _organizationId &&
|
||||
((!_onlyUsers && (ou.Email == _email || u.Email == _email))
|
||||
|| (_onlyUsers && u.Email == _email))
|
||||
select ou;
|
||||
return query;
|
||||
public OrganizationUserReadCountByOrganizationIdEmailQuery(Guid organizationId, string email, bool onlyUsers)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
_email = email;
|
||||
_onlyUsers = onlyUsers;
|
||||
}
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users
|
||||
on ou.UserId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
where ou.OrganizationId == _organizationId &&
|
||||
((!_onlyUsers && (ou.Email == _email || u.Email == _email))
|
||||
|| (_onlyUsers && u.Email == _email))
|
||||
select ou;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,21 +1,22 @@
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserReadCountByOrganizationIdQuery : IQuery<OrganizationUser>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public OrganizationUserReadCountByOrganizationIdQuery(Guid organizationId)
|
||||
public class OrganizationUserReadCountByOrganizationIdQuery : IQuery<OrganizationUser>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.OrganizationId == _organizationId
|
||||
select ou;
|
||||
return query;
|
||||
public OrganizationUserReadCountByOrganizationIdQuery(Guid organizationId)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
|
||||
public IQueryable<OrganizationUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
where ou.OrganizationId == _organizationId
|
||||
select ou;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,104 +2,105 @@
|
||||
using Bit.Core.Models.Data;
|
||||
using CollectionUser = Bit.Infrastructure.EntityFramework.Models.CollectionUser;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsQuery
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public OrganizationUserUpdateWithCollectionsInsertQuery Insert { get; set; }
|
||||
public OrganizationUserUpdateWithCollectionsUpdateQuery Update { get; set; }
|
||||
public OrganizationUserUpdateWithCollectionsDeleteQuery Delete { get; set; }
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsQuery(OrganizationUser organizationUser,
|
||||
IEnumerable<SelectionReadOnly> collections)
|
||||
public class OrganizationUserUpdateWithCollectionsQuery
|
||||
{
|
||||
Insert = new OrganizationUserUpdateWithCollectionsInsertQuery(organizationUser, collections);
|
||||
Update = new OrganizationUserUpdateWithCollectionsUpdateQuery(organizationUser, collections);
|
||||
Delete = new OrganizationUserUpdateWithCollectionsDeleteQuery(organizationUser, collections);
|
||||
}
|
||||
}
|
||||
public OrganizationUserUpdateWithCollectionsInsertQuery Insert { get; set; }
|
||||
public OrganizationUserUpdateWithCollectionsUpdateQuery Update { get; set; }
|
||||
public OrganizationUserUpdateWithCollectionsDeleteQuery Delete { get; set; }
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsInsertQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsInsertQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var collectionIds = _collections.Select(c => c.Id).ToArray();
|
||||
var t = (from cu in dbContext.CollectionUsers
|
||||
where collectionIds.Contains(cu.CollectionId) &&
|
||||
cu.OrganizationUserId == _organizationUser.Id
|
||||
select cu).AsEnumerable();
|
||||
var insertQuery = (from c in dbContext.Collections
|
||||
where collectionIds.Contains(c.Id) &&
|
||||
c.OrganizationId == _organizationUser.OrganizationId &&
|
||||
!t.Any()
|
||||
select c).AsEnumerable();
|
||||
return insertQuery.Select(x => new CollectionUser
|
||||
public OrganizationUserUpdateWithCollectionsQuery(OrganizationUser organizationUser,
|
||||
IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
CollectionId = x.Id,
|
||||
OrganizationUserId = _organizationUser.Id,
|
||||
ReadOnly = _collections.FirstOrDefault(c => c.Id == x.Id).ReadOnly,
|
||||
HidePasswords = _collections.FirstOrDefault(c => c.Id == x.Id).HidePasswords,
|
||||
}).AsQueryable();
|
||||
}
|
||||
}
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsUpdateQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsUpdateQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
Insert = new OrganizationUserUpdateWithCollectionsInsertQuery(organizationUser, collections);
|
||||
Update = new OrganizationUserUpdateWithCollectionsUpdateQuery(organizationUser, collections);
|
||||
Delete = new OrganizationUserUpdateWithCollectionsDeleteQuery(organizationUser, collections);
|
||||
}
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
public class OrganizationUserUpdateWithCollectionsInsertQuery : IQuery<CollectionUser>
|
||||
{
|
||||
var collectionIds = _collections.Select(c => c.Id).ToArray();
|
||||
var updateQuery = (from target in dbContext.CollectionUsers
|
||||
where collectionIds.Contains(target.CollectionId) &&
|
||||
target.OrganizationUserId == _organizationUser.Id
|
||||
select new { target }).AsEnumerable();
|
||||
updateQuery = updateQuery.Where(cu =>
|
||||
cu.target.ReadOnly == _collections.FirstOrDefault(u => u.Id == cu.target.CollectionId).ReadOnly &&
|
||||
cu.target.HidePasswords == _collections.FirstOrDefault(u => u.Id == cu.target.CollectionId).HidePasswords);
|
||||
return updateQuery.Select(x => new CollectionUser
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsInsertQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
CollectionId = x.target.CollectionId,
|
||||
OrganizationUserId = _organizationUser.Id,
|
||||
ReadOnly = x.target.ReadOnly,
|
||||
HidePasswords = x.target.HidePasswords,
|
||||
}).AsQueryable();
|
||||
}
|
||||
}
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsDeleteQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsDeleteQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var deleteQuery = from cu in dbContext.CollectionUsers
|
||||
where !_collections.Any(
|
||||
c => c.Id == cu.CollectionId)
|
||||
select cu;
|
||||
return deleteQuery;
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var collectionIds = _collections.Select(c => c.Id).ToArray();
|
||||
var t = (from cu in dbContext.CollectionUsers
|
||||
where collectionIds.Contains(cu.CollectionId) &&
|
||||
cu.OrganizationUserId == _organizationUser.Id
|
||||
select cu).AsEnumerable();
|
||||
var insertQuery = (from c in dbContext.Collections
|
||||
where collectionIds.Contains(c.Id) &&
|
||||
c.OrganizationId == _organizationUser.OrganizationId &&
|
||||
!t.Any()
|
||||
select c).AsEnumerable();
|
||||
return insertQuery.Select(x => new CollectionUser
|
||||
{
|
||||
CollectionId = x.Id,
|
||||
OrganizationUserId = _organizationUser.Id,
|
||||
ReadOnly = _collections.FirstOrDefault(c => c.Id == x.Id).ReadOnly,
|
||||
HidePasswords = _collections.FirstOrDefault(c => c.Id == x.Id).HidePasswords,
|
||||
}).AsQueryable();
|
||||
}
|
||||
}
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsUpdateQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsUpdateQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var collectionIds = _collections.Select(c => c.Id).ToArray();
|
||||
var updateQuery = (from target in dbContext.CollectionUsers
|
||||
where collectionIds.Contains(target.CollectionId) &&
|
||||
target.OrganizationUserId == _organizationUser.Id
|
||||
select new { target }).AsEnumerable();
|
||||
updateQuery = updateQuery.Where(cu =>
|
||||
cu.target.ReadOnly == _collections.FirstOrDefault(u => u.Id == cu.target.CollectionId).ReadOnly &&
|
||||
cu.target.HidePasswords == _collections.FirstOrDefault(u => u.Id == cu.target.CollectionId).HidePasswords);
|
||||
return updateQuery.Select(x => new CollectionUser
|
||||
{
|
||||
CollectionId = x.target.CollectionId,
|
||||
OrganizationUserId = _organizationUser.Id,
|
||||
ReadOnly = x.target.ReadOnly,
|
||||
HidePasswords = x.target.HidePasswords,
|
||||
}).AsQueryable();
|
||||
}
|
||||
}
|
||||
|
||||
public class OrganizationUserUpdateWithCollectionsDeleteQuery : IQuery<CollectionUser>
|
||||
{
|
||||
private readonly OrganizationUser _organizationUser;
|
||||
private readonly IEnumerable<SelectionReadOnly> _collections;
|
||||
|
||||
public OrganizationUserUpdateWithCollectionsDeleteQuery(OrganizationUser organizationUser, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
_organizationUser = organizationUser;
|
||||
_collections = collections;
|
||||
}
|
||||
|
||||
public IQueryable<CollectionUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var deleteQuery = from cu in dbContext.CollectionUsers
|
||||
where !_collections.Any(
|
||||
c => c.Id == cu.CollectionId)
|
||||
select cu;
|
||||
return deleteQuery;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,34 +1,35 @@
|
||||
using Bit.Core.Models.Data.Organizations.OrganizationUsers;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class OrganizationUserUserDetailsViewQuery : IQuery<OrganizationUserUserDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public IQueryable<OrganizationUserUserDetails> Run(DatabaseContext dbContext)
|
||||
public class OrganizationUserUserDetailsViewQuery : IQuery<OrganizationUserUserDetails>
|
||||
{
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users on ou.UserId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
join su in dbContext.SsoUsers on u.Id equals su.UserId into su_g
|
||||
from su in su_g.DefaultIfEmpty()
|
||||
select new { ou, u, su };
|
||||
return query.Select(x => new OrganizationUserUserDetails
|
||||
public IQueryable<OrganizationUserUserDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
Id = x.ou.Id,
|
||||
OrganizationId = x.ou.OrganizationId,
|
||||
UserId = x.ou.UserId,
|
||||
Name = x.u.Name,
|
||||
Email = x.u.Email ?? x.ou.Email,
|
||||
TwoFactorProviders = x.u.TwoFactorProviders,
|
||||
Premium = x.u.Premium,
|
||||
Status = x.ou.Status,
|
||||
Type = x.ou.Type,
|
||||
AccessAll = x.ou.AccessAll,
|
||||
ExternalId = x.ou.ExternalId,
|
||||
SsoExternalId = x.su.ExternalId,
|
||||
Permissions = x.ou.Permissions,
|
||||
ResetPasswordKey = x.ou.ResetPasswordKey,
|
||||
UsesKeyConnector = x.u != null && x.u.UsesKeyConnector,
|
||||
});
|
||||
var query = from ou in dbContext.OrganizationUsers
|
||||
join u in dbContext.Users on ou.UserId equals u.Id into u_g
|
||||
from u in u_g.DefaultIfEmpty()
|
||||
join su in dbContext.SsoUsers on u.Id equals su.UserId into su_g
|
||||
from su in su_g.DefaultIfEmpty()
|
||||
select new { ou, u, su };
|
||||
return query.Select(x => new OrganizationUserUserDetails
|
||||
{
|
||||
Id = x.ou.Id,
|
||||
OrganizationId = x.ou.OrganizationId,
|
||||
UserId = x.ou.UserId,
|
||||
Name = x.u.Name,
|
||||
Email = x.u.Email ?? x.ou.Email,
|
||||
TwoFactorProviders = x.u.TwoFactorProviders,
|
||||
Premium = x.u.Premium,
|
||||
Status = x.ou.Status,
|
||||
Type = x.ou.Type,
|
||||
AccessAll = x.ou.AccessAll,
|
||||
ExternalId = x.ou.ExternalId,
|
||||
SsoExternalId = x.su.ExternalId,
|
||||
Permissions = x.ou.Permissions,
|
||||
ResetPasswordKey = x.ou.ResetPasswordKey,
|
||||
UsesKeyConnector = x.u != null && x.u.UsesKeyConnector,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,50 +1,51 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class PolicyReadByTypeApplicableToUserQuery : IQuery<Policy>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
private readonly PolicyType _policyType;
|
||||
private readonly OrganizationUserStatusType _minimumStatus;
|
||||
|
||||
public PolicyReadByTypeApplicableToUserQuery(Guid userId, PolicyType policyType, OrganizationUserStatusType minimumStatus)
|
||||
public class PolicyReadByTypeApplicableToUserQuery : IQuery<Policy>
|
||||
{
|
||||
_userId = userId;
|
||||
_policyType = policyType;
|
||||
_minimumStatus = minimumStatus;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
private readonly PolicyType _policyType;
|
||||
private readonly OrganizationUserStatusType _minimumStatus;
|
||||
|
||||
public IQueryable<Policy> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var providerOrganizations = from pu in dbContext.ProviderUsers
|
||||
where pu.UserId == _userId
|
||||
join po in dbContext.ProviderOrganizations
|
||||
on pu.ProviderId equals po.ProviderId
|
||||
select po;
|
||||
|
||||
string userEmail = null;
|
||||
if (_minimumStatus == OrganizationUserStatusType.Invited)
|
||||
public PolicyReadByTypeApplicableToUserQuery(Guid userId, PolicyType policyType, OrganizationUserStatusType minimumStatus)
|
||||
{
|
||||
// Invited orgUsers do not have a UserId associated with them, so we have to match up their email
|
||||
userEmail = dbContext.Users.Find(_userId)?.Email;
|
||||
_userId = userId;
|
||||
_policyType = policyType;
|
||||
_minimumStatus = minimumStatus;
|
||||
}
|
||||
|
||||
var query = from p in dbContext.Policies
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on p.OrganizationId equals ou.OrganizationId
|
||||
where
|
||||
((_minimumStatus > OrganizationUserStatusType.Invited && ou.UserId == _userId) ||
|
||||
(_minimumStatus == OrganizationUserStatusType.Invited && ou.Email == userEmail)) &&
|
||||
p.Type == _policyType &&
|
||||
p.Enabled &&
|
||||
ou.Status >= _minimumStatus &&
|
||||
ou.Type >= OrganizationUserType.User &&
|
||||
(ou.Permissions == null ||
|
||||
ou.Permissions.Contains($"\"managePolicies\":false")) &&
|
||||
!providerOrganizations.Any(po => po.OrganizationId == p.OrganizationId)
|
||||
select p;
|
||||
return query;
|
||||
public IQueryable<Policy> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var providerOrganizations = from pu in dbContext.ProviderUsers
|
||||
where pu.UserId == _userId
|
||||
join po in dbContext.ProviderOrganizations
|
||||
on pu.ProviderId equals po.ProviderId
|
||||
select po;
|
||||
|
||||
string userEmail = null;
|
||||
if (_minimumStatus == OrganizationUserStatusType.Invited)
|
||||
{
|
||||
// Invited orgUsers do not have a UserId associated with them, so we have to match up their email
|
||||
userEmail = dbContext.Users.Find(_userId)?.Email;
|
||||
}
|
||||
|
||||
var query = from p in dbContext.Policies
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on p.OrganizationId equals ou.OrganizationId
|
||||
where
|
||||
((_minimumStatus > OrganizationUserStatusType.Invited && ou.UserId == _userId) ||
|
||||
(_minimumStatus == OrganizationUserStatusType.Invited && ou.Email == userEmail)) &&
|
||||
p.Type == _policyType &&
|
||||
p.Enabled &&
|
||||
ou.Status >= _minimumStatus &&
|
||||
ou.Type >= OrganizationUserType.User &&
|
||||
(ou.Permissions == null ||
|
||||
ou.Permissions.Contains($"\"managePolicies\":false")) &&
|
||||
!providerOrganizations.Any(po => po.OrganizationId == p.OrganizationId)
|
||||
select p;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,29 +1,30 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class PolicyReadByUserIdQuery : IQuery<Policy>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public PolicyReadByUserIdQuery(Guid userId)
|
||||
public class PolicyReadByUserIdQuery : IQuery<Policy>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
|
||||
public IQueryable<Policy> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from p in dbContext.Policies
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on p.OrganizationId equals ou.OrganizationId
|
||||
join o in dbContext.Organizations
|
||||
on ou.OrganizationId equals o.Id
|
||||
where ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
o.Enabled == true
|
||||
select p;
|
||||
public PolicyReadByUserIdQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
return query;
|
||||
public IQueryable<Policy> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from p in dbContext.Policies
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on p.OrganizationId equals ou.OrganizationId
|
||||
join o in dbContext.Organizations
|
||||
on ou.OrganizationId equals o.Id
|
||||
where ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
o.Enabled == true
|
||||
select p;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,37 +1,38 @@
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class ProviderOrganizationOrganizationDetailsReadByProviderIdQuery : IQuery<ProviderOrganizationOrganizationDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _providerId;
|
||||
public ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(Guid providerId)
|
||||
public class ProviderOrganizationOrganizationDetailsReadByProviderIdQuery : IQuery<ProviderOrganizationOrganizationDetails>
|
||||
{
|
||||
_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()
|
||||
private readonly Guid _providerId;
|
||||
public ProviderOrganizationOrganizationDetailsReadByProviderIdQuery(Guid providerId)
|
||||
{
|
||||
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
|
||||
});
|
||||
_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
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,45 +1,46 @@
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class ProviderUserOrganizationDetailsViewQuery : IQuery<ProviderUserOrganizationDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
public IQueryable<ProviderUserOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
public class ProviderUserOrganizationDetailsViewQuery : IQuery<ProviderUserOrganizationDetails>
|
||||
{
|
||||
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
|
||||
public IQueryable<ProviderUserOrganizationDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
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,
|
||||
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,
|
||||
});
|
||||
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,
|
||||
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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,38 +1,39 @@
|
||||
using Bit.Core.Enums.Provider;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class ProviderUserProviderDetailsReadByUserIdStatusQuery : IQuery<ProviderUserProviderDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
private readonly ProviderUserStatusType? _status;
|
||||
public ProviderUserProviderDetailsReadByUserIdStatusQuery(Guid userId, ProviderUserStatusType? status)
|
||||
public class ProviderUserProviderDetailsReadByUserIdStatusQuery : IQuery<ProviderUserProviderDetails>
|
||||
{
|
||||
_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()
|
||||
private readonly Guid _userId;
|
||||
private readonly ProviderUserStatusType? _status;
|
||||
public ProviderUserProviderDetailsReadByUserIdStatusQuery(Guid userId, ProviderUserStatusType? status)
|
||||
{
|
||||
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,
|
||||
});
|
||||
_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,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,36 +1,37 @@
|
||||
using Bit.Core.Enums.Provider;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class ProviderUserReadCountByOnlyOwnerQuery : IQuery<ProviderUser>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _userId;
|
||||
|
||||
public ProviderUserReadCountByOnlyOwnerQuery(Guid userId)
|
||||
public class ProviderUserReadCountByOnlyOwnerQuery : IQuery<ProviderUser>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
private readonly Guid _userId;
|
||||
|
||||
public IQueryable<ProviderUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var owners = from pu in dbContext.ProviderUsers
|
||||
where pu.Type == ProviderUserType.ProviderAdmin &&
|
||||
pu.Status == ProviderUserStatusType.Confirmed
|
||||
group pu by pu.ProviderId into g
|
||||
select new
|
||||
{
|
||||
ProviderUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(),
|
||||
ConfirmedOwnerCount = g.Count(),
|
||||
};
|
||||
public ProviderUserReadCountByOnlyOwnerQuery(Guid userId)
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
|
||||
var query = from owner in owners
|
||||
join pu in dbContext.ProviderUsers
|
||||
on owner.ProviderUser.Id equals pu.Id
|
||||
where owner.ProviderUser.UserId == _userId &&
|
||||
owner.ConfirmedOwnerCount == 1
|
||||
select pu;
|
||||
public IQueryable<ProviderUser> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var owners = from pu in dbContext.ProviderUsers
|
||||
where pu.Type == ProviderUserType.ProviderAdmin &&
|
||||
pu.Status == ProviderUserStatusType.Confirmed
|
||||
group pu by pu.ProviderId into g
|
||||
select new
|
||||
{
|
||||
ProviderUser = g.Select(x => new { x.UserId, x.Id }).FirstOrDefault(),
|
||||
ConfirmedOwnerCount = g.Count(),
|
||||
};
|
||||
|
||||
return query;
|
||||
var query = from owner in owners
|
||||
join pu in dbContext.ProviderUsers
|
||||
on owner.ProviderUser.Id equals pu.Id
|
||||
where owner.ProviderUser.UserId == _userId &&
|
||||
owner.ConfirmedOwnerCount == 1
|
||||
select pu;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,50 +2,51 @@
|
||||
using Bit.Core.Enums;
|
||||
using User = Bit.Infrastructure.EntityFramework.Models.User;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery<User>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Cipher _cipher;
|
||||
|
||||
public UserBumpAccountRevisionDateByCipherIdQuery(Cipher cipher)
|
||||
public class UserBumpAccountRevisionDateByCipherIdQuery : IQuery<User>
|
||||
{
|
||||
_cipher = cipher;
|
||||
}
|
||||
private readonly Cipher _cipher;
|
||||
|
||||
public IQueryable<User> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
join collectionCipher in dbContext.CollectionCiphers
|
||||
on _cipher.Id equals collectionCipher.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
join collectionUser in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals collectionUser.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.AccessAll &&
|
||||
cu.OrganizationUserId == ou.Id
|
||||
join groupUser in dbContext.GroupUsers
|
||||
on ou.Id equals groupUser.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null &&
|
||||
!ou.AccessAll
|
||||
join grp in dbContext.Groups
|
||||
on gu.GroupId equals grp.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join collectionGroup in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals collectionGroup.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll &&
|
||||
cg.GroupId == gu.GroupId
|
||||
where ou.OrganizationId == _cipher.OrganizationId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(cu.CollectionId != null ||
|
||||
cg.CollectionId != null ||
|
||||
ou.AccessAll ||
|
||||
g.AccessAll)
|
||||
select u;
|
||||
return query;
|
||||
public UserBumpAccountRevisionDateByCipherIdQuery(Cipher cipher)
|
||||
{
|
||||
_cipher = cipher;
|
||||
}
|
||||
|
||||
public IQueryable<User> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
join collectionCipher in dbContext.CollectionCiphers
|
||||
on _cipher.Id equals collectionCipher.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
join collectionUser in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals collectionUser.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.AccessAll &&
|
||||
cu.OrganizationUserId == ou.Id
|
||||
join groupUser in dbContext.GroupUsers
|
||||
on ou.Id equals groupUser.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null &&
|
||||
!ou.AccessAll
|
||||
join grp in dbContext.Groups
|
||||
on gu.GroupId equals grp.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join collectionGroup in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals collectionGroup.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll &&
|
||||
cg.GroupId == gu.GroupId
|
||||
where ou.OrganizationId == _cipher.OrganizationId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
(cu.CollectionId != null ||
|
||||
cg.CollectionId != null ||
|
||||
ou.AccessAll ||
|
||||
g.AccessAll)
|
||||
select u;
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,26 +1,27 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Infrastructure.EntityFramework.Models;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class UserBumpAccountRevisionDateByOrganizationIdQuery : IQuery<User>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public UserBumpAccountRevisionDateByOrganizationIdQuery(Guid organizationId)
|
||||
public class UserBumpAccountRevisionDateByOrganizationIdQuery : IQuery<User>
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
private readonly Guid _organizationId;
|
||||
|
||||
public IQueryable<User> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
where ou.OrganizationId == _organizationId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
select u;
|
||||
public UserBumpAccountRevisionDateByOrganizationIdQuery(Guid organizationId)
|
||||
{
|
||||
_organizationId = organizationId;
|
||||
}
|
||||
|
||||
return query;
|
||||
public IQueryable<User> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from u in dbContext.Users
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on u.Id equals ou.UserId
|
||||
where ou.OrganizationId == _organizationId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
select u;
|
||||
|
||||
return query;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -2,70 +2,71 @@
|
||||
using Core.Models.Data;
|
||||
using Newtonsoft.Json.Linq;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class UserCipherDetailsQuery : IQuery<CipherDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid? _userId;
|
||||
public UserCipherDetailsQuery(Guid? userId)
|
||||
public class UserCipherDetailsQuery : IQuery<CipherDetails>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
public virtual IQueryable<CipherDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
where ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
where o.Id == ou.OrganizationId && o.Enabled
|
||||
join cc in dbContext.CollectionCiphers
|
||||
on c.Id equals cc.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
where ou.AccessAll
|
||||
join cu in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals cg.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.GroupId == gu.GroupId &&
|
||||
ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null
|
||||
select new { c, ou, o, cc, cu, gu, g, cg }.c;
|
||||
|
||||
var query2 = from c in dbContext.Ciphers
|
||||
where c.UserId == _userId
|
||||
select c;
|
||||
|
||||
var union = query.Union(query2).Select(c => new CipherDetails
|
||||
private readonly Guid? _userId;
|
||||
public UserCipherDetailsQuery(Guid? userId)
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
Favorite = _userId.HasValue && c.Favorites != null && c.Favorites.Contains($"\"{_userId}\":true"),
|
||||
FolderId = _userId.HasValue && !string.IsNullOrWhiteSpace(c.Folders) ?
|
||||
Guid.Parse(JObject.Parse(c.Folders)[_userId.Value.ToString()].Value<string>()) :
|
||||
null,
|
||||
Edit = true,
|
||||
ViewPassword = true,
|
||||
OrganizationUseTotp = false,
|
||||
});
|
||||
return union;
|
||||
_userId = userId;
|
||||
}
|
||||
public virtual IQueryable<CipherDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Ciphers
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
where ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
where o.Id == ou.OrganizationId && o.Enabled
|
||||
join cc in dbContext.CollectionCiphers
|
||||
on c.Id equals cc.CipherId into cc_g
|
||||
from cc in cc_g.DefaultIfEmpty()
|
||||
where ou.AccessAll
|
||||
join cu in dbContext.CollectionUsers
|
||||
on cc.CollectionId equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on cc.CollectionId equals cg.CollectionId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.GroupId == gu.GroupId &&
|
||||
ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null
|
||||
select new { c, ou, o, cc, cu, gu, g, cg }.c;
|
||||
|
||||
var query2 = from c in dbContext.Ciphers
|
||||
where c.UserId == _userId
|
||||
select c;
|
||||
|
||||
var union = query.Union(query2).Select(c => new CipherDetails
|
||||
{
|
||||
Id = c.Id,
|
||||
UserId = c.UserId,
|
||||
OrganizationId = c.OrganizationId,
|
||||
Type = c.Type,
|
||||
Data = c.Data,
|
||||
Attachments = c.Attachments,
|
||||
CreationDate = c.CreationDate,
|
||||
RevisionDate = c.RevisionDate,
|
||||
DeletedDate = c.DeletedDate,
|
||||
Favorite = _userId.HasValue && c.Favorites != null && c.Favorites.Contains($"\"{_userId}\":true"),
|
||||
FolderId = _userId.HasValue && !string.IsNullOrWhiteSpace(c.Folders) ?
|
||||
Guid.Parse(JObject.Parse(c.Folders)[_userId.Value.ToString()].Value<string>()) :
|
||||
null,
|
||||
Edit = true,
|
||||
ViewPassword = true,
|
||||
OrganizationUseTotp = false,
|
||||
});
|
||||
return union;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +1,53 @@
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class UserCollectionDetailsQuery : IQuery<CollectionDetails>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid? _userId;
|
||||
public UserCollectionDetailsQuery(Guid? userId)
|
||||
public class UserCollectionDetailsQuery : IQuery<CollectionDetails>
|
||||
{
|
||||
_userId = userId;
|
||||
}
|
||||
public virtual IQueryable<CollectionDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Collections
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
o.Enabled &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
|
||||
select new { c, ou, o, cu, gu, g, cg };
|
||||
return query.Select(x => new CollectionDetails
|
||||
private readonly Guid? _userId;
|
||||
public UserCollectionDetailsQuery(Guid? userId)
|
||||
{
|
||||
Id = x.c.Id,
|
||||
OrganizationId = x.c.OrganizationId,
|
||||
Name = x.c.Name,
|
||||
ExternalId = x.c.ExternalId,
|
||||
CreationDate = x.c.CreationDate,
|
||||
RevisionDate = x.c.RevisionDate,
|
||||
ReadOnly = !x.ou.AccessAll || !x.g.AccessAll || (x.cu.ReadOnly || x.cg.ReadOnly),
|
||||
HidePasswords = !x.ou.AccessAll || !x.g.AccessAll || (x.cu.HidePasswords || x.cg.HidePasswords),
|
||||
});
|
||||
_userId = userId;
|
||||
}
|
||||
public virtual IQueryable<CollectionDetails> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from c in dbContext.Collections
|
||||
join ou in dbContext.OrganizationUsers
|
||||
on c.OrganizationId equals ou.OrganizationId
|
||||
join o in dbContext.Organizations
|
||||
on c.OrganizationId equals o.Id
|
||||
join cu in dbContext.CollectionUsers
|
||||
on c.Id equals cu.CollectionId into cu_g
|
||||
from cu in cu_g.DefaultIfEmpty()
|
||||
where ou.AccessAll && cu.OrganizationUserId == ou.Id
|
||||
join gu in dbContext.GroupUsers
|
||||
on ou.Id equals gu.OrganizationUserId into gu_g
|
||||
from gu in gu_g.DefaultIfEmpty()
|
||||
where cu.CollectionId == null && !ou.AccessAll
|
||||
join g in dbContext.Groups
|
||||
on gu.GroupId equals g.Id into g_g
|
||||
from g in g_g.DefaultIfEmpty()
|
||||
join cg in dbContext.CollectionGroups
|
||||
on gu.GroupId equals cg.GroupId into cg_g
|
||||
from cg in cg_g.DefaultIfEmpty()
|
||||
where !g.AccessAll && cg.CollectionId == c.Id &&
|
||||
ou.UserId == _userId &&
|
||||
ou.Status == OrganizationUserStatusType.Confirmed &&
|
||||
o.Enabled &&
|
||||
(ou.AccessAll || cu.CollectionId != null || g.AccessAll || cg.CollectionId != null)
|
||||
select new { c, ou, o, cu, gu, g, cg };
|
||||
return query.Select(x => new CollectionDetails
|
||||
{
|
||||
Id = x.c.Id,
|
||||
OrganizationId = x.c.OrganizationId,
|
||||
Name = x.c.Name,
|
||||
ExternalId = x.c.ExternalId,
|
||||
CreationDate = x.c.CreationDate,
|
||||
RevisionDate = x.c.RevisionDate,
|
||||
ReadOnly = !x.ou.AccessAll || !x.g.AccessAll || (x.cu.ReadOnly || x.cg.ReadOnly),
|
||||
HidePasswords = !x.ou.AccessAll || !x.g.AccessAll || (x.cu.HidePasswords || x.cg.HidePasswords),
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -1,32 +1,33 @@
|
||||
using Bit.Core.Enums.Provider;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
|
||||
public class UserReadPublicKeysByProviderUserIdsQuery : IQuery<ProviderUserPublicKey>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries
|
||||
{
|
||||
private readonly Guid _providerId;
|
||||
private readonly IEnumerable<Guid> _ids;
|
||||
|
||||
public UserReadPublicKeysByProviderUserIdsQuery(Guid providerId, IEnumerable<Guid> Ids)
|
||||
public class UserReadPublicKeysByProviderUserIdsQuery : IQuery<ProviderUserPublicKey>
|
||||
{
|
||||
_providerId = providerId;
|
||||
_ids = Ids;
|
||||
}
|
||||
private readonly Guid _providerId;
|
||||
private readonly IEnumerable<Guid> _ids;
|
||||
|
||||
public virtual IQueryable<ProviderUserPublicKey> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from pu in dbContext.ProviderUsers
|
||||
join u in dbContext.Users
|
||||
on pu.UserId equals u.Id
|
||||
where _ids.Contains(pu.Id) &&
|
||||
pu.Status == ProviderUserStatusType.Accepted &&
|
||||
pu.ProviderId == _providerId
|
||||
select new { pu, u };
|
||||
return query.Select(x => new ProviderUserPublicKey
|
||||
public UserReadPublicKeysByProviderUserIdsQuery(Guid providerId, IEnumerable<Guid> Ids)
|
||||
{
|
||||
Id = x.pu.Id,
|
||||
PublicKey = x.u.PublicKey,
|
||||
});
|
||||
_providerId = providerId;
|
||||
_ids = Ids;
|
||||
}
|
||||
|
||||
public virtual IQueryable<ProviderUserPublicKey> Run(DatabaseContext dbContext)
|
||||
{
|
||||
var query = from pu in dbContext.ProviderUsers
|
||||
join u in dbContext.Users
|
||||
on pu.UserId equals u.Id
|
||||
where _ids.Contains(pu.Id) &&
|
||||
pu.Status == ProviderUserStatusType.Accepted &&
|
||||
pu.ProviderId == _providerId
|
||||
select new { pu, u };
|
||||
return query.Select(x => new ProviderUserPublicKey
|
||||
{
|
||||
Id = x.pu.Id,
|
||||
PublicKey = x.u.PublicKey,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,117 +5,118 @@ using Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public abstract class Repository<T, TEntity, TId> : BaseEntityFrameworkRepository, IRepository<T, TId>
|
||||
where TId : IEquatable<TId>
|
||||
where T : class, ITableObject<TId>
|
||||
where TEntity : class, ITableObject<TId>
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public Repository(IServiceScopeFactory serviceScopeFactory, IMapper mapper, Func<DatabaseContext, DbSet<TEntity>> getDbSet)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
public abstract class Repository<T, TEntity, TId> : BaseEntityFrameworkRepository, IRepository<T, TId>
|
||||
where TId : IEquatable<TId>
|
||||
where T : class, ITableObject<TId>
|
||||
where TEntity : class, ITableObject<TId>
|
||||
{
|
||||
GetDbSet = getDbSet;
|
||||
}
|
||||
|
||||
protected Func<DatabaseContext, DbSet<TEntity>> GetDbSet { get; private set; }
|
||||
|
||||
public virtual async Task<T> GetByIdAsync(TId id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public Repository(IServiceScopeFactory serviceScopeFactory, IMapper mapper, Func<DatabaseContext, DbSet<TEntity>> getDbSet)
|
||||
: base(serviceScopeFactory, mapper)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(id);
|
||||
return Mapper.Map<T>(entity);
|
||||
GetDbSet = getDbSet;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<T> CreateAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
obj.SetNewId();
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
await dbContext.AddAsync(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
obj.Id = entity.Id;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
protected Func<DatabaseContext, DbSet<TEntity>> GetDbSet { get; private set; }
|
||||
|
||||
public virtual async Task ReplaceAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public virtual async Task<T> GetByIdAsync(TId id)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(obj.Id);
|
||||
if (entity != null)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var mappedEntity = Mapper.Map<TEntity>(obj);
|
||||
dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(id);
|
||||
return Mapper.Map<T>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<T> CreateAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
obj.SetNewId();
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
await dbContext.AddAsync(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
obj.Id = entity.Id;
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task ReplaceAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FindAsync(obj.Id);
|
||||
if (entity != null)
|
||||
{
|
||||
var mappedEntity = Mapper.Map<TEntity>(obj);
|
||||
dbContext.Entry(entity).CurrentValues.SetValues(mappedEntity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task UpsertAsync(T obj)
|
||||
{
|
||||
if (obj.Id.Equals(default(TId)))
|
||||
{
|
||||
await CreateAsync(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplaceAsync(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
dbContext.Remove(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task UpsertAsync(T obj)
|
||||
{
|
||||
if (obj.Id.Equals(default(TId)))
|
||||
public virtual async Task RefreshDb()
|
||||
{
|
||||
await CreateAsync(obj);
|
||||
}
|
||||
else
|
||||
{
|
||||
await ReplaceAsync(obj);
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task DeleteAsync(T obj)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = Mapper.Map<TEntity>(obj);
|
||||
dbContext.Remove(entity);
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task RefreshDb()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var context = GetDatabaseContext(scope);
|
||||
await context.Database.EnsureDeletedAsync();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public virtual async Task<List<T>> CreateMany(List<T> objs)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var entities = new List<TEntity>();
|
||||
foreach (var o in objs)
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
o.SetNewId();
|
||||
var entity = Mapper.Map<TEntity>(o);
|
||||
entities.Add(entity);
|
||||
var context = GetDatabaseContext(scope);
|
||||
await context.Database.EnsureDeletedAsync();
|
||||
await context.Database.EnsureCreatedAsync();
|
||||
}
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
await GetDbSet(dbContext).AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return objs;
|
||||
}
|
||||
}
|
||||
|
||||
public IQueryable<Tout> Run<Tout>(IQuery<Tout> query)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public virtual async Task<List<T>> CreateMany(List<T> objs)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return query.Run(dbContext);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var entities = new List<TEntity>();
|
||||
foreach (var o in objs)
|
||||
{
|
||||
o.SetNewId();
|
||||
var entity = Mapper.Map<TEntity>(o);
|
||||
entities.Add(entity);
|
||||
}
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
await GetDbSet(dbContext).AddRangeAsync(entities);
|
||||
await dbContext.SaveChangesAsync();
|
||||
return objs;
|
||||
}
|
||||
}
|
||||
|
||||
public IQueryable<Tout> Run<Tout>(IQuery<Tout> query)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return query.Run(dbContext);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,42 +4,43 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class SendRepository : Repository<Core.Entities.Send, Send, Guid>, ISendRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public SendRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Sends)
|
||||
{ }
|
||||
|
||||
public override async Task<Core.Entities.Send> CreateAsync(Core.Entities.Send send)
|
||||
public class SendRepository : Repository<Core.Entities.Send, Send, Guid>, ISendRepository
|
||||
{
|
||||
send = await base.CreateAsync(send);
|
||||
if (send.UserId.HasValue)
|
||||
public SendRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Sends)
|
||||
{ }
|
||||
|
||||
public override async Task<Core.Entities.Send> CreateAsync(Core.Entities.Send send)
|
||||
{
|
||||
await UserUpdateStorage(send.UserId.Value);
|
||||
await UserBumpAccountRevisionDate(send.UserId.Value);
|
||||
send = await base.CreateAsync(send);
|
||||
if (send.UserId.HasValue)
|
||||
{
|
||||
await UserUpdateStorage(send.UserId.Value);
|
||||
await UserBumpAccountRevisionDate(send.UserId.Value);
|
||||
}
|
||||
return send;
|
||||
}
|
||||
return send;
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Send>> GetManyByDeletionDateAsync(DateTime deletionDateBefore)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Send>> GetManyByDeletionDateAsync(DateTime deletionDateBefore)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Sends.Where(s => s.DeletionDate < deletionDateBefore).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Send>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Sends.Where(s => s.DeletionDate < deletionDateBefore).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Send>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Send>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Send>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Sends.Where(s => s.UserId == userId).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Send>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Sends.Where(s => s.UserId == userId).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Send>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,42 +4,43 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class SsoConfigRepository : Repository<Core.Entities.SsoConfig, SsoConfig, long>, ISsoConfigRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public SsoConfigRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.SsoConfigs)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.SsoConfig> GetByOrganizationIdAsync(Guid organizationId)
|
||||
public class SsoConfigRepository : Repository<Core.Entities.SsoConfig, SsoConfig, long>, ISsoConfigRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public SsoConfigRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.SsoConfigs)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.SsoConfig> GetByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfig = await GetDbSet(dbContext).SingleOrDefaultAsync(sc => sc.OrganizationId == organizationId);
|
||||
return Mapper.Map<Core.Entities.SsoConfig>(ssoConfig);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfig = await GetDbSet(dbContext).SingleOrDefaultAsync(sc => sc.OrganizationId == organizationId);
|
||||
return Mapper.Map<Core.Entities.SsoConfig>(ssoConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.SsoConfig> GetByIdentifierAsync(string identifier)
|
||||
{
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.SsoConfig> GetByIdentifierAsync(string identifier)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfig = await GetDbSet(dbContext).SingleOrDefaultAsync(sc => sc.Organization.Identifier == identifier);
|
||||
return Mapper.Map<Core.Entities.SsoConfig>(ssoConfig);
|
||||
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfig = await GetDbSet(dbContext).SingleOrDefaultAsync(sc => sc.Organization.Identifier == identifier);
|
||||
return Mapper.Map<Core.Entities.SsoConfig>(ssoConfig);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.SsoConfig>> GetManyByRevisionNotBeforeDate(DateTime? notBefore)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.SsoConfig>> GetManyByRevisionNotBeforeDate(DateTime? notBefore)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfigs = await GetDbSet(dbContext).Where(sc => sc.Enabled && sc.RevisionDate >= notBefore).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.SsoConfig>>(ssoConfigs);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoConfigs = await GetDbSet(dbContext).Where(sc => sc.Enabled && sc.RevisionDate >= notBefore).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.SsoConfig>>(ssoConfigs);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,33 +4,34 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class SsoUserRepository : Repository<Core.Entities.SsoUser, SsoUser, long>, ISsoUserRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public SsoUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.SsoUsers)
|
||||
{ }
|
||||
|
||||
public async Task DeleteAsync(Guid userId, Guid? organizationId)
|
||||
public class SsoUserRepository : Repository<Core.Entities.SsoUser, SsoUser, long>, ISsoUserRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public SsoUserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.SsoUsers)
|
||||
{ }
|
||||
|
||||
public async Task DeleteAsync(Guid userId, Guid? organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).SingleOrDefaultAsync(su => su.UserId == userId && su.OrganizationId == organizationId);
|
||||
dbContext.Entry(entity).State = EntityState.Deleted;
|
||||
await dbContext.SaveChangesAsync();
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).SingleOrDefaultAsync(su => su.UserId == userId && su.OrganizationId == organizationId);
|
||||
dbContext.Entry(entity).State = EntityState.Deleted;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.SsoUser> GetByUserIdOrganizationIdAsync(Guid organizationId, Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<Core.Entities.SsoUser> GetByUserIdOrganizationIdAsync(Guid organizationId, Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(e => e.OrganizationId == organizationId && e.UserId == userId);
|
||||
return entity;
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext)
|
||||
.FirstOrDefaultAsync(e => e.OrganizationId == organizationId && e.UserId == userId);
|
||||
return entity;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -4,63 +4,64 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class TaxRateRepository : Repository<Core.Entities.TaxRate, TaxRate, string>, ITaxRateRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public TaxRateRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.TaxRates)
|
||||
{ }
|
||||
|
||||
public async Task ArchiveAsync(Core.Entities.TaxRate model)
|
||||
public class TaxRateRepository : Repository<Core.Entities.TaxRate, TaxRate, string>, ITaxRateRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public TaxRateRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.TaxRates)
|
||||
{ }
|
||||
|
||||
public async Task ArchiveAsync(Core.Entities.TaxRate model)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await dbContext.FindAsync<Core.Entities.TaxRate>(model);
|
||||
entity.Active = false;
|
||||
await dbContext.SaveChangesAsync();
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await dbContext.FindAsync<Core.Entities.TaxRate>(model);
|
||||
entity.Active = false;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> GetAllActiveAsync()
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> GetAllActiveAsync()
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Where(t => t.Active)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Where(t => t.Active)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> GetByLocationAsync(Core.Entities.TaxRate taxRate)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> GetByLocationAsync(Core.Entities.TaxRate taxRate)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Where(t => t.Active &&
|
||||
t.Country == taxRate.Country &&
|
||||
t.PostalCode == taxRate.PostalCode)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Where(t => t.Active &&
|
||||
t.Country == taxRate.Country &&
|
||||
t.PostalCode == taxRate.PostalCode)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> SearchAsync(int skip, int count)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.TaxRate>> SearchAsync(int skip, int count)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Skip(skip)
|
||||
.Take(count)
|
||||
.Where(t => t.Active)
|
||||
.OrderBy(t => t.Country).ThenByDescending(t => t.PostalCode)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.TaxRates
|
||||
.Skip(skip)
|
||||
.Take(count)
|
||||
.Where(t => t.Active)
|
||||
.OrderBy(t => t.Country).ThenByDescending(t => t.PostalCode)
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.TaxRate>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,46 +5,47 @@ using Bit.Infrastructure.EntityFramework.Models;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class TransactionRepository : Repository<Core.Entities.Transaction, Transaction, Guid>, ITransactionRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public TransactionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Transactions)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Transaction> GetByGatewayIdAsync(GatewayType gatewayType, string gatewayId)
|
||||
public class TransactionRepository : Repository<Core.Entities.Transaction, Transaction, Guid>, ITransactionRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public TransactionRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Transactions)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.Transaction> GetByGatewayIdAsync(GatewayType gatewayType, string gatewayId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.FirstOrDefaultAsync(t => (t.GatewayId == gatewayId && t.Gateway == gatewayType));
|
||||
return Mapper.Map<Core.Entities.Transaction>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.FirstOrDefaultAsync(t => (t.GatewayId == gatewayId && t.Gateway == gatewayType));
|
||||
return Mapper.Map<Core.Entities.Transaction>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Transaction>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Transaction>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.Where(t => (t.OrganizationId == organizationId && !t.UserId.HasValue))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Transaction>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.Where(t => (t.OrganizationId == organizationId && !t.UserId.HasValue))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Transaction>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.Transaction>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<ICollection<Core.Entities.Transaction>> GetManyByUserIdAsync(Guid userId)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.Where(t => (t.UserId == userId))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Transaction>>(results);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var results = await dbContext.Transactions
|
||||
.Where(t => (t.UserId == userId))
|
||||
.ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.Transaction>>(results);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -5,141 +5,142 @@ using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using DataModel = Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories;
|
||||
|
||||
public class UserRepository : Repository<Core.Entities.User, User, Guid>, IUserRepository
|
||||
namespace Bit.Infrastructure.EntityFramework.Repositories
|
||||
{
|
||||
public UserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Users)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.User> GetByEmailAsync(string email)
|
||||
public class UserRepository : Repository<Core.Entities.User, User, Guid>, IUserRepository
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public UserRepository(IServiceScopeFactory serviceScopeFactory, IMapper mapper)
|
||||
: base(serviceScopeFactory, mapper, (DatabaseContext context) => context.Users)
|
||||
{ }
|
||||
|
||||
public async Task<Core.Entities.User> GetByEmailAsync(string email)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FirstOrDefaultAsync(e => e.Email == email);
|
||||
return Mapper.Map<Core.Entities.User>(entity);
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var entity = await GetDbSet(dbContext).FirstOrDefaultAsync(e => e.Email == email);
|
||||
return Mapper.Map<Core.Entities.User>(entity);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DataModel.UserKdfInformation> GetKdfInformationByEmailAsync(string email)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<DataModel.UserKdfInformation> GetKdfInformationByEmailAsync(string email)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Email == email)
|
||||
.Select(e => new DataModel.UserKdfInformation
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Email == email)
|
||||
.Select(e => new DataModel.UserKdfInformation
|
||||
{
|
||||
Kdf = e.Kdf,
|
||||
KdfIterations = e.KdfIterations
|
||||
}).SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.User>> SearchAsync(string email, int skip, int take)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
List<User> users;
|
||||
if (dbContext.Database.IsNpgsql())
|
||||
{
|
||||
Kdf = e.Kdf,
|
||||
KdfIterations = e.KdfIterations
|
||||
}).SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.User>> SearchAsync(string email, int skip, int take)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
List<User> users;
|
||||
if (dbContext.Database.IsNpgsql())
|
||||
{
|
||||
users = await GetDbSet(dbContext)
|
||||
.Where(e => e.Email == null ||
|
||||
EF.Functions.ILike(EF.Functions.Collate(e.Email, "default"), "a%"))
|
||||
.OrderBy(e => e.Email)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
users = await GetDbSet(dbContext)
|
||||
.Where(e => e.Email == null ||
|
||||
EF.Functions.ILike(EF.Functions.Collate(e.Email, "default"), "a%"))
|
||||
.OrderBy(e => e.Email)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
users = await GetDbSet(dbContext)
|
||||
.Where(e => email == null || e.Email.StartsWith(email))
|
||||
.OrderBy(e => e.Email)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
}
|
||||
return Mapper.Map<List<Core.Entities.User>>(users);
|
||||
}
|
||||
else
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.User>> GetManyByPremiumAsync(bool premium)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
users = await GetDbSet(dbContext)
|
||||
.Where(e => email == null || e.Email.StartsWith(email))
|
||||
.OrderBy(e => e.Email)
|
||||
.Skip(skip).Take(take)
|
||||
.ToListAsync();
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = await GetDbSet(dbContext).Where(e => e.Premium == premium).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.User>>(users);
|
||||
}
|
||||
return Mapper.Map<List<Core.Entities.User>>(users);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Core.Entities.User>> GetManyByPremiumAsync(bool premium)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<string> GetPublicKeyAsync(Guid id)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = await GetDbSet(dbContext).Where(e => e.Premium == premium).ToListAsync();
|
||||
return Mapper.Map<List<Core.Entities.User>>(users);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetPublicKeyAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.PublicKey).SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.AccountRevisionDate)
|
||||
.SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
{
|
||||
await base.UserUpdateStorage(id);
|
||||
}
|
||||
|
||||
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var user = new User
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
Id = id,
|
||||
RenewalReminderDate = renewalReminderDate,
|
||||
};
|
||||
var set = GetDbSet(dbContext);
|
||||
set.Attach(user);
|
||||
dbContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.User> GetBySsoUserAsync(string externalId, Guid? organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoUser = await dbContext.SsoUsers.SingleOrDefaultAsync(e =>
|
||||
e.OrganizationId == organizationId && e.ExternalId == externalId);
|
||||
|
||||
if (ssoUser == null)
|
||||
{
|
||||
return null;
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.PublicKey).SingleOrDefaultAsync();
|
||||
}
|
||||
|
||||
var entity = await dbContext.Users.SingleOrDefaultAsync(e => e.Id == ssoUser.UserId);
|
||||
return Mapper.Map<Core.Entities.User>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Core.Entities.User>> GetManyAsync(IEnumerable<Guid> ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = dbContext.Users.Where(x => ids.Contains(x.Id));
|
||||
return await users.ToListAsync();
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
return await GetDbSet(dbContext).Where(e => e.Id == id).Select(e => e.AccountRevisionDate)
|
||||
.SingleOrDefaultAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task UpdateStorageAsync(Guid id)
|
||||
{
|
||||
await base.UserUpdateStorage(id);
|
||||
}
|
||||
|
||||
public async Task UpdateRenewalReminderDateAsync(Guid id, DateTime renewalReminderDate)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var user = new User
|
||||
{
|
||||
Id = id,
|
||||
RenewalReminderDate = renewalReminderDate,
|
||||
};
|
||||
var set = GetDbSet(dbContext);
|
||||
set.Attach(user);
|
||||
dbContext.Entry(user).Property(e => e.RenewalReminderDate).IsModified = true;
|
||||
await dbContext.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<Core.Entities.User> GetBySsoUserAsync(string externalId, Guid? organizationId)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var ssoUser = await dbContext.SsoUsers.SingleOrDefaultAsync(e =>
|
||||
e.OrganizationId == organizationId && e.ExternalId == externalId);
|
||||
|
||||
if (ssoUser == null)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
var entity = await dbContext.Users.SingleOrDefaultAsync(e => e.Id == ssoUser.UserId);
|
||||
return Mapper.Map<Core.Entities.User>(entity);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<IEnumerable<Core.Entities.User>> GetManyAsync(IEnumerable<Guid> ids)
|
||||
{
|
||||
using (var scope = ServiceScopeFactory.CreateScope())
|
||||
{
|
||||
var dbContext = GetDatabaseContext(scope);
|
||||
var users = dbContext.Users.Where(x => ids.Contains(x.Id));
|
||||
return await users.ToListAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user