mirror of
https://github.com/bitwarden/server.git
synced 2025-07-02 00:22:50 -05:00
collection user refactor
This commit is contained in:
@ -1,70 +0,0 @@
|
||||
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}/collectionUsers")]
|
||||
[Authorize("Application")]
|
||||
public class CollectionUsersController : Controller
|
||||
{
|
||||
private readonly ICollectionRepository _collectionRepository;
|
||||
private readonly ICollectionUserRepository _collectionUserRepository;
|
||||
private readonly IUserService _userService;
|
||||
private readonly CurrentContext _currentContext;
|
||||
|
||||
public CollectionUsersController(
|
||||
ICollectionRepository collectionRepository,
|
||||
ICollectionUserRepository collectionUserRepository,
|
||||
IUserService userService,
|
||||
CurrentContext currentContext)
|
||||
{
|
||||
_collectionRepository = collectionRepository;
|
||||
_collectionUserRepository = collectionUserRepository;
|
||||
_userService = userService;
|
||||
_currentContext = currentContext;
|
||||
}
|
||||
|
||||
[HttpGet("{collectionId}")]
|
||||
public async Task<ListResponseModel<CollectionUserResponseModel>> GetByCollection(string orgId, string collectionId)
|
||||
{
|
||||
var collectionIdGuid = new Guid(collectionId);
|
||||
var collection = await _collectionRepository.GetByIdAsync(collectionIdGuid);
|
||||
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var collectionUsers = await _collectionUserRepository.GetManyDetailsByCollectionIdAsync(collection.OrganizationId,
|
||||
collection.Id);
|
||||
var responses = collectionUsers.Select(c => new CollectionUserResponseModel(c));
|
||||
return new ListResponseModel<CollectionUserResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
public async Task Delete(string orgId, string id)
|
||||
{
|
||||
var user = await _collectionUserRepository.GetByIdAsync(new Guid(id));
|
||||
if(user == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var collection = await _collectionRepository.GetByIdAsync(user.CollectionId);
|
||||
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _collectionUserRepository.DeleteAsync(user);
|
||||
}
|
||||
}
|
||||
}
|
@ -17,20 +17,17 @@ namespace Bit.Api.Controllers
|
||||
public class CollectionsController : Controller
|
||||
{
|
||||
private readonly ICollectionRepository _collectionRepository;
|
||||
private readonly ICollectionUserRepository _collectionUserRepository;
|
||||
private readonly ICollectionService _collectionService;
|
||||
private readonly IUserService _userService;
|
||||
private readonly CurrentContext _currentContext;
|
||||
|
||||
public CollectionsController(
|
||||
ICollectionRepository collectionRepository,
|
||||
ICollectionUserRepository collectionUserRepository,
|
||||
ICollectionService collectionService,
|
||||
IUserService userService,
|
||||
CurrentContext currentContext)
|
||||
{
|
||||
_collectionRepository = collectionRepository;
|
||||
_collectionUserRepository = collectionUserRepository;
|
||||
_collectionService = collectionService;
|
||||
_userService = userService;
|
||||
_currentContext = currentContext;
|
||||
@ -82,6 +79,24 @@ namespace Bit.Api.Controllers
|
||||
return new ListResponseModel<CollectionResponseModel>(responses);
|
||||
}
|
||||
|
||||
|
||||
|
||||
[HttpGet("{id}/users")]
|
||||
public async Task<ListResponseModel<CollectionUserResponseModel>> GetUsers(string orgId, string id)
|
||||
{
|
||||
var idGuid = new Guid(id);
|
||||
var collection = await _collectionRepository.GetByIdAsync(idGuid);
|
||||
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
var collectionUsers = await _collectionRepository.GetManyUserDetailsByIdAsync(collection.OrganizationId,
|
||||
collection.Id);
|
||||
var responses = collectionUsers.Select(c => new CollectionUserResponseModel(c));
|
||||
return new ListResponseModel<CollectionUserResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<CollectionResponseModel> Post(string orgId, [FromBody]CollectionRequestModel model)
|
||||
{
|
||||
@ -123,5 +138,18 @@ namespace Bit.Api.Controllers
|
||||
|
||||
await _collectionRepository.DeleteAsync(collection);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}/user/{orgUserId}")]
|
||||
[HttpPost("{id}/delete-user/{orgUserId}")]
|
||||
public async Task Delete(string orgId, string id, string orgUserId)
|
||||
{
|
||||
var collection = await _collectionRepository.GetByIdAsync(new Guid(id));
|
||||
if(collection == null || !_currentContext.OrganizationAdmin(collection.OrganizationId))
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _collectionRepository.DeleteUserAsync(collection.Id, new Guid(orgUserId));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -93,7 +93,7 @@ namespace Bit.Api.Controllers
|
||||
|
||||
var userId = _userService.GetProperUserId(User);
|
||||
var result = await _organizationService.InviteUserAsync(orgGuidId, userId.Value, model.Email, model.Type.Value,
|
||||
model.AccessAll, model.Collections?.Select(c => c.ToCollectionUser()));
|
||||
model.AccessAll, model.Collections?.Select(c => c.ToSelectionReadOnly()));
|
||||
}
|
||||
|
||||
[HttpPut("{id}/reinvite")]
|
||||
@ -150,7 +150,7 @@ namespace Bit.Api.Controllers
|
||||
|
||||
var userId = _userService.GetProperUserId(User);
|
||||
await _organizationService.SaveUserAsync(model.ToOrganizationUser(organizationUser), userId.Value,
|
||||
model.Collections?.Select(c => c.ToCollectionUser()));
|
||||
model.Collections?.Select(c => c.ToSelectionReadOnly()));
|
||||
}
|
||||
|
||||
[HttpPut("{id}/groups")]
|
||||
|
Reference in New Issue
Block a user