2016-08-04 17:34:57 -04:00
|
|
|
module Members
|
|
|
|
class AuthorizedDestroyService < BaseService
|
|
|
|
attr_accessor :member, :user
|
|
|
|
|
|
|
|
def initialize(member, user = nil)
|
|
|
|
@member, @user = member, user
|
|
|
|
end
|
|
|
|
|
|
|
|
def execute
|
2016-08-18 16:32:42 -04:00
|
|
|
return false if member.is_a?(GroupMember) && member.source.last_owner?(member.user)
|
|
|
|
|
2017-04-17 05:27:00 -04:00
|
|
|
Member.transaction do
|
2017-05-18 09:18:26 -04:00
|
|
|
unassign_issues_and_merge_requests(member) unless member.invite?
|
2017-04-17 05:27:00 -04:00
|
|
|
|
|
|
|
member.destroy
|
|
|
|
end
|
2016-08-04 17:34:57 -04:00
|
|
|
|
|
|
|
if member.request? && member.user != user
|
|
|
|
notification_service.decline_access_request(member)
|
|
|
|
end
|
2016-09-09 12:51:31 -04:00
|
|
|
|
|
|
|
member
|
2016-08-04 17:34:57 -04:00
|
|
|
end
|
2017-04-17 05:27:00 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def unassign_issues_and_merge_requests(member)
|
|
|
|
if member.is_a?(GroupMember)
|
2017-05-17 05:37:37 -04:00
|
|
|
issues = Issue.unscoped.select(1).
|
|
|
|
joins(:project).
|
|
|
|
where('issues.id = issue_assignees.issue_id AND projects.namespace_id = ?', member.source_id)
|
2017-05-05 06:41:35 -04:00
|
|
|
|
2017-05-17 05:37:37 -04:00
|
|
|
# DELETE FROM issue_assignees WHERE user_id = X AND EXISTS (...)
|
|
|
|
IssueAssignee.unscoped.
|
|
|
|
where('user_id = :user_id AND EXISTS (:sub)', user_id: member.user_id, sub: issues).
|
|
|
|
delete_all
|
2017-05-05 06:41:35 -04:00
|
|
|
|
2017-04-17 05:27:00 -04:00
|
|
|
MergeRequestsFinder.new(user, group_id: member.source_id, assignee_id: member.user_id).
|
|
|
|
execute.
|
|
|
|
update_all(assignee_id: nil)
|
|
|
|
else
|
|
|
|
project = member.source
|
2017-05-05 06:41:35 -04:00
|
|
|
|
2017-05-16 23:17:02 -04:00
|
|
|
# SELECT 1 FROM issues WHERE issues.id = issue_assignees.issue_id AND issues.project_id = X
|
|
|
|
issues = Issue.unscoped.select(1).
|
|
|
|
where('issues.id = issue_assignees.issue_id').
|
|
|
|
where(project_id: project.id)
|
|
|
|
|
|
|
|
# DELETE FROM issue_assignees WHERE user_id = X AND EXISTS (...)
|
|
|
|
IssueAssignee.unscoped.
|
|
|
|
where('user_id = :user_id AND EXISTS (:sub)', user_id: member.user_id, sub: issues).
|
|
|
|
delete_all
|
2017-05-05 06:41:35 -04:00
|
|
|
|
2017-04-17 05:27:00 -04:00
|
|
|
project.merge_requests.opened.assigned_to(member.user).update_all(assignee_id: nil)
|
|
|
|
end
|
2017-05-10 16:54:10 -04:00
|
|
|
|
|
|
|
member.user.invalidate_cache_counts
|
2017-04-17 05:27:00 -04:00
|
|
|
end
|
2016-08-04 17:34:57 -04:00
|
|
|
end
|
|
|
|
end
|