diff options
author | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
---|---|---|
committer | GitLab Bot <gitlab-bot@gitlab.com> | 2020-07-20 15:26:25 +0300 |
commit | a09983ae35713f5a2bbb100981116d31ce99826e (patch) | |
tree | 2ee2af7bd104d57086db360a7e6d8c9d5d43667a /app/services/members | |
parent | 18c5ab32b738c0b6ecb4d0df3994000482f34bd8 (diff) |
Add latest changes from gitlab-org/gitlab@13-2-stable-ee
Diffstat (limited to 'app/services/members')
-rw-r--r-- | app/services/members/create_service.rb | 2 | ||||
-rw-r--r-- | app/services/members/destroy_service.rb | 29 | ||||
-rw-r--r-- | app/services/members/unassign_issuables_service.rb | 23 |
3 files changed, 51 insertions, 3 deletions
diff --git a/app/services/members/create_service.rb b/app/services/members/create_service.rb index 0b729981a93..610288c5e76 100644 --- a/app/services/members/create_service.rb +++ b/app/services/members/create_service.rb @@ -22,7 +22,7 @@ module Members errors = [] members.each do |member| - if member.errors.any? + if member.invalid? current_error = # Invited users may not have an associated user if member.user.present? diff --git a/app/services/members/destroy_service.rb b/app/services/members/destroy_service.rb index 20f64a99ad7..fdd43260521 100644 --- a/app/services/members/destroy_service.rb +++ b/app/services/members/destroy_service.rb @@ -2,8 +2,8 @@ module Members class DestroyService < Members::BaseService - def execute(member, skip_authorization: false, skip_subresources: false) - raise Gitlab::Access::AccessDeniedError unless skip_authorization || can_destroy_member?(member) + def execute(member, skip_authorization: false, skip_subresources: false, unassign_issuables: false, destroy_bot: false) + raise Gitlab::Access::AccessDeniedError unless skip_authorization || authorized?(member, destroy_bot) @skip_auth = skip_authorization @@ -19,6 +19,7 @@ module Members delete_subresources(member) unless skip_subresources enqueue_delete_todos(member) + enqueue_unassign_issuables(member) if unassign_issuables after_execute(member: member) @@ -27,6 +28,12 @@ module Members private + def authorized?(member, destroy_bot) + return can_destroy_bot_member?(member) if destroy_bot + + can_destroy_member?(member) + end + def delete_subresources(member) return unless member.is_a?(GroupMember) && member.user && member.group @@ -54,6 +61,10 @@ module Members can?(current_user, destroy_member_permission(member), member) end + def can_destroy_bot_member?(member) + can?(current_user, destroy_bot_member_permission(member), member) + end + def destroy_member_permission(member) case member when GroupMember @@ -64,6 +75,20 @@ module Members raise "Unknown member type: #{member}!" end end + + def destroy_bot_member_permission(member) + raise "Unsupported bot member type: #{member}" unless member.is_a?(ProjectMember) + + :destroy_project_bot_member + end + + def enqueue_unassign_issuables(member) + source_type = member.is_a?(GroupMember) ? 'Group' : 'Project' + + member.run_after_commit_or_now do + MembersDestroyer::UnassignIssuablesWorker.perform_async(member.user_id, member.source_id, source_type) + end + end end end diff --git a/app/services/members/unassign_issuables_service.rb b/app/services/members/unassign_issuables_service.rb new file mode 100644 index 00000000000..95e07deb761 --- /dev/null +++ b/app/services/members/unassign_issuables_service.rb @@ -0,0 +1,23 @@ +# frozen_string_literal: true + +module Members + class UnassignIssuablesService + attr_reader :user, :entity + + def initialize(user, entity) + @user = user + @entity = entity + end + + def execute + return unless entity && user + + project_ids = entity.is_a?(Group) ? entity.all_projects.select(:id) : [entity.id] + + user.issue_assignees.on_issues(Issue.in_projects(project_ids).select(:id)).delete_all + user.merge_request_assignees.in_projects(project_ids).delete_all + + user.invalidate_cache_counts + end + end +end |