Class: Rails::Authentication::Strategy
- Inherits:
-
Object
- Object
- Rails::Authentication::Strategy
- Defined in:
- lib/rails-auth/strategy.rb
Overview
The Rails::Authentication::Strategy is where all the action happens in the rails-auth framework. Inherit from this class to setup your own strategy. The strategy will automatically be placed in the default_strategy_order array, and will be included in the strategy runs.
The strategy you implment should have a YourStrategy#run! method defined that returns
1. A user object if authenticated
2. nil if no authenticated user was found.
Example
class MyStrategy < Rails::Authentication::Strategy
def run!
u = User.get(params[:login])
u if u.authentic?(params[:password])
end
end
Direct Known Subclasses
Rails::Authentication::Strategies::Basic::Base, Rails::Authentication::Strategies::Basic::OpenID
Instance Attribute Summary collapse
-
#body ⇒ Object
Allows you to provide a body of content to return when halting.
-
#params ⇒ Object
An alias to the request.params hash Only rely on this hash to find any router params you are looking for.
-
#request ⇒ Object
Returns the value of attribute request.
-
#status ⇒ Object
Provides a place to put the status of the response.
Class Method Summary collapse
-
.abstract! ⇒ Object
Mark a strategy as abstract.
-
.abstract? ⇒ Boolean
Asks is this strategy abstract.
-
.after(strategy) ⇒ Object
Use this to declare the strategy should run after another strategy.
-
.before(strategy) ⇒ Object
Use this to declare the strategy should run before another strategy.
- .inherited(klass) ⇒ Object
Instance Method Summary collapse
-
#cookies ⇒ Object
An alials to the request.cookies hash.
-
#halt! ⇒ Object
Mark this strategy as complete for this request.
-
#halted? ⇒ Boolean
Checks to see if this strategy has been halted.
-
#headers ⇒ Object
Provides a place to put headers.
-
#initialize(request, params = {}) ⇒ Strategy
constructor
End class << self.
-
#redirect!(url, opts = {}) ⇒ Object
Redirects causes the strategy to signal a redirect to the provided url.
-
#redirected? ⇒ Boolean
Returns ture if the strategy redirected.
-
#run! ⇒ Object
This is the method that is called as the test for authentication and is where you put your code.
-
#session ⇒ Object
An alias to the request.session hash.
-
#user_class ⇒ Object
Overwrite this method to scope a strategy to a particular user type you can use this with inheritance for example to try the same strategy on different user types.
Constructor Details
#initialize(request, params = {}) ⇒ Strategy
End class << self
108 109 110 111 112 |
# File 'lib/rails-auth/strategy.rb', line 108 def initialize(request, params = {}) # @controller = controller @request = request @params = params end |
Instance Attribute Details
#body ⇒ Object
Allows you to provide a body of content to return when halting
174 175 176 |
# File 'lib/rails-auth/strategy.rb', line 174 def body @body || "" end |
#params ⇒ Object
An alias to the request.params hash Only rely on this hash to find any router params you are looking for. If looking for paramteres use request.params
117 118 119 |
# File 'lib/rails-auth/strategy.rb', line 117 def params @params end |
#request ⇒ Object
Returns the value of attribute request.
65 66 67 |
# File 'lib/rails-auth/strategy.rb', line 65 def request @request end |
#status ⇒ Object
Provides a place to put the status of the response
154 155 156 |
# File 'lib/rails-auth/strategy.rb', line 154 def status @status end |
Class Method Details
.abstract! ⇒ Object
Mark a strategy as abstract. This means that a strategy will not ever be run as part of the authentication. Instead this will be available to inherit from as a way to share code.
You could for example setup a strategy to check for a particular kind of login and then have a subclass for each class type of user in your system. i.e. Customer / Staff, Student / Staff etc
97 98 99 |
# File 'lib/rails-auth/strategy.rb', line 97 def abstract! @abstract = true end |
.abstract? ⇒ Boolean
Asks is this strategy abstract. i.e. can it be run as part of the authentication
102 103 104 |
# File 'lib/rails-auth/strategy.rb', line 102 def abstract? !!@abstract end |
.after(strategy) ⇒ Object
Use this to declare the strategy should run after another strategy
83 84 85 86 87 88 |
# File 'lib/rails-auth/strategy.rb', line 83 def after(strategy) order = Rails::Authentication.default_strategy_order order.delete(self) index = order.index(strategy) index == order.size ? order << self : order.insert(index + 1, self) end |
.before(strategy) ⇒ Object
Use this to declare the strategy should run before another strategy
75 76 77 78 79 80 |
# File 'lib/rails-auth/strategy.rb', line 75 def before(strategy) order = Rails::Authentication.default_strategy_order order.delete(self) index = order.index(strategy) order.insert(index,self) end |
.inherited(klass) ⇒ Object
69 70 71 72 |
# File 'lib/rails-auth/strategy.rb', line 69 def inherited(klass) Rails::Authentication.strategies << klass Rails::Authentication.default_strategy_order << klass end |
Instance Method Details
#cookies ⇒ Object
An alials to the request.cookies hash
122 123 124 |
# File 'lib/rails-auth/strategy.rb', line 122 def request. end |
#halt! ⇒ Object
Mark this strategy as complete for this request. Will cause that no other strategies will be executed.
163 164 165 |
# File 'lib/rails-auth/strategy.rb', line 163 def halt! @halt = true end |
#halted? ⇒ Boolean
Checks to see if this strategy has been halted
168 169 170 |
# File 'lib/rails-auth/strategy.rb', line 168 def halted? !!@halt end |
#headers ⇒ Object
Provides a place to put headers
157 158 159 |
# File 'lib/rails-auth/strategy.rb', line 157 def headers @headers ||={} end |
#redirect!(url, opts = {}) ⇒ Object
Redirects causes the strategy to signal a redirect to the provided url.
Parameters
- url<String>
-
The url to redirect to
- options<Hash>
-
An options hash with the following keys:
+:permanent+ Set this to true to make the redirect permanent
+:status+ Set this to an integer for the status to return
139 140 141 142 143 144 145 146 |
# File 'lib/rails-auth/strategy.rb', line 139 def redirect!(url, opts = {}) self.headers["Location"] = url self.status = opts[:permanent] ? 301 : 302 self.status = opts[:status] if opts[:status] halt! # @controller.send(:redirect_to, url) return true end |
#redirected? ⇒ Boolean
Returns ture if the strategy redirected
149 150 151 |
# File 'lib/rails-auth/strategy.rb', line 149 def redirected? !!headers["Location"] end |
#run! ⇒ Object
This is the method that is called as the test for authentication and is where you put your code.
You must overwrite this method in your strategy
184 185 186 |
# File 'lib/rails-auth/strategy.rb', line 184 def run! raise NotImplemented end |
#session ⇒ Object
An alias to the request.session hash
127 128 129 |
# File 'lib/rails-auth/strategy.rb', line 127 def session request.session end |
#user_class ⇒ Object
Overwrite this method to scope a strategy to a particular user type you can use this with inheritance for example to try the same strategy on different user types
By default, Rails::Authentication.user_class is used. This method allows for particular strategies to deal with a different type of user class.
For example. If Rails::Authentication.user_class is Customer and you have a PasswordStrategy, you can subclass the PasswordStrategy and change this method to return Staff. Giving you a PasswordStrategy strategy for first Customer(s) and then Staff.
201 202 203 |
# File 'lib/rails-auth/strategy.rb', line 201 def user_class Rails::Authentication.user_class end |