devise 0.8.2 → 0.9.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of devise might be problematic. Click here for more details.

Files changed (80) hide show
  1. data/CHANGELOG.rdoc +21 -2
  2. data/README.rdoc +40 -54
  3. data/Rakefile +1 -1
  4. data/TODO +1 -3
  5. data/app/controllers/confirmations_controller.rb +9 -20
  6. data/app/controllers/passwords_controller.rb +9 -20
  7. data/app/controllers/sessions_controller.rb +9 -9
  8. data/app/controllers/unlocks_controller.rb +22 -0
  9. data/app/models/devise_mailer.rb +6 -1
  10. data/app/views/confirmations/new.html.erb +1 -5
  11. data/app/views/devise_mailer/unlock_instructions.html.erb +7 -0
  12. data/app/views/passwords/edit.html.erb +1 -5
  13. data/app/views/passwords/new.html.erb +1 -5
  14. data/app/views/sessions/new.html.erb +1 -7
  15. data/app/views/shared/_devise_links.erb +15 -0
  16. data/app/views/unlocks/new.html.erb +12 -0
  17. data/generators/devise/templates/migration.rb +2 -0
  18. data/generators/devise/templates/model.rb +4 -1
  19. data/generators/devise_install/templates/devise.rb +20 -10
  20. data/lib/devise.rb +62 -18
  21. data/lib/devise/controllers/common.rb +24 -0
  22. data/lib/devise/controllers/helpers.rb +160 -80
  23. data/lib/devise/controllers/internal_helpers.rb +120 -0
  24. data/lib/devise/controllers/url_helpers.rb +2 -10
  25. data/lib/devise/encryptors/bcrypt.rb +2 -2
  26. data/lib/devise/hooks/activatable.rb +1 -4
  27. data/lib/devise/hooks/rememberable.rb +30 -0
  28. data/lib/devise/hooks/timeoutable.rb +4 -2
  29. data/lib/devise/locales/en.yml +9 -2
  30. data/lib/devise/mapping.rb +15 -11
  31. data/lib/devise/models.rb +16 -35
  32. data/lib/devise/models/activatable.rb +1 -1
  33. data/lib/devise/models/authenticatable.rb +1 -9
  34. data/lib/devise/models/confirmable.rb +6 -2
  35. data/lib/devise/models/lockable.rb +142 -0
  36. data/lib/devise/models/rememberable.rb +19 -2
  37. data/lib/devise/models/timeoutable.rb +1 -2
  38. data/lib/devise/orm/active_record.rb +2 -0
  39. data/lib/devise/orm/data_mapper.rb +1 -1
  40. data/lib/devise/orm/mongo_mapper.rb +12 -1
  41. data/lib/devise/rails/routes.rb +5 -1
  42. data/lib/devise/rails/warden_compat.rb +13 -13
  43. data/lib/devise/schema.rb +7 -0
  44. data/lib/devise/strategies/authenticatable.rb +1 -3
  45. data/lib/devise/strategies/base.rb +1 -1
  46. data/lib/devise/strategies/rememberable.rb +37 -0
  47. data/lib/devise/test_helpers.rb +1 -1
  48. data/lib/devise/version.rb +1 -1
  49. data/test/controllers/helpers_test.rb +155 -33
  50. data/test/controllers/internal_helpers_test.rb +55 -0
  51. data/test/devise_test.rb +24 -3
  52. data/test/encryptors_test.rb +3 -1
  53. data/test/integration/lockable_test.rb +83 -0
  54. data/test/integration/rememberable_test.rb +1 -1
  55. data/test/mailers/unlock_instructions_test.rb +62 -0
  56. data/test/models/authenticatable_test.rb +0 -23
  57. data/test/models/lockable_test.rb +202 -0
  58. data/test/models/timeoutable_test.rb +7 -7
  59. data/test/models/validatable_test.rb +2 -2
  60. data/test/models_test.rb +9 -76
  61. data/test/orm/active_record.rb +1 -0
  62. data/test/orm/mongo_mapper.rb +0 -1
  63. data/test/rails_app/app/active_record/admin.rb +1 -1
  64. data/test/rails_app/app/active_record/user.rb +2 -1
  65. data/test/rails_app/app/mongo_mapper/admin.rb +1 -1
  66. data/test/rails_app/app/mongo_mapper/user.rb +2 -1
  67. data/test/rails_app/config/initializers/devise.rb +13 -10
  68. data/test/rails_app/config/routes.rb +5 -3
  69. data/test/routes_test.rb +5 -0
  70. data/test/support/integration_tests_helper.rb +1 -0
  71. metadata +16 -12
  72. data/lib/devise/controllers/filters.rb +0 -186
  73. data/lib/devise/models/cookie_serializer.rb +0 -21
  74. data/lib/devise/models/session_serializer.rb +0 -19
  75. data/lib/devise/serializers/base.rb +0 -23
  76. data/lib/devise/serializers/cookie.rb +0 -43
  77. data/lib/devise/serializers/session.rb +0 -22
  78. data/test/controllers/filters_test.rb +0 -177
  79. data/test/rails_app/app/active_record/account.rb +0 -7
  80. data/test/rails_app/app/mongo_mapper/account.rb +0 -9
@@ -3,26 +3,26 @@ require 'test/test_helper'
3
3
  class TimeoutableTest < ActiveSupport::TestCase
4
4
 
5
5
  test 'should be expired' do
6
- assert new_user.timeout?(31.minutes.ago)
6
+ assert new_user.timedout?(31.minutes.ago)
7
7
  end
8
8
 
9
9
  test 'should not be expired' do
10
- assert_not new_user.timeout?(29.minutes.ago)
10
+ assert_not new_user.timedout?(29.minutes.ago)
11
11
  end
12
12
 
13
13
  test 'should not be expired when params is nil' do
14
- assert_not new_user.timeout?(nil)
14
+ assert_not new_user.timedout?(nil)
15
15
  end
16
16
 
17
17
  test 'fallback to Devise config option' do
18
18
  swap Devise, :timeout_in => 1.minute do
19
19
  user = new_user
20
- assert user.timeout?(2.minutes.ago)
21
- assert_not user.timeout?(30.seconds.ago)
20
+ assert user.timedout?(2.minutes.ago)
21
+ assert_not user.timedout?(30.seconds.ago)
22
22
 
23
23
  Devise.timeout_in = 5.minutes
24
- assert_not user.timeout?(2.minutes.ago)
25
- assert user.timeout?(6.minutes.ago)
24
+ assert_not user.timedout?(2.minutes.ago)
25
+ assert user.timedout?(6.minutes.ago)
26
26
  end
27
27
  end
28
28
  end
@@ -18,7 +18,7 @@ class ValidatableTest < ActiveSupport::TestCase
18
18
  user.email = existing_user.email
19
19
  assert user.invalid?
20
20
  assert user.errors[:email]
21
- assert_equal 1, user.errors[:email].to_a.size
21
+ assert_equal 1, [*user.errors[:email]].size
22
22
  assert_equal 'has already been taken', user.errors[:email]
23
23
  end
24
24
 
@@ -30,7 +30,7 @@ class ValidatableTest < ActiveSupport::TestCase
30
30
  user.email = email
31
31
  assert user.invalid?, 'should be invalid with email ' << email
32
32
  assert user.errors[:email]
33
- assert_equal 1, user.errors[:email].to_a.size
33
+ assert_equal 1, [*user.errors[:email]].size
34
34
  assert_equal 'is invalid', user.errors[:email]
35
35
  end
36
36
  end
@@ -1,47 +1,9 @@
1
1
  require 'test/test_helper'
2
2
 
3
- class Authenticatable < User
4
- devise :authenticatable
5
- end
6
-
7
- class Confirmable < User
8
- devise :authenticatable, :confirmable
9
- end
10
-
11
- class Recoverable < User
12
- devise :authenticatable, :recoverable
13
- end
14
-
15
- class Rememberable < User
16
- devise :authenticatable, :rememberable
17
- end
18
-
19
- class Trackable < User
20
- devise :authenticatable, :trackable
21
- end
22
-
23
- class Timeoutable < User
24
- devise :authenticatable, :timeoutable
25
- end
26
-
27
- class IsValidatable < User
28
- devise :authenticatable, :validatable
29
- end
30
-
31
- class Devisable < User
32
- devise :all
33
- end
34
-
35
- class Exceptable < User
36
- devise :all, :except => [:recoverable, :rememberable, :validatable]
37
- end
38
-
39
3
  class Configurable < User
40
- devise :all, :timeoutable, :stretches => 15,
41
- :pepper => 'abcdef',
42
- :confirm_within => 5.days,
43
- :remember_for => 7.days,
44
- :timeout_in => 15.minutes
4
+ devise :authenticatable, :confirmable, :rememberable, :timeoutable, :lockable,
5
+ :stretches => 15, :pepper => 'abcdef', :confirm_within => 5.days,
6
+ :remember_for => 7.days, :timeout_in => 15.minutes, :unlock_in => 10.days
45
7
  end
46
8
 
47
9
  class ActiveRecordTest < ActiveSupport::TestCase
@@ -60,41 +22,8 @@ class ActiveRecordTest < ActiveSupport::TestCase
60
22
  end
61
23
  end
62
24
 
63
- test 'add authenticatable module only' do
64
- assert_include_modules Authenticatable, :authenticatable
65
- end
66
-
67
- test 'add confirmable module only' do
68
- assert_include_modules Confirmable, :authenticatable, :confirmable
69
- end
70
-
71
- test 'add recoverable module only' do
72
- assert_include_modules Recoverable, :authenticatable, :recoverable
73
- end
74
-
75
- test 'add rememberable module only' do
76
- assert_include_modules Rememberable, :authenticatable, :rememberable
77
- end
78
-
79
- test 'add trackable module only' do
80
- assert_include_modules Trackable, :authenticatable, :trackable
81
- end
82
-
83
- test 'add timeoutable module only' do
84
- assert_include_modules Timeoutable, :authenticatable, :timeoutable
85
- end
86
-
87
- test 'add validatable module only' do
88
- assert_include_modules IsValidatable, :authenticatable, :validatable
89
- end
90
-
91
- test 'add all modules' do
92
- assert_include_modules Devisable,
93
- :authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
94
- end
95
-
96
- test 'configure modules with except option' do
97
- assert_include_modules Exceptable, :authenticatable, :confirmable, :trackable
25
+ test 'add modules cherry pick' do
26
+ assert_include_modules Admin, :authenticatable, :timeoutable
98
27
  end
99
28
 
100
29
  test 'set a default value for stretches' do
@@ -117,6 +46,10 @@ class ActiveRecordTest < ActiveSupport::TestCase
117
46
  assert_equal 15.minutes, Configurable.timeout_in
118
47
  end
119
48
 
49
+ test 'set a default value for unlock_in' do
50
+ assert_equal 10.days, Configurable.unlock_in
51
+ end
52
+
120
53
  test 'set null fields on migrations' do
121
54
  Admin.create!
122
55
  end
@@ -16,6 +16,7 @@ ActiveRecord::Schema.define(:version => 1) do
16
16
  t.recoverable
17
17
  t.rememberable
18
18
  t.trackable
19
+ t.lockable
19
20
  end
20
21
 
21
22
  t.timestamps
@@ -16,6 +16,5 @@ class ActiveSupport::TestCase
16
16
  setup do
17
17
  User.delete_all
18
18
  Admin.delete_all
19
- Account.delete_all
20
19
  end
21
20
  end
@@ -1,5 +1,5 @@
1
1
  class Admin < ActiveRecord::Base
2
- devise :all, :timeoutable, :except => [:recoverable, :confirmable, :rememberable, :validatable, :trackable]
2
+ devise :authenticatable, :timeoutable
3
3
 
4
4
  def self.find_for_authentication(conditions)
5
5
  last(:conditions => conditions)
@@ -1,4 +1,5 @@
1
1
  class User < ActiveRecord::Base
2
- devise :all, :timeoutable
2
+ devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable,
3
+ :validatable, :timeoutable, :lockable
3
4
  attr_accessible :username, :email, :password, :password_confirmation
4
5
  end
@@ -1,7 +1,7 @@
1
1
  class Admin
2
2
  include MongoMapper::Document
3
3
 
4
- devise :all, :timeoutable, :except => [:recoverable, :confirmable, :rememberable, :validatable, :trackable]
4
+ devise :authenticatable, :timeoutable
5
5
 
6
6
  def self.find_for_authentication(conditions)
7
7
  last(:conditions => conditions, :order => "email")
@@ -1,6 +1,7 @@
1
1
  class User
2
2
  include MongoMapper::Document
3
3
  key :created_at, DateTime
4
- devise :all, :timeoutable
4
+ devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable,
5
+ :validatable, :timeoutable, :lockable
5
6
  # attr_accessible :username, :email, :password, :password_confirmation
6
7
  end
@@ -1,15 +1,6 @@
1
1
  # Use this hook to configure devise mailer, warden hooks and so forth. The first
2
2
  # four configuration values can also be set straight in your models.
3
3
  Devise.setup do |config|
4
- # Configure Devise modules used by default. You should always set this value
5
- # because if Devise adds a new strategy, it won't be added to your application
6
- # by default, unless you configure it here.
7
- #
8
- # Remember that Devise includes other modules on its own (like :activatable
9
- # and :timeoutable) which are not included here and also plugins. So be sure
10
- # to check the docs for a complete set.
11
- config.all = [:authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable]
12
-
13
4
  # Invoke `rake secret` and use the printed value to setup a pepper to generate
14
5
  # the encrypted password. By default no pepper is used.
15
6
  # config.pepper = "rake secret output"
@@ -43,7 +34,7 @@ Devise.setup do |config|
43
34
  # config.timeout_in = 10.minutes
44
35
 
45
36
  # Configure the e-mail address which will be shown in DeviseMailer.
46
- # config.mailer_sender = "foo.bar@yourapp.com"
37
+ config.mailer_sender = "please-change-me-omg@yourapp.com"
47
38
 
48
39
  # Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper.
49
40
  require "devise/orm/#{DEVISE_ORM}"
@@ -54,6 +45,18 @@ Devise.setup do |config|
54
45
  # are using only default views.
55
46
  # config.scoped_views = true
56
47
 
48
+ # Number of authentication tries before locking an account.
49
+ # config.maximum_attempts = 20
50
+
51
+ # Defines which strategy will be used to unlock an account.
52
+ # :email = Sends an unlock link to the user email
53
+ # :time = Reanables login after a certain ammount of time (see :unlock_in below)
54
+ # :both = enables both strategies
55
+ # config.unlock_strategy = :both
56
+
57
+ # Time interval to unlock the account if :time is enabled as unlock_strategy.
58
+ # config.unlock_in = 1.hour
59
+
57
60
  # If you want to use other strategies, that are not (yet) supported by Devise,
58
61
  # you can configure them inside the config.warden block. The example below
59
62
  # allows you to setup OAuth, using http://github.com/roman/warden_oauth
@@ -1,9 +1,11 @@
1
1
  ActionController::Routing::Routes.draw do |map|
2
2
  map.devise_for :users
3
3
  map.devise_for :admin, :as => 'admin_area'
4
- map.devise_for :accounts, :path_names => {
5
- :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification'
6
- }, :scope => 'manager', :path_prefix => '/:locale', :requirements => { :extra => 'value' }
4
+ map.devise_for :accounts, :scope => 'manager', :path_prefix => ':locale',
5
+ :class_name => "User", :requirements => { :extra => 'value' }, :path_names => {
6
+ :sign_in => 'login', :sign_out => 'logout', :password => 'secret',
7
+ :confirmation => 'verification', :unlock => 'unblock'
8
+ }
7
9
 
8
10
  map.resources :users, :only => [:index], :member => { :expire => :get }
9
11
  map.resources :admins, :only => :index
@@ -67,4 +67,9 @@ class MapRoutingTest < ActionController::TestCase
67
67
  test 'map account with custom path name for confirmation' do
68
68
  assert_recognizes({:controller => 'confirmations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/verification/new')
69
69
  end
70
+
71
+ test 'map account with custom path name for unlock' do
72
+ assert_recognizes({:controller => 'unlocks', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/unblock/new')
73
+ end
74
+
70
75
  end
@@ -10,6 +10,7 @@ class ActionController::IntegrationTest
10
10
  :email => 'user@test.com', :password => '123456', :password_confirmation => '123456', :created_at => Time.now.utc
11
11
  )
12
12
  user.confirm! unless options[:confirm] == false
13
+ user.lock! if options[:locked] == true
13
14
  user
14
15
  end
15
16
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: devise
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.2
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - "Jos\xC3\xA9 Valim"
@@ -10,7 +10,7 @@ autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
12
 
13
- date: 2010-01-13 00:00:00 +01:00
13
+ date: 2010-01-21 00:00:00 +01:00
14
14
  default_executable:
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
@@ -21,7 +21,7 @@ dependencies:
21
21
  requirements:
22
22
  - - ~>
23
23
  - !ruby/object:Gem::Version
24
- version: 0.8.1
24
+ version: 0.9.0
25
25
  version:
26
26
  description: Flexible authentication solution for Rails with Warden
27
27
  email: contact@plataformatec.com.br
@@ -41,13 +41,17 @@ files:
41
41
  - app/controllers/confirmations_controller.rb
42
42
  - app/controllers/passwords_controller.rb
43
43
  - app/controllers/sessions_controller.rb
44
+ - app/controllers/unlocks_controller.rb
44
45
  - app/models/devise_mailer.rb
45
46
  - app/views/confirmations/new.html.erb
46
47
  - app/views/devise_mailer/confirmation_instructions.html.erb
47
48
  - app/views/devise_mailer/reset_password_instructions.html.erb
49
+ - app/views/devise_mailer/unlock_instructions.html.erb
48
50
  - app/views/passwords/edit.html.erb
49
51
  - app/views/passwords/new.html.erb
50
52
  - app/views/sessions/new.html.erb
53
+ - app/views/shared/_devise_links.erb
54
+ - app/views/unlocks/new.html.erb
51
55
  - generators/devise/USAGE
52
56
  - generators/devise/devise_generator.rb
53
57
  - generators/devise/lib/route_devise.rb
@@ -61,8 +65,9 @@ files:
61
65
  - generators/devise_views/devise_views_generator.rb
62
66
  - init.rb
63
67
  - lib/devise.rb
64
- - lib/devise/controllers/filters.rb
68
+ - lib/devise/controllers/common.rb
65
69
  - lib/devise/controllers/helpers.rb
70
+ - lib/devise/controllers/internal_helpers.rb
66
71
  - lib/devise/controllers/url_helpers.rb
67
72
  - lib/devise/encryptors/authlogic_sha512.rb
68
73
  - lib/devise/encryptors/base.rb
@@ -73,6 +78,7 @@ files:
73
78
  - lib/devise/encryptors/sha512.rb
74
79
  - lib/devise/failure_app.rb
75
80
  - lib/devise/hooks/activatable.rb
81
+ - lib/devise/hooks/rememberable.rb
76
82
  - lib/devise/hooks/timeoutable.rb
77
83
  - lib/devise/hooks/trackable.rb
78
84
  - lib/devise/locales/en.yml
@@ -81,10 +87,9 @@ files:
81
87
  - lib/devise/models/activatable.rb
82
88
  - lib/devise/models/authenticatable.rb
83
89
  - lib/devise/models/confirmable.rb
84
- - lib/devise/models/cookie_serializer.rb
90
+ - lib/devise/models/lockable.rb
85
91
  - lib/devise/models/recoverable.rb
86
92
  - lib/devise/models/rememberable.rb
87
- - lib/devise/models/session_serializer.rb
88
93
  - lib/devise/models/timeoutable.rb
89
94
  - lib/devise/models/trackable.rb
90
95
  - lib/devise/models/validatable.rb
@@ -95,11 +100,9 @@ files:
95
100
  - lib/devise/rails/routes.rb
96
101
  - lib/devise/rails/warden_compat.rb
97
102
  - lib/devise/schema.rb
98
- - lib/devise/serializers/base.rb
99
- - lib/devise/serializers/cookie.rb
100
- - lib/devise/serializers/session.rb
101
103
  - lib/devise/strategies/authenticatable.rb
102
104
  - lib/devise/strategies/base.rb
105
+ - lib/devise/strategies/rememberable.rb
103
106
  - lib/devise/test_helpers.rb
104
107
  - lib/devise/version.rb
105
108
  has_rdoc: true
@@ -131,23 +134,26 @@ signing_key:
131
134
  specification_version: 3
132
135
  summary: Flexible authentication solution for Rails with Warden
133
136
  test_files:
134
- - test/controllers/filters_test.rb
135
137
  - test/controllers/helpers_test.rb
138
+ - test/controllers/internal_helpers_test.rb
136
139
  - test/controllers/url_helpers_test.rb
137
140
  - test/devise_test.rb
138
141
  - test/encryptors_test.rb
139
142
  - test/failure_app_test.rb
140
143
  - test/integration/authenticatable_test.rb
141
144
  - test/integration/confirmable_test.rb
145
+ - test/integration/lockable_test.rb
142
146
  - test/integration/recoverable_test.rb
143
147
  - test/integration/rememberable_test.rb
144
148
  - test/integration/timeoutable_test.rb
145
149
  - test/integration/trackable_test.rb
146
150
  - test/mailers/confirmation_instructions_test.rb
147
151
  - test/mailers/reset_password_instructions_test.rb
152
+ - test/mailers/unlock_instructions_test.rb
148
153
  - test/mapping_test.rb
149
154
  - test/models/authenticatable_test.rb
150
155
  - test/models/confirmable_test.rb
156
+ - test/models/lockable_test.rb
151
157
  - test/models/recoverable_test.rb
152
158
  - test/models/rememberable_test.rb
153
159
  - test/models/timeoutable_test.rb
@@ -156,7 +162,6 @@ test_files:
156
162
  - test/models_test.rb
157
163
  - test/orm/active_record.rb
158
164
  - test/orm/mongo_mapper.rb
159
- - test/rails_app/app/active_record/account.rb
160
165
  - test/rails_app/app/active_record/admin.rb
161
166
  - test/rails_app/app/active_record/user.rb
162
167
  - test/rails_app/app/controllers/admins_controller.rb
@@ -164,7 +169,6 @@ test_files:
164
169
  - test/rails_app/app/controllers/home_controller.rb
165
170
  - test/rails_app/app/controllers/users_controller.rb
166
171
  - test/rails_app/app/helpers/application_helper.rb
167
- - test/rails_app/app/mongo_mapper/account.rb
168
172
  - test/rails_app/app/mongo_mapper/admin.rb
169
173
  - test/rails_app/app/mongo_mapper/user.rb
170
174
  - test/rails_app/config/boot.rb
@@ -1,186 +0,0 @@
1
- module Devise
2
- module Controllers
3
- # Those filters are convenience methods added to ApplicationController to
4
- # deal with Warden.
5
- module Filters
6
-
7
- def self.included(base)
8
- base.class_eval do
9
- helper_method :warden, :signed_in?, :devise_controller?,
10
- *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?"] }.flatten
11
-
12
- # Use devise default_url_options. We have to declare it here to overwrite
13
- # default definitions.
14
- def default_url_options(options=nil)
15
- Devise::Mapping.default_url_options
16
- end
17
- end
18
- end
19
-
20
- # The main accessor for the warden proxy instance
21
- def warden
22
- request.env['warden']
23
- end
24
-
25
- # Return true if it's a devise_controller. false to all controllers unless
26
- # the controllers defined inside devise. Useful if you want to apply a before
27
- # filter to all controller, except the ones in devise:
28
- #
29
- # before_filter :my_filter, :unless => { |c| c.devise_controller? }
30
- def devise_controller?
31
- false
32
- end
33
-
34
- # Attempts to authenticate the given scope by running authentication hooks,
35
- # but does not redirect in case of failures.
36
- def authenticate(scope)
37
- warden.authenticate(:scope => scope)
38
- end
39
-
40
- # Attempts to authenticate the given scope by running authentication hooks,
41
- # redirecting in case of failures.
42
- def authenticate!(scope)
43
- warden.authenticate!(:scope => scope)
44
- end
45
-
46
- # Check if the given scope is signed in session, without running
47
- # authentication hooks.
48
- def signed_in?(scope)
49
- warden.authenticated?(scope)
50
- end
51
-
52
- # Sign in an user that already was authenticated. This helper is useful for logging
53
- # users in after sign up.
54
- #
55
- # Examples:
56
- #
57
- # sign_in :user, @user # sign_in(scope, resource)
58
- # sign_in @user # sign_in(resource)
59
- #
60
- def sign_in(resource_or_scope, resource=nil)
61
- scope ||= Devise::Mapping.find_scope!(resource_or_scope)
62
- resource ||= resource_or_scope
63
- warden.set_user(resource, :scope => scope)
64
- end
65
-
66
- # Sign out a given user or scope. This helper is useful for signing out an user
67
- # after deleting accounts.
68
- #
69
- # Examples:
70
- #
71
- # sign_out :user # sign_out(scope)
72
- # sign_out @user # sign_out(resource)
73
- #
74
- def sign_out(resource_or_scope)
75
- scope = Devise::Mapping.find_scope!(resource_or_scope)
76
- warden.user(scope) # Without loading user here, before_logout hook is not called
77
- warden.raw_session.inspect # Without this inspect here. The session does not clear.
78
- warden.logout(scope)
79
- end
80
-
81
- # Returns and delete the url stored in the session for the given scope. Useful
82
- # for giving redirect backs after sign up:
83
- #
84
- # Example:
85
- #
86
- # redirect_to stored_location_for(:user) || root_path
87
- #
88
- def stored_location_for(resource_or_scope)
89
- scope = Devise::Mapping.find_scope!(resource_or_scope)
90
- session.delete(:"#{scope}.return_to")
91
- end
92
-
93
- # The default url to be used after signing in. This is used by all Devise
94
- # controllers and you can overwrite it in your ApplicationController to
95
- # provide a custom hook for a custom resource.
96
- #
97
- # By default, it first tries to find a resource_root_path, otherwise it
98
- # uses the root path. For a user scope, you can define the default url in
99
- # the following way:
100
- #
101
- # map.user_root '/users', :controller => 'users' # creates user_root_path
102
- #
103
- # map.resources :users do |users|
104
- # users.root # creates user_root_path
105
- # end
106
- #
107
- # If none of these are defined, root_path is used.
108
- def after_sign_in_path_for(resource_or_scope)
109
- scope = Devise::Mapping.find_scope!(resource_or_scope)
110
- home_path = :"#{scope}_root_path"
111
- respond_to?(home_path, true) ? send(home_path) : root_path
112
- end
113
-
114
- # The default to be used after signing out. This is used by all Devise
115
- # controllers and you can overwrite it in your ApplicationController to
116
- # provide a custom hook for a custom resource.
117
- #
118
- # By default is the root_path.
119
- def after_sign_out_path_for(resource_or_scope)
120
- root_path
121
- end
122
-
123
- # Sign in an user and tries to redirect first to the stored location and
124
- # then to the url specified by after_sign_in_path_for.
125
- #
126
- # If just a symbol is given, consider that the user was already signed in
127
- # through other means and just perform the redirection.
128
- def sign_in_and_redirect(*args)
129
- sign_in(*args) unless args.size == 1 && args.first.is_a?(Symbol)
130
- redirect_to stored_location_for(args.first) || after_sign_in_path_for(args.first)
131
- end
132
-
133
- # Sign out an user and tries to redirect to the url specified by
134
- # after_sign_out_path_for.
135
- def sign_out_and_redirect(resource_or_scope)
136
- sign_out(resource_or_scope)
137
- redirect_to after_sign_out_path_for(resource_or_scope)
138
- end
139
-
140
- # Define authentication filters and accessor helpers based on mappings.
141
- # These filters should be used inside the controllers as before_filters,
142
- # so you can control the scope of the user who should be signed in to
143
- # access that specific controller/action.
144
- # Example:
145
- #
146
- # Maps:
147
- # User => :authenticatable
148
- # Admin => :authenticatable
149
- #
150
- # Generated methods:
151
- # authenticate_user! # Signs user in or redirect
152
- # authenticate_admin! # Signs admin in or redirect
153
- # user_signed_in? # Checks whether there is an user signed in or not
154
- # admin_signed_in? # Checks whether there is an admin signed in or not
155
- # current_user # Current signed in user
156
- # current_admin # Currend signed in admin
157
- # user_session # Session data available only to the user scope
158
- # admin_session # Session data available only to the admin scope
159
- #
160
- # Use:
161
- # before_filter :authenticate_user! # Tell devise to use :user map
162
- # before_filter :authenticate_admin! # Tell devise to use :admin map
163
- #
164
- Devise.mappings.each_key do |mapping|
165
- class_eval <<-METHODS, __FILE__, __LINE__
166
- def authenticate_#{mapping}!
167
- warden.authenticate!(:scope => :#{mapping})
168
- end
169
-
170
- def #{mapping}_signed_in?
171
- warden.authenticated?(:#{mapping})
172
- end
173
-
174
- def current_#{mapping}
175
- @current_#{mapping} ||= warden.user(:#{mapping})
176
- end
177
-
178
- def #{mapping}_session
179
- warden.session(:#{mapping})
180
- end
181
- METHODS
182
- end
183
-
184
- end
185
- end
186
- end