Commit 7bc803a6 authored by Max Woolf's avatar Max Woolf

Merge branch '27843_update_transfer_error_message' into 'master'

Improve error message for TransferService

See merge request gitlab-org/gitlab!68536
parents 78768457 21b9cf49
...@@ -20,8 +20,16 @@ module Projects ...@@ -20,8 +20,16 @@ module Projects
raise TransferError, s_('TransferProject|Please select a new namespace for your project.') raise TransferError, s_('TransferProject|Please select a new namespace for your project.')
end end
unless allowed_transfer?(current_user, project) if @new_namespace.id == project.namespace_id
raise TransferError, s_('TransferProject|Transfer failed, please contact an admin.') raise TransferError, s_('TransferProject|Project is already in this namespace.')
end
unless allowed_transfer_project?(current_user, project)
raise TransferError, s_("TransferProject|You don't have permission to transfer this project.")
end
unless allowed_to_transfer_to_namespace?(current_user, @new_namespace)
raise TransferError, s_("TransferProject|You don't have permission to transfer projects into that namespace.")
end end
transfer(project) transfer(project)
...@@ -121,11 +129,12 @@ module Projects ...@@ -121,11 +129,12 @@ module Projects
Milestones::TransferService.new(current_user, group, project).execute Milestones::TransferService.new(current_user, group, project).execute
end end
def allowed_transfer?(current_user, project) def allowed_transfer_project?(current_user, project)
@new_namespace && current_user.can?(:change_namespace, project)
can?(current_user, :change_namespace, project) && end
@new_namespace.id != project.namespace_id &&
current_user.can?(:transfer_projects, @new_namespace) def allowed_to_transfer_to_namespace?(current_user, namespace)
current_user.can?(:transfer_projects, namespace)
end end
def update_namespace_and_visibility(to_namespace) def update_namespace_and_visibility(to_namespace)
......
...@@ -35204,13 +35204,19 @@ msgstr "" ...@@ -35204,13 +35204,19 @@ msgstr ""
msgid "TransferProject|Project cannot be transferred, because tags are present in its container registry" msgid "TransferProject|Project cannot be transferred, because tags are present in its container registry"
msgstr "" msgstr ""
msgid "TransferProject|Project is already in this namespace."
msgstr ""
msgid "TransferProject|Project with same name or path in target namespace already exists" msgid "TransferProject|Project with same name or path in target namespace already exists"
msgstr "" msgstr ""
msgid "TransferProject|Root namespace can't be updated if project has NPM packages" msgid "TransferProject|Root namespace can't be updated if project has NPM packages"
msgstr "" msgstr ""
msgid "TransferProject|Transfer failed, please contact an admin." msgid "TransferProject|You don't have permission to transfer projects into that namespace."
msgstr ""
msgid "TransferProject|You don't have permission to transfer this project."
msgstr "" msgstr ""
msgid "Tree view" msgid "Tree view"
......
...@@ -292,10 +292,37 @@ RSpec.describe Projects::TransferService do ...@@ -292,10 +292,37 @@ RSpec.describe Projects::TransferService do
end end
end end
context 'target namespace allows developers to create projects' do context 'target namespace matches current namespace' do
let(:group) { user.namespace }
it 'does not allow project transfer' do
transfer_result = execute_transfer
expect(transfer_result).to eq false
expect(project.namespace).to eq(user.namespace)
expect(project.errors[:new_namespace]).to include('Project is already in this namespace.')
end
end
context 'when user does not own the project' do
let(:project) { create(:project, :repository, :legacy_storage) }
before do
project.add_developer(user)
end
it 'does not allow project transfer to the target namespace' do
transfer_result = execute_transfer
expect(transfer_result).to eq false
expect(project.errors[:new_namespace]).to include("You don't have permission to transfer this project.")
end
end
context 'when user can create projects in the target namespace' do
let(:group) { create(:group, project_creation_level: ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS) } let(:group) { create(:group, project_creation_level: ::Gitlab::Access::DEVELOPER_MAINTAINER_PROJECT_ACCESS) }
context 'the user is a member of the target namespace with developer permissions' do context 'but has only developer permissions in the target namespace' do
before do before do
group.add_developer(user) group.add_developer(user)
end end
...@@ -305,7 +332,7 @@ RSpec.describe Projects::TransferService do ...@@ -305,7 +332,7 @@ RSpec.describe Projects::TransferService do
expect(transfer_result).to eq false expect(transfer_result).to eq false
expect(project.namespace).to eq(user.namespace) expect(project.namespace).to eq(user.namespace)
expect(project.errors[:new_namespace]).to include('Transfer failed, please contact an admin.') expect(project.errors[:new_namespace]).to include("You don't have permission to transfer projects into that namespace.")
end end
end end
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment