Files
server/src/Core/Services/Implementations/PolicyService.cs

86 lines
3.6 KiB
C#
Raw Normal View History

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;
private readonly IMailService _mailService;
2020-01-15 09:19:28 -05:00
public PolicyService(
IEventService eventService,
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
IPolicyRepository policyRepository,
IMailService mailService)
2020-01-15 09:19:28 -05:00
{
_eventService = eventService;
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_policyRepository = policyRepository;
_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);
if (org == null)
2020-01-15 09:19:28 -05:00
{
throw new BadRequestException("Organization not found");
}
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;
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
}
else if (policy.Enabled)
2020-02-19 14:56:16 -05:00
{
var currentPolicy = await _policyRepository.GetByIdAsync(policy.Id);
if (!currentPolicy?.Enabled ?? true)
2020-02-19 14:56:16 -05:00
{
if (currentPolicy.Type == Enums.PolicyType.TwoFactorAuthentication)
2020-02-19 14:56:16 -05:00
{
Organization organization = null;
2020-02-19 14:56:16 -05:00
var orgUsers = await _organizationUserRepository.GetManyDetailsByOrganizationAsync(
policy.OrganizationId);
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))
{
if (orgUser.UserId != savingUserId && !await userService.TwoFactorIsEnabledAsync(orgUser))
2020-02-19 14:56:16 -05:00
{
if (organization == null)
{
organization = await _organizationRepository.GetByIdAsync(policy.OrganizationId);
}
2020-02-19 14:56:16 -05:00
await organizationService.DeleteUserAsync(policy.OrganizationId, orgUser.Id,
savingUserId);
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
}
}
}