Skip to content

Commit 82e188e

Browse files
committed
Completely hide any CodeHarbor actions if disabled in global config
Previously, a button to configure a CodeHarbor link might have been displayed, even if globally disabled. Now, the UI elements are only shown if CodeHarbor is enabled.
1 parent 28d4ce0 commit 82e188e

File tree

5 files changed

+60
-29
lines changed

5 files changed

+60
-29
lines changed

app/policies/exercise_policy.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def detailed_statistics?
2222
end
2323

2424
%i[export_external_check? export_external_confirm?].each do |action|
25-
define_method(action) { (admin? || teacher_in_study_group? || author?) && @user.codeharbor_link }
25+
define_method(action) { Pundit.policy(@user, CodeharborLink).enabled? && (admin? || teacher_in_study_group? || author?) && @user.codeharbor_link }
2626
end
2727

2828
%i[implement? working_times? intervention? reload?].each do |action|

app/views/exercises/show.html.slim

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ ul.list-unstyled#files
6464
.clearfix = link_to(t('shared.destroy'), file, class: 'btn btn-warning btn-sm float-end', data: {confirm: t('shared.confirm_destroy')}, method: :delete)
6565
= render('shared/file', file:)
6666

67-
= render 'shared/modal',
68-
title: t('exercises.export_codeharbor.dialogtitle'),
69-
modal_root_attributes: {id: 'export-modal'},
70-
template: 'exercises/_export_dialogcontent'
67+
- if policy(@exercise).export_external_confirm?
68+
= render 'shared/modal',
69+
title: t('exercises.export_codeharbor.dialogtitle'),
70+
modal_root_attributes: {id: 'export-modal'},
71+
template: 'exercises/_export_dialogcontent'

app/views/external_users/show.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ h1 = @user.displayname
3232
= t('users.show.no_groups')
3333

3434
- if @user == current_user || current_user.admin?
35-
= row(label: 'codeharbor_link.profile_label', value: @user.codeharbor_link.nil? ? link_to(t('codeharbor_link.new'), polymorphic_path([@user, CodeharborLink], action: :new), class: 'btn btn-secondary') : link_to(t('codeharbor_link.edit'), polymorphic_path([@user, @user.codeharbor_link], action: :edit), class: 'btn btn-secondary'))
35+
= row(label: 'codeharbor_link.profile_label', value: @user.codeharbor_link.nil? ? link_to(t('codeharbor_link.new'), polymorphic_path([@user, CodeharborLink], action: :new), class: 'btn btn-secondary') : link_to(t('codeharbor_link.edit'), polymorphic_path([@user, @user.codeharbor_link], action: :edit), class: 'btn btn-secondary')) if policy(CodeharborLink).enabled?
3636
= render 'webauthn_credentials/list'
3737

3838
h4.mt-4 = link_to(t('.exercise_statistics'), statistics_external_user_path(@user)) if policy(@user).statistics?

app/views/internal_users/show.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ h1
2929
= t('users.show.no_groups')
3030

3131
- if @user == current_user || current_user.admin?
32-
= row(label: 'codeharbor_link.profile_label', value: @user.codeharbor_link.nil? ? link_to(t('codeharbor_link.new'), polymorphic_path([@user, CodeharborLink], action: :new), class: 'btn btn-secondary') : link_to(t('codeharbor_link.edit'), polymorphic_path([@user, @user.codeharbor_link], action: :edit), class: 'btn btn-secondary'))
32+
= row(label: 'codeharbor_link.profile_label', value: @user.codeharbor_link.nil? ? link_to(t('codeharbor_link.new'), polymorphic_path([@user, CodeharborLink], action: :new), class: 'btn btn-secondary') : link_to(t('codeharbor_link.edit'), polymorphic_path([@user, @user.codeharbor_link], action: :edit), class: 'btn btn-secondary')) if policy(CodeharborLink).enabled?
3333
= row(label: 'internal_user.password', value: link_to(t('.change_password'), change_password_internal_user_path(@user), class: 'btn btn-secondary'))
3434
= render 'webauthn_credentials/list'

spec/policies/exercise_policy_spec.rb

Lines changed: 52 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -54,41 +54,53 @@
5454

5555
%i[export_external_check? export_external_confirm?].each do |action|
5656
permissions(action) do
57-
context 'when user is author' do
58-
let(:user) { exercise.author }
59-
60-
it 'does not grant access' do
61-
expect(policy).not_to permit(user, exercise)
57+
context 'when CodeHarbor is disabled' do
58+
before do
59+
stub_const('CodeharborLinkPolicy::CODEHARBOR_CONFIG', {enabled: false})
6260
end
6361

64-
context 'when user has codeharbor_link' do
65-
before { user.codeharbor_link = build(:codeharbor_link) }
62+
%i[external_user teacher admin].each do |factory_name|
63+
context "when user is #{factory_name}" do
64+
let(:user) { create(factory_name) }
6665

67-
it 'grants access' do
68-
expect(policy).to permit(user, exercise)
66+
it 'does not grant access' do
67+
expect(policy).not_to permit(user, exercise)
68+
end
69+
70+
context 'when user has codeharbor_link' do
71+
before { user.codeharbor_link = build(:codeharbor_link) }
72+
73+
it 'does not grant access' do
74+
expect(policy).not_to permit(user, exercise)
75+
end
76+
end
6977
end
7078
end
7179
end
7280

73-
context 'when user is admin' do
74-
let(:user) { build(:admin) }
75-
76-
it 'does not grant access' do
77-
expect(policy).not_to permit(user, exercise)
81+
context 'when CodeHarbor is enabled' do
82+
before do
83+
stub_const('CodeharborLinkPolicy::CODEHARBOR_CONFIG', {enabled: true})
7884
end
7985

80-
context 'when user has codeharbor_link' do
81-
before { user.codeharbor_link = build(:codeharbor_link) }
86+
context 'when user is author' do
87+
let(:user) { exercise.author }
88+
89+
it 'does not grant access' do
90+
expect(policy).not_to permit(user, exercise)
91+
end
92+
93+
context 'when user has codeharbor_link' do
94+
before { user.codeharbor_link = build(:codeharbor_link) }
8295

83-
it 'grants access' do
84-
expect(policy).to permit(user, exercise)
96+
it 'grants access' do
97+
expect(policy).to permit(user, exercise)
98+
end
8599
end
86100
end
87-
end
88101

89-
%i[external_user teacher].each do |factory_name|
90-
context "when user is #{factory_name}" do
91-
let(:user) { create(factory_name) }
102+
context 'when user is admin' do
103+
let(:user) { build(:admin) }
92104

93105
it 'does not grant access' do
94106
expect(policy).not_to permit(user, exercise)
@@ -97,9 +109,27 @@
97109
context 'when user has codeharbor_link' do
98110
before { user.codeharbor_link = build(:codeharbor_link) }
99111

112+
it 'grants access' do
113+
expect(policy).to permit(user, exercise)
114+
end
115+
end
116+
end
117+
118+
%i[external_user teacher].each do |factory_name|
119+
context "when user is #{factory_name}" do
120+
let(:user) { create(factory_name) }
121+
100122
it 'does not grant access' do
101123
expect(policy).not_to permit(user, exercise)
102124
end
125+
126+
context 'when user has codeharbor_link' do
127+
before { user.codeharbor_link = build(:codeharbor_link) }
128+
129+
it 'does not grant access' do
130+
expect(policy).not_to permit(user, exercise)
131+
end
132+
end
103133
end
104134
end
105135
end

0 commit comments

Comments
 (0)