mirror of
https://github.com/bitwarden/server.git
synced 2024-12-23 17:07:42 +01:00
added groups apis
This commit is contained in:
parent
a03c19d693
commit
fdf7546f33
99
src/Api/Controllers/GroupsController.cs
Normal file
99
src/Api/Controllers/GroupsController.cs
Normal file
@ -0,0 +1,99 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Core.Models.Api;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Services;
|
||||
using Bit.Core;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
[Route("organizations/{orgId}/groups")]
|
||||
[Authorize("Application")]
|
||||
public class GroupsController : Controller
|
||||
{
|
||||
private readonly IGroupRepository _groupRepository;
|
||||
private readonly IUserService _userService;
|
||||
private readonly CurrentContext _currentContext;
|
||||
|
||||
public GroupsController(
|
||||
IGroupRepository groupRepository,
|
||||
IUserService userService,
|
||||
CurrentContext currentContext)
|
||||
{
|
||||
_groupRepository = groupRepository;
|
||||
_userService = userService;
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<GroupResponseModel> Get(string orgId, string id)
|
||||
{
|
||||
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
||||
if(group == null || !_currentContext.OrganizationAdmin(group.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<GroupResponseModel>> Get(string orgId)
|
||||
{
|
||||
var orgIdGuid = new Guid(orgId);
|
||||
if(!_currentContext.OrganizationAdmin(orgIdGuid))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var groups = await _groupRepository.GetManyByOrganizationIdAsync(orgIdGuid);
|
||||
var responses = groups.Select(g => new GroupResponseModel(g));
|
||||
return new ListResponseModel<GroupResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<GroupResponseModel> Post(string orgId, [FromBody]GroupRequestModel model)
|
||||
{
|
||||
var orgIdGuid = new Guid(orgId);
|
||||
if(!_currentContext.OrganizationAdmin(orgIdGuid))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var group = model.ToGroup(orgIdGuid);
|
||||
await _groupRepository.CreateAsync(group);
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[HttpPost("{id}")]
|
||||
public async Task<GroupResponseModel> Put(string orgId, string id, [FromBody]GroupRequestModel model)
|
||||
{
|
||||
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
||||
if(group == null || !_currentContext.OrganizationAdmin(group.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _groupRepository.ReplaceAsync(model.ToGroup(group));
|
||||
return new GroupResponseModel(group);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
public async Task Delete(string orgId, string id)
|
||||
{
|
||||
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
||||
if(group == null || !_currentContext.OrganizationAdmin(group.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _groupRepository.DeleteAsync(group);
|
||||
}
|
||||
}
|
||||
}
|
28
src/Core/Models/Api/Request/GroupRequestModel.cs
Normal file
28
src/Core/Models/Api/Request/GroupRequestModel.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Core.Models.Table;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class GroupRequestModel
|
||||
{
|
||||
[Required]
|
||||
[StringLength(300)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public Group ToGroup(Guid orgId)
|
||||
{
|
||||
return ToGroup(new Group
|
||||
{
|
||||
OrganizationId = orgId
|
||||
});
|
||||
}
|
||||
|
||||
public Group ToGroup(Group existingGroup)
|
||||
{
|
||||
existingGroup.Name = Name;
|
||||
return existingGroup;
|
||||
}
|
||||
}
|
||||
}
|
25
src/Core/Models/Api/Response/GroupResponseModel.cs
Normal file
25
src/Core/Models/Api/Response/GroupResponseModel.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
|
||||
namespace Bit.Core.Models.Api
|
||||
{
|
||||
public class GroupResponseModel : ResponseModel
|
||||
{
|
||||
public GroupResponseModel(Group group, string obj = "group")
|
||||
: base(obj)
|
||||
{
|
||||
if(group == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(group));
|
||||
}
|
||||
|
||||
Id = group.Id.ToString();
|
||||
OrganizationId = group.OrganizationId.ToString();
|
||||
Name = group.Name;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string OrganizationId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
19
src/Core/Models/Table/Group.cs
Normal file
19
src/Core/Models/Table/Group.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Bit.Core.Utilities;
|
||||
|
||||
namespace Bit.Core.Models.Table
|
||||
{
|
||||
public class Group : IDataObject<Guid>
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid OrganizationId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public DateTime CreationDate { get; internal set; } = DateTime.UtcNow;
|
||||
public DateTime RevisionDate { get; internal set; } = DateTime.UtcNow;
|
||||
|
||||
public void SetNewId()
|
||||
{
|
||||
Id = CoreHelpers.GenerateComb();
|
||||
}
|
||||
}
|
||||
}
|
12
src/Core/Repositories/IGroupRepository.cs
Normal file
12
src/Core/Repositories/IGroupRepository.cs
Normal file
@ -0,0 +1,12 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Bit.Core.Repositories
|
||||
{
|
||||
public interface IGroupRepository : IRepository<Group, Guid>
|
||||
{
|
||||
Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId);
|
||||
}
|
||||
}
|
35
src/Core/Repositories/SqlServer/GroupRepository.cs
Normal file
35
src/Core/Repositories/SqlServer/GroupRepository.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using Bit.Core.Models.Table;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using System.Data;
|
||||
using System.Data.SqlClient;
|
||||
using System.Linq;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
public class GroupRepository : Repository<Group, Guid>, IGroupRepository
|
||||
{
|
||||
public GroupRepository(GlobalSettings globalSettings)
|
||||
: this(globalSettings.SqlServer.ConnectionString)
|
||||
{ }
|
||||
|
||||
public GroupRepository(string connectionString)
|
||||
: base(connectionString)
|
||||
{ }
|
||||
|
||||
public async Task<ICollection<Group>> GetManyByOrganizationIdAsync(Guid organizationId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<Group>(
|
||||
$"[{Schema}].[Group_ReadByOrganizationId]",
|
||||
new { OrganizationId = organizationId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
@ -35,6 +35,7 @@ namespace Bit.Core.Utilities
|
||||
services.AddSingleton<ICollectionUserRepository, SqlServerRepos.CollectionUserRepository>();
|
||||
services.AddSingleton<IFolderRepository, SqlServerRepos.FolderRepository>();
|
||||
services.AddSingleton<ICollectionCipherRepository, SqlServerRepos.CollectionCipherRepository>();
|
||||
services.AddSingleton<IGroupRepository, SqlServerRepos.GroupRepository>();
|
||||
}
|
||||
|
||||
public static void AddBaseServices(this IServiceCollection services)
|
||||
|
@ -178,5 +178,11 @@
|
||||
<Build Include="dbo\User Defined Types\GuidIdArray.sql" />
|
||||
<Build Include="dbo\Stored Procedures\OrganizationUser_ReadCountByOrganizationOwnerUser.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Cipher_ReadCanEditByIdUserId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_Create.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_DeleteById.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_ReadById.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_ReadByOrganizationId.sql" />
|
||||
<Build Include="dbo\Stored Procedures\Group_Update.sql" />
|
||||
<Build Include="dbo\Views\GroupView.sql" />
|
||||
</ItemGroup>
|
||||
</Project>
|
@ -5,9 +5,9 @@ BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
S.*
|
||||
*
|
||||
FROM
|
||||
[dbo].[CollectionView] S
|
||||
[dbo].[CollectionView]
|
||||
WHERE
|
||||
S.[OrganizationId] = @OrganizationId
|
||||
[OrganizationId] = @OrganizationId
|
||||
END
|
@ -5,15 +5,15 @@ BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
S.*
|
||||
C.*
|
||||
FROM
|
||||
[dbo].[CollectionView] S
|
||||
[dbo].[CollectionView] C
|
||||
INNER JOIN
|
||||
[Organization] O ON O.[Id] = S.[OrganizationId]
|
||||
[Organization] O ON O.[Id] = C.[OrganizationId]
|
||||
INNER JOIN
|
||||
[dbo].[OrganizationUser] OU ON OU.[OrganizationId] = O.[Id] AND OU.[UserId] = @UserId
|
||||
LEFT JOIN
|
||||
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = S.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||||
[dbo].[CollectionUser] CU ON OU.[AccessAll] = 0 AND CU.[CollectionId] = C.[Id] AND CU.[OrganizationUserId] = OU.[Id]
|
||||
WHERE
|
||||
OU.[Status] = 2 -- Confirmed
|
||||
AND O.[Enabled] = 1
|
||||
|
27
src/Sql/dbo/Stored Procedures/Group_Create.sql
Normal file
27
src/Sql/dbo/Stored Procedures/Group_Create.sql
Normal file
@ -0,0 +1,27 @@
|
||||
CREATE PROCEDURE [dbo].[Group_Create]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Name VARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
INSERT INTO [dbo].[Group]
|
||||
(
|
||||
[Id],
|
||||
[OrganizationId],
|
||||
[Name],
|
||||
[CreationDate],
|
||||
[RevisionDate]
|
||||
)
|
||||
VALUES
|
||||
(
|
||||
@Id,
|
||||
@OrganizationId,
|
||||
@Name,
|
||||
@CreationDate,
|
||||
@RevisionDate
|
||||
)
|
||||
END
|
12
src/Sql/dbo/Stored Procedures/Group_DeleteById.sql
Normal file
12
src/Sql/dbo/Stored Procedures/Group_DeleteById.sql
Normal file
@ -0,0 +1,12 @@
|
||||
CREATE PROCEDURE [dbo].[Group_DeleteById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
DELETE
|
||||
FROM
|
||||
[dbo].[Group]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
13
src/Sql/dbo/Stored Procedures/Group_ReadById.sql
Normal file
13
src/Sql/dbo/Stored Procedures/Group_ReadById.sql
Normal file
@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[Group_ReadById]
|
||||
@Id UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[GroupView]
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
13
src/Sql/dbo/Stored Procedures/Group_ReadByOrganizationId.sql
Normal file
13
src/Sql/dbo/Stored Procedures/Group_ReadByOrganizationId.sql
Normal file
@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[Group_ReadByOrganizationId]
|
||||
@OrganizationId UNIQUEIDENTIFIER
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[GroupView]
|
||||
WHERE
|
||||
[OrganizationId] = @OrganizationId
|
||||
END
|
20
src/Sql/dbo/Stored Procedures/Group_Update.sql
Normal file
20
src/Sql/dbo/Stored Procedures/Group_Update.sql
Normal file
@ -0,0 +1,20 @@
|
||||
CREATE PROCEDURE [dbo].[Group_Update]
|
||||
@Id UNIQUEIDENTIFIER,
|
||||
@OrganizationId UNIQUEIDENTIFIER,
|
||||
@Name VARCHAR(MAX),
|
||||
@CreationDate DATETIME2(7),
|
||||
@RevisionDate DATETIME2(7)
|
||||
AS
|
||||
BEGIN
|
||||
SET NOCOUNT ON
|
||||
|
||||
UPDATE
|
||||
[dbo].[Group]
|
||||
SET
|
||||
[OrganizationId] = @OrganizationId,
|
||||
[Name] = @Name,
|
||||
[CreationDate] = @CreationDate,
|
||||
[RevisionDate] = @RevisionDate
|
||||
WHERE
|
||||
[Id] = @Id
|
||||
END
|
6
src/Sql/dbo/Views/GroupView.sql
Normal file
6
src/Sql/dbo/Views/GroupView.sql
Normal file
@ -0,0 +1,6 @@
|
||||
CREATE VIEW [dbo].[GroupView]
|
||||
AS
|
||||
SELECT
|
||||
*
|
||||
FROM
|
||||
[dbo].[Group]
|
Loading…
Reference in New Issue
Block a user