diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-03 21:11:11 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2021-12-03 21:11:11 +0300 |
commit | 5b0916450cebb6a88bd651fd0579737541c355c8 (patch) | |
tree | 3a788fd8e2ef18b1c6e06d222e84bb5d84307870 /app/services/todos | |
parent | 78aaabb87f180e93fa53aba407a0eef1e12a7a56 (diff) |
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'app/services/todos')
-rw-r--r-- | app/services/todos/destroy/private_features_service.rb | 46 | ||||
-rw-r--r-- | app/services/todos/destroy/unauthorized_features_service.rb | 43 |
2 files changed, 43 insertions, 46 deletions
diff --git a/app/services/todos/destroy/private_features_service.rb b/app/services/todos/destroy/private_features_service.rb deleted file mode 100644 index 44c3ff231f8..00000000000 --- a/app/services/todos/destroy/private_features_service.rb +++ /dev/null @@ -1,46 +0,0 @@ -# frozen_string_literal: true - -module Todos - module Destroy - class PrivateFeaturesService < ::Todos::Destroy::BaseService - attr_reader :project_ids, :user_id - - def initialize(project_ids, user_id = nil) - @project_ids = project_ids - @user_id = user_id - end - - # rubocop: disable CodeReuse/ActiveRecord - def execute - ProjectFeature.where(project_id: project_ids).each do |project_features| - target_types = [] - target_types << Issue.name if private?(project_features.issues_access_level) - target_types << MergeRequest.name if private?(project_features.merge_requests_access_level) - target_types << Commit.name if private?(project_features.repository_access_level) - - next if target_types.empty? - - remove_todos(project_features.project_id, target_types) - end - end - # rubocop: enable CodeReuse/ActiveRecord - - private - - def private?(feature_level) - feature_level == ProjectFeature::PRIVATE - end - - # rubocop: disable CodeReuse/ActiveRecord - def remove_todos(project_id, target_types) - items = Todo.where(project_id: project_id) - items = items.where(user_id: user_id) if user_id - - items.where.not(user_id: authorized_users) - .where(target_type: target_types) - .delete_all - end - # rubocop: enable CodeReuse/ActiveRecord - end - end -end diff --git a/app/services/todos/destroy/unauthorized_features_service.rb b/app/services/todos/destroy/unauthorized_features_service.rb new file mode 100644 index 00000000000..513def10575 --- /dev/null +++ b/app/services/todos/destroy/unauthorized_features_service.rb @@ -0,0 +1,43 @@ +# frozen_string_literal: true + +module Todos + module Destroy + class UnauthorizedFeaturesService < ::Todos::Destroy::BaseService + attr_reader :project_id, :user_id + + BATCH_SIZE = 1000 + + def initialize(project_id, user_id = nil) + @project_id = project_id + @user_id = user_id + end + + # rubocop: disable CodeReuse/ActiveRecord + def execute + return if user_id && authorized_users.where(user_id: user_id).exists? + + related_todos.each_batch(of: BATCH_SIZE) do |batch| + pending_delete = without_authorized(batch).includes(:target, :user).reject do |todo| + Ability.allowed?(todo.user, :read_todo, todo, scope: :user) + end + Todo.where(id: pending_delete).delete_all if pending_delete.present? + end + end + # rubocop: enable CodeReuse/ActiveRecord + + private + + def related_todos + base_scope = Todo.for_project(project_id) + base_scope = base_scope.for_user(user_id) if user_id + base_scope + end + + # Compatibility for #authorized_users in this class we always work + # with 1 project for queries efficiency + def project_ids + [project_id] + end + end + end +end |