Skip to content

[PM-8895] Moving the groups controller business logic to a service#55

Open
lizard-boy wants to merge 2 commits into
mainfrom
tools/pm-8895/groups-controller-decouple
Open

[PM-8895] Moving the groups controller business logic to a service#55
lizard-boy wants to merge 2 commits into
mainfrom
tools/pm-8895/groups-controller-decouple

Conversation

@lizard-boy

@lizard-boy lizard-boy commented Oct 19, 2024

Copy link
Copy Markdown

🎟️ Tracking

https://bitwarden.atlassian.net/browse/PM-8995

📔 Objective

The GroupsController contained a lot of logic that is responsible for creating the objects for the endpoints. Moving to a service allows reusability of the code in the new reports being developed. The controller has repositories injected in. Exposing repos in a controller could open up issues with bypassing logic in services designed to filter. Also a potential issue if the repo gets exposed.

⏰ Reminders before review

  • Contributor guidelines followed
  • All formatters and local linters executed and passed
  • Written new unit and / or integration tests where applicable
  • Protected functional changes with optionality (feature flags)
  • Used internationalization (i18n) for all UI strings
  • CI builds passed
  • Communicated to DevOps any deployment requirements
  • Updated any necessary documentation (Confluence, contributing docs) or informed the documentation team

🦮 Reviewer guidelines

  • 👍 (:+1:) or similar for great changes
  • 📝 (:memo:) or ℹ️ (:information_source:) for notes or general info
  • ❓ (:question:) for questions
  • 🤔 (:thinking:) or 💭 (:thought_balloon:) for more open inquiry that's not quite a confirmed issue and could potentially benefit from discussion
  • 🎨 (:art:) for suggestions / improvements
  • ❌ (:x:) or ⚠️ (:warning:) for more significant problems or concerns needing attention
  • 🌱 (:seedling:) or ♻️ (:recycle:) for future improvements or indications of technical debt
  • ⛏ (:pick:) for minor or nitpick changes

Greptile Summary

This PR refactors the GroupsController by moving business logic to a new GroupsControllerService, improving code organization and reusability while maintaining existing functionality.

  • Introduced GroupsControllerService to encapsulate group-related operations, separating concerns from the controller
  • Added IGroupsControllerService interface to define group management methods, enhancing modularity
  • Updated GroupsController to delegate operations to the new service, simplifying its structure
  • Modified Startup.cs to register IGroupsControllerService for dependency injection

@greptile-apps greptile-apps Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

4 file(s) reviewed, 9 comment(s)
Edit PR Review Bot Settings | Greptile

Comment on lines 23 to 27
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;
}

Copy link
Copy Markdown

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

Comment on lines 44 to 48
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;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: The 'id' parameter is not used in the service method call

/// <summary>
/// Gets the basic group information of an organization
/// </summary>
/// <param name="orgId">Organization id/param>

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

syntax: Missing closing bracket in XML comment for 'orgId' parameter

(await _authorizationService.AuthorizeAsync(user, GroupOperations.ReadAll(orgId))).Succeeded;
if (!authorized)
{
throw new NotFoundException();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using ForbiddenException instead of NotFoundException for unauthorized access

.Succeeded;
if (!authorized)
{
throw new NotFoundException("You are not authorized to grant access to these collections.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Use ForbiddenException instead of NotFoundException for unauthorized access

Comment on lines +279 to +281
if (organizationUser != null && !currentGroupUsers.Contains(organizationUser.Id) && model.Users.Contains(organizationUser.Id))
{
throw new BadRequestException("You cannot add yourself to groups.");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider extracting this check into a separate method for better readability

using Bit.Api.AdminConsole.Models.Request;
using Bit.Api.AdminConsole.Models.Response;

namespace Api.AdminConsole.Services;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider using 'Bit.Api.AdminConsole.Services' for consistency with other namespaces

Comment on lines +9 to +10
Task<GroupResponseModel> GetOrganizationGroup(string orgId, string groupId);
Task<GroupDetailsResponseModel> GetOrganizationGroupDetail(string orgId, string groupId);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic: Inconsistent parameter types: string for orgId and groupId here, but Guid in other methods

Comment thread src/Api/Startup.cs
services.AddSingleton<BitPayClient>();

// Groups
services.AddScoped<IGroupsControllerService, GroupsControllerService>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: Consider adding a comment explaining the purpose of this service

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants