rmello-devise 2.1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (208) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +845 -0
  4. data/Gemfile +35 -0
  5. data/Gemfile.lock +165 -0
  6. data/MIT-LICENSE +20 -0
  7. data/README.md +383 -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 +184 -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 +440 -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 +16 -0
  40. data/lib/devise/failure_app.rb +187 -0
  41. data/lib/devise/hooks/activatable.rb +11 -0
  42. data/lib/devise/hooks/forgetable.rb +9 -0
  43. data/lib/devise/hooks/lockable.rb +7 -0
  44. data/lib/devise/hooks/rememberable.rb +6 -0
  45. data/lib/devise/hooks/timeoutable.rb +22 -0
  46. data/lib/devise/hooks/trackable.rb +9 -0
  47. data/lib/devise/mailers/helpers.rb +86 -0
  48. data/lib/devise/mapping.rb +172 -0
  49. data/lib/devise/models.rb +128 -0
  50. data/lib/devise/models/authenticatable.rb +231 -0
  51. data/lib/devise/models/confirmable.rb +268 -0
  52. data/lib/devise/models/database_authenticatable.rb +126 -0
  53. data/lib/devise/models/lockable.rb +185 -0
  54. data/lib/devise/models/omniauthable.rb +27 -0
  55. data/lib/devise/models/recoverable.rb +140 -0
  56. data/lib/devise/models/registerable.rb +25 -0
  57. data/lib/devise/models/rememberable.rb +125 -0
  58. data/lib/devise/models/timeoutable.rb +49 -0
  59. data/lib/devise/models/token_authenticatable.rb +77 -0
  60. data/lib/devise/models/trackable.rb +35 -0
  61. data/lib/devise/models/validatable.rb +66 -0
  62. data/lib/devise/modules.rb +29 -0
  63. data/lib/devise/omniauth.rb +28 -0
  64. data/lib/devise/omniauth/config.rb +45 -0
  65. data/lib/devise/omniauth/url_helpers.rb +33 -0
  66. data/lib/devise/orm/active_record.rb +3 -0
  67. data/lib/devise/orm/mongoid.rb +3 -0
  68. data/lib/devise/param_filter.rb +41 -0
  69. data/lib/devise/rails.rb +54 -0
  70. data/lib/devise/rails/routes.rb +426 -0
  71. data/lib/devise/rails/warden_compat.rb +43 -0
  72. data/lib/devise/strategies/authenticatable.rb +176 -0
  73. data/lib/devise/strategies/base.rb +15 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +20 -0
  75. data/lib/devise/strategies/rememberable.rb +55 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +56 -0
  77. data/lib/devise/test_helpers.rb +130 -0
  78. data/lib/devise/version.rb +3 -0
  79. data/lib/generators/active_record/devise_generator.rb +75 -0
  80. data/lib/generators/active_record/templates/migration.rb +19 -0
  81. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  82. data/lib/generators/devise/devise_generator.rb +24 -0
  83. data/lib/generators/devise/install_generator.rb +24 -0
  84. data/lib/generators/devise/orm_helpers.rb +32 -0
  85. data/lib/generators/devise/views_generator.rb +110 -0
  86. data/lib/generators/mongoid/devise_generator.rb +57 -0
  87. data/lib/generators/templates/README +31 -0
  88. data/lib/generators/templates/devise.rb +216 -0
  89. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  90. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  91. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  92. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  93. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  94. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  96. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  97. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  98. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  99. data/test/controllers/custom_strategy_test.rb +62 -0
  100. data/test/controllers/helpers_test.rb +254 -0
  101. data/test/controllers/internal_helpers_test.rb +104 -0
  102. data/test/controllers/sessions_controller_test.rb +43 -0
  103. data/test/controllers/url_helpers_test.rb +59 -0
  104. data/test/delegator_test.rb +19 -0
  105. data/test/devise_test.rb +72 -0
  106. data/test/failure_app_test.rb +221 -0
  107. data/test/generators/active_record_generator_test.rb +69 -0
  108. data/test/generators/devise_generator_test.rb +39 -0
  109. data/test/generators/install_generator_test.rb +13 -0
  110. data/test/generators/mongoid_generator_test.rb +23 -0
  111. data/test/generators/views_generator_test.rb +52 -0
  112. data/test/helpers/devise_helper_test.rb +51 -0
  113. data/test/indifferent_hash.rb +33 -0
  114. data/test/integration/authenticatable_test.rb +587 -0
  115. data/test/integration/confirmable_test.rb +255 -0
  116. data/test/integration/database_authenticatable_test.rb +82 -0
  117. data/test/integration/http_authenticatable_test.rb +97 -0
  118. data/test/integration/lockable_test.rb +224 -0
  119. data/test/integration/omniauthable_test.rb +133 -0
  120. data/test/integration/recoverable_test.rb +300 -0
  121. data/test/integration/registerable_test.rb +324 -0
  122. data/test/integration/rememberable_test.rb +158 -0
  123. data/test/integration/timeoutable_test.rb +114 -0
  124. data/test/integration/token_authenticatable_test.rb +161 -0
  125. data/test/integration/trackable_test.rb +92 -0
  126. data/test/mailers/confirmation_instructions_test.rb +95 -0
  127. data/test/mailers/reset_password_instructions_test.rb +83 -0
  128. data/test/mailers/unlock_instructions_test.rb +77 -0
  129. data/test/mapping_test.rb +127 -0
  130. data/test/models/authenticatable_test.rb +7 -0
  131. data/test/models/confirmable_test.rb +377 -0
  132. data/test/models/database_authenticatable_test.rb +189 -0
  133. data/test/models/lockable_test.rb +263 -0
  134. data/test/models/omniauthable_test.rb +7 -0
  135. data/test/models/recoverable_test.rb +205 -0
  136. data/test/models/registerable_test.rb +7 -0
  137. data/test/models/rememberable_test.rb +174 -0
  138. data/test/models/serializable_test.rb +48 -0
  139. data/test/models/timeoutable_test.rb +46 -0
  140. data/test/models/token_authenticatable_test.rb +55 -0
  141. data/test/models/trackable_test.rb +13 -0
  142. data/test/models/validatable_test.rb +117 -0
  143. data/test/models_test.rb +179 -0
  144. data/test/omniauth/config_test.rb +57 -0
  145. data/test/omniauth/url_helpers_test.rb +58 -0
  146. data/test/orm/active_record.rb +9 -0
  147. data/test/orm/mongoid.rb +14 -0
  148. data/test/rails_app/Rakefile +10 -0
  149. data/test/rails_app/app/active_record/admin.rb +6 -0
  150. data/test/rails_app/app/active_record/mobile_user.rb +6 -0
  151. data/test/rails_app/app/active_record/shim.rb +2 -0
  152. data/test/rails_app/app/active_record/user.rb +6 -0
  153. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  154. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  155. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  156. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  157. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  158. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  159. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  160. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  161. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  162. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  163. data/test/rails_app/app/mongoid/admin.rb +27 -0
  164. data/test/rails_app/app/mongoid/shim.rb +24 -0
  165. data/test/rails_app/app/mongoid/user.rb +42 -0
  166. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  167. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  168. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  169. data/test/rails_app/app/views/home/index.html.erb +1 -0
  170. data/test/rails_app/app/views/home/join.html.erb +1 -0
  171. data/test/rails_app/app/views/home/private.html.erb +1 -0
  172. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  173. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  174. data/test/rails_app/app/views/users/index.html.erb +1 -0
  175. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  176. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  177. data/test/rails_app/config.ru +4 -0
  178. data/test/rails_app/config/application.rb +41 -0
  179. data/test/rails_app/config/boot.rb +8 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +5 -0
  182. data/test/rails_app/config/environments/development.rb +18 -0
  183. data/test/rails_app/config/environments/production.rb +33 -0
  184. data/test/rails_app/config/environments/test.rb +33 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  186. data/test/rails_app/config/initializers/devise.rb +178 -0
  187. data/test/rails_app/config/initializers/inflections.rb +2 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  189. data/test/rails_app/config/routes.rb +93 -0
  190. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +88 -0
  191. data/test/rails_app/db/schema.rb +52 -0
  192. data/test/rails_app/lib/shared_admin.rb +14 -0
  193. data/test/rails_app/lib/shared_mobile_user.rb +13 -0
  194. data/test/rails_app/lib/shared_user.rb +26 -0
  195. data/test/rails_app/public/404.html +26 -0
  196. data/test/rails_app/public/422.html +26 -0
  197. data/test/rails_app/public/500.html +26 -0
  198. data/test/rails_app/public/favicon.ico +0 -0
  199. data/test/rails_app/script/rails +10 -0
  200. data/test/routes_test.rb +248 -0
  201. data/test/support/assertions.rb +40 -0
  202. data/test/support/helpers.rb +97 -0
  203. data/test/support/integration.rb +90 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +24 -0
  206. data/test/test_helper.rb +27 -0
  207. data/test/test_helpers_test.rb +134 -0
  208. metadata +425 -0
@@ -0,0 +1,34 @@
1
+ # encoding: UTF-8
2
+ require "bundler/gem_tasks"
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(resource_params)
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(resource_params)
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(resource_params)
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(resource_params)
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 ||= resource_params || {}
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(resource_params)
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,184 @@
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 resource_params 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
+ def resource_params
32
+ params[resource_name]
33
+ end
34
+
35
+ # Returns a signed in resource from session (if one exists)
36
+ def signed_in_resource
37
+ warden.authenticate(:scope => resource_name)
38
+ end
39
+
40
+ # Attempt to find the mapped route for devise based on request path
41
+ def devise_mapping
42
+ @devise_mapping ||= request.env["devise.mapping"]
43
+ end
44
+
45
+ # Override prefixes to consider the scoped view.
46
+ # Notice we need to check for the request due to a bug in
47
+ # Action Controller tests that forces _prefixes to be
48
+ # loaded before even having a request object.
49
+ def _prefixes #:nodoc:
50
+ @_prefixes ||= if self.class.scoped_views? && request && devise_mapping
51
+ super.unshift("#{devise_mapping.scoped_path}/#{controller_name}")
52
+ else
53
+ super
54
+ end
55
+ end
56
+
57
+ hide_action :_prefixes
58
+
59
+ protected
60
+
61
+ # Checks whether it's a devise mapped resource or not.
62
+ def assert_is_devise_resource! #:nodoc:
63
+ unknown_action! <<-MESSAGE unless devise_mapping
64
+ Could not find devise mapping for path #{request.fullpath.inspect}.
65
+ Maybe you forgot to wrap your route inside the scope block? For example:
66
+
67
+ devise_scope :user do
68
+ match "/some/route" => "some_devise_controller"
69
+ end
70
+ MESSAGE
71
+ end
72
+
73
+ # Returns real navigational formats which are supported by Rails
74
+ def navigational_formats
75
+ @navigational_formats ||= Devise.navigational_formats.select { |format| Mime::EXTENSION_LOOKUP[format.to_s] }
76
+ end
77
+
78
+ def unknown_action!(msg)
79
+ logger.debug "[Devise] #{msg}" if logger
80
+ raise AbstractController::ActionNotFound, msg
81
+ end
82
+
83
+ # Sets the resource creating an instance variable
84
+ def resource=(new_resource)
85
+ instance_variable_set(:"@#{resource_name}", new_resource)
86
+ end
87
+
88
+ # Build a devise resource.
89
+ # Assignment bypasses attribute protection when :unsafe option is passed
90
+ def build_resource(hash = nil, options = {})
91
+ hash ||= resource_params || {}
92
+
93
+ if options[:unsafe]
94
+ self.resource = resource_class.new.tap do |resource|
95
+ hash.each do |key, value|
96
+ setter = :"#{key}="
97
+ resource.send(setter, value) if resource.respond_to?(setter)
98
+ end
99
+ end
100
+ else
101
+ self.resource = resource_class.new(hash)
102
+ end
103
+ end
104
+
105
+ # Helper for use in before_filters where no authentication is required.
106
+ #
107
+ # Example:
108
+ # before_filter :require_no_authentication, :only => :new
109
+ def require_no_authentication
110
+ assert_is_devise_resource!
111
+ return unless is_navigational_format?
112
+ no_input = devise_mapping.no_input_strategies
113
+
114
+ authenticated = if no_input.present?
115
+ args = no_input.dup.push :scope => resource_name
116
+ warden.authenticate?(*args)
117
+ else
118
+ warden.authenticated?(resource_name)
119
+ end
120
+
121
+ if authenticated && resource = warden.user(resource_name)
122
+ flash[:alert] = I18n.t("devise.failure.already_authenticated")
123
+ redirect_to after_sign_in_path_for(resource)
124
+ end
125
+ end
126
+
127
+ # Helper for use after calling send_*_instructions methods on a resource.
128
+ # If we are in paranoid mode, we always act as if the resource was valid
129
+ # and instructions were sent.
130
+ def successfully_sent?(resource)
131
+ notice = if Devise.paranoid
132
+ resource.errors.clear
133
+ :send_paranoid_instructions
134
+ elsif resource.errors.empty?
135
+ :send_instructions
136
+ end
137
+
138
+ if notice
139
+ set_flash_message :notice, notice if is_navigational_format?
140
+ true
141
+ end
142
+ end
143
+
144
+ # Sets the flash message with :key, using I18n. By default you are able
145
+ # to setup your messages using specific resource scope, and if no one is
146
+ # found we look to default scope.
147
+ # Example (i18n locale file):
148
+ #
149
+ # en:
150
+ # devise:
151
+ # passwords:
152
+ # #default_scope_messages - only if resource_scope is not found
153
+ # user:
154
+ # #resource_scope_messages
155
+ #
156
+ # Please refer to README or en.yml locale file to check what messages are
157
+ # available.
158
+ def set_flash_message(key, kind, options={})
159
+ options[:scope] = "devise.#{controller_name}"
160
+ options[:default] = Array(options[:default]).unshift(kind.to_sym)
161
+ options[:resource_name] = resource_name
162
+ options = devise_i18n_options(options) if respond_to?(:devise_i18n_options, true)
163
+ message = I18n.t("#{resource_name}.#{kind}", options)
164
+ flash[key] = message if message.present?
165
+ end
166
+
167
+ def clean_up_passwords(object)
168
+ object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
169
+ end
170
+
171
+ def respond_with_navigational(*args, &block)
172
+ respond_with(*args) do |format|
173
+ format.any(*navigational_formats, &block)
174
+ end
175
+ end
176
+
177
+ def request_format
178
+ @request_format ||= request.format.try(:ref)
179
+ end
180
+
181
+ def is_navigational_format?
182
+ Devise.navigational_formats.include?(request.format.try(:ref))
183
+ end
184
+ end