1
0
mirror of https://github.com/bitwarden/server.git synced 2025-04-06 13:38:13 -05:00

added push events and moved cipher writing to cipher service.

This commit is contained in:
Kyle Spearrin 2016-06-29 01:15:37 -04:00
parent afa37f5ab1
commit ef0a808687
8 changed files with 120 additions and 17 deletions

View File

@ -81,7 +81,7 @@ namespace Bit.Api.Controllers
cipher.Favorite = !cipher.Favorite; cipher.Favorite = !cipher.Favorite;
await _cipherRepository.ReplaceAsync(cipher); await _cipherService.SaveAsync(cipher);
} }
[HttpDelete("{id}")] [HttpDelete("{id}")]
@ -93,7 +93,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherRepository.DeleteAsync(cipher); await _cipherService.DeleteAsync(cipher);
} }
} }
} }

View File

@ -9,6 +9,7 @@ using Bit.Api.Models;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Domains; using Bit.Core.Domains;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Bit.Core.Services;
namespace Bit.Api.Controllers namespace Bit.Api.Controllers
{ {
@ -17,13 +18,16 @@ namespace Bit.Api.Controllers
public class FoldersController : Controller public class FoldersController : Controller
{ {
private readonly ICipherRepository _cipherRepository; private readonly ICipherRepository _cipherRepository;
private readonly ICipherService _cipherService;
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
public FoldersController( public FoldersController(
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
ICipherService cipherService,
UserManager<User> userManager) UserManager<User> userManager)
{ {
_cipherRepository = cipherRepository; _cipherRepository = cipherRepository;
_cipherService = cipherService;
_userManager = userManager; _userManager = userManager;
} }
@ -51,7 +55,7 @@ namespace Bit.Api.Controllers
public async Task<FolderResponseModel> Post([FromBody]FolderRequestModel model) public async Task<FolderResponseModel> Post([FromBody]FolderRequestModel model)
{ {
var folder = model.ToCipher(_userManager.GetUserId(User)); var folder = model.ToCipher(_userManager.GetUserId(User));
await _cipherRepository.CreateAsync(folder); await _cipherService.SaveAsync(folder);
return new FolderResponseModel(folder); return new FolderResponseModel(folder);
} }
@ -64,7 +68,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherRepository.ReplaceAsync(model.ToCipher(folder)); await _cipherService.SaveAsync(model.ToCipher(folder));
return new FolderResponseModel(folder); return new FolderResponseModel(folder);
} }
@ -77,7 +81,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherRepository.DeleteAsync(folder); await _cipherService.DeleteAsync(folder);
} }
} }
} }

View File

@ -9,6 +9,7 @@ using Bit.Api.Models;
using Bit.Core.Exceptions; using Bit.Core.Exceptions;
using Bit.Core.Domains; using Bit.Core.Domains;
using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity;
using Bit.Core.Services;
namespace Bit.Api.Controllers namespace Bit.Api.Controllers
{ {
@ -17,13 +18,16 @@ namespace Bit.Api.Controllers
public class SitesController : Controller public class SitesController : Controller
{ {
private readonly ICipherRepository _cipherRepository; private readonly ICipherRepository _cipherRepository;
private readonly ICipherService _cipherService;
private readonly UserManager<User> _userManager; private readonly UserManager<User> _userManager;
public SitesController( public SitesController(
ICipherRepository cipherRepository, ICipherRepository cipherRepository,
ICipherService cipherService,
UserManager<User> userManager) UserManager<User> userManager)
{ {
_cipherRepository = cipherRepository; _cipherRepository = cipherRepository;
_cipherService = cipherService;
_userManager = userManager; _userManager = userManager;
} }
@ -54,7 +58,7 @@ namespace Bit.Api.Controllers
public async Task<SiteResponseModel> Post([FromBody]SiteRequestModel model, string[] expand = null) public async Task<SiteResponseModel> Post([FromBody]SiteRequestModel model, string[] expand = null)
{ {
var site = model.ToCipher(_userManager.GetUserId(User)); var site = model.ToCipher(_userManager.GetUserId(User));
await _cipherRepository.CreateAsync(site); await _cipherService.SaveAsync(site);
var response = new SiteResponseModel(site); var response = new SiteResponseModel(site);
await ExpandAsync(site, response, expand, null); await ExpandAsync(site, response, expand, null);
@ -70,7 +74,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherRepository.ReplaceAsync(model.ToCipher(site)); await _cipherService.SaveAsync(model.ToCipher(site));
var response = new SiteResponseModel(site); var response = new SiteResponseModel(site);
await ExpandAsync(site, response, expand, null); await ExpandAsync(site, response, expand, null);
@ -86,7 +90,7 @@ namespace Bit.Api.Controllers
throw new NotFoundException(); throw new NotFoundException();
} }
await _cipherRepository.DeleteAsync(site); await _cipherService.DeleteAsync(site);
} }
private async Task ExpandAsync(Cipher site, SiteResponseModel response, string[] expand, Cipher folder) private async Task ExpandAsync(Cipher site, SiteResponseModel response, string[] expand, Cipher folder)

View File

@ -0,0 +1,10 @@
namespace Bit.Core.Enums
{
public enum PushType : short
{
SyncCipherUpdate = 0,
SyncCipherCreate = 1,
SyncCipherDelete = 2,
SyncCiphers = 3
}
}

View File

@ -10,11 +10,40 @@ namespace Bit.Core.Services
public class CipherService : ICipherService public class CipherService : ICipherService
{ {
private readonly ICipherRepository _cipherRepository; private readonly ICipherRepository _cipherRepository;
private readonly IPushService _pushService;
public CipherService( public CipherService(
ICipherRepository cipherRepository) ICipherRepository cipherRepository,
IPushService pushService)
{ {
_cipherRepository = cipherRepository; _cipherRepository = cipherRepository;
_pushService = pushService;
}
public async Task SaveAsync(Cipher cipher)
{
if(cipher.Id == default(Guid))
{
await _cipherRepository.CreateAsync(cipher);
// push
await _pushService.PushSyncCipherCreateAsync(cipher);
}
else
{
await _cipherRepository.ReplaceAsync(cipher);
// push
await _pushService.PushSyncCipherUpdateAsync(cipher);
}
}
public async Task DeleteAsync(Cipher cipher)
{
await _cipherRepository.DeleteAsync(cipher);
// push
await _pushService.PushSyncCipherDeleteAsync(cipher);
} }
public async Task ImportCiphersAsync( public async Task ImportCiphersAsync(
@ -46,6 +75,13 @@ namespace Bit.Core.Services
// create all the ciphers // create all the ciphers
await _cipherRepository.CreateAsync(ciphers); await _cipherRepository.CreateAsync(ciphers);
// push
var userId = folders.FirstOrDefault()?.UserId ?? ciphers.FirstOrDefault()?.UserId;
if(userId.HasValue)
{
await _pushService.PushSyncCiphersAsync(userId.Value);
}
} }
} }
} }

View File

@ -6,6 +6,8 @@ namespace Bit.Core.Services
{ {
public interface ICipherService public interface ICipherService
{ {
Task SaveAsync(Cipher cipher);
Task DeleteAsync(Cipher cipher);
Task ImportCiphersAsync(List<Cipher> folders, List<Cipher> ciphers, IEnumerable<KeyValuePair<int, int>> folderRelationships); Task ImportCiphersAsync(List<Cipher> folders, List<Cipher> ciphers, IEnumerable<KeyValuePair<int, int>> folderRelationships);
} }
} }

View File

@ -1,7 +1,14 @@
namespace Bit.Core.Services using System;
using System.Threading.Tasks;
using Bit.Core.Domains;
namespace Bit.Core.Services
{ {
public interface IPushService public interface IPushService
{ {
Task PushSyncCipherCreateAsync(Cipher cipher);
Task PushSyncCipherUpdateAsync(Cipher cipher);
Task PushSyncCipherDeleteAsync(Cipher cipher);
Task PushSyncCiphersAsync(Guid userId);
} }
} }

View File

@ -9,6 +9,8 @@ using PushSharp.Apple;
using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Hosting;
using PushSharp.Core; using PushSharp.Core;
using System.Security.Cryptography.X509Certificates; using System.Security.Cryptography.X509Certificates;
using Bit.Core.Domains;
using Bit.Core.Enums;
namespace Bit.Core.Services namespace Bit.Core.Services
{ {
@ -29,6 +31,44 @@ namespace Bit.Core.Services
InitApnsBroker(globalSettings, hostingEnvironment); InitApnsBroker(globalSettings, hostingEnvironment);
} }
public async Task PushSyncCipherCreateAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherCreate);
}
public async Task PushSyncCipherUpdateAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherUpdate);
}
public async Task PushSyncCipherDeleteAsync(Cipher cipher)
{
await PushCipherAsync(cipher, PushType.SyncCipherDelete);
}
private async Task PushCipherAsync(Cipher cipher, PushType type)
{
var message = new
{
Type = type,
Id = cipher.Id,
UserId = cipher.UserId
};
await PushToAllUserDevicesAsync(cipher.UserId, new JObject(message));
}
public async Task PushSyncCiphersAsync(Guid userId)
{
var message = new
{
Type = PushType.SyncCiphers,
UserId = userId
};
await PushToAllUserDevicesAsync(userId, new JObject(message));
}
private void InitGcmBroker(GlobalSettings globalSettings) private void InitGcmBroker(GlobalSettings globalSettings)
{ {
if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey) if(string.IsNullOrWhiteSpace(globalSettings.Push.GcmSenderId) || string.IsNullOrWhiteSpace(globalSettings.Push.GcmApiKey)
@ -192,7 +232,7 @@ namespace Bit.Core.Services
private async Task PushToAllUserDevicesAsync(Guid userId, JObject message) private async Task PushToAllUserDevicesAsync(Guid userId, JObject message)
{ {
var devices = (await _deviceRepository.GetManyByUserIdAsync(userId)).Where(d => d.PushToken != null); var devices = (await _deviceRepository.GetManyByUserIdAsync(userId)).Where(d => !string.IsNullOrWhiteSpace(d.PushToken));
if(devices.Count() == 0) if(devices.Count() == 0)
{ {
return; return;
@ -201,7 +241,7 @@ namespace Bit.Core.Services
if(_apnsBroker != null) if(_apnsBroker != null)
{ {
// Send to each iOS device // Send to each iOS device
foreach(var device in devices.Where(d => d.Type == Enums.DeviceType.iOS && d.PushToken != null)) foreach(var device in devices.Where(d => d.Type == DeviceType.iOS))
{ {
_apnsBroker.QueueNotification(new ApnsNotification _apnsBroker.QueueNotification(new ApnsNotification
{ {
@ -212,11 +252,11 @@ namespace Bit.Core.Services
} }
// Android can send to many devices at once // Android can send to many devices at once
if(_gcmBroker != null && devices.Any(d => d.Type == Enums.DeviceType.Android)) if(_gcmBroker != null && devices.Any(d => d.Type == DeviceType.Android))
{ {
_gcmBroker.QueueNotification(new GcmNotification _gcmBroker.QueueNotification(new GcmNotification
{ {
RegistrationIds = devices.Where(d => d.Type == Enums.DeviceType.Android && d.PushToken != null) RegistrationIds = devices.Where(d => d.Type == DeviceType.Android)
.Select(d => d.PushToken).ToList(), .Select(d => d.PushToken).ToList(),
Data = message Data = message
}); });