mirror of
https://github.com/bitwarden/server
synced 2025-12-26 21:23:39 +00:00
collection user refactor
This commit is contained in:
@@ -81,6 +81,24 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionUserDetails>> GetManyUserDetailsByIdAsync(Guid organizationId,
|
||||
Guid collectionId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<CollectionUserDetails>(
|
||||
$"[{Schema}].[CollectionUserDetails_ReadByCollectionId]",
|
||||
new { OrganizationId = organizationId, CollectionId = collectionId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
// Return distinct Id results. If at least one of the grouped results is not ReadOnly, that we return it.
|
||||
return results
|
||||
.GroupBy(c => c.OrganizationUserId)
|
||||
.Select(g => g.OrderBy(og => og.ReadOnly).First())
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateAsync(Collection obj, IEnumerable<SelectionReadOnly> groups)
|
||||
{
|
||||
obj.SetNewId();
|
||||
@@ -110,6 +128,17 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task DeleteUserAsync(Guid collectionId, Guid organizationUserId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"[{Schema}].[CollectionUser_Delete]",
|
||||
new { CollectionId = collectionId, OrganizationUserId = organizationUserId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public class CollectionWithGroups : Collection
|
||||
{
|
||||
public DataTable Groups { get; set; }
|
||||
|
||||
@@ -1,54 +0,0 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using Dapper;
|
||||
using System.Linq;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
public class CollectionUserRepository : Repository<CollectionUser, Guid>, ICollectionUserRepository
|
||||
{
|
||||
public CollectionUserRepository(GlobalSettings globalSettings)
|
||||
: this(globalSettings.SqlServer.ConnectionString)
|
||||
{ }
|
||||
|
||||
public CollectionUserRepository(string connectionString)
|
||||
: base(connectionString)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<CollectionUser>> GetManyByOrganizationUserIdAsync(Guid orgUserId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<CollectionUser>(
|
||||
$"[{Schema}].[{Table}_ReadByOrganizationUserId]",
|
||||
new { OrganizationUserId = orgUserId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<CollectionUserUserDetails>> GetManyDetailsByCollectionIdAsync(Guid organizationId,
|
||||
Guid collectionId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<CollectionUserUserDetails>(
|
||||
$"[{Schema}].[CollectionUserUserDetails_ReadByCollectionId]",
|
||||
new { OrganizationId = organizationId, CollectionId = collectionId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
// Return distinct Id results. If at least one of the grouped results is not ReadOnly, that we return it.
|
||||
return results
|
||||
.GroupBy(c => c.Id)
|
||||
.Select(g => g.OrderBy(og => og.ReadOnly).First())
|
||||
.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -51,12 +51,12 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<GroupUserUserDetails>> GetManyUserDetailsByIdAsync(Guid id)
|
||||
public async Task<ICollection<GroupUserDetails>> GetManyUserDetailsByIdAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<GroupUserUserDetails>(
|
||||
$"[{Schema}].[GroupUserUserDetails_ReadByGroupId]",
|
||||
var results = await connection.QueryAsync<GroupUserDetails>(
|
||||
$"[{Schema}].[GroupUserDetails_ReadByGroupId]",
|
||||
new { GroupId = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
|
||||
@@ -9,6 +9,7 @@ using Bit.Core.Models.Data;
|
||||
using System.Collections.Generic;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Utilities;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
@@ -166,5 +167,41 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateAsync(OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
obj.SetNewId();
|
||||
var objWithCollections = JsonConvert.DeserializeObject<OrganizationUserWithCollections>(
|
||||
JsonConvert.SerializeObject(obj));
|
||||
objWithCollections.Collections = collections.ToArrayTVP();
|
||||
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"[{Schema}].[OrganizationUser_CreateWithCollections]",
|
||||
objWithCollections,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(OrganizationUser obj, IEnumerable<SelectionReadOnly> collections)
|
||||
{
|
||||
var objWithCollections = JsonConvert.DeserializeObject<OrganizationUserWithCollections>(
|
||||
JsonConvert.SerializeObject(obj));
|
||||
objWithCollections.Collections = collections.ToArrayTVP();
|
||||
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"[{Schema}].[OrganizationUser_UpdateWithCollections]",
|
||||
objWithCollections,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public class OrganizationUserWithCollections : OrganizationUser
|
||||
{
|
||||
public DataTable Collections { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user