2022-06-29 19:46:41 -04:00
|
|
|
|
using Bit.Api.Models.Request;
|
2021-12-14 15:05:07 +00:00
|
|
|
|
using Bit.Api.Models.Response;
|
2021-02-04 12:54:21 -06:00
|
|
|
|
using Bit.Core.Context;
|
2017-05-08 14:08:44 -04:00
|
|
|
|
using Bit.Core.Exceptions;
|
2022-12-12 09:59:48 +00:00
|
|
|
|
using Bit.Core.OrganizationFeatures.Groups.Interfaces;
|
2017-05-08 14:08:44 -04:00
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Services;
|
|
|
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
|
|
|
using Microsoft.AspNetCore.Mvc;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Api.Controllers;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
[Route("organizations/{orgId}/groups")]
|
|
|
|
|
|
[Authorize("Application")]
|
|
|
|
|
|
public class GroupsController : Controller
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IGroupRepository _groupRepository;
|
|
|
|
|
|
private readonly IGroupService _groupService;
|
2022-12-12 09:59:48 +00:00
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
2017-05-08 14:08:44 -04:00
|
|
|
|
private readonly ICurrentContext _currentContext;
|
2022-12-12 09:59:48 +00:00
|
|
|
|
private readonly ICreateGroupCommand _createGroupCommand;
|
|
|
|
|
|
private readonly IUpdateGroupCommand _updateGroupCommand;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
public GroupsController(
|
2021-02-04 12:54:21 -06:00
|
|
|
|
IGroupRepository groupRepository,
|
2017-05-08 14:08:44 -04:00
|
|
|
|
IGroupService groupService,
|
2022-12-12 09:59:48 +00:00
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
ICurrentContext currentContext,
|
|
|
|
|
|
ICreateGroupCommand createGroupCommand,
|
|
|
|
|
|
IUpdateGroupCommand updateGroupCommand)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-09 14:17:22 -04:00
|
|
|
|
_groupRepository = groupRepository;
|
|
|
|
|
|
_groupService = groupService;
|
2022-12-12 09:59:48 +00:00
|
|
|
|
_organizationRepository = organizationRepository;
|
2017-05-08 14:08:44 -04:00
|
|
|
|
_currentContext = currentContext;
|
2022-12-12 09:59:48 +00:00
|
|
|
|
_createGroupCommand = createGroupCommand;
|
|
|
|
|
|
_updateGroupCommand = updateGroupCommand;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-08 22:14:01 -04:00
|
|
|
|
[HttpGet("{id}")]
|
2017-05-08 14:08:44 -04:00
|
|
|
|
public async Task<GroupResponseModel> Get(string orgId, string id)
|
|
|
|
|
|
{
|
|
|
|
|
|
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
2021-02-04 12:54:21 -06:00
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2017-05-08 14:08:44 -04:00
|
|
|
|
{
|
2017-05-09 14:17:22 -04:00
|
|
|
|
throw new NotFoundException();
|
2017-05-08 14:08:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return new GroupResponseModel(group);
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-09 19:04:01 -04:00
|
|
|
|
[HttpGet("{id}/details")]
|
2017-05-08 14:08:44 -04:00
|
|
|
|
public async Task<GroupDetailsResponseModel> GetDetails(string orgId, string id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 14:08:44 -04:00
|
|
|
|
var groupDetails = await _groupRepository.GetByIdWithCollectionsAsync(new Guid(id));
|
|
|
|
|
|
if (groupDetails?.Item1 == null || !await _currentContext.ManageGroups(groupDetails.Item1.OrganizationId))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 14:08:44 -04:00
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2021-07-08 17:05:32 +02:00
|
|
|
|
return new GroupDetailsResponseModel(groupDetails.Item1, groupDetails.Item2);
|
2017-05-08 22:14:01 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
[HttpGet("")]
|
|
|
|
|
|
public async Task<ListResponseModel<GroupResponseModel>> Get(string orgId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 22:14:01 -04:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
|
|
|
|
|
var canAccess = await _currentContext.ManageGroups(orgIdGuid) ||
|
|
|
|
|
|
await _currentContext.ViewAssignedCollections(orgIdGuid) ||
|
|
|
|
|
|
await _currentContext.ViewAllCollections(orgIdGuid) ||
|
2021-07-08 17:05:32 +02:00
|
|
|
|
await _currentContext.ManageUsers(orgIdGuid);
|
2017-05-08 22:14:01 -04:00
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
if (!canAccess)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var groups = await _groupRepository.GetManyByOrganizationIdAsync(orgIdGuid);
|
|
|
|
|
|
var responses = groups.Select(g => new GroupResponseModel(g));
|
|
|
|
|
|
return new ListResponseModel<GroupResponseModel>(responses);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-09 19:04:01 -04:00
|
|
|
|
[HttpGet("{id}/users")]
|
2018-10-18 08:38:22 -04:00
|
|
|
|
public async Task<IEnumerable<Guid>> GetUsers(string orgId, string id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-09 19:04:01 -04:00
|
|
|
|
var idGuid = new Guid(id);
|
|
|
|
|
|
var group = await _groupRepository.GetByIdAsync(idGuid);
|
2021-07-08 17:05:32 +02:00
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2017-05-09 19:04:01 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-18 08:38:22 -04:00
|
|
|
|
var groupIds = await _groupRepository.GetManyUserIdsByIdAsync(idGuid);
|
|
|
|
|
|
return groupIds;
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
[HttpPost("")]
|
|
|
|
|
|
public async Task<GroupResponseModel> Post(string orgId, [FromBody] GroupRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 14:08:44 -04:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
2021-07-08 17:05:32 +02:00
|
|
|
|
if (!await _currentContext.ManageGroups(orgIdGuid))
|
2017-05-08 14:08:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-12 09:59:48 +00:00
|
|
|
|
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
|
2017-05-08 14:08:44 -04:00
|
|
|
|
var group = model.ToGroup(orgIdGuid);
|
2022-12-12 09:59:48 +00:00
|
|
|
|
await _createGroupCommand.CreateGroupAsync(group, organization, model.Collections?.Select(c => c.ToSelectionReadOnly()));
|
|
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
return new GroupResponseModel(group);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
[HttpPut("{id}")]
|
2018-07-16 17:29:38 -04:00
|
|
|
|
[HttpPost("{id}")]
|
2017-05-08 14:08:44 -04:00
|
|
|
|
public async Task<GroupResponseModel> Put(string orgId, string id, [FromBody] GroupRequestModel model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 14:08:44 -04:00
|
|
|
|
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
2021-07-08 17:05:32 +02:00
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2017-05-08 14:08:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2022-12-12 09:59:48 +00:00
|
|
|
|
var orgIdGuid = new Guid(orgId);
|
|
|
|
|
|
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
|
|
|
|
|
|
|
|
|
|
|
|
await _updateGroupCommand.UpdateGroupAsync(model.ToGroup(group), organization, model.Collections?.Select(c => c.ToSelectionReadOnly()));
|
2017-05-08 14:08:44 -04:00
|
|
|
|
return new GroupResponseModel(group);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-10-18 08:38:22 -04:00
|
|
|
|
[HttpPut("{id}/users")]
|
|
|
|
|
|
public async Task PutUsers(string orgId, string id, [FromBody] IEnumerable<Guid> model)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2018-10-18 08:38:22 -04:00
|
|
|
|
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
2021-07-08 17:05:32 +02:00
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2018-10-18 08:38:22 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
|
|
|
|
|
}
|
|
|
|
|
|
await _groupRepository.UpdateUsersAsync(group.Id, model);
|
2022-08-29 16:06:55 -04:00
|
|
|
|
}
|
2018-10-18 08:38:22 -04:00
|
|
|
|
|
2017-05-08 14:08:44 -04:00
|
|
|
|
[HttpDelete("{id}")]
|
2018-07-16 17:29:38 -04:00
|
|
|
|
[HttpPost("{id}/delete")]
|
2017-05-08 14:08:44 -04:00
|
|
|
|
public async Task Delete(string orgId, string id)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-08 14:08:44 -04:00
|
|
|
|
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
2021-07-08 17:05:32 +02:00
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2017-05-08 14:08:44 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new NotFoundException();
|
2017-05-09 19:24:03 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2018-07-09 23:07:04 -04:00
|
|
|
|
await _groupService.DeleteAsync(group);
|
2017-05-09 19:24:03 -04:00
|
|
|
|
}
|
2022-08-29 14:53:16 -04:00
|
|
|
|
|
2017-05-11 11:41:13 -04:00
|
|
|
|
[HttpDelete("{id}/user/{orgUserId}")]
|
2018-07-16 17:29:38 -04:00
|
|
|
|
[HttpPost("{id}/delete-user/{orgUserId}")]
|
2017-05-11 11:41:13 -04:00
|
|
|
|
public async Task Delete(string orgId, string id, string orgUserId)
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-11 11:41:13 -04:00
|
|
|
|
var group = await _groupRepository.GetByIdAsync(new Guid(id));
|
|
|
|
|
|
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
|
2022-08-29 16:06:55 -04:00
|
|
|
|
{
|
2017-05-11 11:41:13 -04:00
|
|
|
|
throw new NotFoundException();
|
2022-08-29 15:53:48 -04:00
|
|
|
|
}
|
2022-08-29 16:06:55 -04:00
|
|
|
|
|
2017-05-11 11:41:13 -04:00
|
|
|
|
await _groupService.DeleteUserAsync(group, new Guid(orgUserId));
|
2017-05-08 14:08:44 -04:00
|
|
|
|
}
|
|
|
|
|
|
}
|