From 245f0aedd7757d22505c1e755ec99e4ee9b6cefe Mon Sep 17 00:00:00 2001 From: Alex Kriuchykhin Date: Fri, 3 Dec 2021 16:26:19 +0100 Subject: [PATCH] Fix user removal from a team [SCI-6323] (#3718) --- .../user_assignments/propagate_assignment_job.rb | 14 ++++++++------ .../user_assignments/remove_user_assignment_job.rb | 4 ++-- app/models/user_team.rb | 8 +------- 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/app/jobs/user_assignments/propagate_assignment_job.rb b/app/jobs/user_assignments/propagate_assignment_job.rb index b534a8a72..06eb8b3c9 100644 --- a/app/jobs/user_assignments/propagate_assignment_job.rb +++ b/app/jobs/user_assignments/propagate_assignment_job.rb @@ -4,11 +4,12 @@ module UserAssignments class PropagateAssignmentJob < ApplicationJob queue_as :high_priority - def perform(resource, user, user_role, assigned_by, destroy: false) + def perform(resource, user, user_role, assigned_by, options = {}) @user = user @user_role = user_role @assigned_by = assigned_by - @destroy = destroy + @destroy = options.fetch(:destroy, false) + @remove_from_team = options.fetch(:remove_from_team, false) @resource = resource ActiveRecord::Base.transaction do @@ -36,7 +37,7 @@ module UserAssignments child_associations.find_each do |child_association| if @destroy - destroy_user_assignment(child_association) + destroy_or_update_user_assignment(child_association) else create_or_update_user_assignment(child_association) end @@ -55,13 +56,14 @@ module UserAssignments user_assignment.save! end - def destroy_user_assignment(object) + def destroy_or_update_user_assignment(object) # also destroy user designations if it's a MyModule object.user_my_modules.where(user: @user).destroy_all if object.is_a?(MyModule) - user_assignment = UserAssignment.find_by!(user: @user, assignable: object) + user_assignment = object.user_assignments.find { |ua| ua.user_id == @user.id } + return if user_assignment.blank? - if object.project.visible? + if object.project.visible? && !@remove_from_team # if project is public, the assignment # will reset to the default public role diff --git a/app/jobs/user_assignments/remove_user_assignment_job.rb b/app/jobs/user_assignments/remove_user_assignment_job.rb index 9f0f89b87..d03d13325 100644 --- a/app/jobs/user_assignments/remove_user_assignment_job.rb +++ b/app/jobs/user_assignments/remove_user_assignment_job.rb @@ -7,8 +7,8 @@ module UserAssignments def perform(user, team) ActiveRecord::Base.transaction do team.projects.each do |project| - UserAssignments::PropagateAssignmentJob.perform_now(project, user, nil, nil, destroy: true) - UserAssignment.where(user: user, assignable: project).destroy_all + UserAssignments::PropagateAssignmentJob + .perform_now(project, user, nil, nil, destroy: true, remove_from_team: true) end end end diff --git a/app/models/user_team.rb b/app/models/user_team.rb index ab6a2b976..69f7a9bbc 100644 --- a/app/models/user_team.rb +++ b/app/models/user_team.rb @@ -16,15 +16,9 @@ class UserTeam < ApplicationRecord I18n.t("user_teams.enums.role.#{role}") end - def destroy_associations # Destroy the user from all team's projects - team.projects.each do |project| - up2 = (project.user_projects.select { |up| up.user == self.user }).first - if up2.present? - up2.destroy - end - end + team.projects.joins(:user_projects).where(user_projects: { user: user }).destroy_all # destroy all assignments UserAssignments::RemoveUserAssignmentJob.perform_now(user, team) end