forked from bitwarden/server
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[PM-8895] Moving the groups controller business logic to a service #55
Open
lizard-boy
wants to merge
2
commits into
main
Choose a base branch
from
tools/pm-8895/groups-controller-decouple
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,7 @@ | ||
๏ปฟusing Bit.Api.AdminConsole.Models.Request; | ||
๏ปฟusing Api.AdminConsole.Services; | ||
using Bit.Api.AdminConsole.Models.Request; | ||
using Bit.Api.AdminConsole.Models.Response; | ||
using Bit.Api.Models.Response; | ||
using Bit.Api.Utilities; | ||
using Bit.Api.Vault.AuthorizationHandlers.Collections; | ||
using Bit.Api.Vault.AuthorizationHandlers.Groups; | ||
using Bit.Core; | ||
using Bit.Core.AdminConsole.OrganizationFeatures.Groups.Interfaces; | ||
using Bit.Core.AdminConsole.Repositories; | ||
using Bit.Core.AdminConsole.Services; | ||
using Bit.Core.Context; | ||
using Bit.Core.Exceptions; | ||
using Bit.Core.Repositories; | ||
using Bit.Core.Services; | ||
using Microsoft.AspNetCore.Authorization; | ||
using Microsoft.AspNetCore.Mvc; | ||
|
||
|
@@ -21,255 +11,75 @@ namespace Bit.Api.AdminConsole.Controllers; | |
[Authorize("Application")] | ||
public class GroupsController : Controller | ||
{ | ||
private readonly IGroupRepository _groupRepository; | ||
private readonly IGroupService _groupService; | ||
private readonly IDeleteGroupCommand _deleteGroupCommand; | ||
private readonly IOrganizationRepository _organizationRepository; | ||
private readonly ICurrentContext _currentContext; | ||
private readonly ICreateGroupCommand _createGroupCommand; | ||
private readonly IUpdateGroupCommand _updateGroupCommand; | ||
private readonly IAuthorizationService _authorizationService; | ||
private readonly IApplicationCacheService _applicationCacheService; | ||
private readonly IUserService _userService; | ||
private readonly IFeatureService _featureService; | ||
private readonly IOrganizationUserRepository _organizationUserRepository; | ||
private readonly ICollectionRepository _collectionRepository; | ||
private readonly IGroupsControllerService _groupsControllerService; | ||
|
||
public GroupsController( | ||
IGroupRepository groupRepository, | ||
IGroupService groupService, | ||
IOrganizationRepository organizationRepository, | ||
ICurrentContext currentContext, | ||
ICreateGroupCommand createGroupCommand, | ||
IUpdateGroupCommand updateGroupCommand, | ||
IDeleteGroupCommand deleteGroupCommand, | ||
IAuthorizationService authorizationService, | ||
IApplicationCacheService applicationCacheService, | ||
IUserService userService, | ||
IFeatureService featureService, | ||
IOrganizationUserRepository organizationUserRepository, | ||
ICollectionRepository collectionRepository) | ||
IGroupsControllerService groupsControllerService) | ||
{ | ||
_groupRepository = groupRepository; | ||
_groupService = groupService; | ||
_organizationRepository = organizationRepository; | ||
_currentContext = currentContext; | ||
_createGroupCommand = createGroupCommand; | ||
_updateGroupCommand = updateGroupCommand; | ||
_deleteGroupCommand = deleteGroupCommand; | ||
_authorizationService = authorizationService; | ||
_applicationCacheService = applicationCacheService; | ||
_userService = userService; | ||
_featureService = featureService; | ||
_organizationUserRepository = organizationUserRepository; | ||
_collectionRepository = collectionRepository; | ||
_groupsControllerService = groupsControllerService; | ||
} | ||
|
||
[HttpGet("{id}")] | ||
public async Task<GroupResponseModel> Get(string orgId, string id) | ||
{ | ||
var group = await _groupRepository.GetByIdAsync(new Guid(id)); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
return new GroupResponseModel(group); | ||
var group = await _groupsControllerService.GetOrganizationGroup(orgId, id); | ||
return group; | ||
} | ||
|
||
[HttpGet("{id}/details")] | ||
public async Task<GroupDetailsResponseModel> GetDetails(string orgId, string id) | ||
{ | ||
var groupDetails = await _groupRepository.GetByIdWithCollectionsAsync(new Guid(id)); | ||
if (groupDetails?.Item1 == null || !await _currentContext.ManageGroups(groupDetails.Item1.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
return new GroupDetailsResponseModel(groupDetails.Item1, groupDetails.Item2); | ||
var groupDetails = await _groupsControllerService.GetOrganizationGroupDetail(orgId, id); | ||
return groupDetails; | ||
} | ||
|
||
[HttpGet("")] | ||
public async Task<ListResponseModel<GroupDetailsResponseModel>> Get(Guid orgId) | ||
{ | ||
var authorized = | ||
(await _authorizationService.AuthorizeAsync(User, GroupOperations.ReadAll(orgId))).Succeeded; | ||
if (!authorized) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var groups = await _groupRepository.GetManyWithCollectionsByOrganizationIdAsync(orgId); | ||
var responses = groups.Select(g => new GroupDetailsResponseModel(g.Item1, g.Item2)); | ||
var responses = await _groupsControllerService.GetOrganizationGroupsDetails(User, orgId); | ||
return new ListResponseModel<GroupDetailsResponseModel>(responses); | ||
} | ||
|
||
[HttpGet("{id}/users")] | ||
public async Task<IEnumerable<Guid>> GetUsers(string orgId, string id) | ||
{ | ||
var idGuid = new Guid(id); | ||
var group = await _groupRepository.GetByIdAsync(idGuid); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var groupIds = await _groupRepository.GetManyUserIdsByIdAsync(idGuid); | ||
return groupIds; | ||
var userIds = await _groupsControllerService.GetOrganizationUsers(orgId); | ||
return userIds; | ||
} | ||
Comment on lines
44
to
48
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. logic: The 'id' parameter is not used in the service method call |
||
|
||
[HttpPost("")] | ||
public async Task<GroupResponseModel> Post(Guid orgId, [FromBody] GroupRequestModel model) | ||
{ | ||
if (!await _currentContext.ManageGroups(orgId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
// Flexible Collections - check the user has permission to grant access to the collections for the new group | ||
if (_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1) && model.Collections?.Any() == true) | ||
{ | ||
var collections = await _collectionRepository.GetManyByManyIdsAsync(model.Collections.Select(a => a.Id)); | ||
var authorized = | ||
(await _authorizationService.AuthorizeAsync(User, collections, BulkCollectionOperations.ModifyGroupAccess)) | ||
.Succeeded; | ||
if (!authorized) | ||
{ | ||
throw new NotFoundException("You are not authorized to grant access to these collections."); | ||
} | ||
} | ||
|
||
var organization = await _organizationRepository.GetByIdAsync(orgId); | ||
var group = model.ToGroup(orgId); | ||
await _createGroupCommand.CreateGroupAsync(group, organization, model.Collections?.Select(c => c.ToSelectionReadOnly()).ToList(), model.Users); | ||
|
||
return new GroupResponseModel(group); | ||
var group = await _groupsControllerService.CreateGroup(User, orgId, model); | ||
return group; | ||
} | ||
|
||
[HttpPut("{id}")] | ||
[HttpPost("{id}")] | ||
public async Task<GroupResponseModel> Put(Guid orgId, Guid id, [FromBody] GroupRequestModel model) | ||
{ | ||
if (_featureService.IsEnabled(FeatureFlagKeys.FlexibleCollectionsV1)) | ||
{ | ||
// Use new Flexible Collections v1 logic | ||
return await Put_vNext(orgId, id, model); | ||
} | ||
|
||
// Pre-Flexible Collections v1 logic follows | ||
var group = await _groupRepository.GetByIdAsync(id); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
var organization = await _organizationRepository.GetByIdAsync(orgId); | ||
|
||
await _updateGroupCommand.UpdateGroupAsync(model.ToGroup(group), organization, | ||
model.Collections.Select(c => c.ToSelectionReadOnly()).ToList(), model.Users); | ||
return new GroupResponseModel(group); | ||
} | ||
|
||
/// <summary> | ||
/// Put logic for Flexible Collections v1 | ||
/// </summary> | ||
private async Task<GroupResponseModel> Put_vNext(Guid orgId, Guid id, [FromBody] GroupRequestModel model) | ||
{ | ||
var (group, currentAccess) = await _groupRepository.GetByIdWithCollectionsAsync(id); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
// Check whether the user is permitted to add themselves to the group | ||
var orgAbility = await _applicationCacheService.GetOrganizationAbilityAsync(orgId); | ||
if (!orgAbility.AllowAdminAccessToAllCollectionItems) | ||
{ | ||
var userId = _userService.GetProperUserId(User).Value; | ||
var organizationUser = await _organizationUserRepository.GetByOrganizationAsync(orgId, userId); | ||
var currentGroupUsers = await _groupRepository.GetManyUserIdsByIdAsync(id); | ||
// OrganizationUser may be null if the current user is a provider | ||
if (organizationUser != null && !currentGroupUsers.Contains(organizationUser.Id) && model.Users.Contains(organizationUser.Id)) | ||
{ | ||
throw new BadRequestException("You cannot add yourself to groups."); | ||
} | ||
} | ||
|
||
// The client only sends collections that the saving user has permissions to edit. | ||
// On the server side, we need to (1) confirm this and (2) concat these with the collections that the user | ||
// can't edit before saving to the database. | ||
var currentCollections = await _collectionRepository | ||
.GetManyByManyIdsAsync(currentAccess.Select(cas => cas.Id)); | ||
|
||
var readonlyCollectionIds = new HashSet<Guid>(); | ||
foreach (var collection in currentCollections) | ||
{ | ||
if (!(await _authorizationService.AuthorizeAsync(User, collection, BulkCollectionOperations.ModifyGroupAccess)) | ||
.Succeeded) | ||
{ | ||
readonlyCollectionIds.Add(collection.Id); | ||
} | ||
} | ||
|
||
if (model.Collections.Any(c => readonlyCollectionIds.Contains(c.Id))) | ||
{ | ||
throw new BadRequestException("You must have Can Manage permissions to edit a collection's membership"); | ||
} | ||
|
||
var editedCollectionAccess = model.Collections | ||
.Select(c => c.ToSelectionReadOnly()); | ||
var readonlyCollectionAccess = currentAccess | ||
.Where(ca => readonlyCollectionIds.Contains(ca.Id)); | ||
var collectionsToSave = editedCollectionAccess | ||
.Concat(readonlyCollectionAccess) | ||
.ToList(); | ||
|
||
var organization = await _organizationRepository.GetByIdAsync(orgId); | ||
|
||
await _updateGroupCommand.UpdateGroupAsync(model.ToGroup(group), organization, collectionsToSave, model.Users); | ||
return new GroupResponseModel(group); | ||
var group = await _groupsControllerService.UpdateGroup(User, orgId, id, model); | ||
return group; | ||
} | ||
|
||
[HttpDelete("{id}")] | ||
[HttpPost("{id}/delete")] | ||
public async Task Delete(string orgId, string id) | ||
{ | ||
var group = await _groupRepository.GetByIdAsync(new Guid(id)); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
await _deleteGroupCommand.DeleteAsync(group); | ||
await _groupsControllerService.DeleteGroup(orgId, id); | ||
} | ||
|
||
[HttpDelete("")] | ||
[HttpPost("delete")] | ||
public async Task BulkDelete([FromBody] GroupBulkRequestModel model) | ||
{ | ||
var groups = await _groupRepository.GetManyByManyIds(model.Ids); | ||
|
||
foreach (var group in groups) | ||
{ | ||
if (!await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
} | ||
|
||
await _deleteGroupCommand.DeleteManyAsync(groups); | ||
await _groupsControllerService.BulkDeleteGroups(model); | ||
} | ||
|
||
[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)); | ||
if (group == null || !await _currentContext.ManageGroups(group.OrganizationId)) | ||
{ | ||
throw new NotFoundException(); | ||
} | ||
|
||
await _groupService.DeleteUserAsync(group, new Guid(orgUserId)); | ||
await _groupsControllerService.DeleteGroupUser(orgId, id, orgUserId); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style: Consider using a more descriptive variable name than 'group' for clarity