1
0
mirror of https://github.com/bitwarden/server synced 2025-12-17 08:43:27 +00:00

Revert filescoped (#2227)

* Revert "Add git blame entry (#2226)"

This reverts commit 239286737d.

* Revert "Turn on file scoped namespaces (#2225)"

This reverts commit 34fb4cca2a.
This commit is contained in:
Justin Baur
2022-08-29 15:53:48 -04:00
committed by GitHub
parent 239286737d
commit bae03feffe
1208 changed files with 74317 additions and 73126 deletions

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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,
});
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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);
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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);
}
}

View File

@@ -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
});
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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,
});
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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
});
}
}
}

View File

@@ -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,
});
}
}
}

View File

@@ -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,
});
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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;
}
}
}

View File

@@ -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),
});
}
}
}

View File

@@ -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,
});
}
}
}