mirror of
https://github.com/bitwarden/server.git
synced 2025-07-03 00:52:49 -05:00
org user management apis
This commit is contained in:
@ -8,7 +8,7 @@ namespace Bit.Core.Domains
|
||||
{
|
||||
public Guid Id { get; set; }
|
||||
public Guid OrganizationId { get; set; }
|
||||
public Guid UserId { get; set; }
|
||||
public Guid? UserId { get; set; }
|
||||
public string Email { get; set; }
|
||||
public string Key { get; set; }
|
||||
public OrganizationUserStatusType Status { get; set; }
|
||||
|
@ -3,8 +3,7 @@
|
||||
public enum OrganizationUserStatusType : byte
|
||||
{
|
||||
Invited = 0,
|
||||
Pending = 1,
|
||||
Accepted = 2,
|
||||
Confirmed = 3
|
||||
Accepted = 1,
|
||||
Confirmed = 2
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,6 @@
|
||||
{
|
||||
Owner = 0,
|
||||
Admin = 1,
|
||||
Regular = 2
|
||||
User = 2
|
||||
}
|
||||
}
|
||||
|
14
src/Core/Models/Data/OrganizationUserDetails.cs
Normal file
14
src/Core/Models/Data/OrganizationUserDetails.cs
Normal file
@ -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; }
|
||||
}
|
||||
}
|
@ -2,11 +2,14 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Bit.Core.Domains;
|
||||
using Bit.Core.Models.Data;
|
||||
|
||||
namespace Bit.Core.Repositories
|
||||
{
|
||||
public interface IOrganizationUserRepository : IRepository<OrganizationUser, Guid>
|
||||
{
|
||||
Task<OrganizationUser> GetByOrganizationAsync(Guid organizationId, Guid userId);
|
||||
Task<OrganizationUserDetails> GetDetailsByIdAsync(Guid id);
|
||||
Task<ICollection<OrganizationUserDetails>> GetManyDetailsByOrganizationsAsync(Guid organizationId);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ namespace Bit.Core.Repositories
|
||||
public interface IUserRepository : IRepository<User, Guid>
|
||||
{
|
||||
Task<User> GetByEmailAsync(string email);
|
||||
Task<string> GetPublicKeyAsync(Guid id);
|
||||
Task<DateTime> GetAccountRevisionDateAsync(Guid id);
|
||||
}
|
||||
}
|
||||
|
@ -5,6 +5,8 @@ using System.Data.SqlClient;
|
||||
using System.Threading.Tasks;
|
||||
using Dapper;
|
||||
using System.Linq;
|
||||
using Bit.Core.Models.Data;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Bit.Core.Repositories.SqlServer
|
||||
{
|
||||
@ -30,5 +32,31 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationUserDetails> GetDetailsByIdAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<OrganizationUserDetails>(
|
||||
"[dbo].[OrganizationUserDetails_ReadById]",
|
||||
new { Id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<ICollection<OrganizationUserDetails>> GetManyDetailsByOrganizationsAsync(Guid organizationId)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<OrganizationUserDetails>(
|
||||
"[dbo].[OrganizationUserDetails_ReadByOrganizationId]",
|
||||
new { OrganizationId = organizationId },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.ToList();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -36,6 +36,19 @@ namespace Bit.Core.Repositories.SqlServer
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<string> GetPublicKeyAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
{
|
||||
var results = await connection.QueryAsync<string>(
|
||||
$"[{Schema}].[{Table}_ReadPublicKeyById]",
|
||||
new { Id = id },
|
||||
commandType: CommandType.StoredProcedure);
|
||||
|
||||
return results.SingleOrDefault();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<DateTime> GetAccountRevisionDateAsync(Guid id)
|
||||
{
|
||||
using(var connection = new SqlConnection(ConnectionString))
|
||||
|
@ -8,5 +8,8 @@ namespace Bit.Core.Services
|
||||
public interface IOrganizationService
|
||||
{
|
||||
Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup);
|
||||
Task<OrganizationUser> InviteUserAsync(Guid organizationId, string email);
|
||||
Task<OrganizationUser> AcceptUserAsync(Guid organizationUserId, User user, string token);
|
||||
Task<OrganizationUser> ConfirmUserAsync(Guid organizationUserId, string key);
|
||||
}
|
||||
}
|
||||
|
@ -13,18 +13,21 @@ namespace Bit.Core.Services
|
||||
{
|
||||
private readonly IOrganizationRepository _organizationRepository;
|
||||
private readonly IOrganizationUserRepository _organizationUserRepository;
|
||||
private readonly IUserRepository _userRepository;
|
||||
|
||||
public OrganizationService(
|
||||
IOrganizationRepository organizationRepository,
|
||||
IOrganizationUserRepository organizationUserRepository)
|
||||
IOrganizationUserRepository organizationUserRepository,
|
||||
IUserRepository userRepository)
|
||||
{
|
||||
_organizationRepository = organizationRepository;
|
||||
_organizationUserRepository = organizationUserRepository;
|
||||
_userRepository = userRepository;
|
||||
}
|
||||
|
||||
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup organizationSignup)
|
||||
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
||||
{
|
||||
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == organizationSignup.Plan);
|
||||
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan);
|
||||
if(plan == null)
|
||||
{
|
||||
throw new BadRequestException("Plan not found.");
|
||||
@ -32,8 +35,8 @@ namespace Bit.Core.Services
|
||||
|
||||
var organization = new Organization
|
||||
{
|
||||
Name = organizationSignup.Name,
|
||||
UserId = organizationSignup.Owner.Id,
|
||||
Name = signup.Name,
|
||||
UserId = signup.Owner.Id,
|
||||
PlanType = plan.Type,
|
||||
MaxUsers = plan.MaxUsers,
|
||||
PlanTrial = plan.Trial.HasValue,
|
||||
@ -60,9 +63,9 @@ namespace Bit.Core.Services
|
||||
var orgUser = new OrganizationUser
|
||||
{
|
||||
OrganizationId = organization.Id,
|
||||
UserId = organizationSignup.Owner.Id,
|
||||
Email = organizationSignup.Owner.Email,
|
||||
Key = organizationSignup.OwnerKey,
|
||||
UserId = signup.Owner.Id,
|
||||
Email = signup.Owner.Email,
|
||||
Key = signup.OwnerKey,
|
||||
Type = Enums.OrganizationUserType.Owner,
|
||||
Status = Enums.OrganizationUserStatusType.Confirmed,
|
||||
CreationDate = DateTime.UtcNow,
|
||||
@ -79,5 +82,64 @@ namespace Bit.Core.Services
|
||||
throw;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<OrganizationUser> InviteUserAsync(Guid organizationId, string email)
|
||||
{
|
||||
var orgUser = new OrganizationUser
|
||||
{
|
||||
OrganizationId = organizationId,
|
||||
UserId = null,
|
||||
Email = email,
|
||||
Key = null,
|
||||
Type = Enums.OrganizationUserType.User,
|
||||
Status = Enums.OrganizationUserStatusType.Invited,
|
||||
CreationDate = DateTime.UtcNow,
|
||||
RevisionDate = DateTime.UtcNow
|
||||
};
|
||||
|
||||
await _organizationUserRepository.CreateAsync(orgUser);
|
||||
|
||||
// TODO: send email
|
||||
|
||||
return orgUser;
|
||||
}
|
||||
|
||||
public async Task<OrganizationUser> AcceptUserAsync(Guid organizationUserId, User user, string token)
|
||||
{
|
||||
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
||||
if(orgUser.Email != user.Email)
|
||||
{
|
||||
throw new BadRequestException("User invalid.");
|
||||
}
|
||||
|
||||
// TODO: validate token
|
||||
|
||||
orgUser.Status = Enums.OrganizationUserStatusType.Accepted;
|
||||
orgUser.UserId = orgUser.Id;
|
||||
orgUser.Email = null;
|
||||
await _organizationUserRepository.ReplaceAsync(orgUser);
|
||||
|
||||
// TODO: send email
|
||||
|
||||
return orgUser;
|
||||
}
|
||||
|
||||
public async Task<OrganizationUser> ConfirmUserAsync(Guid organizationUserId, string key)
|
||||
{
|
||||
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
||||
if(orgUser.Status != Enums.OrganizationUserStatusType.Accepted)
|
||||
{
|
||||
throw new BadRequestException("User not accepted.");
|
||||
}
|
||||
|
||||
orgUser.Status = Enums.OrganizationUserStatusType.Confirmed;
|
||||
orgUser.Key = key;
|
||||
orgUser.Email = null;
|
||||
await _organizationUserRepository.ReplaceAsync(orgUser);
|
||||
|
||||
// TODO: send email
|
||||
|
||||
return orgUser;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user