deviseOne 1.0.0

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 (246) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +12 -0
  3. data/.travis.yml +38 -0
  4. data/.yardopts +9 -0
  5. data/CHANGELOG.md +1117 -0
  6. data/CONTRIBUTING.md +14 -0
  7. data/Gemfile +29 -0
  8. data/Gemfile.lock +199 -0
  9. data/MIT-LICENSE +20 -0
  10. data/README.md +529 -0
  11. data/Rakefile +35 -0
  12. data/app/controllers/devise/confirmations_controller.rb +47 -0
  13. data/app/controllers/devise/omniauth_callbacks_controller.rb +30 -0
  14. data/app/controllers/devise/passwords_controller.rb +71 -0
  15. data/app/controllers/devise/registrations_controller.rb +143 -0
  16. data/app/controllers/devise/sessions_controller.rb +166 -0
  17. data/app/controllers/devise/unlocks_controller.rb +46 -0
  18. data/app/controllers/devise_controller.rb +193 -0
  19. data/app/helpers/devise_helper.rb +25 -0
  20. data/app/mailers/devise/mailer.rb +20 -0
  21. data/app/views/devise/confirmations/new.html.erb +16 -0
  22. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  23. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  24. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  25. data/app/views/devise/passwords/edit.html.erb +25 -0
  26. data/app/views/devise/passwords/new.html.erb +16 -0
  27. data/app/views/devise/registrations/edit.html.erb +39 -0
  28. data/app/views/devise/registrations/new.html.erb +29 -0
  29. data/app/views/devise/sessions/new.html.erb +27 -0
  30. data/app/views/devise/shared/_links.html.erb +21 -0
  31. data/app/views/devise/unlocks/new.html.erb +16 -0
  32. data/config/locales/en.yml +70 -0
  33. data/devise.gemspec +33 -0
  34. data/devise.png +0 -0
  35. data/gemfiles/Gemfile.rails-3.2-stable +29 -0
  36. data/gemfiles/Gemfile.rails-3.2-stable.lock +169 -0
  37. data/gemfiles/Gemfile.rails-4.0-stable +29 -0
  38. data/gemfiles/Gemfile.rails-4.0-stable.lock +165 -0
  39. data/gemfiles/Gemfile.rails-4.1-stable +29 -0
  40. data/gemfiles/Gemfile.rails-4.1-stable.lock +170 -0
  41. data/lib/devise.rb +499 -0
  42. data/lib/devise/controllers/helpers.rb +284 -0
  43. data/lib/devise/controllers/rememberable.rb +47 -0
  44. data/lib/devise/controllers/scoped_views.rb +17 -0
  45. data/lib/devise/controllers/sign_in_out.rb +102 -0
  46. data/lib/devise/controllers/store_location.rb +58 -0
  47. data/lib/devise/controllers/url_helpers.rb +69 -0
  48. data/lib/devise/delegator.rb +16 -0
  49. data/lib/devise/failure_app.rb +212 -0
  50. data/lib/devise/hooks/activatable.rb +10 -0
  51. data/lib/devise/hooks/csrf_cleaner.rb +7 -0
  52. data/lib/devise/hooks/forgetable.rb +9 -0
  53. data/lib/devise/hooks/lockable.rb +7 -0
  54. data/lib/devise/hooks/proxy.rb +21 -0
  55. data/lib/devise/hooks/rememberable.rb +7 -0
  56. data/lib/devise/hooks/timeoutable.rb +35 -0
  57. data/lib/devise/hooks/trackable.rb +9 -0
  58. data/lib/devise/mailers/helpers.rb +90 -0
  59. data/lib/devise/mapping.rb +175 -0
  60. data/lib/devise/models.rb +119 -0
  61. data/lib/devise/models/authenticatable.rb +290 -0
  62. data/lib/devise/models/confirmable.rb +305 -0
  63. data/lib/devise/models/database_authenticatable.rb +164 -0
  64. data/lib/devise/models/lockable.rb +196 -0
  65. data/lib/devise/models/omniauthable.rb +27 -0
  66. data/lib/devise/models/recoverable.rb +157 -0
  67. data/lib/devise/models/registerable.rb +25 -0
  68. data/lib/devise/models/rememberable.rb +142 -0
  69. data/lib/devise/models/timeoutable.rb +49 -0
  70. data/lib/devise/models/trackable.rb +38 -0
  71. data/lib/devise/models/validatable.rb +66 -0
  72. data/lib/devise/modules.rb +28 -0
  73. data/lib/devise/omniauth.rb +28 -0
  74. data/lib/devise/omniauth/config.rb +45 -0
  75. data/lib/devise/omniauth/url_helpers.rb +18 -0
  76. data/lib/devise/orm/active_record.rb +3 -0
  77. data/lib/devise/orm/mongoid.rb +3 -0
  78. data/lib/devise/parameter_filter.rb +40 -0
  79. data/lib/devise/parameter_sanitizer.rb +99 -0
  80. data/lib/devise/rails.rb +56 -0
  81. data/lib/devise/rails/routes.rb +495 -0
  82. data/lib/devise/rails/warden_compat.rb +22 -0
  83. data/lib/devise/strategies/authenticatable.rb +173 -0
  84. data/lib/devise/strategies/base.rb +20 -0
  85. data/lib/devise/strategies/database_authenticatable.rb +24 -0
  86. data/lib/devise/strategies/rememberable.rb +59 -0
  87. data/lib/devise/test_helpers.rb +132 -0
  88. data/lib/devise/time_inflector.rb +14 -0
  89. data/lib/devise/token_generator.rb +70 -0
  90. data/lib/devise/version.rb +3 -0
  91. data/lib/generators/active_record/devise_generator.rb +91 -0
  92. data/lib/generators/active_record/templates/migration.rb +18 -0
  93. data/lib/generators/active_record/templates/migration_existing.rb +25 -0
  94. data/lib/generators/devise/controllers_generator.rb +44 -0
  95. data/lib/generators/devise/devise_generator.rb +26 -0
  96. data/lib/generators/devise/install_generator.rb +29 -0
  97. data/lib/generators/devise/orm_helpers.rb +51 -0
  98. data/lib/generators/devise/views_generator.rb +135 -0
  99. data/lib/generators/mongoid/devise_generator.rb +55 -0
  100. data/lib/generators/templates/README +35 -0
  101. data/lib/generators/templates/controllers/README +14 -0
  102. data/lib/generators/templates/controllers/confirmations_controller.rb +28 -0
  103. data/lib/generators/templates/controllers/omniauth_callbacks_controller.rb +28 -0
  104. data/lib/generators/templates/controllers/passwords_controller.rb +32 -0
  105. data/lib/generators/templates/controllers/registrations_controller.rb +60 -0
  106. data/lib/generators/templates/controllers/sessions_controller.rb +25 -0
  107. data/lib/generators/templates/controllers/unlocks_controller.rb +28 -0
  108. data/lib/generators/templates/devise.rb +263 -0
  109. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  110. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  111. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  112. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +16 -0
  113. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  114. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  115. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +27 -0
  116. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  117. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  118. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +16 -0
  119. data/script/cached-bundle +49 -0
  120. data/script/s3-put +71 -0
  121. data/test/controllers/custom_registrations_controller_test.rb +35 -0
  122. data/test/controllers/custom_strategy_test.rb +62 -0
  123. data/test/controllers/helpers_test.rb +316 -0
  124. data/test/controllers/internal_helpers_test.rb +129 -0
  125. data/test/controllers/load_hooks_controller_test.rb +19 -0
  126. data/test/controllers/passwords_controller_test.rb +31 -0
  127. data/test/controllers/sessions_controller_test.rb +102 -0
  128. data/test/controllers/url_helpers_test.rb +65 -0
  129. data/test/delegator_test.rb +19 -0
  130. data/test/devise_test.rb +107 -0
  131. data/test/failure_app_test.rb +275 -0
  132. data/test/generators/active_record_generator_test.rb +109 -0
  133. data/test/generators/controllers_generator_test.rb +48 -0
  134. data/test/generators/devise_generator_test.rb +39 -0
  135. data/test/generators/install_generator_test.rb +13 -0
  136. data/test/generators/mongoid_generator_test.rb +23 -0
  137. data/test/generators/views_generator_test.rb +96 -0
  138. data/test/helpers/devise_helper_test.rb +49 -0
  139. data/test/integration/authenticatable_test.rb +731 -0
  140. data/test/integration/confirmable_test.rb +324 -0
  141. data/test/integration/database_authenticatable_test.rb +94 -0
  142. data/test/integration/http_authenticatable_test.rb +105 -0
  143. data/test/integration/lockable_test.rb +239 -0
  144. data/test/integration/omniauthable_test.rb +133 -0
  145. data/test/integration/recoverable_test.rb +334 -0
  146. data/test/integration/registerable_test.rb +361 -0
  147. data/test/integration/rememberable_test.rb +176 -0
  148. data/test/integration/timeoutable_test.rb +189 -0
  149. data/test/integration/trackable_test.rb +92 -0
  150. data/test/mailers/confirmation_instructions_test.rb +115 -0
  151. data/test/mailers/reset_password_instructions_test.rb +96 -0
  152. data/test/mailers/unlock_instructions_test.rb +91 -0
  153. data/test/mapping_test.rb +128 -0
  154. data/test/models/authenticatable_test.rb +23 -0
  155. data/test/models/confirmable_test.rb +461 -0
  156. data/test/models/database_authenticatable_test.rb +249 -0
  157. data/test/models/lockable_test.rb +328 -0
  158. data/test/models/omniauthable_test.rb +7 -0
  159. data/test/models/recoverable_test.rb +205 -0
  160. data/test/models/registerable_test.rb +7 -0
  161. data/test/models/rememberable_test.rb +198 -0
  162. data/test/models/serializable_test.rb +49 -0
  163. data/test/models/timeoutable_test.rb +51 -0
  164. data/test/models/trackable_test.rb +41 -0
  165. data/test/models/validatable_test.rb +127 -0
  166. data/test/models_test.rb +144 -0
  167. data/test/omniauth/config_test.rb +57 -0
  168. data/test/omniauth/url_helpers_test.rb +54 -0
  169. data/test/orm/active_record.rb +10 -0
  170. data/test/orm/mongoid.rb +13 -0
  171. data/test/parameter_sanitizer_test.rb +81 -0
  172. data/test/rails_app/Rakefile +6 -0
  173. data/test/rails_app/app/active_record/admin.rb +6 -0
  174. data/test/rails_app/app/active_record/shim.rb +2 -0
  175. data/test/rails_app/app/active_record/user.rb +6 -0
  176. data/test/rails_app/app/active_record/user_on_engine.rb +7 -0
  177. data/test/rails_app/app/active_record/user_on_main_app.rb +7 -0
  178. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  179. data/test/rails_app/app/controllers/admins_controller.rb +11 -0
  180. data/test/rails_app/app/controllers/application_controller.rb +12 -0
  181. data/test/rails_app/app/controllers/application_with_fake_engine.rb +30 -0
  182. data/test/rails_app/app/controllers/custom/registrations_controller.rb +21 -0
  183. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  184. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  185. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  186. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  187. data/test/rails_app/app/controllers/users_controller.rb +31 -0
  188. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  189. data/test/rails_app/app/mailers/users/from_proc_mailer.rb +3 -0
  190. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  191. data/test/rails_app/app/mailers/users/reply_to_mailer.rb +4 -0
  192. data/test/rails_app/app/mongoid/admin.rb +29 -0
  193. data/test/rails_app/app/mongoid/shim.rb +23 -0
  194. data/test/rails_app/app/mongoid/user.rb +39 -0
  195. data/test/rails_app/app/mongoid/user_on_engine.rb +39 -0
  196. data/test/rails_app/app/mongoid/user_on_main_app.rb +39 -0
  197. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  198. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  199. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  200. data/test/rails_app/app/views/home/index.html.erb +1 -0
  201. data/test/rails_app/app/views/home/join.html.erb +1 -0
  202. data/test/rails_app/app/views/home/private.html.erb +1 -0
  203. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  204. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  205. data/test/rails_app/app/views/users/edit_form.html.erb +1 -0
  206. data/test/rails_app/app/views/users/index.html.erb +1 -0
  207. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  208. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  209. data/test/rails_app/bin/bundle +3 -0
  210. data/test/rails_app/bin/rails +4 -0
  211. data/test/rails_app/bin/rake +4 -0
  212. data/test/rails_app/config.ru +4 -0
  213. data/test/rails_app/config/application.rb +40 -0
  214. data/test/rails_app/config/boot.rb +14 -0
  215. data/test/rails_app/config/database.yml +18 -0
  216. data/test/rails_app/config/environment.rb +5 -0
  217. data/test/rails_app/config/environments/development.rb +30 -0
  218. data/test/rails_app/config/environments/production.rb +80 -0
  219. data/test/rails_app/config/environments/test.rb +36 -0
  220. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  221. data/test/rails_app/config/initializers/devise.rb +180 -0
  222. data/test/rails_app/config/initializers/inflections.rb +2 -0
  223. data/test/rails_app/config/initializers/secret_token.rb +8 -0
  224. data/test/rails_app/config/initializers/session_store.rb +1 -0
  225. data/test/rails_app/config/routes.rb +122 -0
  226. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  227. data/test/rails_app/db/schema.rb +55 -0
  228. data/test/rails_app/lib/shared_admin.rb +17 -0
  229. data/test/rails_app/lib/shared_user.rb +29 -0
  230. data/test/rails_app/lib/shared_user_without_omniauth.rb +13 -0
  231. data/test/rails_app/public/404.html +26 -0
  232. data/test/rails_app/public/422.html +26 -0
  233. data/test/rails_app/public/500.html +26 -0
  234. data/test/rails_app/public/favicon.ico +0 -0
  235. data/test/routes_test.rb +264 -0
  236. data/test/support/action_controller/record_identifier.rb +10 -0
  237. data/test/support/assertions.rb +39 -0
  238. data/test/support/helpers.rb +73 -0
  239. data/test/support/integration.rb +92 -0
  240. data/test/support/locale/en.yml +8 -0
  241. data/test/support/mongoid.yml +6 -0
  242. data/test/support/webrat/integrations/rails.rb +24 -0
  243. data/test/test_helper.rb +34 -0
  244. data/test/test_helpers_test.rb +163 -0
  245. data/test/test_models.rb +33 -0
  246. metadata +531 -0
@@ -0,0 +1,35 @@
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
+ t.warning = false
26
+ end
27
+
28
+ desc 'Generate documentation for Devise.'
29
+ Rake::RDocTask.new(:rdoc) do |rdoc|
30
+ rdoc.rdoc_dir = 'rdoc'
31
+ rdoc.title = 'Devise'
32
+ rdoc.options << '--line-numbers' << '--inline-source'
33
+ rdoc.rdoc_files.include('README.md')
34
+ rdoc.rdoc_files.include('lib/**/*.rb')
35
+ end
@@ -0,0 +1,47 @@
1
+ class Devise::ConfirmationsController < DeviseController
2
+ # GET /resource/confirmation/new
3
+ def new
4
+ self.resource = resource_class.new
5
+ end
6
+
7
+ # POST /resource/confirmation
8
+ def create
9
+ self.resource = resource_class.send_confirmation_instructions(resource_params)
10
+ yield resource if block_given?
11
+
12
+ if successfully_sent?(resource)
13
+ respond_with({}, location: after_resending_confirmation_instructions_path_for(resource_name))
14
+ else
15
+ respond_with(resource)
16
+ end
17
+ end
18
+
19
+ # GET /resource/confirmation?confirmation_token=abcdef
20
+ def show
21
+ self.resource = resource_class.confirm_by_token(params[:confirmation_token])
22
+ yield resource if block_given?
23
+
24
+ if resource.errors.empty?
25
+ set_flash_message(:notice, :confirmed) if is_flashing_format?
26
+ respond_with_navigational(resource){ redirect_to after_confirmation_path_for(resource_name, 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 resending confirmation instructions.
35
+ def after_resending_confirmation_instructions_path_for(resource_name)
36
+ is_navigational_format? ? new_session_path(resource_name) : '/'
37
+ end
38
+
39
+ # The path used after confirmation.
40
+ def after_confirmation_path_for(resource_name, resource)
41
+ if signed_in?(resource_name)
42
+ signed_in_root_path(resource)
43
+ else
44
+ new_session_path(resource_name)
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,30 @@
1
+ class Devise::OmniauthCallbacksController < DeviseController
2
+ prepend_before_filter { request.env["devise.skip_timeout"] = true }
3
+
4
+ def passthru
5
+ render status: 404, text: "Not found. Authentication passthru."
6
+ end
7
+
8
+ def failure
9
+ set_flash_message :alert, :failure, kind: OmniAuth::Utils.camelize(failed_strategy.name), reason: failure_message
10
+ redirect_to after_omniauth_failure_path_for(resource_name)
11
+ end
12
+
13
+ protected
14
+
15
+ def failed_strategy
16
+ env["omniauth.error.strategy"]
17
+ end
18
+
19
+ def failure_message
20
+ exception = env["omniauth.error"]
21
+ error = exception.error_reason if exception.respond_to?(:error_reason)
22
+ error ||= exception.error if exception.respond_to?(:error)
23
+ error ||= env["omniauth.error.type"].to_s
24
+ error.to_s.humanize if error
25
+ end
26
+
27
+ def after_omniauth_failure_path_for(scope)
28
+ new_session_path(scope)
29
+ end
30
+ end
@@ -0,0 +1,71 @@
1
+ class Devise::PasswordsController < DeviseController
2
+ prepend_before_filter :require_no_authentication
3
+ # Render the #edit only if coming from a reset password email link
4
+ append_before_filter :assert_reset_token_passed, only: :edit
5
+
6
+ # GET /resource/password/new
7
+ def new
8
+ self.resource = resource_class.new
9
+ end
10
+
11
+ # POST /resource/password
12
+ def create
13
+ self.resource = resource_class.send_reset_password_instructions(resource_params)
14
+ yield resource if block_given?
15
+
16
+ if successfully_sent?(resource)
17
+ respond_with({}, location: after_sending_reset_password_instructions_path_for(resource_name))
18
+ else
19
+ respond_with(resource)
20
+ end
21
+ end
22
+
23
+ # GET /resource/password/edit?reset_password_token=abcdef
24
+ def edit
25
+ self.resource = resource_class.new
26
+ set_minimum_password_length
27
+ resource.reset_password_token = params[:reset_password_token]
28
+ end
29
+
30
+ # PUT /resource/password
31
+ def update
32
+ self.resource = resource_class.reset_password_by_token(resource_params)
33
+ yield resource if block_given?
34
+
35
+ if resource.errors.empty?
36
+ resource.unlock_access! if unlockable?(resource)
37
+ flash_message = resource.active_for_authentication? ? :updated : :updated_not_active
38
+ set_flash_message(:notice, flash_message) if is_flashing_format?
39
+ sign_in(resource_name, resource)
40
+ respond_with resource, location: after_resetting_password_path_for(resource)
41
+ else
42
+ respond_with resource
43
+ end
44
+ end
45
+
46
+ protected
47
+ def after_resetting_password_path_for(resource)
48
+ after_sign_in_path_for(resource)
49
+ end
50
+
51
+ # The path used after sending reset password instructions
52
+ def after_sending_reset_password_instructions_path_for(resource_name)
53
+ new_session_path(resource_name) if is_navigational_format?
54
+ end
55
+
56
+ # Check if a reset_password_token is provided in the request
57
+ def assert_reset_token_passed
58
+ if params[:reset_password_token].blank?
59
+ set_flash_message(:alert, :no_token)
60
+ redirect_to new_session_path(resource_name)
61
+ end
62
+ end
63
+
64
+ # Check if proper Lockable module methods are present & unlock strategy
65
+ # allows to unlock resource on password reset
66
+ def unlockable?(resource)
67
+ resource.respond_to?(:unlock_access!) &&
68
+ resource.respond_to?(:unlock_strategy_enabled?) &&
69
+ resource.unlock_strategy_enabled?(:email)
70
+ end
71
+ end
@@ -0,0 +1,143 @@
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
+ build_resource({})
8
+ set_minimum_password_length
9
+ yield resource if block_given?
10
+ respond_with self.resource
11
+ end
12
+
13
+ # POST /resource
14
+ def create
15
+ build_resource(sign_up_params)
16
+
17
+ resource_saved = resource.save
18
+ yield resource if block_given?
19
+ if resource_saved
20
+ if resource.active_for_authentication?
21
+ set_flash_message :notice, :signed_up if is_flashing_format?
22
+ sign_up(resource_name, resource)
23
+ respond_with resource, location: after_sign_up_path_for(resource)
24
+ else
25
+ set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
26
+ expire_data_after_sign_in!
27
+ respond_with resource, location: after_inactive_sign_up_path_for(resource)
28
+ end
29
+ else
30
+ clean_up_passwords resource
31
+ set_minimum_password_length
32
+ respond_with resource
33
+ end
34
+ end
35
+
36
+ # GET /resource/edit
37
+ def edit
38
+ render :edit
39
+ end
40
+
41
+ # PUT /resource
42
+ # We need to use a copy of the resource because we don't want to change
43
+ # the current user in place.
44
+ def update
45
+ self.resource = resource_class.to_adapter.get!(send(:"current_#{resource_name}").to_key)
46
+ prev_unconfirmed_email = resource.unconfirmed_email if resource.respond_to?(:unconfirmed_email)
47
+
48
+ resource_updated = update_resource(resource, account_update_params)
49
+ yield resource if block_given?
50
+ if resource_updated
51
+ if is_flashing_format?
52
+ flash_key = update_needs_confirmation?(resource, prev_unconfirmed_email) ?
53
+ :update_needs_confirmation : :updated
54
+ set_flash_message :notice, flash_key
55
+ end
56
+ sign_in resource_name, resource, bypass: true
57
+ respond_with resource, location: after_update_path_for(resource)
58
+ else
59
+ clean_up_passwords resource
60
+ respond_with resource
61
+ end
62
+ end
63
+
64
+ # DELETE /resource
65
+ def destroy
66
+ resource.destroy
67
+ Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name)
68
+ set_flash_message :notice, :destroyed if is_flashing_format?
69
+ yield resource if block_given?
70
+ respond_with_navigational(resource){ redirect_to after_sign_out_path_for(resource_name) }
71
+ end
72
+
73
+ # GET /resource/cancel
74
+ # Forces the session data which is usually expired after sign
75
+ # in to be expired now. This is useful if the user wants to
76
+ # cancel oauth signing in/up in the middle of the process,
77
+ # removing all OAuth session data.
78
+ def cancel
79
+ expire_data_after_sign_in!
80
+ redirect_to new_registration_path(resource_name)
81
+ end
82
+
83
+ protected
84
+
85
+ def update_needs_confirmation?(resource, previous)
86
+ resource.respond_to?(:pending_reconfirmation?) &&
87
+ resource.pending_reconfirmation? &&
88
+ previous != resource.unconfirmed_email
89
+ end
90
+
91
+ # By default we want to require a password checks on update.
92
+ # You can overwrite this method in your own RegistrationsController.
93
+ def update_resource(resource, params)
94
+ resource.update_with_password(params)
95
+ end
96
+
97
+ # Build a devise resource passing in the session. Useful to move
98
+ # temporary session data to the newly created user.
99
+ def build_resource(hash=nil)
100
+ self.resource = resource_class.new_with_session(hash || {}, session)
101
+ end
102
+
103
+ # Signs in a user on sign up. You can overwrite this method in your own
104
+ # RegistrationsController.
105
+ def sign_up(resource_name, resource)
106
+ sign_in(resource_name, resource)
107
+ end
108
+
109
+ # The path used after sign up. You need to overwrite this method
110
+ # in your own RegistrationsController.
111
+ def after_sign_up_path_for(resource)
112
+ after_sign_in_path_for(resource)
113
+ end
114
+
115
+ # The path used after sign up for inactive accounts. You need to overwrite
116
+ # this method in your own RegistrationsController.
117
+ def after_inactive_sign_up_path_for(resource)
118
+ scope = Devise::Mapping.find_scope!(resource)
119
+ router_name = Devise.mappings[scope].router_name
120
+ context = router_name ? send(router_name) : self
121
+ context.respond_to?(:root_path) ? context.root_path : "/"
122
+ end
123
+
124
+ # The default url to be used after updating a resource. You need to overwrite
125
+ # this method in your own RegistrationsController.
126
+ def after_update_path_for(resource)
127
+ signed_in_root_path(resource)
128
+ end
129
+
130
+ # Authenticates the current scope and gets the current resource from the session.
131
+ def authenticate_scope!
132
+ send(:"authenticate_#{resource_name}!", force: true)
133
+ self.resource = send(:"current_#{resource_name}")
134
+ end
135
+
136
+ def sign_up_params
137
+ devise_parameter_sanitizer.sanitize(:sign_up)
138
+ end
139
+
140
+ def account_update_params
141
+ devise_parameter_sanitizer.sanitize(:account_update)
142
+ end
143
+ end
@@ -0,0 +1,166 @@
1
+ require 'multimap'
2
+ require 'rest_client'
3
+
4
+ class Devise::SessionsController < DeviseController
5
+ prepend_before_filter :require_no_authentication, only: [ :new, :create ]
6
+ prepend_before_filter :allow_params_authentication!, only: :create
7
+ prepend_before_filter :verify_signed_out_user, only: :destroy
8
+ prepend_before_filter only: [ :create, :destroy ] { request.env["devise.skip_timeout"] = true }
9
+
10
+ # Uncomment if you need mailgun validation
11
+ # prepend_before_filter :get_validate_with_mailgun, only: [ :create ]
12
+
13
+ # GET /resource/sign_in
14
+ def new
15
+ self.resource = resource_class.new(sign_in_params)
16
+ clean_up_passwords(resource)
17
+ yield resource if block_given?
18
+ respond_with(resource, serialize_options(resource))
19
+ end
20
+
21
+ # POST /resource/sign_in
22
+ def create
23
+ existing_user = resource_class.find_by_email(sign_in_params[:email])
24
+
25
+ # if user not exist then sign up else sign in
26
+ if existing_user.nil?
27
+
28
+ build_resource(sign_in_params)
29
+ resource_saved = resource.save
30
+
31
+ yield resource if block_given?
32
+ if resource_saved
33
+ if resource.active_for_authentication?
34
+ flash[:mailgun_suggestion] = ""
35
+ flash[:mailgun_response] = ""
36
+ set_flash_message :notice, :signed_up if is_flashing_format?
37
+ sign_up(resource_name, resource)
38
+ respond_with resource, location: after_sign_up_path_for(resource)
39
+ else
40
+ set_flash_message :notice, :"signed_up_but_#{resource.inactive_message}" if is_flashing_format?
41
+ expire_data_after_sign_in!
42
+ respond_with resource, location: after_inactive_sign_up_path_for(resource)
43
+ end
44
+ else
45
+ clean_up_passwords resource
46
+ @validatable = devise_mapping.validatable?
47
+ if @validatable
48
+ @minimum_password_length = resource_class.password_length.min
49
+ end
50
+ respond_with resource
51
+ end
52
+ else
53
+
54
+ unless existing_user.valid_password?(sign_in_params[:password])
55
+ flash[:notice] = t("devise.failure.verify_password")
56
+ end
57
+
58
+ flash[:mailgun_suggestion] = ""
59
+ flash[:mailgun_response] = ""
60
+ self.resource = warden.authenticate!(auth_options)
61
+ set_flash_message(:notice, :signed_in) if is_flashing_format?
62
+ sign_in(resource_name, resource)
63
+ yield resource if block_given?
64
+ respond_with resource, location: after_sign_in_path_for(resource)
65
+ end
66
+ end
67
+
68
+ # DELETE /resource/sign_out
69
+ def destroy
70
+ signed_out = (Devise.sign_out_all_scopes ? sign_out : sign_out(resource_name))
71
+ set_flash_message :notice, :signed_out if signed_out && is_flashing_format?
72
+ yield if block_given?
73
+ respond_to_on_destroy
74
+ end
75
+
76
+ protected
77
+
78
+ def sign_in_params
79
+ devise_parameter_sanitizer.sanitize(:sign_in)
80
+ end
81
+
82
+ # Build a devise resource passing in the session. Useful to move
83
+ # temporary session data to the newly created user.
84
+ def build_resource(hash=nil)
85
+ self.resource = resource_class.new_with_session(hash || {}, session)
86
+ end
87
+
88
+ # Signs in a user on sign up. You can overwrite this method in your own
89
+ # RegistrationsController.
90
+ def sign_up(resource_name, resource)
91
+ sign_in(resource_name, resource)
92
+ end
93
+
94
+ # The path used after sign up. You need to overwrite this method
95
+ # in your own RegistrationsController.
96
+ def after_sign_up_path_for(resource)
97
+ after_sign_in_path_for(resource)
98
+ end
99
+
100
+ # The path used after sign up for inactive accounts. You need to overwrite
101
+ # this method in your own RegistrationsController.
102
+ def after_inactive_sign_up_path_for(resource)
103
+ scope = Devise::Mapping.find_scope!(resource)
104
+ router_name = Devise.mappings[scope].router_name
105
+ context = router_name ? send(router_name) : self
106
+ context.respond_to?(:root_path) ? context.root_path : "/"
107
+ end
108
+
109
+ def serialize_options(resource)
110
+ methods = resource_class.authentication_keys.dup
111
+ methods = methods.keys if methods.is_a?(Hash)
112
+ methods << :password if resource.respond_to?(:password)
113
+ { methods: methods, only: [:password] }
114
+ end
115
+
116
+ def auth_options
117
+ { scope: resource_name, recall: "#{controller_path}#new" }
118
+ end
119
+
120
+ private
121
+
122
+ # Check if there is no signed in user before doing the sign out.
123
+ #
124
+ # If there is no signed in user, it will set the flash message and redirect
125
+ # to the after_sign_out path.
126
+ def verify_signed_out_user
127
+ if all_signed_out?
128
+ set_flash_message :notice, :already_signed_out if is_flashing_format?
129
+
130
+ respond_to_on_destroy
131
+ end
132
+ end
133
+
134
+ def get_validate_with_mailgun
135
+ url_params = Multimap.new
136
+ url_params[:address] = sign_in_params[:email]
137
+ query_string = url_params.collect {|k, v| "#{k.to_s}=#{CGI::escape(v.to_s)}"}.join("&")
138
+ mailgun_response = RestClient.get "https://api:pubkey-5ogiflzbnjrljiky49qxsiozqef5jxp7@api.mailgun.net/v2/address/validate?#{query_string}"
139
+
140
+ flash[:mailgun_suggestion] = ""
141
+ flash[:mailgun_response] = ""
142
+ unless JSON.parse(mailgun_response)["did_you_mean"].nil?
143
+ flash[:mailgun_suggestion] = t("devise.mailgun.suggestion",
144
+ address: JSON.parse(mailgun_response)["did_you_mean"])
145
+ end
146
+
147
+ unless JSON.parse(mailgun_response)["is_valid"] == true
148
+ flash[:mailgun_response] = t("errors.messages.mailgun_error")
149
+ end
150
+ end
151
+
152
+ def all_signed_out?
153
+ users = Devise.mappings.keys.map { |s| warden.user(scope: s, run_callbacks: false) }
154
+
155
+ users.all?(&:blank?)
156
+ end
157
+
158
+ def respond_to_on_destroy
159
+ # We actually need to hardcode this as Rails default responder doesn't
160
+ # support returning empty response on GET request
161
+ respond_to do |format|
162
+ format.all { head :no_content }
163
+ format.any(*navigational_formats) { redirect_to after_sign_out_path_for(resource_name) }
164
+ end
165
+ end
166
+ end