Last active
February 18, 2016 09:32
-
-
Save upinetree/d744798e134a709169ec to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module SslRedirectable | |
extend ActiveSupport::Concern | |
included do | |
before_action :force_non_ssl, if: :force_non_ssl? | |
class_attribute :ssl_actions | |
SSL_ACTIONS_ALL = [:all] | |
def self.force_ssl(options = {}) | |
self.ssl_actions = options[:only] || SSL_ACTIONS_ALL | |
super | |
end | |
end | |
def force_non_ssl? | |
request.ssl? && request.get? && !ssl_required? | |
end | |
def ssl_required? | |
return false unless ssl_actions | |
ssl_actions.include?(action_name.to_sym) || ssl_actions == SSL_ACTIONS_ALL | |
end | |
def force_non_ssl | |
redirect_to non_ssl_url | |
flash.keep | |
end | |
def non_ssl_url | |
URI(request.original_url).tap { |u| u.scheme = 'http' }.to_s | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment