Skip to content

Create members approve invite service

What does this MR do and why?

This code adds a new service for approving member invitations in GitLab. This service handles the process of someone accepting an invitation to join a project or group, with built-in safety checks to ensure the person's email address matches the one used in the invitation email.

When an invitation is successfully accepted, the system logs the approval and publishes an event to notify other parts of the application. The code includes comprehensive error handling for cases where emails don't match or the invitation fails, along with appropriate error messages that can be translated into different languages. Extensive tests verify all scenarios work correctly, including successful approvals, email mismatches, and various failure conditions.

This service will later on be used, for example, in the invitation controller like the following:

diff --git a/app/controllers/invites_controller.rb b/app/controllers/invites_controller.rb
index e347af72f7fa2..41e8878784f9f 100644
--- a/app/controllers/invites_controller.rb
+++ b/app/controllers/invites_controller.rb
@@ -21,10 +21,12 @@ def show
   end
 
   def accept
-    if current_user_matches_invite? && member.accept_invite!(current_user)
+    response = Members::ApproveInviteService.new(current_user, member: @member).execute
+
+    if response[:status] == :success
       redirect_to invite_details[:path], notice: helpers.invite_accepted_notice(member)
     else
-      redirect_back_or_default(options: { alert: _("The invitation could not be accepted.") })
+      redirect_back_or_default(options: { alert: response[:error] })
     end
   end

References

Screenshots or screen recordings

Before After

How to set up and validate locally

  1. Create a group and invite a user
  2. Execute the following code to use the service to accept the invite
    • Members::ApproveInviteService.new(user, member: Member.last)
    • Check the success response and that Member.last.user changed from nil to user

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Lukas Wanko

Merge request reports

Loading