Added :before option to authentication

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@352 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
David Heinemeier Hansson
2005-01-09 16:43:21 +00:00
parent 75fca04590
commit cbd367b7fa
3 changed files with 21 additions and 3 deletions

View File

@@ -2,6 +2,8 @@
* Added authentication framework to protect actions behind a condition and redirect on failure. See ActionController::Authentication for more.
* Added Base#render_nothing as a cleaner way of doing render_text "" when you're not interested in returning anything but an empty response.
* Added the possibility of passing nil to UrlHelper#link_to to use the link itself as the name

View File

@@ -34,9 +34,11 @@ module ActionController #:nodoc:
#
# Options are:
# * <tt>:by</tt> - the code fragment that will be evaluated on each request to determine whether the request is authenticated.
# * <tt>:before</tt> - a code fragment that's run before the failure redirect happens, such as
# '@session[:return_to] = @request.request_uri'.
# * <tt>:failure</tt> - redirection options following the format of Base#url_for.
def authentication(options)
options.assert_valid_keys([:by, :failure])
options.assert_valid_keys([:by, :failure, :before])
class_eval <<-EOV
protected
def actions_excepted_from_authentication
@@ -71,6 +73,7 @@ module ActionController #:nodoc:
if !action_needs_authentication? || #{options[:by]}
return true
else
#{options[:before]}
redirect_to(#{options[:failure].inspect})
return false
end

View File

@@ -2,7 +2,7 @@ require File.dirname(__FILE__) + '/../abstract_unit'
class AuthenticationTest < Test::Unit::TestCase
class ApplicationController < ActionController::Base
authentication :by => '@session[:authenticated]', :failure => { :controller => "login" }
authentication :by => '@session[:authenticated]', :before => '@session[:return_to] = "/weblog/"', :failure => { :controller => "login" }
end
class WeblogController < ApplicationController
@@ -10,7 +10,10 @@ class AuthenticationTest < Test::Unit::TestCase
def index() render_text "I indexed something" end
def edit() render_text "I edited something" end
def update() render_text "I updated something" end
def login() @session[:authenticated] = true; render_nothing end
def login
@session[:authenticated] = true
@session[:return_to] ? redirect_to_path(@session[:return_to]) : render_nothing
end
end
class AuthenticatesWeblogController < WeblogController
@@ -86,4 +89,14 @@ class AuthenticationTest < Test::Unit::TestCase
get :edit
assert_success
end
def test_before_condition
@controller = AuthenticatesWeblogController.new
get :edit
assert_redirected_to :controller => "login"
get :login
assert_redirect_url "http://test.host/weblog/"
end
end