2018-08-11 03:00:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-16 14:30:17 -04:00
|
|
|
module Todos
|
|
|
|
module Destroy
|
2019-05-21 15:20:27 -04:00
|
|
|
# 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.
|
2018-07-16 14:30:17 -04:00
|
|
|
class ConfidentialIssueService < ::Todos::Destroy::BaseService
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2019-05-21 15:20:27 -04:00
|
|
|
attr_reader :issues
|
2018-07-16 14:30:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-05-21 15:20:27 -04:00
|
|
|
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
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-05-21 15:20:27 -04:00
|
|
|
def project_confidential_issues(project_id)
|
|
|
|
project = Project.find(project_id)
|
|
|
|
|
|
|
|
project.issues.confidential_only
|
|
|
|
end
|
|
|
|
|
2018-07-16 14:30:17 -04:00
|
|
|
override :todos
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
def todos
|
2019-05-21 15:20:27 -04:00
|
|
|
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')
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
|
|
|
|
override :todos_to_remove?
|
|
|
|
def todos_to_remove?
|
2019-05-21 15:20:27 -04:00
|
|
|
issues&.any?(&:confidential?)
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
override :project_ids
|
|
|
|
def project_ids
|
2019-05-21 15:20:27 -04:00
|
|
|
issues&.distinct&.select(:project_id)
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
2018-07-26 10:53:50 -04:00
|
|
|
|
|
|
|
override :authorized_users
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-26 10:53:50 -04:00
|
|
|
def authorized_users
|
|
|
|
ProjectAuthorization.select(:user_id)
|
|
|
|
.where(project_id: project_ids)
|
|
|
|
.where('access_level >= ?', Gitlab::Access::REPORTER)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|