1
0
mirror of https://github.com/bitwarden/server.git synced 2025-07-01 16:12:49 -05:00

Added Several New Reference Events (#1500)

* added enum values for new events

* hooked up directory sync event

* upgraded the OrganizationUpgrade ReferenceEvent

* Added metadata to the OrganizationUserInvited event noting if this is the first event sent from an organization

* Added metadata to the AdjustedSeats event

* Implemented vaultImported event

* Implemented FirstGroupAdded event

* Implemented FirstCollectionAdded event

* Implemented FirstSecretAdded event type

* Implemented SalesAssisted reference event

* changed events to match updated requirements

* renamed an event enum
This commit is contained in:
Addison Beck
2021-08-10 14:38:58 -04:00
committed by GitHub
parent 2e1df91232
commit 7928b25796
11 changed files with 98 additions and 25 deletions

View File

@ -13,6 +13,7 @@ using Bit.Core.Enums;
using Bit.Core.Utilities;
using Bit.Core.Settings;
using Bit.Core.Models.Api;
using Bit.Core.Models.Business;
namespace Bit.Core.Services
{
@ -34,6 +35,7 @@ namespace Bit.Core.Services
private readonly IPolicyRepository _policyRepository;
private readonly GlobalSettings _globalSettings;
private const long _fileSizeLeeway = 1024L * 1024L; // 1MB
private readonly IReferenceEventService _referenceEventService;
public CipherService(
ICipherRepository cipherRepository,
@ -48,7 +50,8 @@ namespace Bit.Core.Services
IEventService eventService,
IUserService userService,
IPolicyRepository policyRepository,
GlobalSettings globalSettings)
GlobalSettings globalSettings,
IReferenceEventService referenceEventService)
{
_cipherRepository = cipherRepository;
_folderRepository = folderRepository;
@ -63,6 +66,7 @@ namespace Bit.Core.Services
_userService = userService;
_policyRepository = policyRepository;
_globalSettings = globalSettings;
_referenceEventService = referenceEventService;
}
public async Task SaveAsync(Cipher cipher, Guid savingUserId, DateTime? lastKnownRevisionDate,
@ -83,6 +87,9 @@ namespace Bit.Core.Services
cipher.UserId = savingUserId;
}
await _cipherRepository.CreateAsync(cipher, collectionIds);
await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.CipherCreated, await _organizationRepository.GetByIdAsync(cipher.OrganizationId.Value)));
}
else
{
@ -727,17 +734,17 @@ namespace Bit.Core.Services
IEnumerable<KeyValuePair<int, int>> collectionRelationships,
Guid importingUserId)
{
if (collections.Count > 0)
var org = collections.Count > 0 ?
await _organizationRepository.GetByIdAsync(collections[0].OrganizationId) :
await _organizationRepository.GetByIdAsync(ciphers.FirstOrDefault(c => c.OrganizationId.HasValue).OrganizationId.Value);
if (collections.Count > 0 && org != null && org.MaxCollections.HasValue)
{
var org = await _organizationRepository.GetByIdAsync(collections[0].OrganizationId);
if (org != null && org.MaxCollections.HasValue)
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
if (org.MaxCollections.Value < (collectionCount + collections.Count))
{
var collectionCount = await _collectionRepository.GetCountByOrganizationIdAsync(org.Id);
if (org.MaxCollections.Value < (collectionCount + collections.Count))
{
throw new BadRequestException("This organization can only have a maximum of " +
$"{org.MaxCollections.Value} collections.");
}
throw new BadRequestException("This organization can only have a maximum of " +
$"{org.MaxCollections.Value} collections.");
}
}
@ -777,6 +784,13 @@ namespace Bit.Core.Services
// push
await _pushService.PushSyncVaultAsync(importingUserId);
if (org != null)
{
await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.VaultImported, org));
}
}
public async Task SoftDeleteAsync(Cipher cipher, Guid deletingUserId, bool orgAdmin = false)

View File

@ -5,6 +5,8 @@ using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using System.Collections.Generic;
using Bit.Core.Models.Data;
using Bit.Core.Models.Business;
using Bit.Core.Enums;
namespace Bit.Core.Services
{
@ -16,6 +18,7 @@ namespace Bit.Core.Services
private readonly ICollectionRepository _collectionRepository;
private readonly IUserRepository _userRepository;
private readonly IMailService _mailService;
private readonly IReferenceEventService _referenceEventService;
public CollectionService(
IEventService eventService,
@ -23,7 +26,8 @@ namespace Bit.Core.Services
IOrganizationUserRepository organizationUserRepository,
ICollectionRepository collectionRepository,
IUserRepository userRepository,
IMailService mailService)
IMailService mailService,
IReferenceEventService referenceEventService)
{
_eventService = eventService;
_organizationRepository = organizationRepository;
@ -31,6 +35,7 @@ namespace Bit.Core.Services
_collectionRepository = collectionRepository;
_userRepository = userRepository;
_mailService = mailService;
_referenceEventService = referenceEventService;
}
public async Task SaveAsync(Collection collection, IEnumerable<SelectionReadOnly> groups = null,
@ -76,6 +81,7 @@ namespace Bit.Core.Services
}
await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Created);
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.CollectionCreated, org));
}
else
{

View File

@ -5,6 +5,8 @@ using Bit.Core.Models.Table;
using Bit.Core.Repositories;
using System.Collections.Generic;
using Bit.Core.Models.Data;
using Bit.Core.Models.Business;
using Bit.Core.Enums;
namespace Bit.Core.Services
{
@ -14,17 +16,20 @@ namespace Bit.Core.Services
private readonly IOrganizationRepository _organizationRepository;
private readonly IOrganizationUserRepository _organizationUserRepository;
private readonly IGroupRepository _groupRepository;
private readonly IReferenceEventService _referenceEventService;
public GroupService(
IEventService eventService,
IOrganizationRepository organizationRepository,
IOrganizationUserRepository organizationUserRepository,
IGroupRepository groupRepository)
IGroupRepository groupRepository,
IReferenceEventService referenceEventService)
{
_eventService = eventService;
_organizationRepository = organizationRepository;
_organizationUserRepository = organizationUserRepository;
_groupRepository = groupRepository;
_referenceEventService = referenceEventService;
}
public async Task SaveAsync(Group group, IEnumerable<SelectionReadOnly> collections = null)
@ -54,6 +59,7 @@ namespace Bit.Core.Services
}
await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Created);
await _referenceEventService.RaiseEventAsync(new ReferenceEvent(ReferenceEventType.GroupCreated, org));
}
else
{

View File

@ -305,6 +305,8 @@ namespace Bit.Core.Services
{
PlanName = newPlan.Name,
PlanType = newPlan.Type,
OldPlanName = existingPlan.Name,
OldPlanType = existingPlan.Type,
Seats = organization.Seats,
Storage = organization.MaxStorageGb,
});
@ -497,14 +499,15 @@ namespace Bit.Core.Services
});
}
organization.Seats = (short?)newSeatTotal;
await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.AdjustSeats, organization)
{
PlanName = plan.Name,
PlanType = plan.Type,
Seats = organization.Seats,
Seats = newSeatTotal,
PreviousSeats = organization.Seats
});
organization.Seats = (short?)newSeatTotal;
await ReplaceAndUpdateCache(organization);
return paymentIntentClientSecret;
}
@ -1977,6 +1980,12 @@ namespace Bit.Core.Services
}
}
}
if (_currentContext.ClientId == BitwardenClient.DirectoryConnector)
{
await _referenceEventService.RaiseEventAsync(
new ReferenceEvent(ReferenceEventType.DirectorySynced, organization));
}
}
public async Task RotateApiKeyAsync(Organization organization)