2022-06-29 19:46:41 -04:00
|
|
|
|
using System.Data;
|
2017-03-23 17:43:12 -04:00
|
|
|
|
using System.Data.SqlClient;
|
2022-01-11 10:40:51 +01:00
|
|
|
|
using Bit.Core.Entities;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
2021-02-22 15:35:16 -06:00
|
|
|
|
using Bit.Core.Settings;
|
2021-12-16 15:35:09 +01:00
|
|
|
|
using Dapper;
|
2017-03-23 17:43:12 -04:00
|
|
|
|
|
2022-01-11 10:40:51 +01:00
|
|
|
|
namespace Bit.Infrastructure.Dapper.Repositories
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public class CollectionCipherRepository : BaseRepository, ICollectionCipherRepository
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public CollectionCipherRepository(GlobalSettings globalSettings)
|
2018-10-09 17:21:12 -04:00
|
|
|
|
: this(globalSettings.SqlServer.ConnectionString, globalSettings.SqlServer.ReadOnlyConnectionString)
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2018-10-09 17:21:12 -04:00
|
|
|
|
public CollectionCipherRepository(string connectionString, string readOnlyConnectionString)
|
|
|
|
|
|
: base(connectionString, readOnlyConnectionString)
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{ }
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task<ICollection<CollectionCipher>> GetManyByUserIdAsync(Guid userId)
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-03-23 17:43:12 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var results = await connection.QueryAsync<CollectionCipher>(
|
|
|
|
|
|
"[dbo].[CollectionCipher_ReadByUserId]",
|
2017-03-23 17:43:12 -04:00
|
|
|
|
new { UserId = userId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-04 17:22:47 -04:00
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task<ICollection<CollectionCipher>> GetManyByOrganizationIdAsync(Guid organizationId)
|
2017-04-17 17:01:23 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-04-17 17:01:23 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var results = await connection.QueryAsync<CollectionCipher>(
|
|
|
|
|
|
"[dbo].[CollectionCipher_ReadByOrganizationId]",
|
2017-04-17 17:01:23 -04:00
|
|
|
|
new { OrganizationId = organizationId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task<ICollection<CollectionCipher>> GetManyByUserIdCipherIdAsync(Guid userId, Guid cipherId)
|
2017-04-04 17:22:47 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-04-04 17:22:47 -04:00
|
|
|
|
{
|
2017-04-27 09:19:30 -04:00
|
|
|
|
var results = await connection.QueryAsync<CollectionCipher>(
|
|
|
|
|
|
"[dbo].[CollectionCipher_ReadByUserIdCipherId]",
|
2017-04-04 17:22:47 -04:00
|
|
|
|
new { UserId = userId, CipherId = cipherId },
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
|
|
|
|
|
|
return results.ToList();
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-12 12:42:00 -04:00
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task UpdateCollectionsAsync(Guid cipherId, Guid userId, IEnumerable<Guid> collectionIds)
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-04-12 12:42:00 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
2017-04-27 09:19:30 -04:00
|
|
|
|
"[dbo].[CollectionCipher_UpdateCollections]",
|
|
|
|
|
|
new { CipherId = cipherId, UserId = userId, CollectionIds = collectionIds.ToGuidIdArrayTVP() },
|
2017-04-12 12:42:00 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-04-17 23:12:48 -04:00
|
|
|
|
|
2017-04-27 09:19:30 -04:00
|
|
|
|
public async Task UpdateCollectionsForAdminAsync(Guid cipherId, Guid organizationId, IEnumerable<Guid> collectionIds)
|
2017-04-17 23:12:48 -04:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2017-04-17 23:12:48 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
2017-04-27 09:19:30 -04:00
|
|
|
|
"[dbo].[CollectionCipher_UpdateCollectionsAdmin]",
|
|
|
|
|
|
new { CipherId = cipherId, OrganizationId = organizationId, CollectionIds = collectionIds.ToGuidIdArrayTVP() },
|
2017-04-17 23:12:48 -04:00
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2018-06-13 14:03:44 -04:00
|
|
|
|
|
|
|
|
|
|
public async Task UpdateCollectionsForCiphersAsync(IEnumerable<Guid> cipherIds, Guid userId,
|
|
|
|
|
|
Guid organizationId, IEnumerable<Guid> collectionIds)
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
using (var connection = new SqlConnection(ConnectionString))
|
2018-06-13 14:03:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
var results = await connection.ExecuteAsync(
|
|
|
|
|
|
"[dbo].[CollectionCipher_UpdateCollectionsForCiphers]",
|
|
|
|
|
|
new
|
|
|
|
|
|
{
|
|
|
|
|
|
CipherIds = cipherIds.ToGuidIdArrayTVP(),
|
|
|
|
|
|
UserId = userId,
|
|
|
|
|
|
OrganizationId = organizationId,
|
|
|
|
|
|
CollectionIds = collectionIds.ToGuidIdArrayTVP()
|
|
|
|
|
|
},
|
|
|
|
|
|
commandType: CommandType.StoredProcedure);
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-03-23 17:43:12 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|