2021-02-04 21:08:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class UserAssignment < ApplicationRecord
|
2022-06-24 16:44:28 +08:00
|
|
|
before_validation -> { self.team ||= (assignable.is_a?(Team) ? assignable : assignable.team) }
|
2022-06-28 19:13:36 +08:00
|
|
|
after_create :assign_shared_inventories, if: -> { assignable.is_a?(Team) }
|
2022-10-06 04:40:46 +08:00
|
|
|
after_create :assign_public_projects, if: -> { assignable.is_a?(Team) }
|
2022-10-10 21:27:42 +08:00
|
|
|
after_update :update_team_children_assignments, if: -> { assignable.is_a?(Team) && saved_change_to_user_role_id? }
|
2022-06-28 19:13:36 +08:00
|
|
|
before_destroy :unassign_shared_inventories, if: -> { assignable.is_a?(Team) }
|
2022-10-10 21:27:42 +08:00
|
|
|
before_destroy :unassign_team_child_objects, if: -> { assignable.is_a?(Team) }
|
2022-06-24 16:44:28 +08:00
|
|
|
|
2021-10-19 22:00:52 +08:00
|
|
|
belongs_to :assignable, polymorphic: true, touch: true
|
2021-02-04 21:08:20 +08:00
|
|
|
belongs_to :user_role
|
|
|
|
belongs_to :user
|
2022-06-24 16:44:28 +08:00
|
|
|
belongs_to :team
|
2021-10-05 19:45:52 +08:00
|
|
|
belongs_to :assigned_by, class_name: 'User', optional: true
|
2021-06-12 15:36:41 +08:00
|
|
|
|
|
|
|
enum assigned: { automatically: 0, manually: 1 }, _suffix: true
|
2021-11-22 17:47:20 +08:00
|
|
|
|
2022-06-24 16:44:28 +08:00
|
|
|
validates :user, uniqueness: { scope: %i(assignable team_id) }
|
2022-06-28 19:13:36 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def assign_shared_inventories
|
2022-06-29 21:22:22 +08:00
|
|
|
viewer_role = UserRole.find_by(name: UserRole.public_send('viewer_role').name)
|
|
|
|
normal_user_role = UserRole.find_by(name: UserRole.public_send('normal_user_role').name)
|
|
|
|
|
|
|
|
assignable.team_shared_repositories.find_each do |team_shared_repository|
|
|
|
|
assignable.repository_sharing_user_assignments.create!(
|
|
|
|
user: user,
|
|
|
|
user_role: team_shared_repository.shared_write? ? normal_user_role : viewer_role,
|
|
|
|
assignable: team_shared_repository.shared_object
|
|
|
|
)
|
2022-06-28 19:13:36 +08:00
|
|
|
end
|
2022-07-08 04:31:07 +08:00
|
|
|
|
|
|
|
Repository.globally_shared.find_each do |repository|
|
|
|
|
assignable.repository_sharing_user_assignments.create!(
|
|
|
|
user: user,
|
|
|
|
user_role: repository.shared_write? ? normal_user_role : viewer_role,
|
|
|
|
assignable: repository
|
|
|
|
)
|
|
|
|
end
|
2022-06-28 19:13:36 +08:00
|
|
|
end
|
|
|
|
|
2022-10-06 04:40:46 +08:00
|
|
|
def assign_public_projects
|
|
|
|
assignable.projects.visible.find_each do |project|
|
|
|
|
UserAssignments::GroupAssignmentJob.perform_later(
|
|
|
|
assignable,
|
|
|
|
project,
|
|
|
|
assigned_by
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2022-10-10 21:27:42 +08:00
|
|
|
def update_team_children_assignments
|
|
|
|
UserAssignments::UpdateTeamUserAssignmentService.new(user, assignable, user_role).call
|
|
|
|
end
|
|
|
|
|
2022-06-28 19:13:36 +08:00
|
|
|
def unassign_shared_inventories
|
|
|
|
assignable.repository_sharing_user_assignments.where(user: user).find_each(&:destroy!)
|
|
|
|
end
|
2022-10-06 04:40:46 +08:00
|
|
|
|
2022-10-10 21:27:42 +08:00
|
|
|
def unassign_team_child_objects
|
|
|
|
UserAssignments::RemoveTeamUserAssignmentService.new(user, assignable).call
|
2022-10-06 04:40:46 +08:00
|
|
|
end
|
2021-02-04 21:08:20 +08:00
|
|
|
end
|