1
0
Fork 0
mirror of https://github.com/heartcombo/devise.git synced 2022-11-09 12:18:31 -05:00

Add an easy way to configure an application to sign in users through "/sign_in".

First, configure your routes:

  map.devise_for :users
  map.sign_in "/sign_in", :controller => "sessions", :action => "new"

Then, in config/initializers/devise.rb:

  config.use_default_scope = true

The default scope is always the first declaration in routes.rb, but if you need
to change it, you can also do it through the initializer:

  config.default_scope = :user
This commit is contained in:
José Valim 2010-01-06 14:31:00 +01:00
parent 27a515fcbf
commit 4d8f5ea165
8 changed files with 51 additions and 14 deletions

View file

@ -1,5 +1,6 @@
* enhancements * enhancements
* Warden 0.8.0 compatibility * Warden 0.8.0 compatibility
* Add an easy for map.connect 'sign_in', :controller => "sessions", :action => "new" to work
* deprecation * deprecation
* Removed DeviseMailer.sender * Removed DeviseMailer.sender

View file

@ -10,6 +10,9 @@ Devise.setup do |config|
# to check the docs for a complete set. # to check the docs for a complete set.
config.all = [:authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable] config.all = [:authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable]
# Configure the e-mail address which will be shown in DeviseMailer.
config.mailer_sender = "please-change-me@config-initializers-devise.com"
# Invoke `rake secret` and use the printed value to setup a pepper to generate # Invoke `rake secret` and use the printed value to setup a pepper to generate
# the encrypted password. By default no pepper is used. # the encrypted password. By default no pepper is used.
# config.pepper = "rake secret output" # config.pepper = "rake secret output"
@ -42,9 +45,6 @@ Devise.setup do |config|
# time the user will be asked for credentials again. # time the user will be asked for credentials again.
# config.timeout_in = 10.minutes # config.timeout_in = 10.minutes
# Configure the e-mail address which will be shown in DeviseMailer.
config.mailer_sender = "please-change-me@config-initializers-devise.com"
# Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper. # Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper.
# require 'devise/orm/mongo_mapper' # require 'devise/orm/mongo_mapper'
# config.orm = :mongo_mapper # config.orm = :mongo_mapper
@ -54,6 +54,16 @@ Devise.setup do |config|
# are using only default views. # are using only default views.
# config.scoped_views = true # config.scoped_views = true
# By default, devise detects the role accessed based on the url. So whenever
# accessing "/users/sign_in", it knows you are accessing an User. This makes
# routes as "/sign_in" not possible, unless you tell Devise to use the default
# scope, setting true below.
# config.use_default_scope = true
# Configure the default scope used by Devise. By default it's the first devise
# role declared in your routes.
# config.default_scope = :user
# If you want to use other strategies, that are not (yet) supported by Devise, # If you want to use other strategies, that are not (yet) supported by Devise,
# you can configure them inside the config.warden block. The example below # you can configure them inside the config.warden block. The example below
# allows you to setup OAuth, using http://github.com/roman/warden_oauth # allows you to setup OAuth, using http://github.com/roman/warden_oauth

View file

@ -103,11 +103,15 @@ module Devise
mattr_accessor :scoped_views mattr_accessor :scoped_views
@@scoped_views = false @@scoped_views = false
# The default scope which is used by warden # Tell when to use the default scope, if one cannot be found from routes.
mattr_accessor :use_default_scope
@@use_default_scope
# The default scope which is used by warden.
mattr_accessor :default_scope mattr_accessor :default_scope
@@default_scope = nil @@default_scope = nil
# Address which sends Devise e-mails # Address which sends Devise e-mails.
mattr_accessor :mailer_sender mattr_accessor :mailer_sender
@@mailer_sender @@mailer_sender

View file

@ -35,7 +35,11 @@ module Devise
# Attempt to find the mapped route for devise based on request path # Attempt to find the mapped route for devise based on request path
def devise_mapping def devise_mapping
@devise_mapping ||= Devise::Mapping.find_by_path(request.path) @devise_mapping ||= begin
mapping = Devise::Mapping.find_by_path(request.path)
mapping ||= Devise.mappings[Devise.default_scope] if Devise.use_default_scope
mapping
end
end end
# Overwrites devise_controller? to return true # Overwrites devise_controller? to return true

View file

@ -29,7 +29,7 @@ module Devise
def self.find_by_path(path) def self.find_by_path(path)
Devise.mappings.each_value do |mapping| Devise.mappings.each_value do |mapping|
route = path.split("/")[mapping.as_position] route = path.split("/")[mapping.as_position]
return mapping if mapping.as == route.to_sym return mapping if route && mapping.as == route.to_sym
end end
nil nil
end end

View file

@ -154,12 +154,6 @@ class AuthenticationTest < ActionController::IntegrationTest
assert_contain 'You need to sign in or sign up before continuing.' assert_contain 'You need to sign in or sign up before continuing.'
end end
test 'render 404 on roles without permission' do
get 'admin_area/password/new'
assert_response :not_found
assert_not_contain 'Send me reset password instructions'
end
test 'return to default url if no other was requested' do test 'return to default url if no other was requested' do
sign_in_as_user sign_in_as_user
@ -221,4 +215,24 @@ class AuthenticationTest < ActionController::IntegrationTest
end end
end end
end end
test 'render 404 on roles without permission' do
get 'admin_area/password/new'
assert_response :not_found
assert_not_contain 'Send me reset password instructions'
end
test 'render 404 on roles without mapping' do
get 'sign_in'
assert_response :not_found
assert_not_contain 'Sign in'
end
test 'uses the mapping from the default scope if specified' do
swap Devise, :use_default_scope => true do
get 'sign_in'
assert_response :ok
assert_contain 'Sign in'
end
end
end end

View file

@ -18,4 +18,7 @@ ActiveSupport.use_standard_json_time_format = true
# Don't escape HTML entities in JSON, leave that for the #json_escape helper. # Don't escape HTML entities in JSON, leave that for the #json_escape helper.
# if you're including raw json in an HTML page. # if you're including raw json in an HTML page.
ActiveSupport.escape_html_entities_in_json = false ActiveSupport.escape_html_entities_in_json = false
# Clean up silencers
Rails.backtrace_cleaner.remove_silencers!

View file

@ -12,6 +12,7 @@ ActionController::Routing::Routes.draw do |map|
map.connect '/admin_area/password/new', :controller => "passwords", :action => "new" map.connect '/admin_area/password/new', :controller => "passwords", :action => "new"
map.admin_root '/admin_area/home', :controller => "admins", :action => "index" map.admin_root '/admin_area/home', :controller => "admins", :action => "index"
map.connect '/sign_in', :controller => "sessions", :action => "new"
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id'
map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id.:format'
end end