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

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

141 lines
5.6 KiB
C#
Raw Normal View History

using Bit.Core.Context;
using Bit.Core.Entities;
using Bit.Core.Enums;
2017-04-03 12:27:02 -04:00
using Bit.Core.Exceptions;
using Bit.Core.Models.Business;
2017-05-11 12:22:14 -04:00
using Bit.Core.Models.Data;
using Bit.Core.Repositories;
2017-04-03 12:27:02 -04:00
2022-08-29 14:53:16 -04:00
namespace Bit.Core.Services;
public class CollectionService : ICollectionService
2017-04-03 12:27:02 -04:00
{
2022-08-29 14:53:16 -04:00
private readonly IEventService _eventService;
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly ICollectionRepository _collectionRepository;
private readonly IUserRepository _userRepository;
private readonly IMailService _mailService;
private readonly IReferenceEventService _referenceEventService;
private readonly ICurrentContext _currentContext;
public CollectionService(
IEventService eventService,
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
ICollectionRepository collectionRepository,
IUserRepository userRepository,
IMailService mailService,
IReferenceEventService referenceEventService,
ICurrentContext currentContext)
2017-04-03 12:27:02 -04:00
{
2022-08-29 14:53:16 -04:00
_eventService = eventService;
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_collectionRepository = collectionRepository;
_userRepository = userRepository;
_mailService = mailService;
_referenceEventService = referenceEventService;
_currentContext = currentContext;
}
2017-04-03 12:27:02 -04:00
2022-08-29 14:53:16 -04:00
public async Task SaveAsync(Collection collection, IEnumerable<SelectionReadOnly> groups = null,
Guid? assignUserId = null)
{
var org = await _organizationRepository.GetByIdAsync(collection.OrganizationId);
if (org == null)
2017-04-03 12:27:02 -04:00
{
2022-08-29 14:53:16 -04:00
throw new BadRequestException("Organization not found");
2017-04-03 12:27:02 -04:00
}
2022-08-29 14:53:16 -04:00
if (collection.Id == default(Guid))
{
2022-08-29 14:53:16 -04:00
if (org.MaxCollections.HasValue)
{
2022-08-29 14:53:16 -04:00
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
if (org.MaxCollections.Value <= collectionCount)
{
2022-08-29 14:53:16 -04:00
throw new BadRequestException("You have reached the maximum number of collections " +
$"({org.MaxCollections.Value}) for this organization.");
}
2022-08-29 14:53:16 -04:00
}
2017-04-03 12:27:02 -04:00
2022-08-29 14:53:16 -04:00
if (groups == null || !org.UseGroups)
{
await _collectionRepository.CreateAsync(collection);
}
else
{
await _collectionRepository.CreateAsync(collection, groups);
}
2022-08-29 14:53:16 -04:00
// Assign a user to the newly created collection.
if (assignUserId.HasValue)
{
var orgUser = await _organizationUserRepository.GetByOrganizationAsync(org.Id, assignUserId.Value);
if (orgUser != null && orgUser.Status == Enums.OrganizationUserStatusType.Confirmed)
{
2022-08-29 14:53:16 -04:00
await _collectionRepository.UpdateUsersAsync(collection.Id,
new List<SelectionReadOnly> {
new SelectionReadOnly { Id = orgUser.Id, ReadOnly = false } });
}
2022-08-29 14:53:16 -04:00
}
2022-08-29 14:53:16 -04:00
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Created);
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.CollectionCreated, org));
}
else
{
if (!org.UseGroups)
{
await _collectionRepository.ReplaceAsync(collection);
}
else
{
2022-08-29 14:53:16 -04:00
await _collectionRepository.ReplaceAsync(collection, groups ?? new List<SelectionReadOnly>());
}
2022-08-29 14:53:16 -04:00
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Updated);
}
2022-08-29 14:53:16 -04:00
}
public async Task DeleteAsync(Collection collection)
{
await _collectionRepository.DeleteAsync(collection);
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Deleted);
}
2022-08-29 14:53:16 -04:00
public async Task DeleteUserAsync(Collection collection, Guid organizationUserId)
{
var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId);
if (orgUser == null || orgUser.OrganizationId != collection.OrganizationId)
{
2022-08-29 14:53:16 -04:00
throw new NotFoundException();
}
2022-08-29 14:53:16 -04:00
await _collectionRepository.DeleteUserAsync(collection.Id, organizationUserId);
await _eventService.LogOrganizationUserEventAsync(orgUser, Enums.EventType.OrganizationUser_Updated);
}
2022-08-29 14:53:16 -04:00
public async Task<IEnumerable<Collection>> GetOrganizationCollections(Guid organizationId)
{
if (!await _currentContext.ViewAllCollections(organizationId) && !await _currentContext.ManageUsers(organizationId))
{
2022-08-29 14:53:16 -04:00
throw new NotFoundException();
}
2022-08-29 14:53:16 -04:00
IEnumerable<Collection> orgCollections;
if (await _currentContext.OrganizationAdmin(organizationId) || await _currentContext.ViewAllCollections(organizationId))
{
2022-08-29 14:53:16 -04:00
// Admins, Owners, Providers and Custom (with collection management permissions) can access all items even if not assigned to them
orgCollections = await _collectionRepository.GetManyByOrganizationIdAsync(organizationId);
}
2022-08-29 14:53:16 -04:00
else
{
var collections = await _collectionRepository.GetManyByUserIdAsync(_currentContext.UserId.Value);
orgCollections = collections.Where(c => c.OrganizationId == organizationId);
}
return orgCollections;
2017-04-03 12:27:02 -04:00
}
}