From 9feb9455ce33d6cf35fb483b93cb3af1fe6cfee5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jos=C3=A9=20Valim?= Date: Sun, 18 Oct 2009 15:25:16 -0200 Subject: [PATCH] Allow page after sign in to be configured. --- README.rdoc | 27 ++++++++++++++++++-------- app/controllers/sessions_controller.rb | 7 ++++++- lib/devise/active_record.rb | 4 ++-- test/integration/authenticable_test.rb | 15 +++++++++----- test/rails_app/config/routes.rb | 2 ++ 5 files changed, 39 insertions(+), 16 deletions(-) diff --git a/README.rdoc b/README.rdoc index 7ee6c22f81..436ede9872 100644 --- a/README.rdoc +++ b/README.rdoc @@ -16,7 +16,7 @@ Right now it's composed of four mainly modules: == Dependencies -Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework from hassox (http://github.com/hassox), so you're gonna need to install this gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below). +Devise is based on Warden (http://github.com/hassox/warden), a Rack Authentication Framework so you need to install it as a gem. Current warden version is 0.4.0. Please ensure you have it installed in order to use devise (see instalation below). == Installation @@ -28,7 +28,7 @@ Install devise as an engine (plugin) inside your app: script/plugin install git://github.com/plataformatec/devise.git -And you're ready to use devise. +And you're ready to go. == Basic Usage @@ -40,10 +40,12 @@ We're assuming here you want a User model. First of all you have to setup a migr t.string :email, :null => false t.string :encrypted_password, :null => false t.string :password_salt, :null => false + # required for confirmable t.string :confirmation_token t.datetime :confirmation_sent_at t.datetime :confirmed_at + # required for recoverable t.string :reset_password_token @@ -57,17 +59,22 @@ This line adds devise authenticable automatically for you inside your User class # Same as using only devise, authenticable is activated by default devise :authenticable - # Include confirmable + + # Include authenticable + confirmable devise :confirmable - # Include recoverable + + # Include authenticable + recoverable devise :recoverable - # Include validatable - devise :validatable - # Include all of them + + # Include authenticable + conformable + recoverable + validatable devise :confirmable, :recoverable, :validatable + # Same as above, include all of them devise :all + # Include all except recoverable + devise :all, :except => :recoverable + Note that validations aren't added by default, so you're able to customize it. In order to have automatic validations working just include :validatable. The next step after setting up your model is to configure your routes for devise. You do this by opening up your config/routes.rb and adding: @@ -113,7 +120,7 @@ There are also some options available for configuring your routes: And that is it! Devise is gonna create some helpers to use inside your controllers and views. To setup a controller that needs user authentication, just add this before_filter: - before_filter :sign_in_user! + before_filter :authenticate_user! To verify if a user is signed in, you have the following helper: @@ -129,12 +136,16 @@ Devise let's you setup as many roles as you want, so let's say you already have t.string :email, :null => false t.string :encrypted_password, :null => false t.string :password_salt, :null => false + # Inside your Admin model devise :validatable + # Inside your routes map.devise_for :admin + # Inside your protected controller before_filter :sign_in_admin! + # Inside your controllers and views admin_signed_in? current_admin diff --git a/app/controllers/sessions_controller.rb b/app/controllers/sessions_controller.rb index 351f65c41e..c6bf1b984c 100644 --- a/app/controllers/sessions_controller.rb +++ b/app/controllers/sessions_controller.rb @@ -11,7 +11,7 @@ def new def create if authenticate(resource_name) set_flash_message :success, :signed_in - redirect_back_or_to root_path + redirect_back_or_to home_or_root_path else unauthenticated! render :new @@ -31,4 +31,9 @@ def unauthenticated! flash.now[:failure] = I18n.t(:"#{resource_name}.unauthenticated", :scope => [:devise, :sessions], :default => :unauthenticated) end + + def home_or_root_path + home_path = :"#{resource_name}_home_path" + respond_to?(home_path, true) ? send(home_path) : root_path + end end diff --git a/lib/devise/active_record.rb b/lib/devise/active_record.rb index cdff2d237b..106c0b100f 100644 --- a/lib/devise/active_record.rb +++ b/lib/devise/active_record.rb @@ -16,13 +16,13 @@ module ActiveRecord # # include authenticable + validatable modules # devise :validatable # - # # include all modules + # # include authenticable + confirmable + recoverable + validatable # devise :confirmable, :recoverable, :validatable # # # shortcut to include all modules (same as above) # devise :all # - # # include all except :recoverable + # # include all except recoverable # devise :all, :except => :recoverable # def devise(*modules) diff --git a/test/integration/authenticable_test.rb b/test/integration/authenticable_test.rb index b2b5ad48b2..6bbabc14c4 100644 --- a/test/integration/authenticable_test.rb +++ b/test/integration/authenticable_test.rb @@ -2,7 +2,7 @@ class AuthenticationTest < ActionController::IntegrationTest - test 'home should be accessible without signed in admins' do + test 'home should be accessible without signed in' do visit '/' assert_response :success assert_template 'home/index' @@ -64,7 +64,7 @@ class AuthenticationTest < ActionController::IntegrationTest assert_redirected_to new_admin_session_path(:unauthenticated => true) end - test 'signed in as admin should be able to access admin actions successfully' do + test 'signed in as admin should be able to access admin actions' do sign_in_as_admin assert warden.authenticated?(:admin) assert_not warden.authenticated?(:user) @@ -135,7 +135,7 @@ class AuthenticationTest < ActionController::IntegrationTest assert_not warden.authenticated?(:admin) end - test 'not authenticated admin does not set error message on sign out' do + test 'unauthenticated admin does not set message on sign out' do get destroy_admin_session_path assert_response :redirect assert_redirected_to root_path @@ -144,7 +144,7 @@ class AuthenticationTest < ActionController::IntegrationTest assert_not_contain 'Signed out successfully' end - test 'redirect with warden show error message' do + test 'redirect from warden shows error message' do get admins_path warden_path = new_admin_session_path(:unauthenticated => true) @@ -160,7 +160,7 @@ class AuthenticationTest < ActionController::IntegrationTest assert_not_contain 'Send me reset password instructions' end - test 'return to default url if no one was requested' do + test 'return to default url if no other was requested' do sign_in_as_user assert_template 'home/index' @@ -178,6 +178,11 @@ class AuthenticationTest < ActionController::IntegrationTest assert_nil session[:"user.return_to"] end + test 'return to configured home path after sign in' do + sign_in_as_admin + assert_equal "/admin_area/home", @request.path + end + test 'allows session to be set by a given scope' do sign_in_as_user visit 'users/index' diff --git a/test/rails_app/config/routes.rb b/test/rails_app/config/routes.rb index 14137a438b..549dea6861 100644 --- a/test/rails_app/config/routes.rb +++ b/test/rails_app/config/routes.rb @@ -10,6 +10,8 @@ map.root :controller => :home map.connect '/admin_area/password/new', :controller => "passwords", :action => "new" + map.admin_home '/admin_area/home', :controller => "admins", :action => "index" + map.connect ':controller/:action/:id' map.connect ':controller/:action/:id.:format' end