Sha256: 87b05ed91419435982fe769d3f48408eaaf7abfc958d0f6b5ce1f800ff8cb5b0

Contents?: true

Size: 912 Bytes

Versions: 22

Compression:

Stored size: 912 Bytes

Contents

module Controller
  module PunditAuthorizationFailureHandlingConcern
    extend ActiveSupport::Concern

    included do
      rescue_from Pundit::NotAuthorizedError, with: :not_authorized
    end

    private

    def not_authorized(exception)
      resource_name   = exception.record.respond_to?(:model_name) ? exception.record.model_name.human : exception.record
      collection_name = exception.record.respond_to?(:model_name) ? exception.record.model_name.human(count: :other) : exception.record
      flash[:error] = I18n.t("pundit.not_authorized.#{action_name}", collection_name: collection_name,resource_name: resource_name)
      respond_to do |format|
        format.html { redirect_to after_authorization_failure_path }
        format.json { render json: { error: 'not authorized' }, status: 403 }
      end
    end

    def after_authorization_failure_path
      main_app.root_path
    end
  end
end

Version data entries

22 entries across 22 versions & 1 rubygems

Version Path
itsf_backend-5.0.0.pre app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.2.3 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.2.2 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.2.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.2.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.0.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-4.0.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.1.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.1.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.7 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.6 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.5 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.4 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.3 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.2 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-3.0.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-2.2.1 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-2.2.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb
itsf_backend-2.1.0 app/controllers/concerns/controller/pundit_authorization_failure_handling_concern.rb