Fix error when deleting a user who has projects

Closes #1856
Closes https://github.com/gitlabhq/gitlabhq/issues/9394
This commit is contained in:
Stan Hu 2015-06-22 15:08:02 -07:00
parent 883438970d
commit e80d7a804f
6 changed files with 34 additions and 3 deletions

View File

@ -1,6 +1,7 @@
Please view this file on the master branch, on stable branches it's out of date.
v 7.13.0 (unreleased)
- Fix error when deleting a user who has projects (Stan Hu)
- Update maintenance documentation to explain no need to recompile asssets for omnibus installations (Stan Hu)
- Support commenting on diffs in side-by-side mode (Stan Hu)
- Fix JavaScript error when clicking on the comment button on a diff line that has a comment already (Stan Hu)

View File

@ -86,7 +86,7 @@ class Admin::UsersController < Admin::ApplicationController
end
def destroy
DeleteUserService.new.execute(user)
DeleteUserService.new(current_user).execute(user)
respond_to do |format|
format.html { redirect_to admin_users_path }

View File

@ -6,7 +6,7 @@ class RegistrationsController < Devise::RegistrationsController
end
def destroy
DeleteUserService.new.execute(current_user)
DeleteUserService.new(current_user).execute(current_user)
respond_to do |format|
format.html { redirect_to new_user_session_path, notice: "Account successfully removed." }

View File

@ -1,4 +1,10 @@
class DeleteUserService
attr_accessor :current_user
def initialize(current_user)
@current_user = current_user
end
def execute(user)
if user.solo_owned_groups.present?
user.errors[:base] << 'You must transfer ownership or delete groups before you can remove user'

View File

@ -194,7 +194,7 @@ module API
user = User.find_by(id: params[:id])
if user
DeleteUserService.new.execute(user)
DeleteUserService.new(current_user).execute(user)
else
not_found!('User')
end

View File

@ -0,0 +1,24 @@
require 'spec_helper'
describe Admin::UsersController do
let(:admin) { create(:admin) }
before do
sign_in(admin)
end
describe 'DELETE #user with projects' do
let(:user) { create(:user) }
let(:project) { create(:project, namespace: user.namespace) }
before do
project.team << [user, :developer]
end
it 'deletes user' do
delete :destroy, id: user.username, format: :json
expect(response.status).to eq(200)
expect { User.find(user.id) }.to raise_exception(ActiveRecord::RecordNotFound)
end
end
end