Files
server/src/Api/Controllers/GroupsController.cs

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

167 lines
5.9 KiB
C#
Raw Normal View History

using Bit.Api.Models.Request;
2021-12-14 15:05:07 +00:00
using Bit.Api.Models.Response;
using Bit.Core.Context;
2017-05-08 14:08:44 -04:00
using Bit.Core.Exceptions;
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;
private readonly IOrganizationRepository _organizationRepository;
2017-05-08 14:08:44 -04:00
private readonly ICurrentContext _currentContext;
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(
IGroupRepository groupRepository,
2017-05-08 14:08:44 -04:00
IGroupService groupService,
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;
_organizationRepository = organizationRepository;
2017-05-08 14:08:44 -04:00
_currentContext = currentContext;
_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));
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);
}
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();
}
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) ||
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);
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);
if (!await _currentContext.ManageGroups(orgIdGuid))
2017-05-08 14:08:44 -04:00
{
throw new NotFoundException();
}
var organization = await _organizationRepository.GetByIdAsync(orgIdGuid);
2017-05-08 14:08:44 -04:00
var group = model.ToGroup(orgIdGuid);
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}")]
[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));
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId))
2017-05-08 14:08:44 -04:00
{
throw new NotFoundException();
}
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));
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}")]
[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));
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
}
await _groupService.DeleteAsync(group);
2017-05-09 19:24:03 -04:00
}
2022-08-29 14:53:16 -04:00
[HttpDelete("{id}/user/{orgUserId}")]
[HttpPost("{id}/delete-user/{orgUserId}")]
public async Task Delete(string orgId, string id, string orgUserId)
2022-08-29 16:06:55 -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
{
throw new NotFoundException();
}
2022-08-29 16:06:55 -04:00
await _groupService.DeleteUserAsync(group, new Guid(orgUserId));
2017-05-08 14:08:44 -04:00
}
}