Skip to content

Commit

Permalink
Merge pull request #201 from scientist-softserv/limit-importer-export…
Browse files Browse the repository at this point in the history
…er-by-user

Limit importers and exporters by user
  • Loading branch information
laritakr authored Nov 29, 2023
2 parents f98d95f + 7b47b19 commit a07bb7c
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
27 changes: 27 additions & 0 deletions app/controllers/bulkrax/exporters_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# 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
raise CanCan::AccessDenied unless current_ability.can_export_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
end
end
end

Bulkrax::ExportersController.prepend(Bulkrax::ExportersControllerDecorator)
32 changes: 32 additions & 0 deletions app/controllers/bulkrax/importers_controller_decorator.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 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
raise CanCan::AccessDenied unless 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
end
end
end

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

0 comments on commit a07bb7c

Please sign in to comment.