From 937bb4359f62ebf4510047c74064d7bf9125d1b5 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Wed, 15 Jan 2020 09:19:28 -0500 Subject: [PATCH] policy service --- src/Core/Services/IPolicyService.cs | 11 ++++ .../Services/Implementations/PolicyService.cs | 65 +++++++++++++++++++ 2 files changed, 76 insertions(+) create mode 100644 src/Core/Services/IPolicyService.cs create mode 100644 src/Core/Services/Implementations/PolicyService.cs diff --git a/src/Core/Services/IPolicyService.cs b/src/Core/Services/IPolicyService.cs new file mode 100644 index 000000000..daf3caeaf --- /dev/null +++ b/src/Core/Services/IPolicyService.cs @@ -0,0 +1,11 @@ +using System.Threading.Tasks; +using Bit.Core.Models.Table; + +namespace Bit.Core.Services +{ + public interface IPolicyService + { + Task SaveAsync(Policy policy); + Task DeleteAsync(Policy policy); + } +} diff --git a/src/Core/Services/Implementations/PolicyService.cs b/src/Core/Services/Implementations/PolicyService.cs new file mode 100644 index 000000000..7e7f7c80d --- /dev/null +++ b/src/Core/Services/Implementations/PolicyService.cs @@ -0,0 +1,65 @@ +using System; +using System.Threading.Tasks; +using Bit.Core.Exceptions; +using Bit.Core.Models.Table; +using Bit.Core.Repositories; +using System.Collections.Generic; +using Bit.Core.Models.Data; + +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"); + } + + /* + if(!org.UseGroups) + { + throw new BadRequestException("This organization cannot use policies."); + } + */ + + if(policy.Id == default(Guid)) + { + policy.CreationDate = policy.RevisionDate = DateTime.UtcNow; + await _policyRepository.CreateAsync(policy); + //await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Created); + } + else + { + policy.RevisionDate = DateTime.UtcNow; + await _policyRepository.ReplaceAsync(policy); + //await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Updated); + } + } + + public async Task DeleteAsync(Policy policy) + { + await _policyRepository.DeleteAsync(policy); + //await _eventService.LogGroupEventAsync(policy, Enums.EventType.Group_Deleted); + } + } +}