2017-03-03 00:07:11 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
using System.Linq;
|
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
using Bit.Core.Models.Business;
|
2017-03-08 21:45:08 -05:00
|
|
|
|
using Bit.Core.Models.Table;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
using Bit.Core.Utilities;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
2017-03-09 23:58:43 -05:00
|
|
|
|
using System.Collections.Generic;
|
2017-03-23 00:17:34 -04:00
|
|
|
|
using Microsoft.AspNetCore.DataProtection;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class OrganizationService : IOrganizationService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
2017-03-09 23:58:43 -05:00
|
|
|
|
private readonly ISubvaultRepository _subvaultRepository;
|
|
|
|
|
|
private readonly ISubvaultUserRepository _subvaultUserRepository;
|
2017-03-04 21:28:41 -05:00
|
|
|
|
private readonly IUserRepository _userRepository;
|
2017-03-23 00:17:34 -04:00
|
|
|
|
private readonly IDataProtector _dataProtector;
|
|
|
|
|
|
private readonly IMailService _mailService;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
|
|
|
|
|
|
public OrganizationService(
|
|
|
|
|
|
IOrganizationRepository organizationRepository,
|
2017-03-04 21:28:41 -05:00
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
2017-03-09 23:58:43 -05:00
|
|
|
|
ISubvaultRepository subvaultRepository,
|
|
|
|
|
|
ISubvaultUserRepository subvaultUserRepository,
|
2017-03-23 00:17:34 -04:00
|
|
|
|
IUserRepository userRepository,
|
|
|
|
|
|
IDataProtectionProvider dataProtectionProvider,
|
|
|
|
|
|
IMailService mailService)
|
2017-03-03 00:07:11 -05:00
|
|
|
|
{
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
2017-03-09 23:58:43 -05:00
|
|
|
|
_subvaultRepository = subvaultRepository;
|
|
|
|
|
|
_subvaultUserRepository = subvaultUserRepository;
|
2017-03-04 21:28:41 -05:00
|
|
|
|
_userRepository = userRepository;
|
2017-03-23 00:17:34 -04:00
|
|
|
|
_dataProtector = dataProtectionProvider.CreateProtector("OrganizationServiceDataProtector");
|
|
|
|
|
|
_mailService = mailService;
|
2017-03-03 00:07:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
public async Task<Tuple<Organization, OrganizationUser>> SignUpAsync(OrganizationSignup signup)
|
2017-03-03 00:07:11 -05:00
|
|
|
|
{
|
2017-03-04 21:28:41 -05:00
|
|
|
|
var plan = StaticStore.Plans.FirstOrDefault(p => p.Type == signup.Plan);
|
2017-03-03 00:07:11 -05:00
|
|
|
|
if(plan == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Plan not found.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var organization = new Organization
|
|
|
|
|
|
{
|
2017-03-04 21:28:41 -05:00
|
|
|
|
Name = signup.Name,
|
|
|
|
|
|
UserId = signup.Owner.Id,
|
2017-03-03 00:07:11 -05:00
|
|
|
|
PlanType = plan.Type,
|
|
|
|
|
|
MaxUsers = plan.MaxUsers,
|
|
|
|
|
|
PlanTrial = plan.Trial.HasValue,
|
|
|
|
|
|
PlanPrice = plan.Trial.HasValue ? 0 : plan.Price,
|
|
|
|
|
|
PlanRenewalPrice = plan.Price,
|
|
|
|
|
|
Plan = plan.ToString(),
|
|
|
|
|
|
CreationDate = DateTime.UtcNow,
|
|
|
|
|
|
RevisionDate = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
if(plan.Trial.HasValue)
|
|
|
|
|
|
{
|
|
|
|
|
|
organization.PlanRenewalDate = DateTime.UtcNow.Add(plan.Trial.Value);
|
|
|
|
|
|
}
|
|
|
|
|
|
else if(plan.Cycle != null)
|
|
|
|
|
|
{
|
2017-03-08 22:33:31 -05:00
|
|
|
|
organization.PlanRenewalDate = DateTime.UtcNow.Add(plan.Cycle(DateTime.UtcNow));
|
2017-03-03 00:07:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationRepository.CreateAsync(organization);
|
|
|
|
|
|
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var orgUser = new OrganizationUser
|
|
|
|
|
|
{
|
|
|
|
|
|
OrganizationId = organization.Id,
|
2017-03-04 21:28:41 -05:00
|
|
|
|
UserId = signup.Owner.Id,
|
|
|
|
|
|
Email = signup.Owner.Email,
|
|
|
|
|
|
Key = signup.OwnerKey,
|
2017-03-03 00:07:11 -05:00
|
|
|
|
Type = Enums.OrganizationUserType.Owner,
|
|
|
|
|
|
Status = Enums.OrganizationUserStatusType.Confirmed,
|
|
|
|
|
|
CreationDate = DateTime.UtcNow,
|
|
|
|
|
|
RevisionDate = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationUserRepository.CreateAsync(orgUser);
|
|
|
|
|
|
|
|
|
|
|
|
return new Tuple<Organization, OrganizationUser>(organization, orgUser);
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
await _organizationRepository.DeleteAsync(organization);
|
|
|
|
|
|
throw;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-03-04 21:28:41 -05:00
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
public async Task<OrganizationUser> InviteUserAsync(Guid organizationId, Guid invitingUserId, string email,
|
|
|
|
|
|
Enums.OrganizationUserType type, IEnumerable<SubvaultUser> subvaults)
|
2017-03-04 21:28:41 -05:00
|
|
|
|
{
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(!(await OrganizationUserHasAdminRightsAsync(organizationId, invitingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cannot invite users.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: make sure user is not already invited
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: validate subvaults?
|
|
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
var orgUser = new OrganizationUser
|
|
|
|
|
|
{
|
|
|
|
|
|
OrganizationId = organizationId,
|
|
|
|
|
|
UserId = null,
|
|
|
|
|
|
Email = email,
|
|
|
|
|
|
Key = null,
|
2017-03-13 23:31:17 -04:00
|
|
|
|
Type = type,
|
2017-03-04 21:28:41 -05:00
|
|
|
|
Status = Enums.OrganizationUserStatusType.Invited,
|
|
|
|
|
|
CreationDate = DateTime.UtcNow,
|
|
|
|
|
|
RevisionDate = DateTime.UtcNow
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationUserRepository.CreateAsync(orgUser);
|
2017-03-11 22:42:27 -05:00
|
|
|
|
await SaveUserSubvaultsAsync(orgUser, subvaults, true);
|
2017-03-23 11:51:37 -04:00
|
|
|
|
await SendInviteAsync(orgUser);
|
2017-03-04 21:28:41 -05:00
|
|
|
|
|
|
|
|
|
|
return orgUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
public async Task ResendInviteAsync(Guid organizationId, Guid invitingUserId, Guid organizationUserId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!(await OrganizationUserHasAdminRightsAsync(organizationId, invitingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cannot invite users.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
|
|
|
|
|
if(orgUser == null || orgUser.OrganizationId != organizationId ||
|
2017-03-23 00:39:55 -04:00
|
|
|
|
orgUser.Status != Enums.OrganizationUserStatusType.Invited)
|
2017-03-23 00:17:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("User invalid.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 11:51:37 -04:00
|
|
|
|
await SendInviteAsync(orgUser);
|
2017-03-23 00:17:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 11:51:37 -04:00
|
|
|
|
private async Task SendInviteAsync(OrganizationUser orgUser)
|
2017-03-23 00:17:34 -04:00
|
|
|
|
{
|
2017-03-23 11:51:37 -04:00
|
|
|
|
var nowMillis = CoreHelpers.ToEpocMilliseconds(DateTime.UtcNow);
|
2017-03-23 00:17:34 -04:00
|
|
|
|
var token = _dataProtector.Protect(
|
2017-03-23 11:51:37 -04:00
|
|
|
|
$"OrganizationUserInvite {orgUser.Id} {orgUser.Email} {nowMillis}");
|
|
|
|
|
|
await _mailService.SendOrganizationInviteEmailAsync("Organization Name", orgUser.Email, token);
|
2017-03-23 00:17:34 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
public async Task<OrganizationUser> AcceptUserAsync(Guid organizationUserId, User user, string token)
|
|
|
|
|
|
{
|
|
|
|
|
|
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(orgUser == null || orgUser.Email != user.Email)
|
2017-03-04 21:28:41 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("User invalid.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(orgUser.Status != Enums.OrganizationUserStatusType.Invited)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Already accepted.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var tokenValidationFailed = true;
|
|
|
|
|
|
try
|
|
|
|
|
|
{
|
|
|
|
|
|
var unprotectedData = _dataProtector.Unprotect(token);
|
|
|
|
|
|
var dataParts = unprotectedData.Split(' ');
|
2017-03-23 11:51:37 -04:00
|
|
|
|
if(dataParts.Length == 4 && dataParts[0] == "OrganizationUserInvite" &&
|
|
|
|
|
|
new Guid(dataParts[1]) == orgUser.Id && dataParts[2] == user.Email)
|
2017-03-23 00:17:34 -04:00
|
|
|
|
{
|
|
|
|
|
|
var creationTime = CoreHelpers.FromEpocMilliseconds(Convert.ToInt64(dataParts[3]));
|
|
|
|
|
|
tokenValidationFailed = creationTime.AddDays(5) < DateTime.UtcNow;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
catch
|
|
|
|
|
|
{
|
|
|
|
|
|
tokenValidationFailed = true;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if(tokenValidationFailed)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Invalid token.");
|
|
|
|
|
|
}
|
2017-03-04 21:28:41 -05:00
|
|
|
|
|
|
|
|
|
|
orgUser.Status = Enums.OrganizationUserStatusType.Accepted;
|
|
|
|
|
|
orgUser.UserId = orgUser.Id;
|
|
|
|
|
|
orgUser.Email = null;
|
|
|
|
|
|
await _organizationUserRepository.ReplaceAsync(orgUser);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: send email
|
|
|
|
|
|
|
|
|
|
|
|
return orgUser;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
public async Task<OrganizationUser> ConfirmUserAsync(Guid organizationId, Guid organizationUserId, string key,
|
|
|
|
|
|
Guid confirmingUserId)
|
2017-03-04 21:28:41 -05:00
|
|
|
|
{
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(!(await OrganizationUserHasAdminRightsAsync(organizationId, confirmingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cannot confirm users.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-04 21:28:41 -05:00
|
|
|
|
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(orgUser == null || orgUser.Status != Enums.OrganizationUserStatusType.Accepted ||
|
|
|
|
|
|
orgUser.OrganizationId != organizationId)
|
2017-03-04 21:28:41 -05:00
|
|
|
|
{
|
2017-03-23 00:17:34 -04:00
|
|
|
|
throw new BadRequestException("User not valid.");
|
2017-03-04 21:28:41 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
orgUser.Status = Enums.OrganizationUserStatusType.Confirmed;
|
|
|
|
|
|
orgUser.Key = key;
|
|
|
|
|
|
orgUser.Email = null;
|
|
|
|
|
|
await _organizationUserRepository.ReplaceAsync(orgUser);
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: send email
|
|
|
|
|
|
|
|
|
|
|
|
return orgUser;
|
|
|
|
|
|
}
|
2017-03-09 23:58:43 -05:00
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
public async Task SaveUserAsync(OrganizationUser user, Guid savingUserId, IEnumerable<SubvaultUser> subvaults)
|
2017-03-09 23:58:43 -05:00
|
|
|
|
{
|
|
|
|
|
|
if(user.Id.Equals(default(Guid)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Invite the user first.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
if(!(await OrganizationUserHasAdminRightsAsync(user.OrganizationId, savingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cannot update users.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: validate subvaults?
|
|
|
|
|
|
|
2017-03-09 23:58:43 -05:00
|
|
|
|
await _organizationUserRepository.ReplaceAsync(user);
|
2017-03-11 22:42:27 -05:00
|
|
|
|
await SaveUserSubvaultsAsync(user, subvaults, false);
|
|
|
|
|
|
}
|
2017-03-09 23:58:43 -05:00
|
|
|
|
|
2017-03-23 00:17:34 -04:00
|
|
|
|
public async Task DeleteUserAsync(Guid organizationId, Guid organizationUserId, Guid deletingUserId)
|
|
|
|
|
|
{
|
|
|
|
|
|
if(!(await OrganizationUserHasAdminRightsAsync(organizationId, deletingUserId)))
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Cannot delete users.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
|
|
|
|
|
|
if(orgUser == null || orgUser.OrganizationId != organizationId)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("User not valid.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
await _organizationUserRepository.DeleteAsync(orgUser);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
private async Task<bool> OrganizationUserHasAdminRightsAsync(Guid organizationId, Guid userId)
|
|
|
|
|
|
{
|
|
|
|
|
|
var orgUser = await _organizationUserRepository.GetByOrganizationAsync(organizationId, userId);
|
|
|
|
|
|
if(orgUser == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return orgUser.Status == Enums.OrganizationUserStatusType.Confirmed &&
|
|
|
|
|
|
orgUser.Type != Enums.OrganizationUserType.User;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 22:42:27 -05:00
|
|
|
|
private async Task SaveUserSubvaultsAsync(OrganizationUser user, IEnumerable<SubvaultUser> subvaults, bool newUser)
|
|
|
|
|
|
{
|
2017-03-13 22:54:24 -04:00
|
|
|
|
if(subvaults == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
subvaults = new List<SubvaultUser>();
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-09 23:58:43 -05:00
|
|
|
|
var orgSubvaults = await _subvaultRepository.GetManyByOrganizationIdAsync(user.OrganizationId);
|
2017-03-11 22:42:27 -05:00
|
|
|
|
var currentUserSubvaults = newUser ? null : await _subvaultUserRepository.GetManyByOrganizationUserIdAsync(user.Id);
|
2017-03-09 23:58:43 -05:00
|
|
|
|
|
|
|
|
|
|
// Let's make sure all these belong to this user and organization.
|
2017-03-11 22:42:27 -05:00
|
|
|
|
var filteredSubvaults = subvaults.Where(s => orgSubvaults.Any(os => os.Id == s.SubvaultId));
|
2017-03-09 23:58:43 -05:00
|
|
|
|
foreach(var subvault in filteredSubvaults)
|
|
|
|
|
|
{
|
2017-03-13 22:54:24 -04:00
|
|
|
|
var existingSubvaultUser = currentUserSubvaults?.FirstOrDefault(cs => cs.SubvaultId == subvault.SubvaultId);
|
|
|
|
|
|
if(existingSubvaultUser != null)
|
|
|
|
|
|
{
|
|
|
|
|
|
subvault.Id = existingSubvaultUser.Id;
|
|
|
|
|
|
subvault.CreationDate = existingSubvaultUser.CreationDate;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 22:42:27 -05:00
|
|
|
|
subvault.OrganizationUserId = user.Id;
|
2017-03-09 23:58:43 -05:00
|
|
|
|
await _subvaultUserRepository.UpsertAsync(subvault);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2017-03-11 22:42:27 -05:00
|
|
|
|
if(!newUser)
|
2017-03-09 23:58:43 -05:00
|
|
|
|
{
|
2017-03-13 22:54:24 -04:00
|
|
|
|
var subvaultsToDelete = currentUserSubvaults.Where(cs =>
|
|
|
|
|
|
!filteredSubvaults.Any(s => s.SubvaultId == cs.SubvaultId));
|
2017-03-11 22:42:27 -05:00
|
|
|
|
foreach(var subvault in subvaultsToDelete)
|
|
|
|
|
|
{
|
|
|
|
|
|
await _subvaultUserRepository.DeleteAsync(subvault);
|
|
|
|
|
|
}
|
2017-03-09 23:58:43 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2017-03-03 00:07:11 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|