Sha256: 438bf0d6b1ce21631d8cb630f4ea8573c1d9a09dc4d2e7a956a12d9524b52c48

Contents?: true

Size: 1.99 KB

Versions: 25

Compression:

Stored size: 1.99 KB

Contents

require 'cancan'

module Ckeditor
  module Hooks
    # This adapter is for the CanCan[https://github.com/ryanb/cancan] authorization library.
    # You can create another adapter for different authorization behavior, just be certain it
    # responds to each of the public methods here.
    class CanCanAuthorization
      # See the +authorize_with+ config method for where the initialization happens.
      def initialize(controller, ability = ::Ability)
        @controller = controller
        @controller.instance_variable_set '@ability', ability
        @controller.extend ControllerExtension
        @controller.current_ability.authorize! :access, :ckeditor
      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)
        @controller.authorize!(action.to_sym, model_object) if action
      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)
        @controller.current_ability.can?(action.to_sym, model_object) if action
      end

      private

      module ControllerExtension
        def current_ability
          # use ckeditor_current_user instead of default current_user so it works with
          # whatever current user method is defined with Ckeditor
          @current_ability ||= @ability.new(ckeditor_current_user)
        end
      end
    end
  end
end

Ckeditor::AUTHORIZATION_ADAPTERS[:cancan] = Ckeditor::Hooks::CanCanAuthorization

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
glebtv-ckeditor-4.4.7.4 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.7.3 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.7.2 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.7.1 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.7 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.6 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.3.4 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.3.3 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.3.2 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.3.1 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.4.3.0 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.6 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.5 lib/ckeditor/hooks/cancan.rb
ckeditor-4.0.11 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.4 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.3 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.2 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.1 lib/ckeditor/hooks/cancan.rb
glebtv-ckeditor-4.3.2.0 lib/ckeditor/hooks/cancan.rb
ckeditor-4.0.10 lib/ckeditor/hooks/cancan.rb