1
0
mirror of https://github.com/bitwarden/server.git synced 2025-01-23 22:01:28 +01:00

Reference events for Send (#1165)

This commit is contained in:
Chad Scharf 2021-02-25 13:40:26 -05:00 committed by GitHub
parent 07427623b3
commit 2f7c2a64e0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 30 additions and 0 deletions

View File

@ -24,5 +24,9 @@ namespace Bit.Core.Enums
InvitedUsers,
[EnumMember(Value = "rebilled")]
Rebilled,
[EnumMember(Value = "send-created")]
SendCreated,
[EnumMember(Value = "send-accessed")]
SendAccessed,
}
}

View File

@ -45,5 +45,12 @@ namespace Bit.Core.Models.Business
public short? Seats { get; set; }
public short? Storage { get; set; }
[JsonConverter(typeof(StringEnumConverter))]
public SendType? SendType { get; set; }
public int? MaxAccessCount { get; set; }
public bool? HasPassword { get; set; }
}
}

View File

@ -5,6 +5,7 @@ using System.Threading.Tasks;
using Bit.Core.Context;
using Bit.Core.Enums;
using Bit.Core.Exceptions;
using Bit.Core.Models.Business;
using Bit.Core.Models.Data;
using Bit.Core.Models.Table;
using Bit.Core.Repositories;
@ -24,6 +25,7 @@ namespace Bit.Core.Services
private readonly ISendFileStorageService _sendFileStorageService;
private readonly IPasswordHasher<User> _passwordHasher;
private readonly IPushNotificationService _pushService;
private readonly IReferenceEventService _referenceEventService;
private readonly GlobalSettings _globalSettings;
private readonly ICurrentContext _currentContext;
@ -35,6 +37,7 @@ namespace Bit.Core.Services
ISendFileStorageService sendFileStorageService,
IPasswordHasher<User> passwordHasher,
IPushNotificationService pushService,
IReferenceEventService referenceEventService,
GlobalSettings globalSettings,
IPolicyRepository policyRepository,
ICurrentContext currentContext)
@ -47,6 +50,7 @@ namespace Bit.Core.Services
_sendFileStorageService = sendFileStorageService;
_passwordHasher = passwordHasher;
_pushService = pushService;
_referenceEventService = referenceEventService;
_globalSettings = globalSettings;
_currentContext = currentContext;
}
@ -60,6 +64,7 @@ namespace Bit.Core.Services
{
await _sendRepository.CreateAsync(send);
await _pushService.PushSyncSendCreateAsync(send);
await RaiseReferenceEventAsync(send, ReferenceEventType.SendCreated);
}
else
{
@ -178,9 +183,23 @@ namespace Bit.Core.Services
// TODO: maybe move this to a simple ++ sproc?
send.AccessCount++;
await _sendRepository.ReplaceAsync(send);
await RaiseReferenceEventAsync(send, ReferenceEventType.SendAccessed);
return (send, false, false);
}
private async Task RaiseReferenceEventAsync(Send send, ReferenceEventType eventType)
{
await _referenceEventService.RaiseEventAsync(new ReferenceEvent
{
Id = send.UserId ?? default,
Type = eventType,
Source = ReferenceEventSource.User,
SendType = send.Type,
MaxAccessCount = send.MaxAccessCount,
HasPassword = !string.IsNullOrWhiteSpace(send.Password),
});
}
public string HashPassword(string password)
{
return _passwordHasher.HashPassword(new User(), password);