summaryrefslogtreecommitdiff
path: root/app/services/todos/destroy/confidential_issue_service.rb
blob: 6cdd8c1689402b4a9612e2b30004b43473ba8226 (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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# frozen_string_literal: true

module Todos
  module Destroy
    # Service class for deleting todos that belongs to confidential issues.
    # It deletes todos for users that are not at least reporters, issue author or assignee.
    #
    # Accepts issue_id or project_id as argument.
    # When issue_id is passed it deletes matching todos for one confidential issue.
    # When project_id is passed it deletes matching todos for all confidential issues of the project.
    class ConfidentialIssueService < ::Todos::Destroy::BaseService
      extend ::Gitlab::Utils::Override

      attr_reader :issues

      # rubocop: disable CodeReuse/ActiveRecord
      def initialize(issue_id: nil, project_id: nil)
        @issues =
          if issue_id
            Issue.where(id: issue_id)
          elsif project_id
            project_confidential_issues(project_id)
          end
      end
      # rubocop: enable CodeReuse/ActiveRecord

      private

      def project_confidential_issues(project_id)
        project = Project.find(project_id)

        project.issues.confidential_only
      end

      override :todos
      # rubocop: disable CodeReuse/ActiveRecord
      def todos
        Todo.joins_issue_and_assignees
          .where(target: issues)
          .where('issues.confidential = ?', true)
          .where('todos.user_id != issues.author_id')
          .where('todos.user_id != issue_assignees.user_id')
      end
      # rubocop: enable CodeReuse/ActiveRecord

      override :todos_to_remove?
      def todos_to_remove?
        issues&.any?(&:confidential?)
      end

      override :project_ids
      def project_ids
        issues&.distinct&.select(:project_id)
      end

      override :authorized_users
      # rubocop: disable CodeReuse/ActiveRecord
      def authorized_users
        ProjectAuthorization.select(:user_id)
          .where(project_id: project_ids)
          .where('access_level >= ?', Gitlab::Access::REPORTER)
      end
      # rubocop: enable CodeReuse/ActiveRecord
    end
  end
end