2020-01-15 09:19:28 -05:00
|
|
|
|
using System;
|
2020-02-19 14:56:16 -05:00
|
|
|
|
using System.Linq;
|
2020-01-15 09:19:28 -05:00
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
using Bit.Core.Exceptions;
|
|
|
|
|
|
using Bit.Core.Models.Table;
|
|
|
|
|
|
using Bit.Core.Repositories;
|
|
|
|
|
|
|
|
|
|
|
|
namespace Bit.Core.Services
|
|
|
|
|
|
{
|
|
|
|
|
|
public class PolicyService : IPolicyService
|
|
|
|
|
|
{
|
|
|
|
|
|
private readonly IEventService _eventService;
|
|
|
|
|
|
private readonly IOrganizationRepository _organizationRepository;
|
|
|
|
|
|
private readonly IOrganizationUserRepository _organizationUserRepository;
|
|
|
|
|
|
private readonly IPolicyRepository _policyRepository;
|
2020-02-27 09:30:04 -05:00
|
|
|
|
private readonly IMailService _mailService;
|
2020-01-15 09:19:28 -05:00
|
|
|
|
|
|
|
|
|
|
public PolicyService(
|
|
|
|
|
|
IEventService eventService,
|
|
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
2020-02-27 09:30:04 -05:00
|
|
|
|
IPolicyRepository policyRepository,
|
|
|
|
|
|
IMailService mailService)
|
2020-01-15 09:19:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
_eventService = eventService;
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_policyRepository = policyRepository;
|
2020-02-27 09:30:04 -05:00
|
|
|
|
_mailService = mailService;
|
2020-01-15 09:19:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
|
2020-02-19 14:56:16 -05:00
|
|
|
|
public async Task SaveAsync(Policy policy, IUserService userService, IOrganizationService organizationService,
|
|
|
|
|
|
Guid? savingUserId)
|
2020-01-15 09:19:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(policy.OrganizationId);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (org == null)
|
2020-01-15 09:19:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Organization not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!org.UsePolicies)
|
2020-01-15 09:19:28 -05:00
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("This organization cannot use policies.");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-20 09:02:41 -05:00
|
|
|
|
var now = DateTime.UtcNow;
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (policy.Id == default(Guid))
|
2020-01-15 09:19:28 -05:00
|
|
|
|
{
|
2020-01-20 09:02:41 -05:00
|
|
|
|
policy.CreationDate = now;
|
2020-01-15 09:19:28 -05:00
|
|
|
|
}
|
2020-03-27 14:36:37 -04:00
|
|
|
|
else if (policy.Enabled)
|
2020-02-19 14:56:16 -05:00
|
|
|
|
{
|
|
|
|
|
|
var currentPolicy = await _policyRepository.GetByIdAsync(policy.Id);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (!currentPolicy?.Enabled ?? true)
|
2020-02-19 14:56:16 -05:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (currentPolicy.Type == Enums.PolicyType.TwoFactorAuthentication)
|
2020-02-19 14:56:16 -05:00
|
|
|
|
{
|
2020-02-27 09:30:04 -05:00
|
|
|
|
Organization organization = null;
|
2020-02-19 14:56:16 -05:00
|
|
|
|
var orgUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(
|
|
|
|
|
|
policy.OrganizationId);
|
2020-03-27 14:36:37 -04:00
|
|
|
|
foreach (var orgUser in orgUsers.Where(ou =>
|
2020-02-19 14:56:16 -05:00
|
|
|
|
ou.Status != Enums.OrganizationUserStatusType.Invited &&
|
|
|
|
|
|
ou.Type != Enums.OrganizationUserType.Owner))
|
|
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (orgUser.UserId != savingUserId && !await userService.TwoFactorIsEnabledAsync(orgUser))
|
2020-02-19 14:56:16 -05:00
|
|
|
|
{
|
2020-03-27 14:36:37 -04:00
|
|
|
|
if (organization == null)
|
2020-02-27 09:30:04 -05:00
|
|
|
|
{
|
|
|
|
|
|
organization = await _organizationRepository.GetByIdAsync(policy.OrganizationId);
|
|
|
|
|
|
}
|
2020-02-19 14:56:16 -05:00
|
|
|
|
await organizationService.DeleteUserAsync(policy.OrganizationId, orgUser.Id,
|
|
|
|
|
|
savingUserId);
|
2020-02-27 09:30:04 -05:00
|
|
|
|
await _mailService.SendOrganizationUserRemovedForPolicyTwoStepEmailAsync(
|
|
|
|
|
|
organization.Name, orgUser.Email);
|
2020-02-19 14:56:16 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2020-01-20 09:02:41 -05:00
|
|
|
|
policy.RevisionDate = DateTime.UtcNow;
|
|
|
|
|
|
await _policyRepository.UpsertAsync(policy);
|
|
|
|
|
|
await _eventService.LogPolicyEventAsync(policy, Enums.EventType.Policy_Updated);
|
2020-01-15 09:19:28 -05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|