namxam-devise 1.1.0.win

Sign up to get free protection for your applications and to get access to all the features.
Files changed (152) hide show
  1. data/CHANGELOG.rdoc +455 -0
  2. data/Gemfile +23 -0
  3. data/Gemfile.lock +118 -0
  4. data/MIT-LICENSE +20 -0
  5. data/README.rdoc +311 -0
  6. data/Rakefile +55 -0
  7. data/TODO +3 -0
  8. data/app/controllers/devise/confirmations_controller.rb +33 -0
  9. data/app/controllers/devise/passwords_controller.rb +41 -0
  10. data/app/controllers/devise/registrations_controller.rb +57 -0
  11. data/app/controllers/devise/sessions_controller.rb +23 -0
  12. data/app/controllers/devise/unlocks_controller.rb +34 -0
  13. data/app/helpers/devise_helper.rb +17 -0
  14. data/app/mailers/devise/mailer.rb +71 -0
  15. data/app/views/devise/confirmations/new.html.erb +12 -0
  16. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  17. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  18. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  19. data/app/views/devise/passwords/edit.html.erb +16 -0
  20. data/app/views/devise/passwords/new.html.erb +12 -0
  21. data/app/views/devise/registrations/edit.html.erb +25 -0
  22. data/app/views/devise/registrations/new.html.erb +18 -0
  23. data/app/views/devise/sessions/new.html.erb +17 -0
  24. data/app/views/devise/shared/_links.erb +19 -0
  25. data/app/views/devise/unlocks/new.html.erb +12 -0
  26. data/config/locales/en.yml +39 -0
  27. data/lib/devise.rb +290 -0
  28. data/lib/devise/controllers/helpers.rb +231 -0
  29. data/lib/devise/controllers/internal_helpers.rb +98 -0
  30. data/lib/devise/controllers/scoped_views.rb +35 -0
  31. data/lib/devise/controllers/url_helpers.rb +41 -0
  32. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  33. data/lib/devise/encryptors/base.rb +20 -0
  34. data/lib/devise/encryptors/bcrypt.rb +19 -0
  35. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  36. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  37. data/lib/devise/encryptors/sha1.rb +25 -0
  38. data/lib/devise/encryptors/sha512.rb +25 -0
  39. data/lib/devise/failure_app.rb +107 -0
  40. data/lib/devise/hooks/activatable.rb +11 -0
  41. data/lib/devise/hooks/forgetable.rb +11 -0
  42. data/lib/devise/hooks/rememberable.rb +35 -0
  43. data/lib/devise/hooks/timeoutable.rb +22 -0
  44. data/lib/devise/hooks/trackable.rb +9 -0
  45. data/lib/devise/mapping.rb +103 -0
  46. data/lib/devise/models.rb +80 -0
  47. data/lib/devise/models/authenticatable.rb +126 -0
  48. data/lib/devise/models/confirmable.rb +164 -0
  49. data/lib/devise/models/database_authenticatable.rb +110 -0
  50. data/lib/devise/models/lockable.rb +165 -0
  51. data/lib/devise/models/recoverable.rb +81 -0
  52. data/lib/devise/models/registerable.rb +8 -0
  53. data/lib/devise/models/rememberable.rb +104 -0
  54. data/lib/devise/models/timeoutable.rb +26 -0
  55. data/lib/devise/models/token_authenticatable.rb +60 -0
  56. data/lib/devise/models/trackable.rb +30 -0
  57. data/lib/devise/models/validatable.rb +53 -0
  58. data/lib/devise/modules.rb +23 -0
  59. data/lib/devise/orm/active_record.rb +36 -0
  60. data/lib/devise/orm/mongoid.rb +29 -0
  61. data/lib/devise/path_checker.rb +18 -0
  62. data/lib/devise/rails.rb +69 -0
  63. data/lib/devise/rails/routes.rb +248 -0
  64. data/lib/devise/rails/warden_compat.rb +39 -0
  65. data/lib/devise/schema.rb +97 -0
  66. data/lib/devise/strategies/authenticatable.rb +111 -0
  67. data/lib/devise/strategies/base.rb +33 -0
  68. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  69. data/lib/devise/strategies/rememberable.rb +43 -0
  70. data/lib/devise/strategies/token_authenticatable.rb +49 -0
  71. data/lib/devise/test_helpers.rb +90 -0
  72. data/lib/devise/version.rb +3 -0
  73. data/lib/generators/active_record/devise_generator.rb +28 -0
  74. data/lib/generators/active_record/templates/migration.rb +29 -0
  75. data/lib/generators/devise/devise_generator.rb +17 -0
  76. data/lib/generators/devise/install_generator.rb +24 -0
  77. data/lib/generators/devise/orm_helpers.rb +23 -0
  78. data/lib/generators/devise/templates/README +25 -0
  79. data/lib/generators/devise/templates/devise.rb +139 -0
  80. data/lib/generators/devise/views_generator.rb +63 -0
  81. data/lib/generators/devise_install_generator.rb +4 -0
  82. data/lib/generators/devise_views_generator.rb +4 -0
  83. data/lib/generators/mongoid/devise_generator.rb +17 -0
  84. data/test/controllers/helpers_test.rb +213 -0
  85. data/test/controllers/internal_helpers_test.rb +51 -0
  86. data/test/controllers/url_helpers_test.rb +58 -0
  87. data/test/devise_test.rb +65 -0
  88. data/test/encryptors_test.rb +30 -0
  89. data/test/failure_app_test.rb +123 -0
  90. data/test/integration/authenticatable_test.rb +344 -0
  91. data/test/integration/confirmable_test.rb +104 -0
  92. data/test/integration/database_authenticatable_test.rb +38 -0
  93. data/test/integration/http_authenticatable_test.rb +49 -0
  94. data/test/integration/lockable_test.rb +109 -0
  95. data/test/integration/recoverable_test.rb +141 -0
  96. data/test/integration/registerable_test.rb +153 -0
  97. data/test/integration/rememberable_test.rb +91 -0
  98. data/test/integration/timeoutable_test.rb +80 -0
  99. data/test/integration/token_authenticatable_test.rb +88 -0
  100. data/test/integration/trackable_test.rb +64 -0
  101. data/test/mailers/confirmation_instructions_test.rb +80 -0
  102. data/test/mailers/reset_password_instructions_test.rb +68 -0
  103. data/test/mailers/unlock_instructions_test.rb +62 -0
  104. data/test/mapping_test.rb +85 -0
  105. data/test/models/confirmable_test.rb +221 -0
  106. data/test/models/database_authenticatable_test.rb +148 -0
  107. data/test/models/lockable_test.rb +188 -0
  108. data/test/models/recoverable_test.rb +138 -0
  109. data/test/models/rememberable_test.rb +176 -0
  110. data/test/models/timeoutable_test.rb +28 -0
  111. data/test/models/token_authenticatable_test.rb +37 -0
  112. data/test/models/trackable_test.rb +5 -0
  113. data/test/models/validatable_test.rb +99 -0
  114. data/test/models_test.rb +77 -0
  115. data/test/orm/active_record.rb +9 -0
  116. data/test/orm/mongoid.rb +10 -0
  117. data/test/rails_app/app/active_record/admin.rb +3 -0
  118. data/test/rails_app/app/active_record/shim.rb +2 -0
  119. data/test/rails_app/app/active_record/user.rb +7 -0
  120. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  121. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  122. data/test/rails_app/app/controllers/home_controller.rb +7 -0
  123. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  124. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  125. data/test/rails_app/app/controllers/sessions_controller.rb +6 -0
  126. data/test/rails_app/app/controllers/users_controller.rb +18 -0
  127. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  128. data/test/rails_app/app/mongoid/admin.rb +6 -0
  129. data/test/rails_app/app/mongoid/shim.rb +16 -0
  130. data/test/rails_app/app/mongoid/user.rb +10 -0
  131. data/test/rails_app/config/application.rb +35 -0
  132. data/test/rails_app/config/boot.rb +13 -0
  133. data/test/rails_app/config/environment.rb +5 -0
  134. data/test/rails_app/config/environments/development.rb +19 -0
  135. data/test/rails_app/config/environments/production.rb +33 -0
  136. data/test/rails_app/config/environments/test.rb +33 -0
  137. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  138. data/test/rails_app/config/initializers/devise.rb +136 -0
  139. data/test/rails_app/config/initializers/inflections.rb +2 -0
  140. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  141. data/test/rails_app/config/routes.rb +47 -0
  142. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +27 -0
  143. data/test/rails_app/db/schema.rb +86 -0
  144. data/test/routes_test.rb +146 -0
  145. data/test/support/assertions.rb +24 -0
  146. data/test/support/helpers.rb +54 -0
  147. data/test/support/integration.rb +88 -0
  148. data/test/support/test_silencer.rb +5 -0
  149. data/test/support/webrat/integrations/rails.rb +32 -0
  150. data/test/test_helper.rb +21 -0
  151. data/test/test_helpers_test.rb +72 -0
  152. metadata +230 -0
@@ -0,0 +1,17 @@
1
+ <h2>Sign in</h2>
2
+
3
+ <%= form_for(resource, :as => resource_name, :url => session_path(resource_name)) do |f| %>
4
+ <p><%= f.label :email %><br />
5
+ <%= f.text_field :email %></p>
6
+
7
+ <p><%= f.label :password %><br />
8
+ <%= f.password_field :password %></p>
9
+
10
+ <% if devise_mapping.rememberable? -%>
11
+ <p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
12
+ <% end -%>
13
+
14
+ <p><%= f.submit "Sign in" %></p>
15
+ <% end %>
16
+
17
+ <%= render :partial => "devise/shared/links" %>
@@ -0,0 +1,19 @@
1
+ <%- if controller_name != 'sessions' %>
2
+ <%= link_to "Sign in", new_session_path(resource_name) %><br />
3
+ <% end -%>
4
+
5
+ <%- if devise_mapping.registerable? && controller_name != 'registrations' %>
6
+ <%= link_to "Sign up", new_registration_path(resource_name) %><br />
7
+ <% end -%>
8
+
9
+ <%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
10
+ <%= link_to "Forgot your password?", new_password_path(resource_name) %><br />
11
+ <% end -%>
12
+
13
+ <%- if devise_mapping.confirmable? && controller_name != 'confirmations' %>
14
+ <%= link_to "Didn't receive confirmation instructions?", new_confirmation_path(resource_name) %><br />
15
+ <% end -%>
16
+
17
+ <%- if devise_mapping.lockable? && resource_class.unlock_strategy_enabled?(:email) && controller_name != 'unlocks' %>
18
+ <%= link_to "Didn't receive unlock instructions?", new_unlock_path(resource_name) %><br />
19
+ <% end -%>
@@ -0,0 +1,12 @@
1
+ <h2>Resend unlock instructions</h2>
2
+
3
+ <%= form_for(resource, :as => resource_name, :url => unlock_path(resource_name), :html => { :method => :post }) do |f| %>
4
+ <%= devise_error_messages! %>
5
+
6
+ <p><%= f.label :email %><br />
7
+ <%= f.text_field :email %></p>
8
+
9
+ <p><%= f.submit "Resend unlock instructions" %></p>
10
+ <% end %>
11
+
12
+ <%= render :partial => "devise/shared/links" %>
@@ -0,0 +1,39 @@
1
+ en:
2
+ errors:
3
+ messages:
4
+ not_found: "not found"
5
+ already_confirmed: "was already confirmed"
6
+ not_locked: "was not locked"
7
+
8
+ devise:
9
+ failure:
10
+ unauthenticated: 'You need to sign in or sign up before continuing.'
11
+ unconfirmed: 'You have to confirm your account before continuing.'
12
+ locked: 'Your account is locked.'
13
+ invalid: 'Invalid email or password.'
14
+ invalid_token: 'Invalid authentication token.'
15
+ timeout: 'Your session expired, please sign in again to continue.'
16
+ inactive: 'Your account was not activated yet.'
17
+ sessions:
18
+ signed_in: 'Signed in successfully.'
19
+ signed_out: 'Signed out successfully.'
20
+ passwords:
21
+ send_instructions: 'You will receive an email with instructions about how to reset your password in a few minutes.'
22
+ updated: 'Your password was changed successfully. You are now signed in.'
23
+ confirmations:
24
+ send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
25
+ confirmed: 'Your account was successfully confirmed. You are now signed in.'
26
+ registrations:
27
+ signed_up: 'You have signed up successfully. If enabled, a confirmation was sent to your e-mail.'
28
+ updated: 'You updated your account successfully.'
29
+ destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
30
+ unlocks:
31
+ send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
32
+ unlocked: 'Your account was successfully unlocked. You are now signed in.'
33
+ mailer:
34
+ confirmation_instructions:
35
+ subject: 'Confirmation instructions'
36
+ reset_password_instructions:
37
+ subject: 'Reset password instructions'
38
+ unlock_instructions:
39
+ subject: 'Unlock Instructions'
@@ -0,0 +1,290 @@
1
+ require 'active_support/core_ext/numeric/time'
2
+ require 'active_support/dependencies'
3
+
4
+ module Devise
5
+ autoload :FailureApp, 'devise/failure_app'
6
+ autoload :PathChecker, 'devise/path_checker'
7
+ autoload :Schema, 'devise/schema'
8
+ autoload :TestHelpers, 'devise/test_helpers'
9
+
10
+ module Controllers
11
+ autoload :Helpers, 'devise/controllers/helpers'
12
+ autoload :InternalHelpers, 'devise/controllers/internal_helpers'
13
+ autoload :ScopedViews, 'devise/controllers/scoped_views'
14
+ autoload :UrlHelpers, 'devise/controllers/url_helpers'
15
+ end
16
+
17
+ module Encryptors
18
+ autoload :Base, 'devise/encryptors/base'
19
+ #autoload :Bcrypt, 'devise/encryptors/bcrypt'
20
+ autoload :AuthlogicSha512, 'devise/encryptors/authlogic_sha512'
21
+ autoload :ClearanceSha1, 'devise/encryptors/clearance_sha1'
22
+ autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
23
+ autoload :Sha512, 'devise/encryptors/sha512'
24
+ autoload :Sha1, 'devise/encryptors/sha1'
25
+ end
26
+
27
+ module Strategies
28
+ autoload :Base, 'devise/strategies/base'
29
+ autoload :Authenticatable, 'devise/strategies/authenticatable'
30
+ end
31
+
32
+ # Constants which holds devise configuration for extensions. Those should
33
+ # not be modified by the "end user".
34
+ ALL = []
35
+ CONTROLLERS = ActiveSupport::OrderedHash.new
36
+ ROUTES = ActiveSupport::OrderedHash.new
37
+ STRATEGIES = ActiveSupport::OrderedHash.new
38
+
39
+ # True values used to check params
40
+ TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
41
+
42
+ # Declare encryptors length which are used in migrations.
43
+ ENCRYPTORS_LENGTH = {
44
+ :sha1 => 40,
45
+ :sha512 => 128,
46
+ :clearance_sha1 => 40,
47
+ :restful_authentication_sha1 => 40,
48
+ :authlogic_sha512 => 128,
49
+ :bcrypt => 60
50
+ }
51
+
52
+ # Custom domain for cookies. Not set by default
53
+ mattr_accessor :cookie_domain
54
+ @@cookie_domain = false
55
+
56
+ # Used to encrypt password. Please generate one with rake secret.
57
+ mattr_accessor :pepper
58
+ @@pepper = nil
59
+
60
+ # The number of times to encrypt password.
61
+ mattr_accessor :stretches
62
+ @@stretches = 10
63
+
64
+ # Keys used when authenticating an user.
65
+ mattr_accessor :authentication_keys
66
+ @@authentication_keys = [ :email ]
67
+
68
+ # If http authentication is enabled by default.
69
+ mattr_accessor :http_authenticatable
70
+ @@http_authenticatable = true
71
+
72
+ # If http authentication is used for ajax requests. True by default.
73
+ mattr_accessor :http_authenticatable_on_xhr
74
+ @@http_authenticatable_on_xhr = true
75
+
76
+ # If params authenticatable is enabled by default.
77
+ mattr_accessor :params_authenticatable
78
+ @@params_authenticatable = true
79
+
80
+ # The realm used in Http Basic Authentication.
81
+ mattr_accessor :http_authentication_realm
82
+ @@http_authentication_realm = "Application"
83
+
84
+ # Email regex used to validate email formats. Adapted from authlogic.
85
+ mattr_accessor :email_regexp
86
+ @@email_regexp = /^([\w\.%\+\-]+)@([\w\-]+\.)+([\w]{2,})$/i
87
+
88
+ # Range validation for password length
89
+ mattr_accessor :password_length
90
+ @@password_length = 6..20
91
+
92
+ # Time interval where the remember me token is valid.
93
+ mattr_accessor :remember_for
94
+ @@remember_for = 2.weeks
95
+
96
+ # If a valid remember token can be re-used between multiple browsers.
97
+ mattr_accessor :remember_across_browsers
98
+ @@remember_across_browsers = true
99
+
100
+ # Time interval you can access your account before confirming your account.
101
+ mattr_accessor :confirm_within
102
+ @@confirm_within = 0.days
103
+
104
+ # Time interval to timeout the user session without activity.
105
+ mattr_accessor :timeout_in
106
+ @@timeout_in = 30.minutes
107
+
108
+ # Used to define the password encryption algorithm.
109
+ mattr_accessor :encryptor
110
+ @@encryptor = nil
111
+
112
+ # Store scopes mappings.
113
+ mattr_accessor :mappings
114
+ @@mappings = ActiveSupport::OrderedHash.new
115
+
116
+ # Tells if devise should apply the schema in ORMs where devise declaration
117
+ # and schema belongs to the same class (as Datamapper and Mongoid).
118
+ mattr_accessor :apply_schema
119
+ @@apply_schema = true
120
+
121
+ # Scoped views. Since it relies on fallbacks to render default views, it's
122
+ # turned off by default.
123
+ mattr_accessor :scoped_views
124
+ @@scoped_views = false
125
+
126
+ # Defines which strategy can be used to lock an account.
127
+ # Values: :failed_attempts, :none
128
+ mattr_accessor :lock_strategy
129
+ @@lock_strategy = :failed_attempts
130
+
131
+ # Defines which strategy can be used to unlock an account.
132
+ # Values: :email, :time, :both
133
+ mattr_accessor :unlock_strategy
134
+ @@unlock_strategy = :both
135
+
136
+ # Number of authentication tries before locking an account
137
+ mattr_accessor :maximum_attempts
138
+ @@maximum_attempts = 20
139
+
140
+ # Time interval to unlock the account if :time is defined as unlock_strategy.
141
+ mattr_accessor :unlock_in
142
+ @@unlock_in = 1.hour
143
+
144
+ # The default scope which is used by warden.
145
+ mattr_accessor :default_scope
146
+ @@default_scope = nil
147
+
148
+ # Address which sends Devise e-mails.
149
+ mattr_accessor :mailer_sender
150
+ @@mailer_sender = nil
151
+
152
+ # Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
153
+ mattr_accessor :token_authentication_key
154
+ @@token_authentication_key = :auth_token
155
+
156
+ # Which formats should be treated as navigational.
157
+ mattr_accessor :navigational_formats
158
+ @@navigational_formats = [:html]
159
+
160
+ # Private methods to interface with Warden.
161
+ mattr_accessor :warden_config
162
+ @@warden_config = nil
163
+ @@warden_config_block = nil
164
+
165
+ # When set to true, signing out an user signs out all other scopes.
166
+ mattr_accessor :sign_out_all_scopes
167
+ @@sign_out_all_scopes = false
168
+
169
+ def self.use_default_scope=(*)
170
+ ActiveSupport::Deprecation.warn "config.use_default_scope is deprecated and removed from Devise. " <<
171
+ "If you are using non conventional routes in Devise, all you need to do is to pass the devise " <<
172
+ "scope in the router DSL:\n\n as :user do\n get \"sign_in\", :to => \"devise/sessions\"\n end\n\n" <<
173
+ "The method :as is also aliased to :devise_scope. Choose the one you prefer.", caller
174
+ end
175
+
176
+ # Default way to setup Devise. Run rails generate devise_install to create
177
+ # a fresh initializer with all configuration values.
178
+ def self.setup
179
+ yield self
180
+ end
181
+
182
+ # Get the mailer class from the mailer reference object.
183
+ def self.mailer
184
+ @@mailer_ref.get
185
+ end
186
+
187
+ # Set the mailer reference object to access the mailer.
188
+ def self.mailer=(class_name)
189
+ @@mailer_ref = ActiveSupport::Dependencies.ref(class_name)
190
+ end
191
+ self.mailer = "Devise::Mailer"
192
+
193
+ # Small method that adds a mapping to Devise.
194
+ def self.add_mapping(resource, options)
195
+ mapping = Devise::Mapping.new(resource, options)
196
+ self.mappings[mapping.name] = mapping
197
+ self.default_scope ||= mapping.name
198
+ mapping
199
+ end
200
+
201
+ # Make Devise aware of an 3rd party Devise-module. For convenience.
202
+ #
203
+ # == Options:
204
+ #
205
+ # +model+ - String representing the load path to a custom *model* for this module (to autoload.)
206
+ # +controller+ - Symbol representing the name of an exisiting or custom *controller* for this module.
207
+ # +route+ - Symbol representing the named *route* helper for this module.
208
+ # +flash+ - Symbol representing the *flash messages* used by this helper.
209
+ # +strategy+ - Symbol representing if this module got a custom *strategy*.
210
+ #
211
+ # All values, except :model, accept also a boolean and will have the same name as the given module
212
+ # name.
213
+ #
214
+ # == Examples:
215
+ #
216
+ # Devise.add_module(:party_module)
217
+ # Devise.add_module(:party_module, :strategy => true, :controller => :sessions)
218
+ # Devise.add_module(:party_module, :model => 'party_module/model')
219
+ #
220
+ def self.add_module(module_name, options = {})
221
+ ALL << module_name
222
+ options.assert_valid_keys(:strategy, :model, :controller, :route)
223
+
224
+ config = {
225
+ :strategy => STRATEGIES,
226
+ :route => ROUTES,
227
+ :controller => CONTROLLERS
228
+ }
229
+
230
+ config.each do |key, value|
231
+ next unless options[key]
232
+ name = (options[key] == true ? module_name : options[key])
233
+
234
+ if value.is_a?(Hash)
235
+ value[module_name] = name
236
+ else
237
+ value << name unless value.include?(name)
238
+ end
239
+ end
240
+
241
+ if options[:model]
242
+ model_path = (options[:model] == true ? "devise/models/#{module_name}" : options[:model])
243
+ Devise::Models.send(:autoload, module_name.to_s.camelize.to_sym, model_path)
244
+ end
245
+
246
+ Devise::Mapping.add_module module_name
247
+ end
248
+
249
+ # Sets warden configuration using a block that will be invoked on warden
250
+ # initialization.
251
+ #
252
+ # Devise.initialize do |config|
253
+ # config.confirm_within = 2.days
254
+ #
255
+ # config.warden do |manager|
256
+ # # Configure warden to use other strategies, like oauth.
257
+ # manager.oauth(:twitter)
258
+ # end
259
+ # end
260
+ def self.warden(&block)
261
+ @@warden_config_block = block
262
+ end
263
+
264
+ # A method used internally to setup warden manager from the Rails initialize
265
+ # block.
266
+ def self.configure_warden! #:nodoc:
267
+ @@warden_configured ||= begin
268
+ warden_config.failure_app = Devise::FailureApp
269
+ warden_config.default_scope = Devise.default_scope
270
+
271
+ Devise.mappings.each_value do |mapping|
272
+ warden_config.scope_defaults mapping.name, :strategies => mapping.strategies
273
+ end
274
+
275
+ @@warden_config_block.try :call, Devise.warden_config
276
+ true
277
+ end
278
+ end
279
+
280
+ # Generate a friendly string randomically to be used as token.
281
+ def self.friendly_token
282
+ ActiveSupport::SecureRandom.base64(15).tr('+/=', '-_ ').strip.delete("\n")
283
+ end
284
+ end
285
+
286
+ require 'warden'
287
+ require 'devise/mapping'
288
+ require 'devise/models'
289
+ require 'devise/modules'
290
+ require 'devise/rails'
@@ -0,0 +1,231 @@
1
+ module Devise
2
+ module Controllers
3
+ # Those helpers are convenience methods added to ApplicationController.
4
+ module Helpers
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ helper_method :warden, :signed_in?, :devise_controller?, :anybody_signed_in?,
9
+ *Devise.mappings.keys.map { |m| [:"current_#{m}", :"#{m}_signed_in?", :"#{m}_session"] }.flatten
10
+ end
11
+
12
+ # The main accessor for the warden proxy instance
13
+ def warden
14
+ request.env['warden']
15
+ end
16
+
17
+ # Return true if it's a devise_controller. false to all controllers unless
18
+ # the controllers defined inside devise. Useful if you want to apply a before
19
+ # filter to all controller, except the ones in devise:
20
+ #
21
+ # before_filter :my_filter, :unless => { |c| c.devise_controller? }
22
+ def devise_controller?
23
+ false
24
+ end
25
+
26
+ # Check if the given scope is signed in session, without running
27
+ # authentication hooks.
28
+ def signed_in?(scope)
29
+ warden.authenticate?(:scope => scope)
30
+ end
31
+
32
+ # Check if the any scope is signed in session, without running
33
+ # authentication hooks.
34
+ def anybody_signed_in?
35
+ Devise.mappings.keys.any? { |scope| signed_in?(scope) }
36
+ end
37
+
38
+ # Sign in an user that already was authenticated. This helper is useful for logging
39
+ # users in after sign up.
40
+ #
41
+ # Examples:
42
+ #
43
+ # sign_in :user, @user # sign_in(scope, resource)
44
+ # sign_in @user # sign_in(resource)
45
+ #
46
+ def sign_in(resource_or_scope, resource=nil)
47
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
48
+ resource ||= resource_or_scope
49
+ warden.set_user(resource, :scope => scope)
50
+ end
51
+
52
+ # Sign out a given user or scope. This helper is useful for signing out an user
53
+ # after deleting accounts.
54
+ #
55
+ # Examples:
56
+ #
57
+ # sign_out :user # sign_out(scope)
58
+ # sign_out @user # sign_out(resource)
59
+ #
60
+ def sign_out(resource_or_scope)
61
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
62
+ warden.user(scope) # Without loading user here, before_logout hook is not called
63
+ warden.raw_session.inspect # Without this inspect here. The session does not clear.
64
+ warden.logout(scope)
65
+ end
66
+
67
+ # Sign out all active users or scopes. This helper is useful for signing out all roles
68
+ # in one click.
69
+ def sign_out_all_scopes
70
+ # Not "warden.logout" since we need to sign_out only devise-defined scopes.
71
+ scopes = Devise.mappings.keys
72
+ scopes.each { |scope| warden.user(scope) }
73
+ warden.raw_session.inspect
74
+ warden.logout(*scopes)
75
+ end
76
+
77
+ # Returns and delete the url stored in the session for the given scope. Useful
78
+ # for giving redirect backs after sign up:
79
+ #
80
+ # Example:
81
+ #
82
+ # redirect_to stored_location_for(:user) || root_path
83
+ #
84
+ def stored_location_for(resource_or_scope)
85
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
86
+ session.delete(:"#{scope}_return_to")
87
+ end
88
+
89
+ # The default url to be used after signing in. This is used by all Devise
90
+ # controllers and you can overwrite it in your ApplicationController to
91
+ # provide a custom hook for a custom resource.
92
+ #
93
+ # By default, it first tries to find a resource_root_path, otherwise it
94
+ # uses the root path. For a user scope, you can define the default url in
95
+ # the following way:
96
+ #
97
+ # map.user_root '/users', :controller => 'users' # creates user_root_path
98
+ #
99
+ # map.namespace :user do |user|
100
+ # user.root :controller => 'users' # creates user_root_path
101
+ # end
102
+ #
103
+ #
104
+ # If the resource root path is not defined, root_path is used. However,
105
+ # if this default is not enough, you can customize it, for example:
106
+ #
107
+ # def after_sign_in_path_for(resource)
108
+ # if resource.is_a?(User) && resource.can_publish?
109
+ # publisher_url
110
+ # else
111
+ # super
112
+ # end
113
+ # end
114
+ #
115
+ def after_sign_in_path_for(resource_or_scope)
116
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
117
+ home_path = :"#{scope}_root_path"
118
+ respond_to?(home_path, true) ? send(home_path) : root_path
119
+ end
120
+
121
+ # The default url to be used after updating a resource. This is used by all Devise
122
+ # controllers and you can overwrite it in your ApplicationController to
123
+ # provide a custom hook for a custom resource.
124
+ #
125
+ # By default, it first tries to find a resource_root_path, otherwise it
126
+ # uses the root path. For a user scope, you can define the default url in
127
+ # the following way:
128
+ #
129
+ # map.user_root '/users', :controller => 'users' # creates user_root_path
130
+ #
131
+ # map.resources :users do |users|
132
+ # users.root # creates user_root_path
133
+ # end
134
+ #
135
+ #
136
+ # If none of these are defined, root_path is used. However, if this default
137
+ # is not enough, you can customize it, for example:
138
+ #
139
+ # def after_update_path_for(resource)
140
+ # if resource.is_a?(User) && resource.can_publish?
141
+ # publisher_url
142
+ # else
143
+ # super
144
+ # end
145
+ # end
146
+ #
147
+ def after_update_path_for(resource_or_scope)
148
+ after_sign_in_path_for(resource_or_scope)
149
+ end
150
+
151
+ # Method used by sessions controller to sign out an user. You can overwrite
152
+ # it in your ApplicationController to provide a custom hook for a custom
153
+ # scope. Notice that differently from +after_sign_in_path_for+ this method
154
+ # receives a symbol with the scope, and not the resource.
155
+ #
156
+ # By default is the root_path.
157
+ def after_sign_out_path_for(resource_or_scope)
158
+ root_path
159
+ end
160
+
161
+ # Sign in an user and tries to redirect first to the stored location and
162
+ # then to the url specified by after_sign_in_path_for.
163
+ #
164
+ # If just a symbol is given, consider that the user was already signed in
165
+ # through other means and just perform the redirection.
166
+ def sign_in_and_redirect(resource_or_scope, resource=nil)
167
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
168
+ resource ||= resource_or_scope
169
+ sign_in(scope, resource) unless warden.user(scope) == resource
170
+ redirect_to stored_location_for(scope) || after_sign_in_path_for(resource)
171
+ end
172
+
173
+ # Sign out an user and tries to redirect to the url specified by
174
+ # after_sign_out_path_for.
175
+ def sign_out_and_redirect(resource_or_scope)
176
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
177
+ if Devise.sign_out_all_scopes
178
+ sign_out_all_scopes
179
+ else
180
+ sign_out(scope)
181
+ end
182
+ redirect_to after_sign_out_path_for(scope)
183
+ end
184
+
185
+ # Define authentication filters and accessor helpers based on mappings.
186
+ # These filters should be used inside the controllers as before_filters,
187
+ # so you can control the scope of the user who should be signed in to
188
+ # access that specific controller/action.
189
+ # Example:
190
+ #
191
+ # Roles:
192
+ # User
193
+ # Admin
194
+ #
195
+ # Generated methods:
196
+ # authenticate_user! # Signs user in or redirect
197
+ # authenticate_admin! # Signs admin in or redirect
198
+ # user_signed_in? # Checks whether there is an user signed in or not
199
+ # admin_signed_in? # Checks whether there is an admin signed in or not
200
+ # current_user # Current signed in user
201
+ # current_admin # Currend signed in admin
202
+ # user_session # Session data available only to the user scope
203
+ # admin_session # Session data available only to the admin scope
204
+ #
205
+ # Use:
206
+ # before_filter :authenticate_user! # Tell devise to use :user map
207
+ # before_filter :authenticate_admin! # Tell devise to use :admin map
208
+ #
209
+ Devise.mappings.each_key do |mapping|
210
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
211
+ def authenticate_#{mapping}!
212
+ warden.authenticate!(:scope => :#{mapping})
213
+ end
214
+
215
+ def #{mapping}_signed_in?
216
+ warden.authenticate?(:scope => :#{mapping})
217
+ end
218
+
219
+ def current_#{mapping}
220
+ @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
221
+ end
222
+
223
+ def #{mapping}_session
224
+ current_#{mapping} && warden.session(:#{mapping})
225
+ end
226
+ METHODS
227
+ end
228
+
229
+ end
230
+ end
231
+ end