2018-08-11 03:00:39 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-07-16 14:30:17 -04:00
|
|
|
module Todos
|
|
|
|
module Destroy
|
|
|
|
class EntityLeaveService < ::Todos::Destroy::BaseService
|
|
|
|
extend ::Gitlab::Utils::Override
|
|
|
|
|
2018-08-06 04:42:51 -04:00
|
|
|
attr_reader :user, :entity
|
2018-07-16 14:30:17 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
def initialize(user_id, entity_id, entity_type)
|
|
|
|
unless %w(Group Project).include?(entity_type)
|
|
|
|
raise ArgumentError.new("#{entity_type} is not an entity user can leave")
|
|
|
|
end
|
|
|
|
|
2018-08-06 04:42:51 -04:00
|
|
|
@user = User.find_by(id: user_id)
|
2018-07-16 14:30:17 -04:00
|
|
|
@entity = entity_type.constantize.find_by(id: entity_id)
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
|
2018-08-06 04:42:51 -04:00
|
|
|
def execute
|
|
|
|
return unless entity && user
|
|
|
|
|
|
|
|
# if at least reporter, all entities including confidential issues can be accessed
|
2018-08-06 05:05:44 -04:00
|
|
|
return if user_has_reporter_access?
|
2018-08-06 04:42:51 -04:00
|
|
|
|
|
|
|
remove_confidential_issue_todos
|
2018-07-16 14:30:17 -04:00
|
|
|
|
|
|
|
if entity.private?
|
2018-08-06 04:42:51 -04:00
|
|
|
remove_project_todos
|
|
|
|
remove_group_todos
|
2018-07-16 14:30:17 -04:00
|
|
|
else
|
2018-08-06 04:42:51 -04:00
|
|
|
enqueue_private_features_worker
|
|
|
|
end
|
|
|
|
end
|
2018-07-27 05:28:17 -04:00
|
|
|
|
2018-08-06 04:42:51 -04:00
|
|
|
private
|
|
|
|
|
|
|
|
def enqueue_private_features_worker
|
2019-04-29 09:42:16 -04:00
|
|
|
projects.each do |project|
|
|
|
|
TodosDestroyer::PrivateFeaturesWorker.perform_async(project.id, user.id)
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def remove_confidential_issue_todos
|
|
|
|
Todo.where(
|
2018-11-17 10:14:36 -05:00
|
|
|
target_id: confidential_issues.select(:id), target_type: Issue.name, user_id: user.id
|
2018-08-06 04:42:51 -04:00
|
|
|
).delete_all
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def remove_project_todos
|
|
|
|
Todo.where(project_id: non_authorized_projects, user_id: user.id).delete_all
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def remove_group_todos
|
|
|
|
Todo.where(group_id: non_authorized_groups, user_id: user.id).delete_all
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2019-04-29 09:42:16 -04:00
|
|
|
def projects
|
2018-08-06 04:42:51 -04:00
|
|
|
condition = case entity
|
|
|
|
when Project
|
|
|
|
{ id: entity.id }
|
|
|
|
when Namespace
|
|
|
|
{ namespace_id: non_member_groups }
|
|
|
|
end
|
|
|
|
|
2019-04-29 09:42:16 -04:00
|
|
|
Project.where(condition)
|
2018-08-02 10:16:58 -04:00
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-02 10:16:58 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def non_authorized_projects
|
2019-04-29 09:42:16 -04:00
|
|
|
projects.where('id NOT IN (?)', user.authorized_projects.select(: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
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def non_authorized_groups
|
|
|
|
return [] unless entity.is_a?(Namespace)
|
|
|
|
|
|
|
|
entity.self_and_descendants.select(:id)
|
|
|
|
.where('id NOT IN (?)', GroupsFinder.new(user).execute.select(:id))
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
def non_member_groups
|
|
|
|
entity.self_and_descendants.select(:id)
|
|
|
|
.where('id NOT IN (?)', user.membership_groups.select(:id))
|
|
|
|
end
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-08-06 04:42:51 -04:00
|
|
|
|
2018-08-06 05:05:44 -04:00
|
|
|
def user_has_reporter_access?
|
2018-08-06 04:42:51 -04:00
|
|
|
return unless entity.is_a?(Namespace)
|
|
|
|
|
|
|
|
entity.member?(User.find(user.id), Gitlab::Access::REPORTER)
|
2018-07-16 14:30:17 -04:00
|
|
|
end
|
|
|
|
|
2018-08-27 11:31:01 -04:00
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-07-16 14:30:17 -04:00
|
|
|
def confidential_issues
|
2018-08-06 04:42:51 -04:00
|
|
|
assigned_ids = IssueAssignee.select(:issue_id).where(user_id: user.id)
|
|
|
|
authorized_reporter_projects = user
|
|
|
|
.authorized_projects(Gitlab::Access::REPORTER).select(:id)
|
2018-07-26 10:53:50 -04:00
|
|
|
|
2019-04-29 09:42:16 -04:00
|
|
|
Issue.where(project_id: projects, confidential: true)
|
2018-08-06 04:42:51 -04:00
|
|
|
.where('project_id NOT IN(?)', authorized_reporter_projects)
|
|
|
|
.where('author_id != ?', user.id)
|
2018-07-26 10:53:50 -04:00
|
|
|
.where('id NOT IN (?)', assigned_ids)
|
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
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|