From 9fee09e204f2db9a0287f855f1775edde087e1c7 Mon Sep 17 00:00:00 2001 From: Kyle Spearrin Date: Mon, 9 Jul 2018 23:07:04 -0400 Subject: [PATCH] log event when removing user from group/collection --- src/Api/Controllers/CollectionsController.cs | 2 +- src/Api/Controllers/GroupsController.cs | 6 +----- .../Api/Response/OrganizationUserResponseModel.cs | 1 + src/Core/Services/ICollectionService.cs | 2 ++ src/Core/Services/IGroupService.cs | 2 ++ .../Services/Implementations/CollectionService.cs | 11 +++++++++++ src/Core/Services/Implementations/GroupService.cs | 14 ++++++++++++++ .../Implementations/OrganizationService.cs | 3 ++- 8 files changed, 34 insertions(+), 7 deletions(-) diff --git a/src/Api/Controllers/CollectionsController.cs b/src/Api/Controllers/CollectionsController.cs index 68d1457eb7..265a51fcc1 100644 --- a/src/Api/Controllers/CollectionsController.cs +++ b/src/Api/Controllers/CollectionsController.cs @@ -155,7 +155,7 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - await _collectionRepository.DeleteUserAsync(collection.Id, new Guid(orgUserId)); + await _collectionService.DeleteUserAsync(collection, new Guid(orgUserId)); } } } diff --git a/src/Api/Controllers/GroupsController.cs b/src/Api/Controllers/GroupsController.cs index b61e0cb5d7..84978235c0 100644 --- a/src/Api/Controllers/GroupsController.cs +++ b/src/Api/Controllers/GroupsController.cs @@ -8,7 +8,6 @@ using Bit.Core.Models.Api; using Bit.Core.Exceptions; using Bit.Core.Services; using Bit.Core; -using System.Collections.Generic; namespace Bit.Api.Controllers { @@ -98,7 +97,6 @@ namespace Bit.Api.Controllers } [HttpPut("{id}")] - [HttpPost("{id}")] public async Task Put(string orgId, string id, [FromBody]GroupRequestModel model) { var group = await _groupRepository.GetByIdAsync(new Guid(id)); @@ -112,7 +110,6 @@ namespace Bit.Api.Controllers } [HttpDelete("{id}")] - [HttpPost("{id}/delete")] public async Task Delete(string orgId, string id) { var group = await _groupRepository.GetByIdAsync(new Guid(id)); @@ -125,7 +122,6 @@ namespace Bit.Api.Controllers } [HttpDelete("{id}/user/{orgUserId}")] - [HttpPost("{id}/delete-user/{orgUserId}")] public async Task Delete(string orgId, string id, string orgUserId) { var group = await _groupRepository.GetByIdAsync(new Guid(id)); @@ -134,7 +130,7 @@ namespace Bit.Api.Controllers throw new NotFoundException(); } - await _groupRepository.DeleteUserAsync(group.Id, new Guid(orgUserId)); + await _groupService.DeleteUserAsync(group, new Guid(orgUserId)); } } } diff --git a/src/Core/Models/Api/Response/OrganizationUserResponseModel.cs b/src/Core/Models/Api/Response/OrganizationUserResponseModel.cs index 43c80fcfa6..22db217627 100644 --- a/src/Core/Models/Api/Response/OrganizationUserResponseModel.cs +++ b/src/Core/Models/Api/Response/OrganizationUserResponseModel.cs @@ -57,6 +57,7 @@ namespace Bit.Core.Models.Api public IEnumerable Collections { get; set; } } + public class OrganizationUserUserDetailsResponseModel : OrganizationUserResponseModel { public OrganizationUserUserDetailsResponseModel(OrganizationUserUserDetails organizationUser, diff --git a/src/Core/Services/ICollectionService.cs b/src/Core/Services/ICollectionService.cs index eb72c8fbec..ae0384f71f 100644 --- a/src/Core/Services/ICollectionService.cs +++ b/src/Core/Services/ICollectionService.cs @@ -2,6 +2,7 @@ using Bit.Core.Models.Table; using System.Collections.Generic; using Bit.Core.Models.Data; +using System; namespace Bit.Core.Services { @@ -9,5 +10,6 @@ namespace Bit.Core.Services { Task SaveAsync(Collection collection, IEnumerable groups = null); Task DeleteAsync(Collection collection); + Task DeleteUserAsync(Collection collection, Guid organizationUserId); } } diff --git a/src/Core/Services/IGroupService.cs b/src/Core/Services/IGroupService.cs index 9be9b43a9b..ce7696021e 100644 --- a/src/Core/Services/IGroupService.cs +++ b/src/Core/Services/IGroupService.cs @@ -2,6 +2,7 @@ using Bit.Core.Models.Table; using System.Collections.Generic; using Bit.Core.Models.Data; +using System; namespace Bit.Core.Services { @@ -9,5 +10,6 @@ namespace Bit.Core.Services { Task SaveAsync(Group group, IEnumerable collections = null); Task DeleteAsync(Group group); + Task DeleteUserAsync(Group group, Guid organizationUserId); } } diff --git a/src/Core/Services/Implementations/CollectionService.cs b/src/Core/Services/Implementations/CollectionService.cs index aa4898757a..102b2a1505 100644 --- a/src/Core/Services/Implementations/CollectionService.cs +++ b/src/Core/Services/Implementations/CollectionService.cs @@ -84,5 +84,16 @@ namespace Bit.Core.Services await _collectionRepository.DeleteAsync(collection); await _eventService.LogCollectionEventAsync(collection, Enums.EventType.Collection_Deleted); } + + public async Task DeleteUserAsync(Collection collection, Guid organizationUserId) + { + var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId); + if(orgUser == null || orgUser.OrganizationId != collection.OrganizationId) + { + throw new NotFoundException(); + } + await _collectionRepository.DeleteUserAsync(collection.Id, organizationUserId); + await _eventService.LogOrganizationUserEventAsync(orgUser, Enums.EventType.OrganizationUser_Updated); + } } } diff --git a/src/Core/Services/Implementations/GroupService.cs b/src/Core/Services/Implementations/GroupService.cs index de8b73cad8..4a7618c444 100644 --- a/src/Core/Services/Implementations/GroupService.cs +++ b/src/Core/Services/Implementations/GroupService.cs @@ -12,15 +12,18 @@ namespace Bit.Core.Services { private readonly IEventService _eventService; private readonly IOrganizationRepository _organizationRepository; + private readonly IOrganizationUserRepository _organizationUserRepository; private readonly IGroupRepository _groupRepository; public GroupService( IEventService eventService, IOrganizationRepository organizationRepository, + IOrganizationUserRepository organizationUserRepository, IGroupRepository groupRepository) { _eventService = eventService; _organizationRepository = organizationRepository; + _organizationUserRepository = organizationUserRepository; _groupRepository = groupRepository; } @@ -65,5 +68,16 @@ namespace Bit.Core.Services await _groupRepository.DeleteAsync(group); await _eventService.LogGroupEventAsync(group, Enums.EventType.Group_Deleted); } + + public async Task DeleteUserAsync(Group group, Guid organizationUserId) + { + var orgUser = await _organizationUserRepository.GetByIdAsync(organizationUserId); + if(orgUser == null || orgUser.OrganizationId != group.OrganizationId) + { + throw new NotFoundException(); + } + await _groupRepository.DeleteUserAsync(group.Id, organizationUserId); + await _eventService.LogOrganizationUserEventAsync(orgUser, Enums.EventType.OrganizationUser_UpdatedGroups); + } } } diff --git a/src/Core/Services/Implementations/OrganizationService.cs b/src/Core/Services/Implementations/OrganizationService.cs index 359d0a1426..ff2cf2019d 100644 --- a/src/Core/Services/Implementations/OrganizationService.cs +++ b/src/Core/Services/Implementations/OrganizationService.cs @@ -919,7 +919,7 @@ namespace Bit.Core.Services organizationId, email, false); if(existingOrgUserCount > 0) { - throw new BadRequestException("User already invited."); + continue; } var orgUser = new OrganizationUser @@ -946,6 +946,7 @@ namespace Bit.Core.Services } await SendInviteAsync(orgUser); + await _eventService.LogOrganizationUserEventAsync(orgUser, EventType.OrganizationUser_Invited); orgUsers.Add(orgUser); }