Commit 01329d6a authored by Vasilii Iakliushin's avatar Vasilii Iakliushin Committed by Paul Slaughter

Show disabled fork button for user without enough permissions

Contributes to https://gitlab.com/gitlab-org/gitlab/-/issues/259693

**Problem**

We currently hide both the fork button and the fork count on
project's page when the user cannot fork the project. It is not
aligned with other pages where the user can see the fork count number.

**Solution**

* Mark fork button as disabled
* Show tooltip with an explanation

Changelog: changed
parent f290a6b5
......@@ -405,6 +405,16 @@ module ProjectsHelper
project.path_with_namespace
end
def fork_button_disabled_tooltip(project)
return unless current_user
if !current_user.can?(:fork_project, project)
s_("ProjectOverview|You don't have permission to fork this project")
elsif !current_user.can?(:create_fork)
s_('ProjectOverview|You have reached your project limit')
end
end
private
def tab_ability_map
......
- unless @project.empty_repo?
- if current_user && can?(current_user, :fork_project, @project)
- if current_user
.count-badge.btn-group
- if current_user.already_forked?(@project) && current_user.manageable_namespaces.size < 2
= link_to namespace_project_path(current_user, current_user.fork_of(@project)), title: s_('ProjectOverview|Go to your fork'), class: 'gl-button btn btn-default btn-sm has-tooltip fork-btn' do
= sprite_icon('fork', css_class: 'icon')
%span= s_('ProjectOverview|Fork')
- else
- can_create_fork = current_user.can?(:create_fork)
- disabled_fork_tooltip = s_('ProjectOverview|You have reached your project limit')
%span.btn-group.has-tooltip{ title: (disabled_fork_tooltip unless can_create_fork) }
= link_to new_project_fork_path(@project), class: "gl-button btn btn-default btn-sm fork-btn #{' disabled' unless can_create_fork }", 'aria-label' => (disabled_fork_tooltip unless can_create_fork) do
- disabled_tooltip = fork_button_disabled_tooltip(@project)
- count_class = 'disabled' unless can?(current_user, :download_code, @project)
- button_class = 'disabled' if disabled_tooltip
%span.btn-group{ class: ('has-tooltip' if disabled_tooltip), title: disabled_tooltip }
= link_to new_project_fork_path(@project), class: "gl-button btn btn-default btn-sm fork-btn #{button_class}" do
= sprite_icon('fork', css_class: 'icon')
%span= s_('ProjectOverview|Fork')
= link_to project_forks_path(@project), title: n_(s_('ProjectOverview|Forks'), s_('ProjectOverview|Forks'), @project.forks_count), class: 'gl-button btn btn-default btn-sm count has-tooltip' do
= link_to project_forks_path(@project), title: n_(s_('ProjectOverview|Forks'), s_('ProjectOverview|Forks'), @project.forks_count), class: "gl-button btn btn-default btn-sm count has-tooltip #{count_class}" do
= @project.forks_count
......@@ -27018,6 +27018,9 @@ msgstr ""
msgid "ProjectOverview|Unstar"
msgstr ""
msgid "ProjectOverview|You don't have permission to fork this project"
msgstr ""
msgid "ProjectOverview|You have reached your project limit"
msgstr ""
......
......@@ -59,10 +59,11 @@ RSpec.describe 'Project fork' do
context 'forking is disabled' do
let(:forking_access_level) { ProjectFeature::DISABLED }
it 'does not render fork button' do
it 'render a disabled fork button' do
visit project_path(project)
expect(page).not_to have_css('a', text: 'Fork')
expect(page).to have_css('a.disabled', text: 'Fork')
expect(page).to have_css('a.count', text: '0')
end
it 'does not render new project fork page' do
......@@ -80,10 +81,11 @@ RSpec.describe 'Project fork' do
end
context 'user is not a team member' do
it 'does not render fork button' do
it 'render a disabled fork button' do
visit project_path(project)
expect(page).not_to have_css('a', text: 'Fork')
expect(page).to have_css('a.disabled', text: 'Fork')
expect(page).to have_css('a.count', text: '0')
end
it 'does not render new project fork page' do
......@@ -102,6 +104,7 @@ RSpec.describe 'Project fork' do
visit project_path(project)
expect(page).to have_css('a', text: 'Fork')
expect(page).to have_css('a.count', text: '0')
expect(page).not_to have_css('a.disabled', text: 'Fork')
end
......
......@@ -991,4 +991,31 @@ RSpec.describe ProjectsHelper do
expect(subject).to eq(project.path_with_namespace)
end
end
describe '#fork_button_disabled_tooltip' do
using RSpec::Parameterized::TableSyntax
subject { helper.fork_button_disabled_tooltip(project) }
where(:has_user, :can_fork_project, :can_create_fork, :expected) do
false | false | false | nil
true | true | true | nil
true | false | true | 'You don\'t have permission to fork this project'
true | true | false | 'You have reached your project limit'
end
with_them do
before do
current_user = user if has_user
allow(helper).to receive(:current_user).and_return(current_user)
allow(user).to receive(:can?).with(:fork_project, project).and_return(can_fork_project)
allow(user).to receive(:can?).with(:create_fork).and_return(can_create_fork)
end
it 'returns tooltip text when user lacks privilege' do
expect(subject).to eq(expected)
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