2021-06-06 15:54:47 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserAssignments
|
|
|
|
class PropagateAssignmentJob < ApplicationJob
|
|
|
|
queue_as :high_priority
|
|
|
|
|
2023-08-11 21:31:29 +08:00
|
|
|
def perform(resource, user_id, user_role, assigned_by_id, options = {})
|
|
|
|
@user = User.find(user_id)
|
2021-06-06 15:54:47 +08:00
|
|
|
@user_role = user_role
|
2023-08-11 21:31:29 +08:00
|
|
|
@assigned_by = User.find_by(id: assigned_by_id)
|
2021-12-03 23:26:19 +08:00
|
|
|
@destroy = options.fetch(:destroy, false)
|
|
|
|
@remove_from_team = options.fetch(:remove_from_team, false)
|
2021-11-12 18:12:06 +08:00
|
|
|
@resource = resource
|
2021-06-06 15:54:47 +08:00
|
|
|
|
|
|
|
ActiveRecord::Base.transaction do
|
2021-11-09 23:09:12 +08:00
|
|
|
sync_resource_user_associations(resource)
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2021-11-09 23:09:12 +08:00
|
|
|
def sync_resource_user_associations(resource)
|
2021-11-12 18:12:06 +08:00
|
|
|
# stop role update propagation for child resources when encountering a manual assignment
|
|
|
|
return if resource != @resource && # child resource
|
|
|
|
!@destroy &&
|
|
|
|
resource.user_assignments.find_by(user: @user).manually_assigned?
|
|
|
|
|
2021-11-09 23:09:12 +08:00
|
|
|
child_associations =
|
|
|
|
case resource
|
|
|
|
when Project
|
|
|
|
resource.experiments
|
|
|
|
when Experiment
|
|
|
|
resource.my_modules
|
2021-06-06 15:54:47 +08:00
|
|
|
else
|
2021-11-09 23:09:12 +08:00
|
|
|
return
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
|
2021-11-09 23:09:12 +08:00
|
|
|
child_associations.find_each do |child_association|
|
2021-06-06 15:54:47 +08:00
|
|
|
if @destroy
|
2021-12-03 23:26:19 +08:00
|
|
|
destroy_or_update_user_assignment(child_association)
|
2021-06-06 15:54:47 +08:00
|
|
|
else
|
2021-11-09 23:09:12 +08:00
|
|
|
create_or_update_user_assignment(child_association)
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
2021-11-09 23:09:12 +08:00
|
|
|
|
|
|
|
sync_resource_user_associations(child_association)
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
2021-12-07 00:33:50 +08:00
|
|
|
|
|
|
|
destroy_or_update_user_assignment(resource) if resource.is_a?(Project) && @destroy
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create_or_update_user_assignment(object)
|
|
|
|
user_assignment = UserAssignment.find_or_initialize_by(user: @user, assignable: object)
|
2021-06-12 15:36:41 +08:00
|
|
|
return if user_assignment.manually_assigned?
|
|
|
|
|
2021-06-06 15:54:47 +08:00
|
|
|
user_assignment.user_role = @user_role
|
|
|
|
user_assignment.assigned_by = @assigned_by
|
2021-06-12 15:36:41 +08:00
|
|
|
user_assignment.assigned = :automatically
|
2021-06-06 15:54:47 +08:00
|
|
|
user_assignment.save!
|
|
|
|
end
|
|
|
|
|
2021-12-03 23:26:19 +08:00
|
|
|
def destroy_or_update_user_assignment(object)
|
2021-11-10 21:45:24 +08:00
|
|
|
# also destroy user designations if it's a MyModule
|
|
|
|
object.user_my_modules.where(user: @user).destroy_all if object.is_a?(MyModule)
|
|
|
|
|
2021-12-03 23:26:19 +08:00
|
|
|
user_assignment = object.user_assignments.find { |ua| ua.user_id == @user.id }
|
|
|
|
return if user_assignment.blank?
|
2021-12-01 23:23:24 +08:00
|
|
|
|
2023-09-05 17:36:26 +08:00
|
|
|
project = object.is_a?(Project) ? object : object.project
|
|
|
|
|
|
|
|
if project.visible? && !@remove_from_team
|
2021-12-01 23:23:24 +08:00
|
|
|
# if project is public, the assignment
|
|
|
|
# will reset to the default public role
|
|
|
|
|
|
|
|
user_assignment.update!(
|
2023-09-05 17:36:26 +08:00
|
|
|
user_role_id: project.default_public_user_role_id,
|
2021-12-01 23:23:24 +08:00
|
|
|
assigned: :automatically,
|
|
|
|
assigned_by: @assigned_by
|
|
|
|
)
|
|
|
|
else
|
|
|
|
user_assignment.destroy!
|
|
|
|
end
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|