Sha256: ed31c4a331ee7ab4a30493f94688396be5e90b3d7ef61ca38b9b9a8dc7cb8443

Contents?: true

Size: 1.9 KB

Versions: 36

Compression:

Stored size: 1.9 KB

Contents

require 'pundit'

module Ckeditor
  module Hooks
    # This adapter is for the Pundit[https://github.com/elabs/pundit] authorization library.
    # You can create another adapter for different authorization behavior, just be certain it
    # responds to each of the public methods here.
    class PunditAuthorization

      include Ckeditor::Helpers::Controllers

      # See the +authorize_with+ config method for where the initialization happens.
      def initialize(controller)
        @controller = controller
        @controller.extend ControllerExtension
      end

      # This method is called in every controller action and should raise an exception
      # when the authorization fails. The first argument is the name of the controller
      # action as a symbol (:create, :destroy, etc.). The second argument is the actual model
      # instance if it is available.
      def authorize(action, model_object = nil)
        raise Pundit::NotAuthorizedError unless authorized?(action, model_object)
      end

      # This method is called primarily from the view to determine whether the given user
      # has access to perform the action on a given model. It should return true when authorized.
      # This takes the same arguments as +authorize+. The difference is that this will
      # return a boolean whereas +authorize+ will raise an exception when not authorized.
      def authorized?(action, model_object = nil)
        Pundit.policy(@controller.current_user_for_pundit, model_object).public_send(action + '?' ) if action
      end

      private

      module ControllerExtension
        def current_user_for_pundit
          # use ckeditor_current_user instead of default current_user so it works with
          # whatever current user method is defined with Ckeditor
          ckeditor_current_user
        end
      end
    end
  end
end

Ckeditor::AUTHORIZATION_ADAPTERS[:pundit] = Ckeditor::Hooks::PunditAuthorization

Version data entries

36 entries across 36 versions & 4 rubygems

Version Path
danabr75-ckeditor-4.1.6 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.6.0 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.5.10.3 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.5.10.2 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.5.10.1 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.5.7.1 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.5.7 lib/ckeditor/hooks/pundit.rb
ckeditor_custom_controller-4.1.6 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.6 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.5 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.4 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.3 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.7.4 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.2 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.7.3 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.7.2 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.7.1 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.7 lib/ckeditor/hooks/pundit.rb
glebtv-ckeditor-4.4.6 lib/ckeditor/hooks/pundit.rb
ckeditor-4.1.1 lib/ckeditor/hooks/pundit.rb