Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

SSL requirement adds a declarative way of specifying that certain act…

…ions should only be allowed to run under SSL, and if theyre accessed without it, they should be redirected.
  • Loading branch information...
commit 79ded847042aa9f8b4d619a70aa709cdf73b6704 0 parents
David Heinemeier Hansson authored October 29, 2005
36  README
... ...
@@ -0,0 +1,36 @@
  1
+SSL Requirement
  2
+===============
  3
+
  4
+SSL requirement adds a declarative way of specifying that certain actions should
  5
+only be allowed to run under SSL, and if they're accessed without it, they should
  6
+be redirected.
  7
+
  8
+The methods are: account_url, account_host, and account_domain.
  9
+
  10
+Example:
  11
+
  12
+  class ApplicationController < ActiveRecord::Base
  13
+    include SslRequirement
  14
+  end
  15
+
  16
+  class AccountController < ApplicationController
  17
+    ssl_required :signup, :payment
  18
+    
  19
+    def signup
  20
+      # Non-SSL access will be redirected to SSL
  21
+    end
  22
+    
  23
+    def payment
  24
+      # Non-SSL access will be redirected to SSL
  25
+    end
  26
+  end
  27
+  
  28
+You can overwrite the protected method ssl_required? to rely on other things than
  29
+just the declarative specification. Say, only premium accounts get SSL.
  30
+
  31
+P.S.: Beware when you include the SslRequirement module. At the time of inclusion,
  32
+it'll add the before_filter that validates the declarations. Some times you'll want to
  33
+run other before_filters before that. They should then be declared ahead of including
  34
+this module.
  35
+
  36
+Copyright (c) 2005 David Heinemeier Hansson, released under the MIT license
50  lib/ssl_requirement.rb
... ...
@@ -0,0 +1,50 @@
  1
+# Copyright (c) 2005 David Heinemeier Hansson
  2
+#
  3
+# Permission is hereby granted, free of charge, to any person obtaining
  4
+# a copy of this software and associated documentation files (the
  5
+# "Software"), to deal in the Software without restriction, including
  6
+# without limitation the rights to use, copy, modify, merge, publish,
  7
+# distribute, sublicense, and/or sell copies of the Software, and to
  8
+# permit persons to whom the Software is furnished to do so, subject to
  9
+# the following conditions:
  10
+#
  11
+# The above copyright notice and this permission notice shall be
  12
+# included in all copies or substantial portions of the Software.
  13
+#
  14
+# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15
+# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16
+# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17
+# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  18
+# LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  19
+# OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20
+# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21
+module SslRequirement
  22
+  def self.included(controller)
  23
+    controller.extend(ClassMethods)
  24
+    controller.before_filter(:ensure_proper_protocol)
  25
+  end
  26
+
  27
+  module ClassMethods
  28
+    # Specifies that the named actions requires an SSL connection to be performed (which is enforced by ensure_proper_protocol).
  29
+    def ssl_required(*actions)
  30
+      write_inheritable_array(:ssl_required_actions, actions)
  31
+    end
  32
+  end
  33
+  
  34
+  protected
  35
+    # Returns true if the current action is supposed to run as SSL
  36
+    def ssl_required?
  37
+      (self.class.read_inheritable_attribute(:ssl_required_actions) || []).include?(action_name.to_sym)
  38
+    end
  39
+
  40
+  private
  41
+    def ensure_proper_protocol
  42
+      if ssl_required? && !request.ssl?
  43
+        redirect_to "https://" + request.host + request.request_uri
  44
+        return false
  45
+      elsif request.ssl? && !ssl_required?
  46
+        redirect_to "http://" + request.host + request.request_uri
  47
+        return false
  48
+      end
  49
+    end
  50
+end

0 notes on commit 79ded84

Please sign in to comment.
Something went wrong with that request. Please try again.