2021-06-06 15:54:47 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
module UserAssignments
|
|
|
|
class PropagateAssignmentJob < ApplicationJob
|
|
|
|
queue_as :high_priority
|
|
|
|
|
2021-11-09 23:09:12 +08:00
|
|
|
def perform(resource, user, user_role, assigned_by, destroy: false)
|
2021-06-06 15:54:47 +08:00
|
|
|
@user = user
|
|
|
|
@user_role = user_role
|
|
|
|
@assigned_by = assigned_by
|
|
|
|
@destroy = destroy
|
|
|
|
|
|
|
|
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)
|
|
|
|
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-11-09 23:09:12 +08:00
|
|
|
destroy_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
|
|
|
|
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
|
|
|
|
|
|
|
|
def destroy_user_assignment(object)
|
2021-06-06 17:46:16 +08:00
|
|
|
UserAssignment.where(user: @user, assignable: object).destroy_all
|
2021-06-06 15:54:47 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|