mirror of
https://github.com/bitwarden/server.git
synced 2025-02-01 23:31:41 +01:00
add/edit group with collection ids
This commit is contained in:
parent
e996a410dc
commit
d166f9cca3
@ -41,6 +41,18 @@ namespace Bit.Api.Controllers
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
[HttpGet("{id}/details")]
|
||||
public async Task<GroupDetailsResponseModel> GetDetails(string orgId, string id)
|
||||
{
|
||||
var groupDetails = await _groupRepository.GetByIdWithCollectionsAsync(new Guid(id));
|
||||
if(groupDetails?.Item1 == null || !_currentContext.OrganizationAdmin(groupDetails.Item1.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new GroupDetailsResponseModel(groupDetails.Item1, groupDetails.Item2);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<GroupResponseModel>> Get(string orgId)
|
||||
{
|
||||
@ -65,7 +77,15 @@ namespace Bit.Api.Controllers
|
||||
}
|
||||
|
||||
var group = model.ToGroup(orgIdGuid);
|
||||
await _groupRepository.CreateAsync(group);
|
||||
if(model.CollectionIds == null)
|
||||
{
|
||||
await _groupRepository.CreateAsync(group);
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.CreateAsync(group, model.CollectionIds.Select(c => new Guid(c)));
|
||||
}
|
||||
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
@ -79,7 +99,15 @@ namespace Bit.Api.Controllers
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group));
|
||||
if(model.CollectionIds == null)
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group));
|
||||
}
|
||||
else
|
||||
{
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group), model.CollectionIds.Select(c => new Guid(c)));
|
||||
}
|
||||
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
|
@ -2,6 +2,7 @@
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Models.Table;
|
||||
using Newtonsoft.Json;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
@ -10,6 +11,7 @@ namespace Bit.Core.Models.Api
|
||||
[Required]
|
||||
[StringLength(300)]
|
||||
public string Name { get; set; }
|
||||
public IEnumerable<string> CollectionIds { get; set; }
|
||||
|
||||
public Group ToGroup(Guid orgId)
|
||||
{
|
||||
|
@ -1,5 +1,6 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
@ -22,4 +23,15 @@ namespace Bit.Core.Models.Api
|
||||
public string OrganizationId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
|
||||
public class GroupDetailsResponseModel : GroupResponseModel
|
||||
{
|
||||
public GroupDetailsResponseModel(Group group, IEnumerable<Guid> collectionIds)
|
||||
: base(group, "groupDetails")
|
||||
{
|
||||
CollectionIds = collectionIds;
|
||||
}
|
||||
|
||||
public IEnumerable<Guid> CollectionIds { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,9 @@ namespace Bit.Core.Repositories
|
||||
{
|
||||
public interface IGroupRepository : IRepository<Group, Guid>
|
||||
{
|
||||
Task<Tuple<Group, ICollection<Guid>>> GetByIdWithCollectionsAsync(Guid id);
|
||||
Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId);
|
||||
Task CreateAsync(Group obj, IEnumerable<Guid> collectionIds);
|
||||
Task ReplaceAsync(Group obj, IEnumerable<Guid> collectionIds);
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,8 @@ using Dapper;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
using Newtonsoft.Json;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
@ -19,6 +21,22 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
: base(connectionString)
|
||||
{ }
|
||||
|
||||
public async Task<Tuple<Group, ICollection<Guid>>> GetByIdWithCollectionsAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryMultipleAsync(
|
||||
$"[{Schema}].[Group_ReadWithCollectionsById]",
|
||||
new { Id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
var group = await results.ReadFirstOrDefaultAsync<Group>();
|
||||
var colletionIds = (await results.ReadAsync<Guid>()).ToList();
|
||||
|
||||
return new Tuple<Group, ICollection<Guid>>(group, colletionIds);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
@ -31,5 +49,39 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task CreateAsync(Group obj, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
obj.SetNewId();
|
||||
var objWithCollections = JsonConvert.DeserializeObject<GroupWithCollections>(JsonConvert.SerializeObject(obj));
|
||||
objWithCollections.CollectionIds = collectionIds.ToGuidIdArrayTVP();
|
||||
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"[{Schema}].[Group_CreateWithCollections]",
|
||||
objWithCollections,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public async Task ReplaceAsync(Group obj, IEnumerable<Guid> collectionIds)
|
||||
{
|
||||
var objWithCollections = JsonConvert.DeserializeObject<GroupWithCollections>(JsonConvert.SerializeObject(obj));
|
||||
objWithCollections.CollectionIds = collectionIds.ToGuidIdArrayTVP();
|
||||
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.ExecuteAsync(
|
||||
$"[{Schema}].[Group_UpdateWithCollections]",
|
||||
objWithCollections,
|
||||
commandType: CommandType.StoredProcedure);
|
||||
}
|
||||
}
|
||||
|
||||
public class GroupWithCollections : Group
|
||||
{
|
||||
public DataTable CollectionIds { get; set; }
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -184,5 +184,8 @@
|
||||
<Build Include="dbo\Stored Procedures\Group_ReadByOrganizationId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_Update.sql" />
|
||||
<Build Include="dbo\Views\GroupView.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_CreateWithCollections.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_UpdateWithCollections.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_ReadWithCollectionsById.sql" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -0,0 +1,34 @@
|
||||
CREATE PROCEDURE [dbo].[Group_CreateWithCollections]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Name VARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7),
|
||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
EXEC [dbo].[Group_Create] @Id, @OrganizationId, @Name, @CreationDate, @RevisionDate
|
||||
|
||||
;WITH [AvailableCollectionsCTE] AS(
|
||||
SELECT
|
||||
[Id]
|
||||
FROM
|
||||
[dbo].[Collection]
|
||||
WHERE
|
||||
[OrganizationId] = @OrganizationId
|
||||
)
|
||||
INSERT INTO [dbo].[CollectionGroup]
|
||||
(
|
||||
[CollectionId],
|
||||
[GroupId]
|
||||
)
|
||||
SELECT
|
||||
[Id],
|
||||
@Id
|
||||
FROM
|
||||
@CollectionIds
|
||||
WHERE
|
||||
[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE])
|
||||
END
|
@ -0,0 +1,15 @@
|
||||
CREATE PROCEDURE [dbo].[Group_ReadWithCollectionsById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
EXEC [dbo].[Group_ReadById] @Id
|
||||
|
||||
SELECT
|
||||
[CollectionId]
|
||||
FROM
|
||||
[dbo].[CollectionGroup]
|
||||
WHERE
|
||||
[GroupId] = @Id
|
||||
END
|
@ -0,0 +1,40 @@
|
||||
CREATE PROCEDURE [dbo].[Group_UpdateWithCollections]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Name VARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7),
|
||||
@CollectionIds AS [dbo].[GuidIdArray] READONLY
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
EXEC [dbo].[Group_Update] @Id, @OrganizationId, @Name, @CreationDate, @RevisionDate
|
||||
|
||||
;WITH [AvailableCollectionsCTE] AS(
|
||||
SELECT
|
||||
Id
|
||||
FROM
|
||||
[dbo].[Collection]
|
||||
WHERE
|
||||
OrganizationId = @OrganizationId
|
||||
)
|
||||
MERGE
|
||||
[dbo].[CollectionGroup] AS [Target]
|
||||
USING
|
||||
@CollectionIds AS [Source]
|
||||
ON
|
||||
[Target].[CollectionId] = [Source].[Id]
|
||||
AND [Target].[GroupId] = @Id
|
||||
WHEN NOT MATCHED BY TARGET
|
||||
AND [Source].[Id] IN (SELECT [Id] FROM [AvailableCollectionsCTE]) THEN
|
||||
INSERT VALUES
|
||||
(
|
||||
[Source].[Id],
|
||||
@Id
|
||||
)
|
||||
WHEN NOT MATCHED BY SOURCE
|
||||
AND [Target].[GroupId] = @Id THEN
|
||||
DELETE
|
||||
;
|
||||
END
|
Loading…
Reference in New Issue
Block a user