Welcome to mirror list, hosted at ThFree Co, Russian Federation.

assignees.rb « callbacks « work_items « services « app - gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 14755ff0b462b1c475c996405b720326a88b979c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# frozen_string_literal: true

module WorkItems
  module Callbacks
    class Assignees < Base
      def before_update
        params[:assignee_ids] = [] if excluded_in_new_type?

        return unless params.present? && params.has_key?(:assignee_ids)
        return unless has_permission?(:set_work_item_metadata)

        assignee_ids = filter_assignees_count(params[:assignee_ids])
        assignee_ids = filter_assignee_permissions(assignee_ids)

        return if assignee_ids.sort == work_item.assignee_ids.sort

        work_item.assignee_ids = assignee_ids
        work_item.touch
      end

      private

      def filter_assignees_count(assignee_ids)
        return assignee_ids if work_item.allows_multiple_assignees?

        assignee_ids.first(1)
      end

      def filter_assignee_permissions(assignee_ids)
        assignees = User.id_in(assignee_ids)

        assignees.select { |assignee| assignee.can?(:read_work_item, work_item) }.map(&:id)
      end
    end
  end
end