mirror of
https://github.com/bitwarden/server.git
synced 2025-07-01 16:12:49 -05:00
org user management apis
This commit is contained in:
90
src/Api/Controllers/OrganizationUsersController.cs
Normal file
90
src/Api/Controllers/OrganizationUsersController.cs
Normal file
@ -0,0 +1,90 @@
|
||||
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/{orgId}/users")]
|
||||
[Authorize("Application")]
|
||||
public class OrganizationUsersController : Controller
|
||||
{
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IOrganizationService _organizationService;
|
||||
private readonly IUserService _userService;
|
||||
|
||||
public OrganizationUsersController(
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IOrganizationService organizationService,
|
||||
IUserService userService)
|
||||
{
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_organizationService = organizationService;
|
||||
_userService = userService;
|
||||
}
|
||||
|
||||
[HttpGet("{id}")]
|
||||
public async Task<OrganizationUserResponseModel> Get(string orgId, string id)
|
||||
{
|
||||
var organizationUser = await _organizationUserRepository.GetDetailsByIdAsync(new Guid(id));
|
||||
if(organizationUser == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new OrganizationUserResponseModel(organizationUser);
|
||||
}
|
||||
|
||||
[HttpGet("")]
|
||||
public async Task<ListResponseModel<OrganizationUserResponseModel>> Get(string orgId)
|
||||
{
|
||||
var organizationUsers = await _organizationUserRepository.GetManyDetailsByOrganizationsAsync(new Guid(orgId));
|
||||
var responses = organizationUsers.Select(o => new OrganizationUserResponseModel(o));
|
||||
return new ListResponseModel<OrganizationUserResponseModel>(responses);
|
||||
}
|
||||
|
||||
[HttpPost("invite")]
|
||||
public async Task Invite(string orgId, [FromBody]OrganizationUserInviteRequestModel model)
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
var result = await _organizationService.InviteUserAsync(new Guid(orgId), model.Email);
|
||||
}
|
||||
|
||||
[HttpPut("accept")]
|
||||
[HttpPost("{id}/accept")]
|
||||
public async Task Accept(string orgId, string id, [FromBody]OrganizationUserAcceptRequestModel model)
|
||||
{
|
||||
var user = await _userService.GetUserByPrincipalAsync(User);
|
||||
var result = await _organizationService.AcceptUserAsync(new Guid(id), user, model.Token);
|
||||
}
|
||||
|
||||
[HttpPost("confirm")]
|
||||
[HttpPost("{id}/confirm")]
|
||||
public async Task Confirm(string orgId, string id, [FromBody]OrganizationUserConfirmRequestModel model)
|
||||
{
|
||||
var result = await _organizationService.ConfirmUserAsync(new Guid(id), model.Key);
|
||||
}
|
||||
|
||||
[HttpDelete("{id}")]
|
||||
[HttpPost("{id}/delete")]
|
||||
public async Task Delete(string orgId, string id)
|
||||
{
|
||||
var organization = await _organizationRepository.GetByIdAsync(new Guid(id),
|
||||
_userService.GetProperUserId(User).Value);
|
||||
if(organization == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
await _organizationRepository.DeleteAsync(organization);
|
||||
}
|
||||
}
|
||||
}
|
36
src/Api/Controllers/UsersController.cs
Normal file
36
src/Api/Controllers/UsersController.cs
Normal file
@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Bit.Core.Repositories;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Bit.Core.Exceptions;
|
||||
using Bit.Api.Models;
|
||||
|
||||
namespace Bit.Api.Controllers
|
||||
{
|
||||
[Route("users")]
|
||||
[Authorize("Application")]
|
||||
public class UsersController : Controller
|
||||
{
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public UsersController(
|
||||
IUserRepository userRepository)
|
||||
{
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
[HttpGet("{id}/public-key")]
|
||||
public async Task<UserKeyResponseModel> Get(string id)
|
||||
{
|
||||
var guidId = new Guid(id);
|
||||
var key = await _userRepository.GetPublicKeyAsync(guidId);
|
||||
if(key == null)
|
||||
{
|
||||
throw new NotFoundException();
|
||||
}
|
||||
|
||||
return new UserKeyResponseModel(guidId, key);
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,19 @@
|
||||
using Bit.Core.Domains;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class OrganizationUserInviteRequestModel
|
||||
{
|
||||
public string Email { get; set; }
|
||||
}
|
||||
|
||||
public class OrganizationUserAcceptRequestModel
|
||||
{
|
||||
public string Token { get; set; }
|
||||
}
|
||||
|
||||
public class OrganizationUserConfirmRequestModel
|
||||
{
|
||||
public string Key { get; set; }
|
||||
}
|
||||
}
|
32
src/Api/Models/Response/OrganizationUserResponseModel.cs
Normal file
32
src/Api/Models/Response/OrganizationUserResponseModel.cs
Normal file
@ -0,0 +1,32 @@
|
||||
using System;
|
||||
using Bit.Core.Enums;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class OrganizationUserResponseModel : ResponseModel
|
||||
{
|
||||
public OrganizationUserResponseModel(OrganizationUserDetails organizationUser, string obj = "organizationUser")
|
||||
: base(obj)
|
||||
{
|
||||
if(organizationUser == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(organizationUser));
|
||||
}
|
||||
|
||||
Id = organizationUser.Id.ToString();
|
||||
UserId = organizationUser.UserId?.ToString();
|
||||
Name = organizationUser.Name;
|
||||
Email = organizationUser.Email;
|
||||
Type = organizationUser.Type;
|
||||
Status = organizationUser.Status;
|
||||
}
|
||||
|
||||
public string Id { get; set; }
|
||||
public string UserId { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Email { get; set; }
|
||||
public OrganizationUserType Type { get; set; }
|
||||
public OrganizationUserStatusType Status { get; set; }
|
||||
}
|
||||
}
|
19
src/Api/Models/Response/UserKeyResponseModel.cs
Normal file
19
src/Api/Models/Response/UserKeyResponseModel.cs
Normal file
@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Enums;
|
||||
|
||||
namespace Bit.Api.Models
|
||||
{
|
||||
public class UserKeyResponseModel : ResponseModel
|
||||
{
|
||||
public UserKeyResponseModel(Guid id, string key)
|
||||
: base("userKey")
|
||||
{
|
||||
UserId = id.ToString();
|
||||
PublicKey = key;
|
||||
}
|
||||
|
||||
public string UserId { get; set; }
|
||||
public string PublicKey { get; set; }
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user