cloudfoundry-devise 1.5.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (210) hide show
  1. data/.gitignore +12 -0
  2. data/.travis.yml +13 -0
  3. data/CHANGELOG.rdoc +755 -0
  4. data/Gemfile +35 -0
  5. data/MIT-LICENSE +20 -0
  6. data/README.rdoc +366 -0
  7. data/Rakefile +34 -0
  8. data/app/controllers/devise/confirmations_controller.rb +46 -0
  9. data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
  10. data/app/controllers/devise/passwords_controller.rb +50 -0
  11. data/app/controllers/devise/registrations_controller.rb +114 -0
  12. data/app/controllers/devise/sessions_controller.rb +49 -0
  13. data/app/controllers/devise/unlocks_controller.rb +34 -0
  14. data/app/helpers/devise_helper.rb +25 -0
  15. data/app/mailers/devise/mailer.rb +15 -0
  16. data/app/views/devise/confirmations/new.html.erb +12 -0
  17. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  18. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  19. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  20. data/app/views/devise/passwords/edit.html.erb +16 -0
  21. data/app/views/devise/passwords/new.html.erb +12 -0
  22. data/app/views/devise/registrations/edit.html.erb +25 -0
  23. data/app/views/devise/registrations/new.html.erb +18 -0
  24. data/app/views/devise/sessions/new.html.erb +17 -0
  25. data/app/views/devise/shared/_links.erb +25 -0
  26. data/app/views/devise/unlocks/new.html.erb +12 -0
  27. data/cloudfoundry-devise.gemspec +25 -0
  28. data/config/locales/en.yml +59 -0
  29. data/lib/devise.rb +453 -0
  30. data/lib/devise/controllers/helpers.rb +260 -0
  31. data/lib/devise/controllers/internal_helpers.rb +161 -0
  32. data/lib/devise/controllers/rememberable.rb +52 -0
  33. data/lib/devise/controllers/scoped_views.rb +33 -0
  34. data/lib/devise/controllers/shared_helpers.rb +26 -0
  35. data/lib/devise/controllers/url_helpers.rb +53 -0
  36. data/lib/devise/delegator.rb +16 -0
  37. data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
  38. data/lib/devise/encryptors/base.rb +20 -0
  39. data/lib/devise/encryptors/clearance_sha1.rb +17 -0
  40. data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
  41. data/lib/devise/encryptors/sha1.rb +25 -0
  42. data/lib/devise/encryptors/sha512.rb +25 -0
  43. data/lib/devise/failure_app.rb +149 -0
  44. data/lib/devise/hooks/activatable.rb +11 -0
  45. data/lib/devise/hooks/forgetable.rb +9 -0
  46. data/lib/devise/hooks/rememberable.rb +6 -0
  47. data/lib/devise/hooks/timeoutable.rb +24 -0
  48. data/lib/devise/hooks/trackable.rb +9 -0
  49. data/lib/devise/mailers/helpers.rb +86 -0
  50. data/lib/devise/mapping.rb +175 -0
  51. data/lib/devise/models.rb +91 -0
  52. data/lib/devise/models/authenticatable.rb +181 -0
  53. data/lib/devise/models/confirmable.rb +220 -0
  54. data/lib/devise/models/database_authenticatable.rb +122 -0
  55. data/lib/devise/models/encryptable.rb +72 -0
  56. data/lib/devise/models/lockable.rb +169 -0
  57. data/lib/devise/models/omniauthable.rb +23 -0
  58. data/lib/devise/models/recoverable.rb +136 -0
  59. data/lib/devise/models/registerable.rb +21 -0
  60. data/lib/devise/models/rememberable.rb +114 -0
  61. data/lib/devise/models/serializable.rb +43 -0
  62. data/lib/devise/models/timeoutable.rb +45 -0
  63. data/lib/devise/models/token_authenticatable.rb +72 -0
  64. data/lib/devise/models/trackable.rb +30 -0
  65. data/lib/devise/models/validatable.rb +62 -0
  66. data/lib/devise/modules.rb +30 -0
  67. data/lib/devise/omniauth.rb +28 -0
  68. data/lib/devise/omniauth/config.rb +45 -0
  69. data/lib/devise/omniauth/url_helpers.rb +33 -0
  70. data/lib/devise/orm/active_record.rb +44 -0
  71. data/lib/devise/orm/mongoid.rb +31 -0
  72. data/lib/devise/param_filter.rb +41 -0
  73. data/lib/devise/path_checker.rb +18 -0
  74. data/lib/devise/rails.rb +73 -0
  75. data/lib/devise/rails/routes.rb +385 -0
  76. data/lib/devise/rails/warden_compat.rb +120 -0
  77. data/lib/devise/schema.rb +109 -0
  78. data/lib/devise/strategies/authenticatable.rb +155 -0
  79. data/lib/devise/strategies/base.rb +15 -0
  80. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  81. data/lib/devise/strategies/rememberable.rb +53 -0
  82. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  83. data/lib/devise/test_helpers.rb +90 -0
  84. data/lib/devise/version.rb +3 -0
  85. data/lib/generators/active_record/devise_generator.rb +71 -0
  86. data/lib/generators/active_record/templates/migration.rb +29 -0
  87. data/lib/generators/active_record/templates/migration_existing.rb +26 -0
  88. data/lib/generators/devise/devise_generator.rb +22 -0
  89. data/lib/generators/devise/install_generator.rb +24 -0
  90. data/lib/generators/devise/orm_helpers.rb +31 -0
  91. data/lib/generators/devise/views_generator.rb +98 -0
  92. data/lib/generators/mongoid/devise_generator.rb +60 -0
  93. data/lib/generators/templates/README +32 -0
  94. data/lib/generators/templates/devise.rb +215 -0
  95. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  96. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  97. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  98. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  99. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  100. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  101. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  102. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  103. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  104. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  105. data/test/controllers/helpers_test.rb +254 -0
  106. data/test/controllers/internal_helpers_test.rb +96 -0
  107. data/test/controllers/sessions_controller_test.rb +16 -0
  108. data/test/controllers/url_helpers_test.rb +59 -0
  109. data/test/delegator_test.rb +19 -0
  110. data/test/devise_test.rb +72 -0
  111. data/test/encryptors_test.rb +30 -0
  112. data/test/failure_app_test.rb +207 -0
  113. data/test/generators/active_record_generator_test.rb +47 -0
  114. data/test/generators/devise_generator_test.rb +39 -0
  115. data/test/generators/install_generator_test.rb +13 -0
  116. data/test/generators/mongoid_generator_test.rb +23 -0
  117. data/test/generators/views_generator_test.rb +52 -0
  118. data/test/helpers/devise_helper_test.rb +51 -0
  119. data/test/indifferent_hash.rb +33 -0
  120. data/test/integration/authenticatable_test.rb +590 -0
  121. data/test/integration/confirmable_test.rb +262 -0
  122. data/test/integration/database_authenticatable_test.rb +82 -0
  123. data/test/integration/http_authenticatable_test.rb +82 -0
  124. data/test/integration/lockable_test.rb +212 -0
  125. data/test/integration/omniauthable_test.rb +133 -0
  126. data/test/integration/recoverable_test.rb +287 -0
  127. data/test/integration/registerable_test.rb +335 -0
  128. data/test/integration/rememberable_test.rb +158 -0
  129. data/test/integration/timeoutable_test.rb +98 -0
  130. data/test/integration/token_authenticatable_test.rb +148 -0
  131. data/test/integration/trackable_test.rb +92 -0
  132. data/test/mailers/confirmation_instructions_test.rb +95 -0
  133. data/test/mailers/reset_password_instructions_test.rb +83 -0
  134. data/test/mailers/unlock_instructions_test.rb +77 -0
  135. data/test/mapping_test.rb +128 -0
  136. data/test/models/confirmable_test.rb +334 -0
  137. data/test/models/database_authenticatable_test.rb +167 -0
  138. data/test/models/encryptable_test.rb +67 -0
  139. data/test/models/lockable_test.rb +225 -0
  140. data/test/models/recoverable_test.rb +198 -0
  141. data/test/models/rememberable_test.rb +168 -0
  142. data/test/models/serializable_test.rb +38 -0
  143. data/test/models/timeoutable_test.rb +42 -0
  144. data/test/models/token_authenticatable_test.rb +49 -0
  145. data/test/models/trackable_test.rb +5 -0
  146. data/test/models/validatable_test.rb +113 -0
  147. data/test/models_test.rb +109 -0
  148. data/test/omniauth/config_test.rb +57 -0
  149. data/test/omniauth/url_helpers_test.rb +58 -0
  150. data/test/orm/active_record.rb +9 -0
  151. data/test/orm/mongoid.rb +14 -0
  152. data/test/rails_app/Rakefile +10 -0
  153. data/test/rails_app/app/active_record/admin.rb +6 -0
  154. data/test/rails_app/app/active_record/shim.rb +2 -0
  155. data/test/rails_app/app/active_record/user.rb +6 -0
  156. data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
  157. data/test/rails_app/app/controllers/admins_controller.rb +6 -0
  158. data/test/rails_app/app/controllers/application_controller.rb +8 -0
  159. data/test/rails_app/app/controllers/home_controller.rb +25 -0
  160. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
  161. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
  162. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
  163. data/test/rails_app/app/controllers/users_controller.rb +23 -0
  164. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  165. data/test/rails_app/app/mailers/users/mailer.rb +3 -0
  166. data/test/rails_app/app/mongoid/admin.rb +24 -0
  167. data/test/rails_app/app/mongoid/shim.rb +24 -0
  168. data/test/rails_app/app/mongoid/user.rb +45 -0
  169. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  170. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  171. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  172. data/test/rails_app/app/views/home/index.html.erb +1 -0
  173. data/test/rails_app/app/views/home/join.html.erb +1 -0
  174. data/test/rails_app/app/views/home/private.html.erb +1 -0
  175. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  176. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  177. data/test/rails_app/app/views/users/index.html.erb +1 -0
  178. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  179. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  180. data/test/rails_app/config.ru +4 -0
  181. data/test/rails_app/config/application.rb +41 -0
  182. data/test/rails_app/config/boot.rb +8 -0
  183. data/test/rails_app/config/database.yml +18 -0
  184. data/test/rails_app/config/environment.rb +5 -0
  185. data/test/rails_app/config/environments/development.rb +18 -0
  186. data/test/rails_app/config/environments/production.rb +33 -0
  187. data/test/rails_app/config/environments/test.rb +33 -0
  188. data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
  189. data/test/rails_app/config/initializers/devise.rb +197 -0
  190. data/test/rails_app/config/initializers/inflections.rb +2 -0
  191. data/test/rails_app/config/initializers/secret_token.rb +2 -0
  192. data/test/rails_app/config/routes.rb +87 -0
  193. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
  194. data/test/rails_app/db/schema.rb +52 -0
  195. data/test/rails_app/lib/shared_admin.rb +10 -0
  196. data/test/rails_app/lib/shared_user.rb +26 -0
  197. data/test/rails_app/public/404.html +26 -0
  198. data/test/rails_app/public/422.html +26 -0
  199. data/test/rails_app/public/500.html +26 -0
  200. data/test/rails_app/public/favicon.ico +0 -0
  201. data/test/rails_app/script/rails +10 -0
  202. data/test/routes_test.rb +240 -0
  203. data/test/support/assertions.rb +27 -0
  204. data/test/support/helpers.rb +109 -0
  205. data/test/support/integration.rb +88 -0
  206. data/test/support/locale/en.yml +4 -0
  207. data/test/support/webrat/integrations/rails.rb +24 -0
  208. data/test/test_helper.rb +27 -0
  209. data/test/test_helpers_test.rb +134 -0
  210. metadata +295 -0
@@ -0,0 +1,260 @@
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?
9
+ end
10
+
11
+ module ClassMethods
12
+ def log_process_action(payload)
13
+ payload[:status] ||= 401 unless payload[:exception]
14
+ super
15
+ end
16
+ end
17
+
18
+ # Define authentication filters and accessor helpers based on mappings.
19
+ # These filters should be used inside the controllers as before_filters,
20
+ # so you can control the scope of the user who should be signed in to
21
+ # access that specific controller/action.
22
+ # Example:
23
+ #
24
+ # Roles:
25
+ # User
26
+ # Admin
27
+ #
28
+ # Generated methods:
29
+ # authenticate_user! # Signs user in or redirect
30
+ # authenticate_admin! # Signs admin in or redirect
31
+ # user_signed_in? # Checks whether there is a user signed in or not
32
+ # admin_signed_in? # Checks whether there is an admin signed in or not
33
+ # current_user # Current signed in user
34
+ # current_admin # Current signed in admin
35
+ # user_session # Session data available only to the user scope
36
+ # admin_session # Session data available only to the admin scope
37
+ #
38
+ # Use:
39
+ # before_filter :authenticate_user! # Tell devise to use :user map
40
+ # before_filter :authenticate_admin! # Tell devise to use :admin map
41
+ #
42
+ def self.define_helpers(mapping) #:nodoc:
43
+ mapping = mapping.name
44
+
45
+ class_eval <<-METHODS, __FILE__, __LINE__ + 1
46
+ def authenticate_#{mapping}!(opts={})
47
+ opts[:scope] = :#{mapping}
48
+ warden.authenticate!(opts) if !devise_controller? || opts.delete(:force)
49
+ end
50
+
51
+ def #{mapping}_signed_in?
52
+ !!current_#{mapping}
53
+ end
54
+
55
+ def current_#{mapping}
56
+ @current_#{mapping} ||= warden.authenticate(:scope => :#{mapping})
57
+ end
58
+
59
+ def #{mapping}_session
60
+ current_#{mapping} && warden.session(:#{mapping})
61
+ end
62
+ METHODS
63
+
64
+ ActiveSupport.on_load(:action_controller) do
65
+ helper_method "current_#{mapping}", "#{mapping}_signed_in?", "#{mapping}_session"
66
+ end
67
+ end
68
+
69
+ # The main accessor for the warden proxy instance
70
+ def warden
71
+ request.env['warden']
72
+ end
73
+
74
+ # Return true if it's a devise_controller. false to all controllers unless
75
+ # the controllers defined inside devise. Useful if you want to apply a before
76
+ # filter to all controllers, except the ones in devise:
77
+ #
78
+ # before_filter :my_filter, :unless => { |c| c.devise_controller? }
79
+ def devise_controller?
80
+ false
81
+ end
82
+
83
+ # Tell warden that params authentication is allowed for that specific page.
84
+ def allow_params_authentication!
85
+ request.env["devise.allow_params_authentication"] = true
86
+ end
87
+
88
+ # Return true if the given scope is signed in session. If no scope given, return
89
+ # true if any scope is signed in. Does not run authentication hooks.
90
+ def signed_in?(scope=nil)
91
+ [ scope || Devise.mappings.keys ].flatten.any? do |scope|
92
+ warden.authenticate?(:scope => scope)
93
+ end
94
+ end
95
+
96
+ # Sign in a user that already was authenticated. This helper is useful for logging
97
+ # users in after sign up.
98
+ #
99
+ # All options given to sign_in is passed forward to the set_user method in warden.
100
+ # The only exception is the :bypass option, which bypass warden callbacks and stores
101
+ # the user straight in session. This option is useful in cases the user is already
102
+ # signed in, but we want to refresh the credentials in session.
103
+ #
104
+ # Examples:
105
+ #
106
+ # sign_in :user, @user # sign_in(scope, resource)
107
+ # sign_in @user # sign_in(resource)
108
+ # sign_in @user, :event => :authentication # sign_in(resource, options)
109
+ # sign_in @user, :bypass => true # sign_in(resource, options)
110
+ #
111
+ def sign_in(resource_or_scope, *args)
112
+ options = args.extract_options!
113
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
114
+ resource = args.last || resource_or_scope
115
+
116
+ expire_session_data_after_sign_in!
117
+
118
+ if options[:bypass]
119
+ warden.session_serializer.store(resource, scope)
120
+ elsif warden.user(scope) == resource && !options.delete(:force)
121
+ # Do nothing. User already signed in and we are not forcing it.
122
+ true
123
+ else
124
+ warden.set_user(resource, options.merge!(:scope => scope))
125
+ end
126
+ end
127
+
128
+ # Sign out a given user or scope. This helper is useful for signing out a user
129
+ # after deleting accounts.
130
+ #
131
+ # Examples:
132
+ #
133
+ # sign_out :user # sign_out(scope)
134
+ # sign_out @user # sign_out(resource)
135
+ #
136
+ def sign_out(resource_or_scope=nil)
137
+ return sign_out_all_scopes unless resource_or_scope
138
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
139
+ warden.user(scope) # Without loading user here, before_logout hook is not called
140
+ warden.raw_session.inspect # Without this inspect here. The session does not clear.
141
+ warden.logout(scope)
142
+ instance_variable_set(:"@current_#{scope}", nil)
143
+ end
144
+
145
+ # Sign out all active users or scopes. This helper is useful for signing out all roles
146
+ # in one click. This signs out ALL scopes in warden.
147
+ def sign_out_all_scopes
148
+ Devise.mappings.keys.each { |s| warden.user(s) }
149
+ warden.raw_session.inspect
150
+ warden.logout
151
+ expire_devise_cached_variables!
152
+ end
153
+
154
+ # Returns and delete the url stored in the session for the given scope. Useful
155
+ # for giving redirect backs after sign up:
156
+ #
157
+ # Example:
158
+ #
159
+ # redirect_to stored_location_for(:user) || root_path
160
+ #
161
+ def stored_location_for(resource_or_scope)
162
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
163
+ session.delete("#{scope}_return_to")
164
+ end
165
+
166
+ # The scope root url to be used when he's signed in. By default, it first
167
+ # tries to find a resource_root_path, otherwise it uses the root_path.
168
+ def signed_in_root_path(resource_or_scope)
169
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
170
+ home_path = "#{scope}_root_path"
171
+ respond_to?(home_path, true) ? send(home_path) : root_path
172
+ end
173
+
174
+ # The default url to be used after signing in. This is used by all Devise
175
+ # controllers and you can overwrite it in your ApplicationController to
176
+ # provide a custom hook for a custom resource.
177
+ #
178
+ # By default, it first tries to find a valid resource_return_to key in the
179
+ # session, then it fallbacks to resource_root_path, otherwise it uses the
180
+ # root path. For a user scope, you can define the default url in
181
+ # the following way:
182
+ #
183
+ # map.user_root '/users', :controller => 'users' # creates user_root_path
184
+ #
185
+ # map.namespace :user do |user|
186
+ # user.root :controller => 'users' # creates user_root_path
187
+ # end
188
+ #
189
+ # If the resource root path is not defined, root_path is used. However,
190
+ # if this default is not enough, you can customize it, for example:
191
+ #
192
+ # def after_sign_in_path_for(resource)
193
+ # stored_location_for(resource) ||
194
+ # if resource.is_a?(User) && resource.can_publish?
195
+ # publisher_url
196
+ # else
197
+ # signed_in_root_path(resource)
198
+ # end
199
+ # end
200
+ #
201
+ def after_sign_in_path_for(resource_or_scope)
202
+ stored_location_for(resource_or_scope) || signed_in_root_path(resource_or_scope)
203
+ end
204
+
205
+ # Method used by sessions controller to sign out a user. You can overwrite
206
+ # it in your ApplicationController to provide a custom hook for a custom
207
+ # scope. Notice that differently from +after_sign_in_path_for+ this method
208
+ # receives a symbol with the scope, and not the resource.
209
+ #
210
+ # By default it is the root_path.
211
+ def after_sign_out_path_for(resource_or_scope)
212
+ root_path
213
+ end
214
+
215
+ # Sign in a user and tries to redirect first to the stored location and
216
+ # then to the url specified by after_sign_in_path_for. It accepts the same
217
+ # parameters as the sign_in method.
218
+ def sign_in_and_redirect(resource_or_scope, *args)
219
+ options = args.extract_options!
220
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
221
+ resource = args.last || resource_or_scope
222
+ sign_in(scope, resource, options)
223
+ redirect_to after_sign_in_path_for(resource)
224
+ end
225
+
226
+ def redirect_location(scope, resource) #:nodoc:
227
+ ActiveSupport::Deprecation.warn "redirect_location in Devise is deprecated. Please use after_sign_in_path_for instead.", caller
228
+ after_sign_in_path_for(resource)
229
+ end
230
+
231
+ def expire_session_data_after_sign_in!
232
+ session.keys.grep(/^devise\./).each { |k| session.delete(k) }
233
+ end
234
+
235
+ # Sign out a user and tries to redirect to the url specified by
236
+ # after_sign_out_path_for.
237
+ def sign_out_and_redirect(resource_or_scope)
238
+ scope = Devise::Mapping.find_scope!(resource_or_scope)
239
+ redirect_path = after_sign_out_path_for(scope)
240
+ Devise.sign_out_all_scopes ? sign_out : sign_out(scope)
241
+ redirect_to redirect_path
242
+ end
243
+
244
+ # Overwrite Rails' handle unverified request to sign out all scopes,
245
+ # clear run strategies and remove cached variables.
246
+ def handle_unverified_request
247
+ sign_out_all_scopes
248
+ warden.clear_strategies_cache!
249
+ expire_devise_cached_variables!
250
+ super # call the default behaviour which resets the session
251
+ end
252
+
253
+ private
254
+
255
+ def expire_devise_cached_variables!
256
+ Devise.mappings.each { |_,m| instance_variable_set("@current_#{m.name}", nil) }
257
+ end
258
+ end
259
+ end
260
+ end
@@ -0,0 +1,161 @@
1
+ module Devise
2
+ module Controllers
3
+ # Those helpers are used only inside Devise controllers and should not be
4
+ # included in ApplicationController since they all depend on the url being
5
+ # accessed.
6
+ module InternalHelpers #:nodoc:
7
+ extend ActiveSupport::Concern
8
+ include Devise::Controllers::ScopedViews
9
+ include Devise::Controllers::SharedHelpers
10
+
11
+ included do
12
+ helper DeviseHelper
13
+
14
+ helpers = %w(resource scope_name resource_name signed_in_resource
15
+ resource_class devise_mapping devise_controller?)
16
+ hide_action *helpers
17
+ helper_method *helpers
18
+
19
+ prepend_before_filter :is_devise_resource?
20
+ respond_to *Mime::SET.map(&:to_sym) if mimes_for_respond_to.empty?
21
+ end
22
+
23
+ # Gets the actual resource stored in the instance variable
24
+ def resource
25
+ instance_variable_get(:"@#{resource_name}")
26
+ end
27
+
28
+ # Proxy to devise map name
29
+ def resource_name
30
+ devise_mapping.name
31
+ end
32
+ alias :scope_name :resource_name
33
+
34
+ # Proxy to devise map class
35
+ def resource_class
36
+ devise_mapping.to
37
+ end
38
+
39
+ # Returns a signed in resource from session (if one exists)
40
+ def signed_in_resource
41
+ warden.authenticate(:scope => resource_name)
42
+ end
43
+
44
+ # Attempt to find the mapped route for devise based on request path
45
+ def devise_mapping
46
+ @devise_mapping ||= request.env["devise.mapping"]
47
+ end
48
+
49
+ # Overwrites devise_controller? to return true
50
+ def devise_controller?
51
+ true
52
+ end
53
+
54
+ protected
55
+
56
+ # Checks whether it's a devise mapped resource or not.
57
+ def is_devise_resource? #:nodoc:
58
+ unknown_action! <<-MESSAGE unless devise_mapping
59
+ Could not find devise mapping for path #{request.fullpath.inspect}.
60
+ Maybe you forgot to wrap your route inside the scope block? For example:
61
+
62
+ devise_scope :user do
63
+ match "/some/route" => "some_devise_controller"
64
+ end
65
+ MESSAGE
66
+ end
67
+
68
+ # Returns real navigational formats which are supported by Rails
69
+ def navigational_formats
70
+ @navigational_formats ||= Devise.navigational_formats.select{ |format| Mime::EXTENSION_LOOKUP[format.to_s] }
71
+ end
72
+
73
+ def unknown_action!(msg)
74
+ logger.debug "[Devise] #{msg}" if logger
75
+ raise ActionController::UnknownAction, msg
76
+ end
77
+
78
+ # Sets the resource creating an instance variable
79
+ def resource=(new_resource)
80
+ instance_variable_set(:"@#{resource_name}", new_resource)
81
+ end
82
+
83
+ # Build a devise resource.
84
+ def build_resource(hash=nil)
85
+ hash ||= params[resource_name] || {}
86
+ self.resource = resource_class.new(hash)
87
+ end
88
+
89
+ # Helper for use in before_filters where no authentication is required.
90
+ #
91
+ # Example:
92
+ # before_filter :require_no_authentication, :only => :new
93
+ def require_no_authentication
94
+ return unless is_navigational_format?
95
+ no_input = devise_mapping.no_input_strategies
96
+
97
+ authenticated = if no_input.present?
98
+ args = no_input.dup.push :scope => resource_name
99
+ warden.authenticate?(*args)
100
+ else
101
+ warden.authenticated?(resource_name)
102
+ end
103
+
104
+ if authenticated
105
+ resource = warden.user(resource_name)
106
+ flash[:alert] = I18n.t("devise.failure.already_authenticated")
107
+ redirect_to after_sign_in_path_for(resource)
108
+ end
109
+ end
110
+
111
+ # Helper for use after calling send_*_instructions methods on a resource.
112
+ # If we are in paranoid mode, we always act as if the resource was valid
113
+ # and instructions were sent.
114
+ def successfully_sent?(resource)
115
+ notice = if Devise.paranoid
116
+ resource.errors.clear
117
+ :send_paranoid_instructions
118
+ elsif resource.errors.empty?
119
+ :send_instructions
120
+ end
121
+
122
+ if notice
123
+ set_flash_message :notice, notice if is_navigational_format?
124
+ true
125
+ end
126
+ end
127
+
128
+ # Sets the flash message with :key, using I18n. By default you are able
129
+ # to setup your messages using specific resource scope, and if no one is
130
+ # found we look to default scope.
131
+ # Example (i18n locale file):
132
+ #
133
+ # en:
134
+ # devise:
135
+ # passwords:
136
+ # #default_scope_messages - only if resource_scope is not found
137
+ # user:
138
+ # #resource_scope_messages
139
+ #
140
+ # Please refer to README or en.yml locale file to check what messages are
141
+ # available.
142
+ def set_flash_message(key, kind, options={}) #:nodoc:
143
+ options[:scope] = "devise.#{controller_name}"
144
+ options[:default] = Array(options[:default]).unshift(kind.to_sym)
145
+ options[:resource_name] = resource_name
146
+ message = I18n.t("#{resource_name}.#{kind}", options)
147
+ flash[key] = message if message.present?
148
+ end
149
+
150
+ def clean_up_passwords(object) #:nodoc:
151
+ object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
152
+ end
153
+
154
+ def respond_with_navigational(*args, &block)
155
+ respond_with(*args) do |format|
156
+ format.any(*navigational_formats, &block)
157
+ end
158
+ end
159
+ end
160
+ end
161
+ end
@@ -0,0 +1,52 @@
1
+ module Devise
2
+ module Controllers
3
+ # A module that may be optionally included in a controller in order
4
+ # to provide remember me behavior.
5
+ module Rememberable
6
+ # Return default cookie values retrieved from session options.
7
+ def self.cookie_values
8
+ Rails.configuration.session_options.slice(:path, :domain, :secure)
9
+ end
10
+
11
+ # A small warden proxy so we can remember and forget uses from hooks.
12
+ class Proxy #:nodoc:
13
+ include Devise::Controllers::Rememberable
14
+
15
+ delegate :cookies, :env, :to => :@warden
16
+
17
+ def initialize(warden)
18
+ @warden = warden
19
+ end
20
+ end
21
+
22
+ # Remembers the given resource by setting up a cookie
23
+ def remember_me(resource)
24
+ scope = Devise::Mapping.find_scope!(resource)
25
+ resource.remember_me!(resource.extend_remember_period)
26
+ cookies.signed["remember_#{scope}_token"] = remember_cookie_values(resource)
27
+ end
28
+
29
+ # Forgets the given resource by deleting a cookie
30
+ def forget_me(resource)
31
+ scope = Devise::Mapping.find_scope!(resource)
32
+ resource.forget_me!
33
+ cookies.delete("remember_#{scope}_token", forget_cookie_values(resource))
34
+ end
35
+
36
+ protected
37
+
38
+ def forget_cookie_values(resource)
39
+ Devise::Controllers::Rememberable.cookie_values.merge!(resource.cookie_options)
40
+ end
41
+
42
+ def remember_cookie_values(resource)
43
+ options = { :httponly => true }
44
+ options.merge!(forget_cookie_values(resource))
45
+ options.merge!(
46
+ :value => resource.class.serialize_into_cookie(resource),
47
+ :expires => resource.remember_expires_at
48
+ )
49
+ end
50
+ end
51
+ end
52
+ end