upstream-devise 2.1.0.rc

Sign up to get free protection for your applications and to get access to all the features.
Files changed (215) hide show
  1. data/.gitignore +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +846 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +384 -0
  8. data/Rakefile +34 -0
  9. data/app/controllers/devise/confirmations_controller.rb +43 -0
  10. data/app/controllers/devise/omniauth_callbacks_controller.rb +24 -0
  11. data/app/controllers/devise/passwords_controller.rb +47 -0
  12. data/app/controllers/devise/registrations_controller.rb +107 -0
  13. data/app/controllers/devise/sessions_controller.rb +49 -0
  14. data/app/controllers/devise/unlocks_controller.rb +44 -0
  15. data/app/controllers/devise_controller.rb +177 -0
  16. data/app/helpers/devise_helper.rb +25 -0
  17. data/app/mailers/devise/mailer.rb +15 -0
  18. data/app/views/devise/_links.erb +3 -0
  19. data/app/views/devise/confirmations/new.html.erb +12 -0
  20. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  21. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  22. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  23. data/app/views/devise/passwords/edit.html.erb +16 -0
  24. data/app/views/devise/passwords/new.html.erb +12 -0
  25. data/app/views/devise/registrations/edit.html.erb +25 -0
  26. data/app/views/devise/registrations/new.html.erb +18 -0
  27. data/app/views/devise/sessions/new.html.erb +17 -0
  28. data/app/views/devise/shared/_links.erb +25 -0
  29. data/app/views/devise/unlocks/new.html.erb +12 -0
  30. data/config/locales/en.yml +57 -0
  31. data/devise.gemspec +25 -0
  32. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  33. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  34. data/lib/devise.rb +455 -0
  35. data/lib/devise/controllers/helpers.rb +269 -0
  36. data/lib/devise/controllers/rememberable.rb +52 -0
  37. data/lib/devise/controllers/scoped_views.rb +17 -0
  38. data/lib/devise/controllers/url_helpers.rb +67 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  41. data/lib/devise/encryptors/base.rb +24 -0
  42. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  43. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  44. data/lib/devise/encryptors/sha1.rb +25 -0
  45. data/lib/devise/encryptors/sha512.rb +25 -0
  46. data/lib/devise/failure_app.rb +185 -0
  47. data/lib/devise/hooks/activatable.rb +11 -0
  48. data/lib/devise/hooks/forgetable.rb +9 -0
  49. data/lib/devise/hooks/lockable.rb +7 -0
  50. data/lib/devise/hooks/rememberable.rb +6 -0
  51. data/lib/devise/hooks/timeoutable.rb +22 -0
  52. data/lib/devise/hooks/trackable.rb +9 -0
  53. data/lib/devise/mailers/helpers.rb +86 -0
  54. data/lib/devise/mapping.rb +172 -0
  55. data/lib/devise/models.rb +123 -0
  56. data/lib/devise/models/authenticatable.rb +231 -0
  57. data/lib/devise/models/confirmable.rb +242 -0
  58. data/lib/devise/models/database_authenticatable.rb +126 -0
  59. data/lib/devise/models/encryptable.rb +86 -0
  60. data/lib/devise/models/lockable.rb +185 -0
  61. data/lib/devise/models/omniauthable.rb +27 -0
  62. data/lib/devise/models/recoverable.rb +140 -0
  63. data/lib/devise/models/registerable.rb +25 -0
  64. data/lib/devise/models/rememberable.rb +125 -0
  65. data/lib/devise/models/timeoutable.rb +49 -0
  66. data/lib/devise/models/token_authenticatable.rb +77 -0
  67. data/lib/devise/models/trackable.rb +35 -0
  68. data/lib/devise/models/validatable.rb +66 -0
  69. data/lib/devise/modules.rb +30 -0
  70. data/lib/devise/omniauth.rb +28 -0
  71. data/lib/devise/omniauth/config.rb +45 -0
  72. data/lib/devise/omniauth/url_helpers.rb +33 -0
  73. data/lib/devise/orm/active_record.rb +3 -0
  74. data/lib/devise/orm/mongoid.rb +3 -0
  75. data/lib/devise/param_filter.rb +41 -0
  76. data/lib/devise/rails.rb +54 -0
  77. data/lib/devise/rails/routes.rb +412 -0
  78. data/lib/devise/rails/warden_compat.rb +43 -0
  79. data/lib/devise/strategies/authenticatable.rb +165 -0
  80. data/lib/devise/strategies/base.rb +15 -0
  81. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  82. data/lib/devise/strategies/rememberable.rb +53 -0
  83. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  84. data/lib/devise/test_helpers.rb +130 -0
  85. data/lib/devise/version.rb +3 -0
  86. data/lib/generators/active_record/devise_generator.rb +78 -0
  87. data/lib/generators/active_record/templates/migration.rb +19 -0
  88. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  89. data/lib/generators/devise/devise_generator.rb +24 -0
  90. data/lib/generators/devise/install_generator.rb +24 -0
  91. data/lib/generators/devise/orm_helpers.rb +32 -0
  92. data/lib/generators/devise/views_generator.rb +110 -0
  93. data/lib/generators/mongoid/devise_generator.rb +60 -0
  94. data/lib/generators/templates/README +31 -0
  95. data/lib/generators/templates/devise.rb +216 -0
  96. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  97. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  98. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  99. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  101. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  102. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  103. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  104. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  105. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  106. data/test/controllers/custom_strategy_test.rb +62 -0
  107. data/test/controllers/helpers_test.rb +254 -0
  108. data/test/controllers/internal_helpers_test.rb +97 -0
  109. data/test/controllers/sessions_controller_test.rb +36 -0
  110. data/test/controllers/url_helpers_test.rb +59 -0
  111. data/test/delegator_test.rb +19 -0
  112. data/test/devise_test.rb +72 -0
  113. data/test/encryptors_test.rb +30 -0
  114. data/test/failure_app_test.rb +211 -0
  115. data/test/generators/active_record_generator_test.rb +69 -0
  116. data/test/generators/devise_generator_test.rb +39 -0
  117. data/test/generators/install_generator_test.rb +13 -0
  118. data/test/generators/mongoid_generator_test.rb +23 -0
  119. data/test/generators/views_generator_test.rb +52 -0
  120. data/test/helpers/devise_helper_test.rb +51 -0
  121. data/test/indifferent_hash.rb +33 -0
  122. data/test/integration/authenticatable_test.rb +587 -0
  123. data/test/integration/confirmable_test.rb +255 -0
  124. data/test/integration/database_authenticatable_test.rb +82 -0
  125. data/test/integration/http_authenticatable_test.rb +97 -0
  126. data/test/integration/lockable_test.rb +224 -0
  127. data/test/integration/omniauthable_test.rb +133 -0
  128. data/test/integration/recoverable_test.rb +300 -0
  129. data/test/integration/registerable_test.rb +324 -0
  130. data/test/integration/rememberable_test.rb +158 -0
  131. data/test/integration/timeoutable_test.rb +114 -0
  132. data/test/integration/token_authenticatable_test.rb +161 -0
  133. data/test/integration/trackable_test.rb +92 -0
  134. data/test/mailers/confirmation_instructions_test.rb +95 -0
  135. data/test/mailers/reset_password_instructions_test.rb +83 -0
  136. data/test/mailers/unlock_instructions_test.rb +77 -0
  137. data/test/mapping_test.rb +127 -0
  138. data/test/models/authenticatable_test.rb +7 -0
  139. data/test/models/confirmable_test.rb +357 -0
  140. data/test/models/database_authenticatable_test.rb +189 -0
  141. data/test/models/encryptable_test.rb +73 -0
  142. data/test/models/lockable_test.rb +263 -0
  143. data/test/models/omniauthable_test.rb +7 -0
  144. data/test/models/recoverable_test.rb +205 -0
  145. data/test/models/registerable_test.rb +7 -0
  146. data/test/models/rememberable_test.rb +174 -0
  147. data/test/models/serializable_test.rb +48 -0
  148. data/test/models/timeoutable_test.rb +46 -0
  149. data/test/models/token_authenticatable_test.rb +55 -0
  150. data/test/models/trackable_test.rb +13 -0
  151. data/test/models/validatable_test.rb +117 -0
  152. data/test/models_test.rb +179 -0
  153. data/test/omniauth/config_test.rb +57 -0
  154. data/test/omniauth/url_helpers_test.rb +58 -0
  155. data/test/orm/active_record.rb +9 -0
  156. data/test/orm/mongoid.rb +14 -0
  157. data/test/rails_app/Rakefile +10 -0
  158. data/test/rails_app/app/active_record/admin.rb +6 -0
  159. data/test/rails_app/app/active_record/shim.rb +2 -0
  160. data/test/rails_app/app/active_record/user.rb +6 -0
  161. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  162. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  163. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  164. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  165. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  166. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  167. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  168. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  169. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  170. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  171. data/test/rails_app/app/mongoid/admin.rb +30 -0
  172. data/test/rails_app/app/mongoid/shim.rb +24 -0
  173. data/test/rails_app/app/mongoid/user.rb +45 -0
  174. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  175. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  176. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  177. data/test/rails_app/app/views/home/index.html.erb +1 -0
  178. data/test/rails_app/app/views/home/join.html.erb +1 -0
  179. data/test/rails_app/app/views/home/private.html.erb +1 -0
  180. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  181. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  182. data/test/rails_app/app/views/users/index.html.erb +1 -0
  183. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  184. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  185. data/test/rails_app/config.ru +4 -0
  186. data/test/rails_app/config/application.rb +41 -0
  187. data/test/rails_app/config/boot.rb +8 -0
  188. data/test/rails_app/config/database.yml +18 -0
  189. data/test/rails_app/config/environment.rb +5 -0
  190. data/test/rails_app/config/environments/development.rb +18 -0
  191. data/test/rails_app/config/environments/production.rb +33 -0
  192. data/test/rails_app/config/environments/test.rb +33 -0
  193. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  194. data/test/rails_app/config/initializers/devise.rb +186 -0
  195. data/test/rails_app/config/initializers/inflections.rb +2 -0
  196. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  197. data/test/rails_app/config/routes.rb +90 -0
  198. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +77 -0
  199. data/test/rails_app/db/schema.rb +52 -0
  200. data/test/rails_app/lib/shared_admin.rb +14 -0
  201. data/test/rails_app/lib/shared_user.rb +26 -0
  202. data/test/rails_app/public/404.html +26 -0
  203. data/test/rails_app/public/422.html +26 -0
  204. data/test/rails_app/public/500.html +26 -0
  205. data/test/rails_app/public/favicon.ico +0 -0
  206. data/test/rails_app/script/rails +10 -0
  207. data/test/routes_test.rb +248 -0
  208. data/test/support/assertions.rb +42 -0
  209. data/test/support/helpers.rb +91 -0
  210. data/test/support/integration.rb +90 -0
  211. data/test/support/locale/en.yml +4 -0
  212. data/test/support/webrat/integrations/rails.rb +24 -0
  213. data/test/test_helper.rb +27 -0
  214. data/test/test_helpers_test.rb +134 -0
  215. metadata +451 -0
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+
3
+ require 'rake/testtask'
4
+ require 'rdoc/task'
5
+
6
+ desc 'Default: run tests for all ORMs.'
7
+ task :default => :test
8
+
9
+ desc 'Run Devise tests for all ORMs.'
10
+ task :pre_commit do
11
+ Dir[File.join(File.dirname(__FILE__), 'test', 'orm', '*.rb')].each do |file|
12
+ orm = File.basename(file).split(".").first
13
+ # "Some day, my son, rake's inner wisdom will reveal itself. Until then,
14
+ # take this `system` -- may its brute force protect you well."
15
+ exit 1 unless system "rake test DEVISE_ORM=#{orm}"
16
+ end
17
+ end
18
+
19
+ desc 'Run Devise unit tests.'
20
+ Rake::TestTask.new(:test) do |t|
21
+ t.libs << 'lib'
22
+ t.libs << 'test'
23
+ t.pattern = 'test/**/*_test.rb'
24
+ t.verbose = true
25
+ end
26
+
27
+ desc 'Generate documentation for Devise.'
28
+ Rake::RDocTask.new(:rdoc) do |rdoc|
29
+ rdoc.rdoc_dir = 'rdoc'
30
+ rdoc.title = 'Devise'
31
+ rdoc.options << '--line-numbers' << '--inline-source'
32
+ rdoc.rdoc_files.include('README.rdoc')
33
+ rdoc.rdoc_files.include('lib/**/*.rb')
34
+ end
@@ -0,0 +1,43 @@
1
+ class Devise::ConfirmationsController < DeviseController
2
+ # GET /resource/confirmation/new
3
+ def new
4
+ build_resource({})
5
+ end
6
+
7
+ # POST /resource/confirmation
8
+ def create
9
+ self.resource = resource_class.send_confirmation_instructions(params[resource_name])
10
+
11
+ if successfully_sent?(resource)
12
+ respond_with({}, :location => after_resending_confirmation_instructions_path_for(resource_name))
13
+ else
14
+ respond_with(resource)
15
+ end
16
+ end
17
+
18
+ # GET /resource/confirmation?confirmation_token=abcdef
19
+ def show
20
+ self.resource = resource_class.confirm_by_token(params[:confirmation_token])
21
+
22
+ if resource.errors.empty?
23
+ set_flash_message(:notice, :confirmed) if is_navigational_format?
24
+ sign_in(resource_name, resource)
25
+ respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, resource) }
26
+ else
27
+ respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
28
+ end
29
+ end
30
+
31
+ protected
32
+
33
+ # The path used after resending confirmation instructions.
34
+ def after_resending_confirmation_instructions_path_for(resource_name)
35
+ new_session_path(resource_name)
36
+ end
37
+
38
+ # The path used after confirmation.
39
+ def after_confirmation_path_for(resource_name, resource)
40
+ after_sign_in_path_for(resource)
41
+ end
42
+
43
+ end
@@ -0,0 +1,24 @@
1
+ class Devise::OmniauthCallbacksController < DeviseController
2
+ def failure
3
+ set_flash_message :alert, :failure, :kind => failed_strategy.name.to_s.humanize, :reason => failure_message
4
+ redirect_to after_omniauth_failure_path_for(resource_name)
5
+ end
6
+
7
+ protected
8
+
9
+ def failed_strategy
10
+ env["omniauth.error.strategy"]
11
+ end
12
+
13
+ def failure_message
14
+ exception = env["omniauth.error"]
15
+ error = exception.error_reason if exception.respond_to?(:error_reason)
16
+ error ||= exception.error if exception.respond_to?(:error)
17
+ error ||= env["omniauth.error.type"].to_s
18
+ error.to_s.humanize if error
19
+ end
20
+
21
+ def after_omniauth_failure_path_for(scope)
22
+ new_session_path(scope)
23
+ end
24
+ end
@@ -0,0 +1,47 @@
1
+ class Devise::PasswordsController < DeviseController
2
+ prepend_before_filter :require_no_authentication
3
+
4
+ # GET /resource/password/new
5
+ def new
6
+ build_resource({})
7
+ end
8
+
9
+ # POST /resource/password
10
+ def create
11
+ self.resource = resource_class.send_reset_password_instructions(params[resource_name])
12
+
13
+ if successfully_sent?(resource)
14
+ respond_with({}, :location => after_sending_reset_password_instructions_path_for(resource_name))
15
+ else
16
+ respond_with(resource)
17
+ end
18
+ end
19
+
20
+ # GET /resource/password/edit?reset_password_token=abcdef
21
+ def edit
22
+ self.resource = resource_class.new
23
+ resource.reset_password_token = params[:reset_password_token]
24
+ end
25
+
26
+ # PUT /resource/password
27
+ def update
28
+ self.resource = resource_class.reset_password_by_token(params[resource_name])
29
+
30
+ if resource.errors.empty?
31
+ flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
32
+ set_flash_message(:notice, flash_message) if is_navigational_format?
33
+ sign_in(resource_name, resource)
34
+ respond_with resource, :location => after_sign_in_path_for(resource)
35
+ else
36
+ respond_with resource
37
+ end
38
+ end
39
+
40
+ protected
41
+
42
+ # The path used after sending reset password instructions
43
+ def after_sending_reset_password_instructions_path_for(resource_name)
44
+ new_session_path(resource_name)
45
+ end
46
+
47
+ end
@@ -0,0 +1,107 @@
1
+ class Devise::RegistrationsController < DeviseController
2
+ prepend_before_filter :require_no_authentication, :only => [ :new, :create, :cancel ]
3
+ prepend_before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
4
+
5
+ # GET /resource/sign_up
6
+ def new
7
+ resource = build_resource({})
8
+ respond_with resource
9
+ end
10
+
11
+ # POST /resource
12
+ def create
13
+ build_resource
14
+
15
+ if resource.save
16
+ if resource.active_for_authentication?
17
+ set_flash_message :notice, :signed_up if is_navigational_format?
18
+ sign_in(resource_name, resource)
19
+ respond_with resource, :location => after_sign_up_path_for(resource)
20
+ else
21
+ set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_navigational_format?
22
+ expire_session_data_after_sign_in!
23
+ respond_with resource, :location => after_inactive_sign_up_path_for(resource)
24
+ end
25
+ else
26
+ clean_up_passwords resource
27
+ respond_with resource
28
+ end
29
+ end
30
+
31
+ # GET /resource/edit
32
+ def edit
33
+ render :edit
34
+ end
35
+
36
+ # PUT /resource
37
+ # We need to use a copy of the resource because we don't want to change
38
+ # the current user in place.
39
+ def update
40
+ self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
41
+
42
+ if resource.update_with_password(params[resource_name])
43
+ if is_navigational_format?
44
+ if resource.respond_to?(:pending_reconfirmation?) && resource.pending_reconfirmation?
45
+ flash_key = :update_needs_confirmation
46
+ end
47
+ set_flash_message :notice, flash_key || :updated
48
+ end
49
+ sign_in resource_name, resource, :bypass => true
50
+ respond_with resource, :location => after_update_path_for(resource)
51
+ else
52
+ clean_up_passwords resource
53
+ respond_with resource
54
+ end
55
+ end
56
+
57
+ # DELETE /resource
58
+ def destroy
59
+ resource.destroy
60
+ Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
61
+ set_flash_message :notice, :destroyed if is_navigational_format?
62
+ respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
63
+ end
64
+
65
+ # GET /resource/cancel
66
+ # Forces the session data which is usually expired after sign
67
+ # in to be expired now. This is useful if the user wants to
68
+ # cancel oauth signing in/up in the middle of the process,
69
+ # removing all OAuth session data.
70
+ def cancel
71
+ expire_session_data_after_sign_in!
72
+ redirect_to new_registration_path(resource_name)
73
+ end
74
+
75
+ protected
76
+
77
+ # Build a devise resource passing in the session. Useful to move
78
+ # temporary session data to the newly created user.
79
+ def build_resource(hash=nil)
80
+ hash ||= params[resource_name] || {}
81
+ self.resource = resource_class.new_with_session(hash, session)
82
+ end
83
+
84
+ # The path used after sign up. You need to overwrite this method
85
+ # in your own RegistrationsController.
86
+ def after_sign_up_path_for(resource)
87
+ after_sign_in_path_for(resource)
88
+ end
89
+
90
+ # The path used after sign up for inactive accounts. You need to overwrite
91
+ # this method in your own RegistrationsController.
92
+ def after_inactive_sign_up_path_for(resource)
93
+ respond_to?(:root_path) ? root_path : "/"
94
+ end
95
+
96
+ # The default url to be used after updating a resource. You need to overwrite
97
+ # this method in your own RegistrationsController.
98
+ def after_update_path_for(resource)
99
+ signed_in_root_path(resource)
100
+ end
101
+
102
+ # Authenticates the current scope and gets the current resource from the session.
103
+ def authenticate_scope!
104
+ send(:"authenticate_#{resource_name}!", :force => true)
105
+ self.resource = send(:"current_#{resource_name}")
106
+ end
107
+ end
@@ -0,0 +1,49 @@
1
+ class Devise::SessionsController < DeviseController
2
+ prepend_before_filter :require_no_authentication, :only => [ :new, :create ]
3
+ prepend_before_filter :allow_params_authentication!, :only => :create
4
+
5
+ # GET /resource/sign_in
6
+ def new
7
+ resource = build_resource(nil, :unsafe => true)
8
+ clean_up_passwords(resource)
9
+ respond_with(resource, serialize_options(resource))
10
+ end
11
+
12
+ # POST /resource/sign_in
13
+ def create
14
+ resource = warden.authenticate!(auth_options)
15
+ set_flash_message(:notice, :signed_in) if is_navigational_format?
16
+ sign_in(resource_name, resource)
17
+ respond_with resource, :location => after_sign_in_path_for(resource)
18
+ end
19
+
20
+ # DELETE /resource/sign_out
21
+ def destroy
22
+ redirect_path = after_sign_out_path_for(resource_name)
23
+ signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
24
+ set_flash_message :notice, :signed_out if signed_out
25
+
26
+ # We actually need to hardcode this as Rails default responder doesn't
27
+ # support returning empty response on GET request
28
+ respond_to do |format|
29
+ format.any(*navigational_formats) { redirect_to redirect_path }
30
+ format.all do
31
+ head :no_content
32
+ end
33
+ end
34
+ end
35
+
36
+ protected
37
+
38
+ def serialize_options(resource)
39
+ methods = resource_class.authentication_keys.dup
40
+ methods = methods.keys if methods.is_a?(Hash)
41
+ methods << :password if resource.respond_to?(:password)
42
+ { :methods => methods, :only => [:password] }
43
+ end
44
+
45
+ def auth_options
46
+ { :scope => resource_name, :recall => "#{controller_path}#new" }
47
+ end
48
+ end
49
+
@@ -0,0 +1,44 @@
1
+ class Devise::UnlocksController < DeviseController
2
+ prepend_before_filter :require_no_authentication
3
+
4
+ # GET /resource/unlock/new
5
+ def new
6
+ build_resource({})
7
+ end
8
+
9
+ # POST /resource/unlock
10
+ def create
11
+ self.resource = resource_class.send_unlock_instructions(params[resource_name])
12
+
13
+ if successfully_sent?(resource)
14
+ respond_with({}, :location => after_sending_unlock_instructions_path_for(resource))
15
+ else
16
+ respond_with(resource)
17
+ end
18
+ end
19
+
20
+ # GET /resource/unlock?unlock_token=abcdef
21
+ def show
22
+ self.resource = resource_class.unlock_access_by_token(params[:unlock_token])
23
+
24
+ if resource.errors.empty?
25
+ set_flash_message :notice, :unlocked if is_navigational_format?
26
+ respond_with_navigational(resource){ redirect_to after_unlock_path_for(resource) }
27
+ else
28
+ respond_with_navigational(resource.errors, :status => :unprocessable_entity){ render :new }
29
+ end
30
+ end
31
+
32
+ protected
33
+
34
+ # The path used after sending unlock password instructions
35
+ def after_sending_unlock_instructions_path_for(resource)
36
+ new_session_path(resource)
37
+ end
38
+
39
+ # The path used after unlocking the resource
40
+ def after_unlock_path_for(resource)
41
+ new_session_path(resource)
42
+ end
43
+
44
+ end
@@ -0,0 +1,177 @@
1
+ # All Devise controllers are inherited from here.
2
+ class DeviseController < Devise.parent_controller.constantize
3
+ include Devise::Controllers::ScopedViews
4
+
5
+ helper DeviseHelper
6
+
7
+ helpers = %w(resource scope_name resource_name signed_in_resource
8
+ resource_class devise_mapping)
9
+ hide_action *helpers
10
+ helper_method *helpers
11
+
12
+ prepend_before_filter :assert_is_devise_resource!
13
+ respond_to *Mime::SET.map(&:to_sym) if mimes_for_respond_to.empty?
14
+
15
+ # Gets the actual resource stored in the instance variable
16
+ def resource
17
+ instance_variable_get(:"@#{resource_name}")
18
+ end
19
+
20
+ # Proxy to devise map name
21
+ def resource_name
22
+ devise_mapping.name
23
+ end
24
+ alias :scope_name :resource_name
25
+
26
+ # Proxy to devise map class
27
+ def resource_class
28
+ devise_mapping.to
29
+ end
30
+
31
+ # Returns a signed in resource from session (if one exists)
32
+ def signed_in_resource
33
+ warden.authenticate(:scope => resource_name)
34
+ end
35
+
36
+ # Attempt to find the mapped route for devise based on request path
37
+ def devise_mapping
38
+ @devise_mapping ||= request.env["devise.mapping"]
39
+ end
40
+
41
+ # Override prefixes to consider the scoped view.
42
+ def _prefixes #:nodoc:
43
+ @_prefixes ||= if self.class.scoped_views? && devise_mapping
44
+ super.unshift("#{devise_mapping.scoped_path}/#{controller_name}")
45
+ else
46
+ super
47
+ end
48
+ end
49
+
50
+ hide_action :_prefixes
51
+
52
+ protected
53
+
54
+ # Checks whether it's a devise mapped resource or not.
55
+ def assert_is_devise_resource! #:nodoc:
56
+ unknown_action! <<-MESSAGE unless devise_mapping
57
+ Could not find devise mapping for path #{request.fullpath.inspect}.
58
+ Maybe you forgot to wrap your route inside the scope block? For example:
59
+
60
+ devise_scope :user do
61
+ match "/some/route" => "some_devise_controller"
62
+ end
63
+ MESSAGE
64
+ end
65
+
66
+ # Returns real navigational formats which are supported by Rails
67
+ def navigational_formats
68
+ @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] }
69
+ end
70
+
71
+ def unknown_action!(msg)
72
+ logger.debug "[Devise] #{msg}" if logger
73
+ raise AbstractController::ActionNotFound, msg
74
+ end
75
+
76
+ # Sets the resource creating an instance variable
77
+ def resource=(new_resource)
78
+ instance_variable_set(:"@#{resource_name}", new_resource)
79
+ end
80
+
81
+ # Build a devise resource.
82
+ # Assignment bypasses attribute protection when :unsafe option is passed
83
+ def build_resource(hash = nil, options = {})
84
+ hash ||= params[resource_name] || {}
85
+
86
+ if options[:unsafe]
87
+ self.resource = resource_class.new.tap do |resource|
88
+ hash.each do |key, value|
89
+ setter = :"#{key}="
90
+ resource.send(setter, value) if resource.respond_to?(setter)
91
+ end
92
+ end
93
+ else
94
+ self.resource = resource_class.new(hash)
95
+ end
96
+ end
97
+
98
+ # Helper for use in before_filters where no authentication is required.
99
+ #
100
+ # Example:
101
+ # before_filter :require_no_authentication, :only => :new
102
+ def require_no_authentication
103
+ assert_is_devise_resource!
104
+ return unless is_navigational_format?
105
+ no_input = devise_mapping.no_input_strategies
106
+
107
+ authenticated = if no_input.present?
108
+ args = no_input.dup.push :scope => resource_name
109
+ warden.authenticate?(*args)
110
+ else
111
+ warden.authenticated?(resource_name)
112
+ end
113
+
114
+ if authenticated && resource = warden.user(resource_name)
115
+ flash[:alert] = I18n.t("devise.failure.already_authenticated")
116
+ redirect_to after_sign_in_path_for(resource)
117
+ end
118
+ end
119
+
120
+ # Helper for use after calling send_*_instructions methods on a resource.
121
+ # If we are in paranoid mode, we always act as if the resource was valid
122
+ # and instructions were sent.
123
+ def successfully_sent?(resource)
124
+ notice = if Devise.paranoid
125
+ resource.errors.clear
126
+ :send_paranoid_instructions
127
+ elsif resource.errors.empty?
128
+ :send_instructions
129
+ end
130
+
131
+ if notice
132
+ set_flash_message :notice, notice if is_navigational_format?
133
+ true
134
+ end
135
+ end
136
+
137
+ # Sets the flash message with :key, using I18n. By default you are able
138
+ # to setup your messages using specific resource scope, and if no one is
139
+ # found we look to default scope.
140
+ # Example (i18n locale file):
141
+ #
142
+ # en:
143
+ # devise:
144
+ # passwords:
145
+ # #default_scope_messages - only if resource_scope is not found
146
+ # user:
147
+ # #resource_scope_messages
148
+ #
149
+ # Please refer to README or en.yml locale file to check what messages are
150
+ # available.
151
+ def set_flash_message(key, kind, options={})
152
+ options[:scope] = "devise.#{controller_name}"
153
+ options[:default] = Array(options[:default]).unshift(kind.to_sym)
154
+ options[:resource_name] = resource_name
155
+ options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
156
+ message = I18n.t("#{resource_name}.#{kind}", options)
157
+ flash[key] = message if message.present?
158
+ end
159
+
160
+ def clean_up_passwords(object)
161
+ object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
162
+ end
163
+
164
+ def respond_with_navigational(*args, &block)
165
+ respond_with(*args) do |format|
166
+ format.any(*navigational_formats, &block)
167
+ end
168
+ end
169
+
170
+ def request_format
171
+ @request_format ||= request.format.try(:ref)
172
+ end
173
+
174
+ def is_navigational_format?
175
+ Devise.navigational_formats.include?(request.format.try(:ref))
176
+ end
177
+ end