2020-01-15 09:19:28 -05:00
|
|
|
|
using System;
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
|
|
public PolicyService(
|
|
|
|
|
|
IEventService eventService,
|
|
|
|
|
|
IOrganizationRepository organizationRepository,
|
|
|
|
|
|
IOrganizationUserRepository organizationUserRepository,
|
|
|
|
|
|
IPolicyRepository policyRepository)
|
|
|
|
|
|
{
|
|
|
|
|
|
_eventService = eventService;
|
|
|
|
|
|
_organizationRepository = organizationRepository;
|
|
|
|
|
|
_organizationUserRepository = organizationUserRepository;
|
|
|
|
|
|
_policyRepository = policyRepository;
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public async Task SaveAsync(Policy policy)
|
|
|
|
|
|
{
|
|
|
|
|
|
var org = await _organizationRepository.GetByIdAsync(policy.OrganizationId);
|
|
|
|
|
|
if(org == null)
|
|
|
|
|
|
{
|
|
|
|
|
|
throw new BadRequestException("Organization not found");
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2020-01-15 15:01:31 -05: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-01-15 09:19:28 -05:00
|
|
|
|
if(policy.Id == default(Guid))
|
|
|
|
|
|
{
|
2020-01-20 09:02:41 -05:00
|
|
|
|
policy.CreationDate = now;
|
2020-01-15 09:19:28 -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
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|