Sha256: 6f1731bf6d965f36da127a5eccbfcc71aba334ea690348c6bac0cf36bf1902b2

Contents?: true

Size: 1.38 KB

Versions: 31

Compression:

Stored size: 1.38 KB

Contents

module Clearance
  module Constraints
    # Can be applied to make a set of routes visible only to users that are
    # signed in.
    #
    #     # config/routes.rb
    #     constraints Clearance::Constraints::SignedIn.new do
    #       resources :posts
    #     end
    #
    # In the example above, requests to `/posts` from users that are not signed
    # in will result in a 404. You can make additional assertions about the user
    # by passing a block. For instance, if you want to require that the
    # signed-in user be an admin:
    #
    #     # config/routes.rb
    #     constraints Clearance::Constraints::SignedIn.new { |user| user.admin? } do
    #       resources :posts
    #     end
    class SignedIn
      def initialize(&block)
        @block = block || lambda { |user| true }
      end

      def matches?(request)
        @request = request
        signed_in? && current_user_fulfills_additional_requirements?
      end

      private

      # @api private
      def clearance_session
        @request.env[:clearance]
      end

      # @api private
      def current_user
        clearance_session.current_user
      end

      # @api private
      def current_user_fulfills_additional_requirements?
        @block.call current_user
      end

      # @api private
      def signed_in?
        clearance_session.present? && clearance_session.signed_in?
      end
    end
  end
end

Version data entries

31 entries across 31 versions & 1 rubygems

Version Path
clearance-2.9.2 lib/clearance/constraints/signed_in.rb
clearance-2.9.1 lib/clearance/constraints/signed_in.rb
clearance-2.9.0 lib/clearance/constraints/signed_in.rb
clearance-2.8.0 lib/clearance/constraints/signed_in.rb
clearance-2.7.2 lib/clearance/constraints/signed_in.rb
clearance-2.7.0 lib/clearance/constraints/signed_in.rb
clearance-2.6.2 lib/clearance/constraints/signed_in.rb
clearance-2.6.1 lib/clearance/constraints/signed_in.rb
clearance-2.6.0 lib/clearance/constraints/signed_in.rb
clearance-2.5.0 lib/clearance/constraints/signed_in.rb
clearance-2.4.0 lib/clearance/constraints/signed_in.rb
clearance-2.3.1 lib/clearance/constraints/signed_in.rb
clearance-2.3.0 lib/clearance/constraints/signed_in.rb
clearance-2.2.1 lib/clearance/constraints/signed_in.rb
clearance-2.2.0 lib/clearance/constraints/signed_in.rb
clearance-2.1.0 lib/clearance/constraints/signed_in.rb
clearance-2.0.0 lib/clearance/constraints/signed_in.rb
clearance-2.0.0.beta2 lib/clearance/constraints/signed_in.rb
clearance-2.0.0.beta1 lib/clearance/constraints/signed_in.rb
clearance-1.17.0 lib/clearance/constraints/signed_in.rb