Skip to content

Commit

Permalink
Limit importers and exporters by user
Browse files Browse the repository at this point in the history
Refs
- #123

Prior work limited the ability to view the importer and exporter pages
by user role, but did not limit what importers and exporters could be
seen.

With this work, only admin users can see all importers and exporters,
while other users can only see importers and exporters they have created.
  • Loading branch information
laritakr committed Nov 28, 2023
1 parent f98d95f commit dfe32ad
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
30 changes: 30 additions & 0 deletions app/controllers/bulkrax/exporters_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# frozen_string_literal: true

# Override Bulkrax main (at v5.3.0) to limit access to only user's own exporters
module Bulkrax
module ExportersControllerDecorator
def index
# NOTE: We're paginating this in the browser.
@exporters = Exporter.order(created_at: :desc)
@exporters = @exporters.where(user: current_user) unless current_ability.admin?
@exporters = @exporters.all

add_exporter_breadcrumbs if defined?(::Hyrax)
end

private

def check_permissions
if current_ability.can_import_works?
return true if current_ability.admin?
return true unless params.key?(:id)
return true if Importer.where(id: params[:id], user: current_user).exists?
raise CanCan::AccessDenied
else
raise CanCan::AccessDenied
end
end
end
end

Bulkrax::ExportersController.prepend(Bulkrax::ExportersControllerDecorator)
35 changes: 35 additions & 0 deletions app/controllers/bulkrax/importers_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# frozen_string_literal: true

# Override Bulkrax main (at v5.3.0) to limit access to only user's own importers
module Bulkrax
module ImportersControllerDecorator
# GET /importers
def index
# NOTE: We're paginating this in the browser.
@importers = Importer.order(created_at: :desc)
@importers = @importers.where(user: current_user) unless current_ability.admin?
@importers = @importers.all

if api_request?
json_response('index')
elsif defined?(::Hyrax)
add_importer_breadcrumbs
end
end

private

def check_permissions
if current_ability.can_import_works?
return true if current_ability.admin?
return true unless params.key?(:id)
return true if Importer.where(id: params[:id], user: current_user).exists?
raise CanCan::AccessDenied
else
raise CanCan::AccessDenied
end
end
end
end

Bulkrax::ImportersController.prepend(Bulkrax::ImportersControllerDecorator)

0 comments on commit dfe32ad

Please sign in to comment.