From 7b1c0d6df109982133cc2f187d715c1a81258d69 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Fri, 2 Jun 2017 16:52:54 -0400 Subject: [PATCH] clear token by id --- src/Api/Controllers/DevicesController.cs | 2 +- src/Core/Repositories/IDeviceRepository.cs | 3 ++- .../SqlServer/DeviceRepository.cs | 22 ++++++++++++++++--- .../Services/Implementations/DeviceService.cs | 2 +- .../NotificationHubPushRegistrationService.cs | 6 ++--- src/Sql/Sql.sqlproj | 3 ++- .../Device_ClearPushTokenById.sql | 13 +++++++++++ .../Device_ClearPushTokenByIdentifier.sql | 13 ----------- .../Device_ReadByIdentifier.sql | 13 +++++++++++ 9 files changed, 54 insertions(+), 23 deletions(-) create mode 100644 src/Sql/dbo/Stored Procedures/Device_ClearPushTokenById.sql delete mode 100644 src/Sql/dbo/Stored Procedures/Device_ClearPushTokenByIdentifier.sql create mode 100644 src/Sql/dbo/Stored Procedures/Device_ReadByIdentifier.sql diff --git a/src/Api/Controllers/DevicesController.cs b/src/Api/Controllers/DevicesController.cs index 4535f9f35d..fe8ee9e0ca 100644 --- a/src/Api/Controllers/DevicesController.cs +++ b/src/Api/Controllers/DevicesController.cs @@ -108,7 +108,7 @@ namespace Bit.Api.Controllers [HttpPost("identifier/{identifier}/clear-token")] public async Task PutClearToken(string identifier) { - var device = await _deviceRepository.GetByIdentifierAsync(identifier, _userService.GetProperUserId(User).Value); + var device = await _deviceRepository.GetByIdentifierAsync(identifier); if(device == null) { throw new NotFoundException(); diff --git a/src/Core/Repositories/IDeviceRepository.cs b/src/Core/Repositories/IDeviceRepository.cs index b79203e932..b1718c63bc 100644 --- a/src/Core/Repositories/IDeviceRepository.cs +++ b/src/Core/Repositories/IDeviceRepository.cs @@ -8,8 +8,9 @@ namespace Bit.Core.Repositories public interface IDeviceRepository : IRepository { Task GetByIdAsync(Guid id, Guid userId); + Task GetByIdentifierAsync(string identifier); Task GetByIdentifierAsync(string identifier, Guid userId); Task> GetManyByUserIdAsync(Guid userId); - Task ClearPushTokenByIdentifierAsync(string identifier); + Task ClearPushTokenAsync(Guid id); } } diff --git a/src/Core/Repositories/SqlServer/DeviceRepository.cs b/src/Core/Repositories/SqlServer/DeviceRepository.cs index 330fa16619..ecfb683529 100644 --- a/src/Core/Repositories/SqlServer/DeviceRepository.cs +++ b/src/Core/Repositories/SqlServer/DeviceRepository.cs @@ -30,6 +30,22 @@ namespace Bit.Core.Repositories.SqlServer return device; } + public async Task GetByIdentifierAsync(string identifier) + { + using(var connection = new SqlConnection(ConnectionString)) + { + var results = await connection.QueryAsync( + $"[{Schema}].[{Table}_ReadByIdentifier]", + new + { + Identifier = identifier + }, + commandType: CommandType.StoredProcedure); + + return results.FirstOrDefault(); + } + } + public async Task GetByIdentifierAsync(string identifier, Guid userId) { using(var connection = new SqlConnection(ConnectionString)) @@ -60,13 +76,13 @@ namespace Bit.Core.Repositories.SqlServer } } - public async Task ClearPushTokenByIdentifierAsync(string identifier) + public async Task ClearPushTokenAsync(Guid id) { using(var connection = new SqlConnection(ConnectionString)) { await connection.ExecuteAsync( - $"[{Schema}].[{Table}_ClearPushTokenByIdentifier]", - new { Identifier = identifier }, + $"[{Schema}].[{Table}_ClearPushTokenById]", + new { Id = id }, commandType: CommandType.StoredProcedure); } } diff --git a/src/Core/Services/Implementations/DeviceService.cs b/src/Core/Services/Implementations/DeviceService.cs index ed9d00e11e..236d946e60 100644 --- a/src/Core/Services/Implementations/DeviceService.cs +++ b/src/Core/Services/Implementations/DeviceService.cs @@ -35,7 +35,7 @@ namespace Bit.Core.Services public async Task ClearTokenAsync(Device device) { - await _deviceRepository.ClearPushTokenByIdentifierAsync(device.Identifier); + await _deviceRepository.ClearPushTokenAsync(device.Id); await _pushRegistrationService.DeleteRegistrationAsync(device.Id); } diff --git a/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs b/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs index 0387157e7d..2ea47ac7d5 100644 --- a/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs +++ b/src/Core/Services/Implementations/NotificationHubPushRegistrationService.cs @@ -50,9 +50,9 @@ namespace Bit.Core.Services switch(device.Type) { case Enums.DeviceType.Android: - payloadTemplate = "{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}"; - messageTemplate = "{\"data\":{\"type\":\"#(type)\"}," + - "\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}"; + payloadTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\",\"payload\":\"$(payload)\"}}}"; + messageTemplate = "{\"data\":{\"data\":{\"type\":\"#(type)\"}," + + "\"notification\":{\"title\":\"$(title)\",\"body\":\"$(message)\"}}}"; installation.Platform = NotificationPlatform.Gcm; break; diff --git a/src/Sql/Sql.sqlproj b/src/Sql/Sql.sqlproj index 2e7d813335..8e1a17205d 100644 --- a/src/Sql/Sql.sqlproj +++ b/src/Sql/Sql.sqlproj @@ -149,7 +149,7 @@ - + @@ -192,5 +192,6 @@ + \ No newline at end of file diff --git a/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenById.sql b/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenById.sql new file mode 100644 index 0000000000..e91edcf56f --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenById.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Device_ClearPushTokenById] + @Id NVARCHAR(50) +AS +BEGIN + SET NOCOUNT ON + + UPDATE + [dbo].[Device] + SET + [PushToken] = NULL + WHERE + [Id] = @Id +END diff --git a/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenByIdentifier.sql b/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenByIdentifier.sql deleted file mode 100644 index b5086eb7e8..0000000000 --- a/src/Sql/dbo/Stored Procedures/Device_ClearPushTokenByIdentifier.sql +++ /dev/null @@ -1,13 +0,0 @@ -CREATE PROCEDURE [dbo].[Device_ClearPushTokenByIdentifier] - @Identifier NVARCHAR(50) -AS -BEGIN - SET NOCOUNT ON - - UPDATE - [dbo].[Device] - SET - [PushToken] = NULL - WHERE - [Identifier] = @Identifier -END diff --git a/src/Sql/dbo/Stored Procedures/Device_ReadByIdentifier.sql b/src/Sql/dbo/Stored Procedures/Device_ReadByIdentifier.sql new file mode 100644 index 0000000000..681ff08a2b --- /dev/null +++ b/src/Sql/dbo/Stored Procedures/Device_ReadByIdentifier.sql @@ -0,0 +1,13 @@ +CREATE PROCEDURE [dbo].[Device_ReadByIdentifier] + @Identifier NVARCHAR(50) +AS +BEGIN + SET NOCOUNT ON + + SELECT + * + FROM + [dbo].[DeviceView] + WHERE + [Identifier] = @Identifier +END \ No newline at end of file