mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 08:02:49 -05:00
Org API controller and supporting data access
This commit is contained in:
@ -6,8 +6,6 @@ using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Api.Models;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Core.Domains;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Bit.Core.Services;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
|
89
src/Api/Controllers/OrganizationsController.cs
Normal file
89
src/Api/Controllers/OrganizationsController.cs
Normal file
@ -0,0 +1,89 @@
|
||||
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("organizations")]
|
||||
[Authorize("Application")]
|
||||
public class OrganizationsController : Controller
|
||||
{
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public OrganizationsController(
|
||||
IOrganizationRepository organizationRepository,
|
||||
IUserService userService)
|
||||
{
|
||||
_organizationRepository = organizationRepository;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<OrganizationResponseModel> Get(string id)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var organization = await _organizationRepository.GetByIdAsync(new Guid(id), userId);
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new OrganizationResponseModel(organization);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<OrganizationResponseModel>> Get()
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var organizations = await _organizationRepository.GetManyByUserIdAsync(userId);
|
||||
var responses = organizations.Select(o => new OrganizationResponseModel(o));
|
||||
return new ListResponseModel<OrganizationResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("")]
|
||||
public async Task<OrganizationResponseModel> Post([FromBody]OrganizationCreateRequestModel model)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var organization = model.ToOrganization(_userService.GetProperUserId(User).Value);
|
||||
await _organizationRepository.ReplaceAsync(organization);
|
||||
return new OrganizationResponseModel(organization);
|
||||
}
|
||||
|
||||
[HttpPut("{id}")]
|
||||
[HttpPost("{id}")]
|
||||
public async Task<OrganizationResponseModel> Put(string id, [FromBody]OrganizationUpdateRequestModel model)
|
||||
{
|
||||
var userId = _userService.GetProperUserId(User).Value;
|
||||
var organization = await _organizationRepository.GetByIdAsync(new Guid(id), userId);
|
||||
// TODO: Permission checks
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _organizationRepository.ReplaceAsync(model.ToOrganization(organization));
|
||||
return new OrganizationResponseModel(organization);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
public async Task Delete(string id)
|
||||
{
|
||||
var organization = await _organizationRepository.GetByIdAsync(new Guid(id),
|
||||
_userService.GetProperUserId(User).Value);
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _organizationRepository.DeleteAsync(organization);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,25 @@
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Enums;
|
||||
using System;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class OrganizationCreateRequestModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
public PlanType Plan { get; set; }
|
||||
// TODO: Billing info for paid plans.
|
||||
|
||||
public virtual Organization ToOrganization(Guid userId)
|
||||
{
|
||||
var organization = new Organization
|
||||
{
|
||||
UserId = userId,
|
||||
Name = Name,
|
||||
Plan = Plan
|
||||
};
|
||||
|
||||
return organization;
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,15 @@
|
||||
using Bit.Core.Domains;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class OrganizationUpdateRequestModel
|
||||
{
|
||||
public string Name { get; set; }
|
||||
|
||||
public virtual Organization ToOrganization(Organization existingOrganization)
|
||||
{
|
||||
existingOrganization.Name = Name;
|
||||
return existingOrganization;
|
||||
}
|
||||
}
|
||||
}
|
28
src/Api/Models/Response/OrganizationResponseModel.cs
Normal file
28
src/Api/Models/Response/OrganizationResponseModel.cs
Normal file
@ -0,0 +1,28 @@
|
||||
using System;
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class OrganizationResponseModel : ResponseModel
|
||||
{
|
||||
public OrganizationResponseModel(Organization organization)
|
||||
: base("organization")
|
||||
{
|
||||
if(organization == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(organization));
|
||||
}
|
||||
|
||||
Id = organization.Id.ToString();
|
||||
Name = organization.Name;
|
||||
Plan = organization.Plan;
|
||||
MaxUsers = organization.MaxUsers;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public PlanType Plan { get; set; }
|
||||
public short MaxUsers { get; set; }
|
||||
}
|
||||
}
|
@ -76,6 +76,8 @@ namespace Bit.Api
|
||||
services.AddSingleton<ICipherRepository, SqlServerRepos.CipherRepository>();
|
||||
services.AddSingleton<IDeviceRepository, SqlServerRepos.DeviceRepository>();
|
||||
services.AddSingleton<IGrantRepository, SqlServerRepos.GrantRepository>();
|
||||
services.AddSingleton<IOrganizationRepository, SqlServerRepos.OrganizationRepository>();
|
||||
services.AddSingleton<IOrganizationUserRepository, SqlServerRepos.OrganizationUserRepository>();
|
||||
|
||||
// Context
|
||||
services.AddScoped<CurrentContext>();
|
||||
|
Reference in New Issue
Block a user