mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
Subvault APIs
This commit is contained in:
@ -35,7 +35,7 @@ namespace Bit.Api.Controllers
|
||||
public async Task<FolderResponseModel> Get(string id)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var folder = await _cipherRepository.GetByIdAsync(new Guid(id), _userService.GetProperUserId(User).Value);
|
||||
var folder = await _cipherRepository.GetByIdAsync(new Guid(id), userId);
|
||||
if(folder == null || folder.Type != Core.Enums.CipherType.Folder)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
|
96
src/Api/Controllers/SubvaultsController.cs
Normal file
96
src/Api/Controllers/SubvaultsController.cs
Normal file
@ -0,0 +1,96 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Api.Models;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
[Route("subvaults")]
|
||||
[Authorize("Application")]
|
||||
public class SubvaultsController : Controller
|
||||
{
|
||||
private readonly ISubvaultRepository _subvaultRepository;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public SubvaultsController(
|
||||
ISubvaultRepository subvaultRepository,
|
||||
IUserService userService)
|
||||
{
|
||||
_subvaultRepository = subvaultRepository;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<SubvaultResponseModel> Get(string id)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id), userId);
|
||||
if(subvault == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new SubvaultResponseModel(subvault);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<SubvaultResponseModel>> Get()
|
||||
{
|
||||
var subvaults = await _subvaultRepository.GetManyByUserIdAsync(_userService.GetProperUserId(User).Value);
|
||||
var responses = subvaults.Select(s => new SubvaultResponseModel(s));
|
||||
return new ListResponseModel<SubvaultResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpGet("organization/{organizationId}")]
|
||||
public async Task<ListResponseModel<SubvaultResponseModel>> GetByOrganization(string organizationId)
|
||||
{
|
||||
var subvaults = await _subvaultRepository.GetManyByOrganizationIdAdminUserIdAsync(new Guid(organizationId),
|
||||
_userService.GetProperUserId(User).Value);
|
||||
var responses = subvaults.Select(s => new SubvaultResponseModel(s));
|
||||
return new ListResponseModel<SubvaultResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<SubvaultResponseModel> Post([FromBody]SubvaultCreateRequestModel model)
|
||||
{
|
||||
// TODO: permission check
|
||||
var subvault = model.ToSubvault();
|
||||
await _subvaultRepository.CreateAsync(subvault);
|
||||
return new SubvaultResponseModel(subvault);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[HttpPost("{id}")]
|
||||
public async Task<SubvaultResponseModel> Put(string id, [FromBody]SubvaultUpdateRequestModel model)
|
||||
{
|
||||
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id),
|
||||
_userService.GetProperUserId(User).Value);
|
||||
if(subvault == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _subvaultRepository.ReplaceAsync(model.ToSubvault(subvault));
|
||||
return new SubvaultResponseModel(subvault);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
public async Task Delete(string id)
|
||||
{
|
||||
var subvault = await _subvaultRepository.GetByIdAdminUserIdAsync(new Guid(id),
|
||||
_userService.GetProperUserId(User).Value);
|
||||
if(subvault == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _subvaultRepository.DeleteAsync(subvault);
|
||||
}
|
||||
}
|
||||
}
|
35
src/Api/Models/Request/SubvaultRequestModel.cs
Normal file
35
src/Api/Models/Request/SubvaultRequestModel.cs
Normal file
@ -0,0 +1,35 @@
|
||||
using System;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using Bit.Api.Utilities;
|
||||
using Bit.Core.Domains;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class SubvaultCreateRequestModel : SubvaultUpdateRequestModel
|
||||
{
|
||||
public string OrganizationId { get; set; }
|
||||
|
||||
public Subvault ToSubvault()
|
||||
{
|
||||
return ToSubvault(new Subvault
|
||||
{
|
||||
OrganizationId = new Guid(OrganizationId)
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
public class SubvaultUpdateRequestModel
|
||||
{
|
||||
[Required]
|
||||
[EncryptedString]
|
||||
[StringLength(300)]
|
||||
public string Name { get; set; }
|
||||
|
||||
public Subvault ToSubvault(Subvault existingSubvault)
|
||||
{
|
||||
existingSubvault.Name = Name;
|
||||
return existingSubvault;
|
||||
}
|
||||
}
|
||||
}
|
25
src/Api/Models/Response/SubvaultResponseModel.cs
Normal file
25
src/Api/Models/Response/SubvaultResponseModel.cs
Normal file
@ -0,0 +1,25 @@
|
||||
using System;
|
||||
using Bit.Core.Domains;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class SubvaultResponseModel : ResponseModel
|
||||
{
|
||||
public SubvaultResponseModel(Subvault subvault)
|
||||
: base("subvault")
|
||||
{
|
||||
if(subvault == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(subvault));
|
||||
}
|
||||
|
||||
Id = subvault.Id.ToString();
|
||||
OrganizationId = subvault.OrganizationId.ToString();
|
||||
Name = subvault.Name;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string OrganizationId { get; set; }
|
||||
public string Name { get; set; }
|
||||
}
|
||||
}
|
@ -78,6 +78,8 @@ namespace Bit.Api
|
||||
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
||||
services.AddSingleton<ISubvaultRepository, SqlServerRepos.SubvaultRepository>();
|
||||
services.AddSingleton<ISubvaultUserRepository, SqlServerRepos.SubvaultUserRepository>();
|
||||
|
||||
// Context
|
||||
services.AddScoped<CurrentContext>();
|
||||
|
Reference in New Issue
Block a user