loyal_devise 2.1.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 +10 -0
  2. data/.travis.yml +15 -0
  3. data/CHANGELOG.rdoc +881 -0
  4. data/CONTRIBUTING.md +12 -0
  5. data/Gemfile +31 -0
  6. data/Gemfile.lock +154 -0
  7. data/MIT-LICENSE +20 -0
  8. data/README.md +388 -0
  9. data/Rakefile +34 -0
  10. data/app/controllers/devise/confirmations_controller.rb +44 -0
  11. data/app/controllers/devise/omniauth_callbacks_controller.rb +31 -0
  12. data/app/controllers/devise/passwords_controller.rb +57 -0
  13. data/app/controllers/devise/registrations_controller.rb +120 -0
  14. data/app/controllers/devise/sessions_controller.rb +51 -0
  15. data/app/controllers/devise/unlocks_controller.rb +45 -0
  16. data/app/controllers/devise_controller.rb +193 -0
  17. data/app/helpers/devise_helper.rb +26 -0
  18. data/app/mailers/devise/mailer.rb +16 -0
  19. data/app/views/devise/_links.erb +3 -0
  20. data/app/views/devise/confirmations/new.html.erb +12 -0
  21. data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
  22. data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
  23. data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
  24. data/app/views/devise/passwords/edit.html.erb +16 -0
  25. data/app/views/devise/passwords/new.html.erb +12 -0
  26. data/app/views/devise/registrations/edit.html.erb +25 -0
  27. data/app/views/devise/registrations/new.html.erb +18 -0
  28. data/app/views/devise/sessions/new.html.erb +17 -0
  29. data/app/views/devise/shared/_links.erb +25 -0
  30. data/app/views/devise/unlocks/new.html.erb +12 -0
  31. data/config/locales/en.yml +59 -0
  32. data/devise.gemspec +26 -0
  33. data/gemfiles/Gemfile.rails-3.1.x +35 -0
  34. data/gemfiles/Gemfile.rails-3.1.x.lock +167 -0
  35. data/lib/devise/controllers/helpers.rb +273 -0
  36. data/lib/devise/controllers/rememberable.rb +53 -0
  37. data/lib/devise/controllers/scoped_views.rb +18 -0
  38. data/lib/devise/controllers/url_helpers.rb +68 -0
  39. data/lib/devise/delegator.rb +17 -0
  40. data/lib/devise/failure_app.rb +188 -0
  41. data/lib/devise/hooks/activatable.rb +12 -0
  42. data/lib/devise/hooks/forgetable.rb +10 -0
  43. data/lib/devise/hooks/lockable.rb +8 -0
  44. data/lib/devise/hooks/rememberable.rb +7 -0
  45. data/lib/devise/hooks/timeoutable.rb +26 -0
  46. data/lib/devise/hooks/trackable.rb +10 -0
  47. data/lib/devise/mailers/helpers.rb +92 -0
  48. data/lib/devise/mapping.rb +173 -0
  49. data/lib/devise/models/authenticatable.rb +269 -0
  50. data/lib/devise/models/confirmable.rb +271 -0
  51. data/lib/devise/models/database_authenticatable.rb +127 -0
  52. data/lib/devise/models/lockable.rb +194 -0
  53. data/lib/devise/models/omniauthable.rb +28 -0
  54. data/lib/devise/models/recoverable.rb +141 -0
  55. data/lib/devise/models/registerable.rb +26 -0
  56. data/lib/devise/models/rememberable.rb +126 -0
  57. data/lib/devise/models/timeoutable.rb +50 -0
  58. data/lib/devise/models/token_authenticatable.rb +90 -0
  59. data/lib/devise/models/trackable.rb +36 -0
  60. data/lib/devise/models/validatable.rb +67 -0
  61. data/lib/devise/models.rb +129 -0
  62. data/lib/devise/modules.rb +30 -0
  63. data/lib/devise/omniauth/config.rb +46 -0
  64. data/lib/devise/omniauth/url_helpers.rb +19 -0
  65. data/lib/devise/omniauth.rb +29 -0
  66. data/lib/devise/orm/active_record.rb +4 -0
  67. data/lib/devise/orm/mongoid.rb +4 -0
  68. data/lib/devise/param_filter.rb +42 -0
  69. data/lib/devise/rails/routes.rb +447 -0
  70. data/lib/devise/rails/warden_compat.rb +44 -0
  71. data/lib/devise/rails.rb +55 -0
  72. data/lib/devise/strategies/authenticatable.rb +177 -0
  73. data/lib/devise/strategies/base.rb +21 -0
  74. data/lib/devise/strategies/database_authenticatable.rb +21 -0
  75. data/lib/devise/strategies/rememberable.rb +56 -0
  76. data/lib/devise/strategies/token_authenticatable.rb +57 -0
  77. data/lib/devise/test_helpers.rb +132 -0
  78. data/lib/devise/time_inflector.rb +15 -0
  79. data/lib/devise/version.rb +4 -0
  80. data/lib/devise.rb +445 -0
  81. data/lib/generators/active_record/devise_generator.rb +80 -0
  82. data/lib/generators/active_record/templates/migration.rb +20 -0
  83. data/lib/generators/active_record/templates/migration_existing.rb +27 -0
  84. data/lib/generators/devise/devise_generator.rb +25 -0
  85. data/lib/generators/devise/install_generator.rb +25 -0
  86. data/lib/generators/devise/orm_helpers.rb +33 -0
  87. data/lib/generators/devise/views_generator.rb +117 -0
  88. data/lib/generators/mongoid/devise_generator.rb +58 -0
  89. data/lib/generators/templates/README +35 -0
  90. data/lib/generators/templates/devise.rb +241 -0
  91. data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
  92. data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
  93. data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
  94. data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
  95. data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
  96. data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
  97. data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
  98. data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
  99. data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
  100. data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
  101. data/test/controllers/custom_strategy_test.rb +63 -0
  102. data/test/controllers/helpers_test.rb +254 -0
  103. data/test/controllers/internal_helpers_test.rb +111 -0
  104. data/test/controllers/sessions_controller_test.rb +58 -0
  105. data/test/controllers/url_helpers_test.rb +60 -0
  106. data/test/delegator_test.rb +20 -0
  107. data/test/devise_test.rb +73 -0
  108. data/test/failure_app_test.rb +222 -0
  109. data/test/generators/active_record_generator_test.rb +76 -0
  110. data/test/generators/devise_generator_test.rb +40 -0
  111. data/test/generators/install_generator_test.rb +14 -0
  112. data/test/generators/mongoid_generator_test.rb +24 -0
  113. data/test/generators/views_generator_test.rb +53 -0
  114. data/test/helpers/devise_helper_test.rb +52 -0
  115. data/test/indifferent_hash.rb +34 -0
  116. data/test/integration/authenticatable_test.rb +634 -0
  117. data/test/integration/confirmable_test.rb +299 -0
  118. data/test/integration/database_authenticatable_test.rb +83 -0
  119. data/test/integration/http_authenticatable_test.rb +98 -0
  120. data/test/integration/lockable_test.rb +243 -0
  121. data/test/integration/omniauthable_test.rb +134 -0
  122. data/test/integration/recoverable_test.rb +307 -0
  123. data/test/integration/registerable_test.rb +346 -0
  124. data/test/integration/rememberable_test.rb +159 -0
  125. data/test/integration/timeoutable_test.rb +141 -0
  126. data/test/integration/token_authenticatable_test.rb +162 -0
  127. data/test/integration/trackable_test.rb +93 -0
  128. data/test/mailers/confirmation_instructions_test.rb +103 -0
  129. data/test/mailers/reset_password_instructions_test.rb +84 -0
  130. data/test/mailers/unlock_instructions_test.rb +78 -0
  131. data/test/mapping_test.rb +128 -0
  132. data/test/models/authenticatable_test.rb +8 -0
  133. data/test/models/confirmable_test.rb +392 -0
  134. data/test/models/database_authenticatable_test.rb +190 -0
  135. data/test/models/lockable_test.rb +274 -0
  136. data/test/models/omniauthable_test.rb +8 -0
  137. data/test/models/recoverable_test.rb +206 -0
  138. data/test/models/registerable_test.rb +8 -0
  139. data/test/models/rememberable_test.rb +175 -0
  140. data/test/models/serializable_test.rb +49 -0
  141. data/test/models/timeoutable_test.rb +47 -0
  142. data/test/models/token_authenticatable_test.rb +56 -0
  143. data/test/models/trackable_test.rb +14 -0
  144. data/test/models/validatable_test.rb +117 -0
  145. data/test/models_test.rb +180 -0
  146. data/test/omniauth/config_test.rb +58 -0
  147. data/test/omniauth/url_helpers_test.rb +52 -0
  148. data/test/orm/active_record.rb +10 -0
  149. data/test/orm/mongoid.rb +15 -0
  150. data/test/rails_app/Rakefile +10 -0
  151. data/test/rails_app/app/active_record/admin.rb +7 -0
  152. data/test/rails_app/app/active_record/shim.rb +3 -0
  153. data/test/rails_app/app/active_record/user.rb +7 -0
  154. data/test/rails_app/app/controllers/admins/sessions_controller.rb +7 -0
  155. data/test/rails_app/app/controllers/admins_controller.rb +12 -0
  156. data/test/rails_app/app/controllers/application_controller.rb +9 -0
  157. data/test/rails_app/app/controllers/home_controller.rb +26 -0
  158. data/test/rails_app/app/controllers/publisher/registrations_controller.rb +3 -0
  159. data/test/rails_app/app/controllers/publisher/sessions_controller.rb +3 -0
  160. data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +15 -0
  161. data/test/rails_app/app/controllers/users_controller.rb +24 -0
  162. data/test/rails_app/app/helpers/application_helper.rb +4 -0
  163. data/test/rails_app/app/mailers/users/mailer.rb +9 -0
  164. data/test/rails_app/app/mongoid/admin.rb +28 -0
  165. data/test/rails_app/app/mongoid/shim.rb +25 -0
  166. data/test/rails_app/app/mongoid/user.rb +43 -0
  167. data/test/rails_app/app/views/admins/index.html.erb +1 -0
  168. data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
  169. data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
  170. data/test/rails_app/app/views/home/index.html.erb +1 -0
  171. data/test/rails_app/app/views/home/join.html.erb +1 -0
  172. data/test/rails_app/app/views/home/private.html.erb +1 -0
  173. data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
  174. data/test/rails_app/app/views/layouts/application.html.erb +24 -0
  175. data/test/rails_app/app/views/users/index.html.erb +1 -0
  176. data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
  177. data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
  178. data/test/rails_app/config/application.rb +42 -0
  179. data/test/rails_app/config/boot.rb +9 -0
  180. data/test/rails_app/config/database.yml +18 -0
  181. data/test/rails_app/config/environment.rb +6 -0
  182. data/test/rails_app/config/environments/development.rb +19 -0
  183. data/test/rails_app/config/environments/production.rb +34 -0
  184. data/test/rails_app/config/environments/test.rb +34 -0
  185. data/test/rails_app/config/initializers/backtrace_silencers.rb +8 -0
  186. data/test/rails_app/config/initializers/devise.rb +179 -0
  187. data/test/rails_app/config/initializers/inflections.rb +3 -0
  188. data/test/rails_app/config/initializers/secret_token.rb +3 -0
  189. data/test/rails_app/config/routes.rb +101 -0
  190. data/test/rails_app/config.ru +4 -0
  191. data/test/rails_app/db/migrate/20100401102949_create_tables.rb +75 -0
  192. data/test/rails_app/db/schema.rb +53 -0
  193. data/test/rails_app/lib/shared_admin.rb +15 -0
  194. data/test/rails_app/lib/shared_user.rb +27 -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 +249 -0
  201. data/test/support/assertions.rb +41 -0
  202. data/test/support/helpers.rb +92 -0
  203. data/test/support/integration.rb +93 -0
  204. data/test/support/locale/en.yml +4 -0
  205. data/test/support/webrat/integrations/rails.rb +25 -0
  206. data/test/test_helper.rb +28 -0
  207. data/test/test_helpers_test.rb +152 -0
  208. metadata +407 -0
@@ -0,0 +1,447 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require "active_support/core_ext/object/try"
3
+ require "active_support/core_ext/hash/slice"
4
+
5
+ module ActionDispatch::Routing
6
+ class RouteSet #:nodoc:
7
+ # Ensure Devise modules are included only after loading routes, because we
8
+ # need devise_for mappings already declared to create filters and helpers.
9
+ def finalize_with_devise!
10
+ result = finalize_without_devise!
11
+
12
+ @devise_finalized ||= begin
13
+ if Devise.router_name.nil? && defined?(@devise_finalized) && self != Rails.application.try(:routes)
14
+ warn "[DEVISE] We have detected that you are using devise_for inside engine routes. " \
15
+ "In this case, you probably want to set Devise.router_name = MOUNT_POINT, where " \
16
+ "MOUNT_POINT is a symbol representing where this engine will be mounted at. For " \
17
+ "now Devise will default the mount point to :main_app. You can explicitly set it" \
18
+ " to :main_app as well in case you want to keep the current behavior."
19
+ end
20
+
21
+ Devise.configure_warden!
22
+ Devise.regenerate_helpers!
23
+ true
24
+ end
25
+
26
+ result
27
+ end
28
+ alias_method_chain :finalize!, :devise
29
+ end
30
+
31
+ class Mapper
32
+ # Includes devise_for method for routes. This method is responsible to
33
+ # generate all needed routes for devise, based on what modules you have
34
+ # defined in your model.
35
+ #
36
+ # ==== Examples
37
+ #
38
+ # Let's say you have an User model configured to use authenticatable,
39
+ # confirmable and recoverable modules. After creating this inside your routes:
40
+ #
41
+ # devise_for :users
42
+ #
43
+ # This method is going to look inside your User model and create the
44
+ # needed routes:
45
+ #
46
+ # # Session routes for Authenticatable (default)
47
+ # new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
48
+ # user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
49
+ # destroy_user_session DELETE /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
50
+ #
51
+ # # Password routes for Recoverable, if User model has :recoverable configured
52
+ # new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
53
+ # edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
54
+ # user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
55
+ # POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
56
+ #
57
+ # # Confirmation routes for Confirmable, if User model has :confirmable configured
58
+ # new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
59
+ # user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
60
+ # POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
61
+ #
62
+ # ==== Options
63
+ #
64
+ # You can configure your routes with some options:
65
+ #
66
+ # * :class_name => setup a different class to be looked up by devise, if it cannot be
67
+ # properly found by the route name.
68
+ #
69
+ # devise_for :users, :class_name => 'Account'
70
+ #
71
+ # * :path => allows you to setup path name that will be used, as rails routes does.
72
+ # The following route configuration would setup your route as /accounts instead of /users:
73
+ #
74
+ # devise_for :users, :path => 'accounts'
75
+ #
76
+ # * :singular => setup the singular name for the given resource. This is used as the instance variable
77
+ # name in controller, as the name in routes and the scope given to warden.
78
+ #
79
+ # devise_for :users, :singular => :user
80
+ #
81
+ # * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
82
+ # :password, :confirmation, :unlock.
83
+ #
84
+ # devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
85
+ #
86
+ # * :controllers => the controller which should be used. All routes by default points to Devise controllers.
87
+ # However, if you want them to point to custom controller, you should do:
88
+ #
89
+ # devise_for :users, :controllers => { :sessions => "users/sessions" }
90
+ #
91
+ # * :failure_app => a rack app which is invoked whenever there is a failure. Strings representing a given
92
+ # are also allowed as parameter.
93
+ #
94
+ # * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
95
+ # if you wish to restrict this to accept only :post or :delete requests you should do:
96
+ #
97
+ # devise_for :users, :sign_out_via => [ :post, :delete ]
98
+ #
99
+ # You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
100
+ #
101
+ # * :module => the namespace to find controllers (default: "devise", thus
102
+ # accessing devise/sessions, devise/registrations, and so on). If you want
103
+ # to namespace all at once, use module:
104
+ #
105
+ # devise_for :users, :module => "users"
106
+ #
107
+ # Notice that whenever you use namespace in the router DSL, it automatically sets the module.
108
+ # So the following setup:
109
+ #
110
+ # namespace :publisher do
111
+ # devise_for :account
112
+ # end
113
+ #
114
+ # Will use publisher/sessions controller instead of devise/sessions controller. You can revert
115
+ # this by providing the :module option to devise_for.
116
+ #
117
+ # Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
118
+ # and views. For example, using the above setup you'll end with following methods:
119
+ # current_publisher_account, authenticate_publisher_account!, publisher_account_signed_in, etc.
120
+ #
121
+ # * :skip => tell which controller you want to skip routes from being created:
122
+ #
123
+ # devise_for :users, :skip => :sessions
124
+ #
125
+ # * :only => the opposite of :skip, tell which controllers only to generate routes to:
126
+ #
127
+ # devise_for :users, :only => :sessions
128
+ #
129
+ # * :skip_helpers => skip generating Devise url helpers like new_session_path(@user).
130
+ # This is useful to avoid conflicts with previous routes and is false by default.
131
+ # It accepts true as option, meaning it will skip all the helpers for the controllers
132
+ # given in :skip but it also accepts specific helpers to be skipped:
133
+ #
134
+ # devise_for :users, :skip => [:registrations, :confirmations], :skip_helpers => true
135
+ # devise_for :users, :skip_helpers => [:registrations, :confirmations]
136
+ #
137
+ # * :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
138
+ #
139
+ # devise_for :users, :format => false
140
+ #
141
+ # * :constraints => works the same as Rails' constraints
142
+ #
143
+ # * :defaults => works the same as Rails' defaults
144
+ #
145
+ # ==== Scoping
146
+ #
147
+ # Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
148
+ #
149
+ # scope "/my" do
150
+ # devise_for :users
151
+ # end
152
+ #
153
+ # However, since Devise uses the request path to retrieve the current user,
154
+ # this has one caveat: If you are using a dynamic segment, like so ...
155
+ #
156
+ # scope ":locale" do
157
+ # devise_for :users
158
+ # end
159
+ #
160
+ # you are required to configure default_url_options in your
161
+ # ApplicationController class, so Devise can pick it:
162
+ #
163
+ # class ApplicationController < ActionController::Base
164
+ # def self.default_url_options
165
+ # { :locale => I18n.locale }
166
+ # end
167
+ # end
168
+ #
169
+ # ==== Adding custom actions to override controllers
170
+ #
171
+ # You can pass a block to devise_for that will add any routes defined in the block to Devise's
172
+ # list of known actions. This is important if you add a custom action to a controller that
173
+ # overrides an out of the box Devise controller.
174
+ # For example:
175
+ #
176
+ # class RegistrationsController < Devise::RegistrationsController
177
+ # def update
178
+ # # do something different here
179
+ # end
180
+ #
181
+ # def deactivate
182
+ # # not a standard action
183
+ # # deactivate code here
184
+ # end
185
+ # end
186
+ #
187
+ # In order to get Devise to recognize the deactivate action, your devise_scope entry should look like this:
188
+ #
189
+ # devise_scope :owner do
190
+ # post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
191
+ # end
192
+ #
193
+ def devise_for(*resources)
194
+ @devise_finalized = false
195
+ options = resources.extract_options!
196
+
197
+ options[:as] ||= @scope[:as] if @scope[:as].present?
198
+ options[:module] ||= @scope[:module] if @scope[:module].present?
199
+ options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
200
+ options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
201
+ options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
202
+ options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
203
+ options[:options] = @scope[:options] || {}
204
+ options[:options][:format] = false if options[:format] == false
205
+
206
+ resources.map!(&:to_sym)
207
+
208
+ resources.each do |resource|
209
+ mapping = Devise.add_mapping(resource, options)
210
+
211
+ begin
212
+ raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
213
+ rescue NameError => e
214
+ raise unless mapping.class_name == resource.to_s.classify
215
+ warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
216
+ "no model #{mapping.class_name} defined in your application"
217
+ next
218
+ rescue NoMethodError => e
219
+ raise unless e.message.include?("undefined method `devise'")
220
+ raise_no_devise_method_error!(mapping.class_name)
221
+ end
222
+
223
+ routes = mapping.used_routes
224
+
225
+ devise_scope mapping.name do
226
+ if block_given?
227
+ ActiveSupport::Deprecation.warn "Passing a block to devise_for is deprecated. " \
228
+ "Please remove the block from devise_for (only the block, the call to " \
229
+ "devise_for must still exist) and call devise_scope :#{mapping.name} do ... end " \
230
+ "with the block instead", caller
231
+ yield
232
+ end
233
+
234
+ with_devise_exclusive_scope mapping.fullpath, mapping.name, options do
235
+ routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
236
+ end
237
+ end
238
+ end
239
+ end
240
+
241
+ # Allow you to add authentication request from the router.
242
+ # Takes an optional scope and block to provide constraints
243
+ # on the model instance itself.
244
+ #
245
+ # authenticate do
246
+ # resources :post
247
+ # end
248
+ #
249
+ # authenticate(:admin) do
250
+ # resources :users
251
+ # end
252
+ #
253
+ # authenticate :user, lambda {|u| u.role == "admin"} do
254
+ # root :to => "admin/dashboard#show"
255
+ # end
256
+ #
257
+ def authenticate(scope=nil, block=nil)
258
+ constraint = lambda do |request|
259
+ request.env["warden"].authenticate!(:scope => scope) && (block.nil? || block.call(request.env["warden"].user(scope)))
260
+ end
261
+
262
+ constraints(constraint) do
263
+ yield
264
+ end
265
+ end
266
+
267
+ # Allow you to route based on whether a scope is authenticated. You
268
+ # can optionally specify which scope and a block. The block accepts
269
+ # a model and allows extra constraints to be done on the instance.
270
+ #
271
+ # authenticated :admin do
272
+ # root :to => 'admin/dashboard#show'
273
+ # end
274
+ #
275
+ # authenticated do
276
+ # root :to => 'dashboard#show'
277
+ # end
278
+ #
279
+ # authenticated :user, lambda {|u| u.role == "admin"} do
280
+ # root :to => "admin/dashboard#show"
281
+ # end
282
+ #
283
+ # root :to => 'landing#show'
284
+ #
285
+ def authenticated(scope=nil, block=nil)
286
+ constraint = lambda do |request|
287
+ request.env["warden"].authenticate?(:scope => scope) && (block.nil? || block.call(request.env["warden"].user(scope)))
288
+ end
289
+
290
+ constraints(constraint) do
291
+ yield
292
+ end
293
+ end
294
+
295
+ # Allow you to route based on whether a scope is *not* authenticated.
296
+ # You can optionally specify which scope.
297
+ #
298
+ # unauthenticated do
299
+ # as :user do
300
+ # root :to => 'devise/registrations#new'
301
+ # end
302
+ # end
303
+ #
304
+ # root :to => 'dashboard#show'
305
+ #
306
+ def unauthenticated(scope=nil)
307
+ constraint = lambda do |request|
308
+ not request.env["warden"].authenticate? :scope => scope
309
+ end
310
+
311
+ constraints(constraint) do
312
+ yield
313
+ end
314
+ end
315
+
316
+ # Sets the devise scope to be used in the controller. If you have custom routes,
317
+ # you are required to call this method (also aliased as :as) in order to specify
318
+ # to which controller it is targetted.
319
+ #
320
+ # as :user do
321
+ # get "sign_in", :to => "devise/sessions#new"
322
+ # end
323
+ #
324
+ # Notice you cannot have two scopes mapping to the same URL. And remember, if
325
+ # you try to access a devise controller without specifying a scope, it will
326
+ # raise ActionNotFound error.
327
+ #
328
+ # Also be aware of that 'devise_scope' and 'as' use the singular form of the
329
+ # noun where other devise route commands expect the plural form. This would be a
330
+ # good and working example.
331
+ #
332
+ # devise_scope :user do
333
+ # match "/some/route" => "some_devise_controller"
334
+ # end
335
+ # devise_for :users
336
+ #
337
+ # Notice and be aware of the differences above between :user and :users
338
+ def devise_scope(scope)
339
+ constraint = lambda do |request|
340
+ request.env["devise.mapping"] = Devise.mappings[scope]
341
+ true
342
+ end
343
+
344
+ constraints(constraint) do
345
+ yield
346
+ end
347
+ end
348
+ alias :as :devise_scope
349
+
350
+ protected
351
+
352
+ def devise_session(mapping, controllers) #:nodoc:
353
+ resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
354
+ get :new, :path => mapping.path_names[:sign_in], :as => "new"
355
+ post :create, :path => mapping.path_names[:sign_in]
356
+ match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
357
+ end
358
+ end
359
+
360
+ def devise_password(mapping, controllers) #:nodoc:
361
+ resource :password, :only => [:new, :create, :edit, :update],
362
+ :path => mapping.path_names[:password], :controller => controllers[:passwords]
363
+ end
364
+
365
+ def devise_confirmation(mapping, controllers) #:nodoc:
366
+ resource :confirmation, :only => [:new, :create, :show],
367
+ :path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
368
+ end
369
+
370
+ def devise_unlock(mapping, controllers) #:nodoc:
371
+ if mapping.to.unlock_strategy_enabled?(:email)
372
+ resource :unlock, :only => [:new, :create, :show],
373
+ :path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
374
+ end
375
+ end
376
+
377
+ def devise_registration(mapping, controllers) #:nodoc:
378
+ path_names = {
379
+ :new => mapping.path_names[:sign_up],
380
+ :cancel => mapping.path_names[:cancel]
381
+ }
382
+
383
+ options = {
384
+ :only => [:new, :create, :edit, :update, :destroy],
385
+ :path => mapping.path_names[:registration],
386
+ :path_names => path_names,
387
+ :controller => controllers[:registrations]
388
+ }
389
+
390
+ resource :registration, options do
391
+ get :cancel
392
+ end
393
+ end
394
+
395
+ def devise_omniauth_callback(mapping, controllers) #:nodoc:
396
+ path, @scope[:path] = @scope[:path], nil
397
+ path_prefix = Devise.omniauth_path_prefix || "/#{mapping.path}/auth".squeeze("/")
398
+ set_omniauth_path_prefix!(path_prefix)
399
+
400
+ providers = Regexp.union(mapping.to.omniauth_providers.map(&:to_s))
401
+
402
+ match "#{path_prefix}/:provider",
403
+ :constraints => { :provider => providers },
404
+ :to => "#{controllers[:omniauth_callbacks]}#passthru",
405
+ :as => :omniauth_authorize
406
+
407
+ match "#{path_prefix}/:action/callback",
408
+ :constraints => { :action => providers },
409
+ :to => controllers[:omniauth_callbacks],
410
+ :as => :omniauth_callback
411
+ ensure
412
+ @scope[:path] = path
413
+ end
414
+
415
+ DEVISE_SCOPE_KEYS = [:as, :path, :module, :constraints, :defaults, :options]
416
+
417
+ def with_devise_exclusive_scope(new_path, new_as, options) #:nodoc:
418
+ old = {}
419
+ DEVISE_SCOPE_KEYS.each { |k| old[k] = @scope[k] }
420
+
421
+ new = { :as => new_as, :path => new_path, :module => nil }
422
+ new.merge!(options.slice(:constraints, :defaults, :options))
423
+
424
+ @scope.merge!(new)
425
+ yield
426
+ ensure
427
+ @scope.merge!(old)
428
+ end
429
+
430
+ def set_omniauth_path_prefix!(path_prefix) #:nodoc:
431
+ if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
432
+ raise "Wrong OmniAuth configuration. If you are getting this exception, it means that either:\n\n" \
433
+ "1) You are manually setting OmniAuth.config.path_prefix and it doesn't match the Devise one\n" \
434
+ "2) You are setting :omniauthable in more than one model\n" \
435
+ "3) You changed your Devise routes/OmniAuth setting and haven't restarted your server"
436
+ else
437
+ ::OmniAuth.config.path_prefix = path_prefix
438
+ end
439
+ end
440
+
441
+ def raise_no_devise_method_error!(klass) #:nodoc:
442
+ raise "#{klass} does not respond to 'devise' method. This usually means you haven't " \
443
+ "loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " \
444
+ "inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
445
+ end
446
+ end
447
+ end
@@ -0,0 +1,44 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Warden::Mixins::Common
3
+ def request
4
+ @request ||= ActionDispatch::Request.new(env)
5
+ end
6
+
7
+ # This is called internally by Warden on logout
8
+ def reset_session!
9
+ request.reset_session
10
+ end
11
+
12
+ def cookies
13
+ request.cookie_jar
14
+ end
15
+ end
16
+
17
+ class Warden::SessionSerializer
18
+ def serialize(record)
19
+ klass = record.class
20
+ array = klass.serialize_into_session(record)
21
+ array.unshift(klass.name)
22
+ end
23
+
24
+ def deserialize(keys)
25
+ klass_name, *args = keys
26
+
27
+ begin
28
+ klass = ActiveSupport::Inflector.constantize(klass_name)
29
+ if klass.respond_to? :serialize_from_session
30
+ klass.serialize_from_session(*args)
31
+ else
32
+ Rails.logger.warn "[Devise] Stored serialized class #{klass_name} seems not to be Devise enabled anymore. Did you do that on purpose?"
33
+ nil
34
+ end
35
+ rescue NameError => e
36
+ if e.message =~ /uninitialized constant/
37
+ Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass_name}"
38
+ nil
39
+ else
40
+ raise
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,55 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'devise/rails/routes'
3
+ require 'devise/rails/warden_compat'
4
+
5
+ module Devise
6
+ class Engine < ::Rails::Engine
7
+ config.devise = Devise
8
+
9
+ # Initialize Warden and copy its configurations.
10
+ config.app_middleware.use Warden::Manager do |config|
11
+ Devise.warden_config = config
12
+ end
13
+
14
+ # Force routes to be loaded if we are doing any eager load.
15
+ config.before_eager_load { |app| app.reload_routes! }
16
+
17
+ initializer "devise.url_helpers" do
18
+ Devise.include_helpers(Devise::Controllers)
19
+ end
20
+
21
+ initializer "devise.omniauth" do |app|
22
+ Devise.omniauth_configs.each do |provider, config|
23
+ app.middleware.use config.strategy_class, *config.args do |strategy|
24
+ config.strategy = strategy
25
+ end
26
+ end
27
+
28
+ if Devise.omniauth_configs.any?
29
+ Devise.include_helpers(Devise::OmniAuth)
30
+ end
31
+ end
32
+
33
+ initializer "devise.mongoid_version_warning" do
34
+ if defined?(Mongoid)
35
+ require 'mongoid/version'
36
+ if Mongoid::VERSION.to_f < 2.1
37
+ puts "\n[DEVISE] Please note that Mongoid versions prior to 2.1 handle dirty model " \
38
+ "object attributes in such a way that the Devise `validatable` module will not apply " \
39
+ "its usual uniqueness and format validations for the email field. It is recommended " \
40
+ "that you upgrade to Mongoid 2.1+ for this and other fixes, but if for some reason you " \
41
+ "are unable to do so, you should add these validations manually.\n"
42
+ end
43
+ end
44
+ end
45
+
46
+ initializer "devise.fix_routes_proxy_missing_respond_to_bug" do
47
+ # We can get rid of this once we support only Rails > 3.2
48
+ ActionDispatch::Routing::RoutesProxy.class_eval do
49
+ def respond_to?(method, include_private = false)
50
+ super || routes.url_helpers.respond_to?(method)
51
+ end
52
+ end
53
+ end
54
+ end
55
+ end