Skip to content
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
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

lizard-boy
Copy link

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

🎟️ 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

Copy link

@greptile-apps greptile-apps bot left a comment

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

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

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

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

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

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

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

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

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

@@ -93,6 +95,9 @@ public void ConfigureServices(IServiceCollection services)
// BitPay
services.AddSingleton<BitPayClient>();

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

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