Fix user removal from a team [SCI-6323] (#3718)

This commit is contained in:
Alex Kriuchykhin 2021-12-03 16:26:19 +01:00 committed by GitHub
parent 272752ae4f
commit 245f0aedd7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 11 additions and 15 deletions

View file

@ -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

View file

@ -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

View file

@ -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