Allow page after sign in to be configured.

This commit is contained in:
José Valim 2009-10-18 15:25:16 -02:00
parent b0a2da72b5
commit 9feb9455ce
5 changed files with 39 additions and 16 deletions

View File

@ -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

View File

@ -11,7 +11,7 @@ class SessionsController < ApplicationController
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 @@ class SessionsController < ApplicationController
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

View File

@ -16,13 +16,13 @@ module Devise
# # 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)

View File

@ -2,7 +2,7 @@ require 'test/test_helper'
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'

View File

@ -10,6 +10,8 @@ ActionController::Routing::Routes.draw do |map|
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