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

70 lines
2.3 KiB
C#
Raw Normal View History

2017-05-09 14:17:22 -04:00
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;
2017-05-09 14:17:22 -04:00
namespace Bit.Core.Services
{
public class GroupService : IGroupService
{
private readonly IEventService _eventService;
2017-05-09 14:17:22 -04:00
private readonly IOrganizationRepository _organizationRepository;
private readonly IGroupRepository _groupRepository;
public GroupService(
IEventService eventService,
2017-05-09 14:17:22 -04:00
IOrganizationRepository organizationRepository,
IGroupRepository groupRepository)
{
_eventService = eventService;
2017-05-09 14:17:22 -04:00
_organizationRepository = organizationRepository;
_groupRepository = groupRepository;
}
public async Task SaveAsync(Group group, IEnumerable<SelectionReadOnly> collections = null)
2017-05-09 14:17:22 -04:00
{
var org = await _organizationRepository.GetByIdAsync(group.OrganizationId);
if(org == null)
{
throw new BadRequestException("Organization not found");
}
if(!org.UseGroups)
{
throw new BadRequestException("This organization cannot use groups.");
}
if(group.Id == default(Guid))
{
group.CreationDate = group.RevisionDate = DateTime.UtcNow;
if(collections == null)
2017-05-09 14:17:22 -04:00
{
await _groupRepository.CreateAsync(group);
}
else
{
await _groupRepository.CreateAsync(group, collections);
2017-05-09 14:17:22 -04:00
}
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Created);
2017-05-09 14:17:22 -04:00
}
else
{
group.RevisionDate = DateTime.UtcNow;
await _groupRepository.ReplaceAsync(group, collections ?? new List<SelectionReadOnly>());
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Updated);
2017-05-09 14:17:22 -04:00
}
}
public async Task DeleteAsync(Group group)
{
await _groupRepository.DeleteAsync(group);
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Deleted);
}
2017-05-09 14:17:22 -04:00
}
}