Sha256: 26e2f4242172e6e34c56df2e10fb189f09468afe38cf44bb44cc3662a4223291

Contents?: true

Size: 1.56 KB

Versions: 37

Compression:

Stored size: 1.56 KB

Contents

# Code adapted from http://github.com/rails/ssl_requirement 
module MuckEngine
  module SslRequirement
    
    extend ActiveSupport::Concern
    
    included do
      before_filter :ensure_proper_protocol
    end
    
    module ClassMethods
      # Specifies that the named actions requires an SSL connection to be performed (which is enforced by ensure_proper_protocol).
      def ssl_required(*actions)
        write_inheritable_array(:ssl_required_actions, actions)
      end

      def ssl_allowed(*actions)
        write_inheritable_array(:ssl_allowed_actions, actions)
      end
    end
    
    protected
      # Only require ssl if we are in production
      def ssl_required?
        return ENV['SSL'] == 'on' ? true : false if defined? ENV['SSL']
        return false if local_request?
        return false if RAILS_ENV == 'test'
        ((self.class.read_inheritable_attribute(:ssl_required_actions) || []).include?(action_name.to_sym)) && (::Rails.env == 'production' || ::Rails.env == 'staging')
      end
      

      def ssl_allowed?
        (self.class.read_inheritable_attribute(:ssl_allowed_actions) || []).include?(action_name.to_sym)
      end

    private
      def ensure_proper_protocol
        return true if ssl_allowed?

        if ssl_required? && !request.ssl?
          redirect_to "https://" + request.host + request.fullpath
          flash.keep
          return false
        elsif request.ssl? && !ssl_required?
          redirect_to "http://" + request.host + request.fullpath
          flash.keep
          return false
        end
      end
    
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
muck-engine-3.4.0 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.18 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.17 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.16 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.15 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.14 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.13 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.12 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.11 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.10 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.9 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.8 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.7 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.6 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.5 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.4 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.3 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.2 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.1 lib/muck-engine/controllers/ssl_requirement.rb
muck-engine-3.3.0 lib/muck-engine/controllers/ssl_requirement.rb