Skip to content

Commit b8f31da

Browse files
committed
Prevent destruction of public project with remixes
The only use case for `Api::PublicProjectsController#destroy` is currently to allow experience-cs admins to delete a project. Although the `Project` model allows such a deletion, it's not obvious that it's entirely sensible. And in any case, while the Learning Team is using the experience-cs admin UI to create projects for the curriculum, there's little danger that they will have remixes until they're associated with lessons, etc. So this seems like a safer option for now. We can always relax it later if it becomes a problem.
1 parent d1f34c2 commit b8f31da

File tree

2 files changed

+18
-0
lines changed

2 files changed

+18
-0
lines changed

app/controllers/api/public_projects_controller.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ class PublicProjectsController < ApiController
66
before_action :restrict_project_type, only: %i[create]
77
before_action :load_project, only: %i[update destroy]
88
before_action :restrict_to_public_projects, only: %i[update destroy]
9+
before_action :prevent_destruction_of_public_project_with_remixes, only: %i[destroy]
910

1011
def create
1112
authorize! :create, :public_project
@@ -69,5 +70,11 @@ def restrict_to_public_projects
6970

7071
raise CanCan::AccessDenied.new('Cannot update non-public project', :update, :public_project)
7172
end
73+
74+
def prevent_destruction_of_public_project_with_remixes
75+
return if @project.remixes.none?
76+
77+
raise CanCan::AccessDenied.new('Cannot destroy public project with remixes', :update, :public_project)
78+
end
7279
end
7380
end

spec/features/public_project/destroying_a_public_project_spec.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,17 @@
4545
end
4646
end
4747

48+
context 'when project has one or more remixes' do
49+
before do
50+
project.remixes.create!(attributes_for(:project))
51+
end
52+
53+
it 'responds 403 Forbidden' do
54+
delete("/api/public_projects/#{project.identifier}?project_type=scratch", headers:)
55+
expect(response).to have_http_status(:forbidden)
56+
end
57+
end
58+
4859
it 'responds 404 Not Found when project is not found' do
4960
delete('/api/public_projects/another-identifier?project_type=scratch', headers:)
5061
expect(response).to have_http_status(:not_found)

0 commit comments

Comments
 (0)