mirror of
https://github.com/bitwarden/server.git
synced 2026-02-01 06:33:17 +08:00
* [AC-2084] Add documentation to existing collection repository getters * [AC-2084] Add new CollectionAdminDetails model * [AC-2084] Add SQL and migration scripts * [AC-2084] Introduce new repository methods to include permission details for collections * [AC-2084] Add EF repository methods and integration tests * [AC-2084] Update CollectionsController and response models * [AC-2084] Fix failing SqlServer test * [AC-2084] Clean up admin endpoint response models - vNext endpoints should now always return CollectionDetailsResponse models - Update constructors in CollectionDetailsResponseModel to be more explicit and add named static constructors for additional clarity * [AC-2084] Fix failing tests * [AC-2084] Fix potential provider/member bug * [AC-2084] Fix broken collections controller * [AC-2084] Cleanup collection response model types and constructors * [AC-2084] Remove redundant authorization check * [AC-2084] Cleanup ambiguous model name * [AC-2084] Add GroupBy clause to sprocs * [AC-2084] Add GroupBy logic to EF repository * [AC-2084] Update collection repository tests * [AC-2084] Update migration script date * Update migration script date --------- Co-authored-by: Thomas Rittson <31796059+eliykat@users.noreply.github.com> Co-authored-by: kejaeger <138028972+kejaeger@users.noreply.github.com>
88 lines
3.8 KiB
C#
88 lines
3.8 KiB
C#
using Bit.Core.Models.Data;
|
|
|
|
namespace Bit.Infrastructure.EntityFramework.Repositories.Queries;
|
|
|
|
/// <summary>
|
|
/// Query to get collection details, including permissions for the specified user if provided.
|
|
/// </summary>
|
|
public class CollectionAdminDetailsQuery : IQuery<CollectionAdminDetails>
|
|
{
|
|
private readonly Guid? _userId;
|
|
private readonly Guid? _organizationId;
|
|
private readonly Guid? _collectionId;
|
|
|
|
private CollectionAdminDetailsQuery(Guid? userId, Guid? organizationId, Guid? collectionId)
|
|
{
|
|
_userId = userId;
|
|
_organizationId = organizationId;
|
|
_collectionId = collectionId;
|
|
}
|
|
|
|
public virtual IQueryable<CollectionAdminDetails> Run(DatabaseContext dbContext)
|
|
{
|
|
var baseCollectionQuery = from c in dbContext.Collections
|
|
join ou in dbContext.OrganizationUsers
|
|
on new { c.OrganizationId, UserId = _userId } equals
|
|
new { ou.OrganizationId, ou.UserId } into ou_g
|
|
from ou in ou_g.DefaultIfEmpty()
|
|
|
|
join cu in dbContext.CollectionUsers
|
|
on new { CollectionId = c.Id, OrganizationUserId = ou.Id } equals
|
|
new { cu.CollectionId, cu.OrganizationUserId } into cu_g
|
|
from cu in cu_g.DefaultIfEmpty()
|
|
|
|
join gu in dbContext.GroupUsers
|
|
on new { CollectionId = (Guid?)cu.CollectionId, OrganizationUserId = ou.Id } equals
|
|
new { CollectionId = (Guid?)null, gu.OrganizationUserId } into gu_g
|
|
from gu in gu_g.DefaultIfEmpty()
|
|
|
|
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 new { CollectionId = c.Id, gu.GroupId } equals
|
|
new { cg.CollectionId, cg.GroupId } into cg_g
|
|
from cg in cg_g.DefaultIfEmpty()
|
|
select new { c, cu, cg };
|
|
|
|
if (_organizationId.HasValue)
|
|
{
|
|
baseCollectionQuery = baseCollectionQuery.Where(x => x.c.OrganizationId == _organizationId);
|
|
}
|
|
else if (_collectionId.HasValue)
|
|
{
|
|
baseCollectionQuery = baseCollectionQuery.Where(x => x.c.Id == _collectionId);
|
|
}
|
|
else
|
|
{
|
|
throw new InvalidOperationException("OrganizationId or CollectionId must be specified.");
|
|
}
|
|
|
|
return baseCollectionQuery.Select(x => new CollectionAdminDetails
|
|
{
|
|
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 = (bool?)x.cu.ReadOnly ?? (bool?)x.cg.ReadOnly ?? false,
|
|
HidePasswords = (bool?)x.cu.HidePasswords ?? (bool?)x.cg.HidePasswords ?? false,
|
|
Manage = (bool?)x.cu.Manage ?? (bool?)x.cg.Manage ?? false,
|
|
Assigned = x.cu != null || x.cg != null,
|
|
});
|
|
}
|
|
|
|
public static CollectionAdminDetailsQuery ByCollectionId(Guid collectionId, Guid? userId)
|
|
{
|
|
return new CollectionAdminDetailsQuery(userId, null, collectionId);
|
|
}
|
|
|
|
public static CollectionAdminDetailsQuery ByOrganizationId(Guid organizationId, Guid? userId)
|
|
{
|
|
return new CollectionAdminDetailsQuery(userId, organizationId, null);
|
|
}
|
|
|
|
}
|