20 changed files with 389 additions and 13 deletions
@ -0,0 +1,90 @@
@@ -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); |
||||
} |
||||
} |
||||
} |
||||
@ -0,0 +1,36 @@
@@ -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 @@
@@ -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; } |
||||
} |
||||
} |
||||
@ -0,0 +1,32 @@
@@ -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; } |
||||
} |
||||
} |
||||
@ -0,0 +1,19 @@
@@ -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; } |
||||
} |
||||
} |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
using System; |
||||
|
||||
namespace Bit.Core.Models.Data |
||||
{ |
||||
public class OrganizationUserDetails |
||||
{ |
||||
public Guid Id { get; set; } |
||||
public Guid? UserId { get; set; } |
||||
public string Name { get; set; } |
||||
public string Email { get; set; } |
||||
public Enums.OrganizationUserStatusType Status { get; set; } |
||||
public Enums.OrganizationUserType Type { get; set; } |
||||
} |
||||
} |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationUserDetails_ReadById] |
||||
@Id UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[OrganizationUserDetailsView] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[OrganizationUserDetails_ReadByOrganizationId] |
||||
@OrganizationId UNIQUEIDENTIFIER |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
* |
||||
FROM |
||||
[dbo].[OrganizationUserDetailsView] |
||||
WHERE |
||||
[OrganizationId] = @OrganizationId |
||||
END |
||||
@ -0,0 +1,13 @@
@@ -0,0 +1,13 @@
|
||||
CREATE PROCEDURE [dbo].[User_ReadPublicKeyById] |
||||
@Id NVARCHAR(50) |
||||
AS |
||||
BEGIN |
||||
SET NOCOUNT ON |
||||
|
||||
SELECT |
||||
[PublicKey] |
||||
FROM |
||||
[dbo].[User] |
||||
WHERE |
||||
[Id] = @Id |
||||
END |
||||
@ -0,0 +1,14 @@
@@ -0,0 +1,14 @@
|
||||
CREATE VIEW [dbo].[OrganizationUserDetailsView] |
||||
AS |
||||
SELECT |
||||
OU.[Id], |
||||
OU.[UserId], |
||||
OU.[OrganizationId], |
||||
U.[Name], |
||||
ISNULL(U.[Email], OU.[Email]) Email, |
||||
OU.[Status], |
||||
OU.[Type] |
||||
FROM |
||||
[dbo].[OrganizationUser] OU |
||||
LEFT JOIN |
||||
[dbo].[User] U ON U.Id = OU.UserId |
||||
Loading…
Reference in new issue