cloudfoundry-devise 1.5.2
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +12 -0
- data/.travis.yml +13 -0
- data/CHANGELOG.rdoc +755 -0
- data/Gemfile +35 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +366 -0
- data/Rakefile +34 -0
- data/app/controllers/devise/confirmations_controller.rb +46 -0
- data/app/controllers/devise/omniauth_callbacks_controller.rb +26 -0
- data/app/controllers/devise/passwords_controller.rb +50 -0
- data/app/controllers/devise/registrations_controller.rb +114 -0
- data/app/controllers/devise/sessions_controller.rb +49 -0
- data/app/controllers/devise/unlocks_controller.rb +34 -0
- data/app/helpers/devise_helper.rb +25 -0
- data/app/mailers/devise/mailer.rb +15 -0
- data/app/views/devise/confirmations/new.html.erb +12 -0
- data/app/views/devise/mailer/confirmation_instructions.html.erb +5 -0
- data/app/views/devise/mailer/reset_password_instructions.html.erb +8 -0
- data/app/views/devise/mailer/unlock_instructions.html.erb +7 -0
- data/app/views/devise/passwords/edit.html.erb +16 -0
- data/app/views/devise/passwords/new.html.erb +12 -0
- data/app/views/devise/registrations/edit.html.erb +25 -0
- data/app/views/devise/registrations/new.html.erb +18 -0
- data/app/views/devise/sessions/new.html.erb +17 -0
- data/app/views/devise/shared/_links.erb +25 -0
- data/app/views/devise/unlocks/new.html.erb +12 -0
- data/cloudfoundry-devise.gemspec +25 -0
- data/config/locales/en.yml +59 -0
- data/lib/devise.rb +453 -0
- data/lib/devise/controllers/helpers.rb +260 -0
- data/lib/devise/controllers/internal_helpers.rb +161 -0
- data/lib/devise/controllers/rememberable.rb +52 -0
- data/lib/devise/controllers/scoped_views.rb +33 -0
- data/lib/devise/controllers/shared_helpers.rb +26 -0
- data/lib/devise/controllers/url_helpers.rb +53 -0
- data/lib/devise/delegator.rb +16 -0
- data/lib/devise/encryptors/authlogic_sha512.rb +19 -0
- data/lib/devise/encryptors/base.rb +20 -0
- data/lib/devise/encryptors/clearance_sha1.rb +17 -0
- data/lib/devise/encryptors/restful_authentication_sha1.rb +22 -0
- data/lib/devise/encryptors/sha1.rb +25 -0
- data/lib/devise/encryptors/sha512.rb +25 -0
- data/lib/devise/failure_app.rb +149 -0
- data/lib/devise/hooks/activatable.rb +11 -0
- data/lib/devise/hooks/forgetable.rb +9 -0
- data/lib/devise/hooks/rememberable.rb +6 -0
- data/lib/devise/hooks/timeoutable.rb +24 -0
- data/lib/devise/hooks/trackable.rb +9 -0
- data/lib/devise/mailers/helpers.rb +86 -0
- data/lib/devise/mapping.rb +175 -0
- data/lib/devise/models.rb +91 -0
- data/lib/devise/models/authenticatable.rb +181 -0
- data/lib/devise/models/confirmable.rb +220 -0
- data/lib/devise/models/database_authenticatable.rb +122 -0
- data/lib/devise/models/encryptable.rb +72 -0
- data/lib/devise/models/lockable.rb +169 -0
- data/lib/devise/models/omniauthable.rb +23 -0
- data/lib/devise/models/recoverable.rb +136 -0
- data/lib/devise/models/registerable.rb +21 -0
- data/lib/devise/models/rememberable.rb +114 -0
- data/lib/devise/models/serializable.rb +43 -0
- data/lib/devise/models/timeoutable.rb +45 -0
- data/lib/devise/models/token_authenticatable.rb +72 -0
- data/lib/devise/models/trackable.rb +30 -0
- data/lib/devise/models/validatable.rb +62 -0
- data/lib/devise/modules.rb +30 -0
- data/lib/devise/omniauth.rb +28 -0
- data/lib/devise/omniauth/config.rb +45 -0
- data/lib/devise/omniauth/url_helpers.rb +33 -0
- data/lib/devise/orm/active_record.rb +44 -0
- data/lib/devise/orm/mongoid.rb +31 -0
- data/lib/devise/param_filter.rb +41 -0
- data/lib/devise/path_checker.rb +18 -0
- data/lib/devise/rails.rb +73 -0
- data/lib/devise/rails/routes.rb +385 -0
- data/lib/devise/rails/warden_compat.rb +120 -0
- data/lib/devise/schema.rb +109 -0
- data/lib/devise/strategies/authenticatable.rb +155 -0
- data/lib/devise/strategies/base.rb +15 -0
- data/lib/devise/strategies/database_authenticatable.rb +21 -0
- data/lib/devise/strategies/rememberable.rb +53 -0
- data/lib/devise/strategies/token_authenticatable.rb +57 -0
- data/lib/devise/test_helpers.rb +90 -0
- data/lib/devise/version.rb +3 -0
- data/lib/generators/active_record/devise_generator.rb +71 -0
- data/lib/generators/active_record/templates/migration.rb +29 -0
- data/lib/generators/active_record/templates/migration_existing.rb +26 -0
- data/lib/generators/devise/devise_generator.rb +22 -0
- data/lib/generators/devise/install_generator.rb +24 -0
- data/lib/generators/devise/orm_helpers.rb +31 -0
- data/lib/generators/devise/views_generator.rb +98 -0
- data/lib/generators/mongoid/devise_generator.rb +60 -0
- data/lib/generators/templates/README +32 -0
- data/lib/generators/templates/devise.rb +215 -0
- data/lib/generators/templates/markerb/confirmation_instructions.markerb +5 -0
- data/lib/generators/templates/markerb/reset_password_instructions.markerb +8 -0
- data/lib/generators/templates/markerb/unlock_instructions.markerb +7 -0
- data/lib/generators/templates/simple_form_for/confirmations/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/passwords/edit.html.erb +19 -0
- data/lib/generators/templates/simple_form_for/passwords/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/registrations/edit.html.erb +22 -0
- data/lib/generators/templates/simple_form_for/registrations/new.html.erb +17 -0
- data/lib/generators/templates/simple_form_for/sessions/new.html.erb +15 -0
- data/lib/generators/templates/simple_form_for/unlocks/new.html.erb +15 -0
- data/test/controllers/helpers_test.rb +254 -0
- data/test/controllers/internal_helpers_test.rb +96 -0
- data/test/controllers/sessions_controller_test.rb +16 -0
- data/test/controllers/url_helpers_test.rb +59 -0
- data/test/delegator_test.rb +19 -0
- data/test/devise_test.rb +72 -0
- data/test/encryptors_test.rb +30 -0
- data/test/failure_app_test.rb +207 -0
- data/test/generators/active_record_generator_test.rb +47 -0
- data/test/generators/devise_generator_test.rb +39 -0
- data/test/generators/install_generator_test.rb +13 -0
- data/test/generators/mongoid_generator_test.rb +23 -0
- data/test/generators/views_generator_test.rb +52 -0
- data/test/helpers/devise_helper_test.rb +51 -0
- data/test/indifferent_hash.rb +33 -0
- data/test/integration/authenticatable_test.rb +590 -0
- data/test/integration/confirmable_test.rb +262 -0
- data/test/integration/database_authenticatable_test.rb +82 -0
- data/test/integration/http_authenticatable_test.rb +82 -0
- data/test/integration/lockable_test.rb +212 -0
- data/test/integration/omniauthable_test.rb +133 -0
- data/test/integration/recoverable_test.rb +287 -0
- data/test/integration/registerable_test.rb +335 -0
- data/test/integration/rememberable_test.rb +158 -0
- data/test/integration/timeoutable_test.rb +98 -0
- data/test/integration/token_authenticatable_test.rb +148 -0
- data/test/integration/trackable_test.rb +92 -0
- data/test/mailers/confirmation_instructions_test.rb +95 -0
- data/test/mailers/reset_password_instructions_test.rb +83 -0
- data/test/mailers/unlock_instructions_test.rb +77 -0
- data/test/mapping_test.rb +128 -0
- data/test/models/confirmable_test.rb +334 -0
- data/test/models/database_authenticatable_test.rb +167 -0
- data/test/models/encryptable_test.rb +67 -0
- data/test/models/lockable_test.rb +225 -0
- data/test/models/recoverable_test.rb +198 -0
- data/test/models/rememberable_test.rb +168 -0
- data/test/models/serializable_test.rb +38 -0
- data/test/models/timeoutable_test.rb +42 -0
- data/test/models/token_authenticatable_test.rb +49 -0
- data/test/models/trackable_test.rb +5 -0
- data/test/models/validatable_test.rb +113 -0
- data/test/models_test.rb +109 -0
- data/test/omniauth/config_test.rb +57 -0
- data/test/omniauth/url_helpers_test.rb +58 -0
- data/test/orm/active_record.rb +9 -0
- data/test/orm/mongoid.rb +14 -0
- data/test/rails_app/Rakefile +10 -0
- data/test/rails_app/app/active_record/admin.rb +6 -0
- data/test/rails_app/app/active_record/shim.rb +2 -0
- data/test/rails_app/app/active_record/user.rb +6 -0
- data/test/rails_app/app/controllers/admins/sessions_controller.rb +6 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +8 -0
- data/test/rails_app/app/controllers/home_controller.rb +25 -0
- data/test/rails_app/app/controllers/publisher/registrations_controller.rb +2 -0
- data/test/rails_app/app/controllers/publisher/sessions_controller.rb +2 -0
- data/test/rails_app/app/controllers/users/omniauth_callbacks_controller.rb +14 -0
- data/test/rails_app/app/controllers/users_controller.rb +23 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/mailers/users/mailer.rb +3 -0
- data/test/rails_app/app/mongoid/admin.rb +24 -0
- data/test/rails_app/app/mongoid/shim.rb +24 -0
- data/test/rails_app/app/mongoid/user.rb +45 -0
- data/test/rails_app/app/views/admins/index.html.erb +1 -0
- data/test/rails_app/app/views/admins/sessions/new.html.erb +2 -0
- data/test/rails_app/app/views/home/admin_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/home/index.html.erb +1 -0
- data/test/rails_app/app/views/home/join.html.erb +1 -0
- data/test/rails_app/app/views/home/private.html.erb +1 -0
- data/test/rails_app/app/views/home/user_dashboard.html.erb +1 -0
- data/test/rails_app/app/views/layouts/application.html.erb +24 -0
- data/test/rails_app/app/views/users/index.html.erb +1 -0
- data/test/rails_app/app/views/users/mailer/confirmation_instructions.erb +1 -0
- data/test/rails_app/app/views/users/sessions/new.html.erb +1 -0
- data/test/rails_app/config.ru +4 -0
- data/test/rails_app/config/application.rb +41 -0
- data/test/rails_app/config/boot.rb +8 -0
- data/test/rails_app/config/database.yml +18 -0
- data/test/rails_app/config/environment.rb +5 -0
- data/test/rails_app/config/environments/development.rb +18 -0
- data/test/rails_app/config/environments/production.rb +33 -0
- data/test/rails_app/config/environments/test.rb +33 -0
- data/test/rails_app/config/initializers/backtrace_silencers.rb +7 -0
- data/test/rails_app/config/initializers/devise.rb +197 -0
- data/test/rails_app/config/initializers/inflections.rb +2 -0
- data/test/rails_app/config/initializers/secret_token.rb +2 -0
- data/test/rails_app/config/routes.rb +87 -0
- data/test/rails_app/db/migrate/20100401102949_create_tables.rb +71 -0
- data/test/rails_app/db/schema.rb +52 -0
- data/test/rails_app/lib/shared_admin.rb +10 -0
- data/test/rails_app/lib/shared_user.rb +26 -0
- data/test/rails_app/public/404.html +26 -0
- data/test/rails_app/public/422.html +26 -0
- data/test/rails_app/public/500.html +26 -0
- data/test/rails_app/public/favicon.ico +0 -0
- data/test/rails_app/script/rails +10 -0
- data/test/routes_test.rb +240 -0
- data/test/support/assertions.rb +27 -0
- data/test/support/helpers.rb +109 -0
- data/test/support/integration.rb +88 -0
- data/test/support/locale/en.yml +4 -0
- data/test/support/webrat/integrations/rails.rb +24 -0
- data/test/test_helper.rb +27 -0
- data/test/test_helpers_test.rb +134 -0
- metadata +295 -0
@@ -0,0 +1,385 @@
|
|
1
|
+
module ActionDispatch::Routing
|
2
|
+
class RouteSet #:nodoc:
|
3
|
+
# Ensure Devise modules are included only after loading routes, because we
|
4
|
+
# need devise_for mappings already declared to create filters and helpers.
|
5
|
+
def finalize_with_devise!
|
6
|
+
finalize_without_devise!
|
7
|
+
|
8
|
+
@devise_finalized ||= begin
|
9
|
+
Devise.configure_warden!
|
10
|
+
Devise.regenerate_helpers!
|
11
|
+
true
|
12
|
+
end
|
13
|
+
end
|
14
|
+
alias_method_chain :finalize!, :devise
|
15
|
+
end
|
16
|
+
|
17
|
+
class Mapper
|
18
|
+
# Includes devise_for method for routes. This method is responsible to
|
19
|
+
# generate all needed routes for devise, based on what modules you have
|
20
|
+
# defined in your model.
|
21
|
+
#
|
22
|
+
# ==== Examples
|
23
|
+
#
|
24
|
+
# Let's say you have an User model configured to use authenticatable,
|
25
|
+
# confirmable and recoverable modules. After creating this inside your routes:
|
26
|
+
#
|
27
|
+
# devise_for :users
|
28
|
+
#
|
29
|
+
# This method is going to look inside your User model and create the
|
30
|
+
# needed routes:
|
31
|
+
#
|
32
|
+
# # Session routes for Authenticatable (default)
|
33
|
+
# new_user_session GET /users/sign_in {:controller=>"devise/sessions", :action=>"new"}
|
34
|
+
# user_session POST /users/sign_in {:controller=>"devise/sessions", :action=>"create"}
|
35
|
+
# destroy_user_session GET /users/sign_out {:controller=>"devise/sessions", :action=>"destroy"}
|
36
|
+
#
|
37
|
+
# # Password routes for Recoverable, if User model has :recoverable configured
|
38
|
+
# new_user_password GET /users/password/new(.:format) {:controller=>"devise/passwords", :action=>"new"}
|
39
|
+
# edit_user_password GET /users/password/edit(.:format) {:controller=>"devise/passwords", :action=>"edit"}
|
40
|
+
# user_password PUT /users/password(.:format) {:controller=>"devise/passwords", :action=>"update"}
|
41
|
+
# POST /users/password(.:format) {:controller=>"devise/passwords", :action=>"create"}
|
42
|
+
#
|
43
|
+
# # Confirmation routes for Confirmable, if User model has :confirmable configured
|
44
|
+
# new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"devise/confirmations", :action=>"new"}
|
45
|
+
# user_confirmation GET /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"show"}
|
46
|
+
# POST /users/confirmation(.:format) {:controller=>"devise/confirmations", :action=>"create"}
|
47
|
+
#
|
48
|
+
# ==== Options
|
49
|
+
#
|
50
|
+
# You can configure your routes with some options:
|
51
|
+
#
|
52
|
+
# * :class_name => setup a different class to be looked up by devise, if it cannot be
|
53
|
+
# properly found by the route name.
|
54
|
+
#
|
55
|
+
# devise_for :users, :class_name => 'Account'
|
56
|
+
#
|
57
|
+
# * :path => allows you to setup path name that will be used, as rails routes does.
|
58
|
+
# The following route configuration would setup your route as /accounts instead of /users:
|
59
|
+
#
|
60
|
+
# devise_for :users, :path => 'accounts'
|
61
|
+
#
|
62
|
+
# * :singular => setup the singular name for the given resource. This is used as the instance variable
|
63
|
+
# name in controller, as the name in routes and the scope given to warden.
|
64
|
+
#
|
65
|
+
# devise_for :users, :singular => :user
|
66
|
+
#
|
67
|
+
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :sign_up,
|
68
|
+
# :password, :confirmation, :unlock.
|
69
|
+
#
|
70
|
+
# devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
71
|
+
#
|
72
|
+
# * :controllers => the controller which should be used. All routes by default points to Devise controllers.
|
73
|
+
# However, if you want them to point to custom controller, you should do:
|
74
|
+
#
|
75
|
+
# devise_for :users, :controllers => { :sessions => "users/sessions" }
|
76
|
+
#
|
77
|
+
# * :failure_app => a rack app which is invoked whenever there is a failure. Strings representing a given
|
78
|
+
# are also allowed as parameter.
|
79
|
+
#
|
80
|
+
# * :sign_out_via => the HTTP method(s) accepted for the :sign_out action (default: :get),
|
81
|
+
# if you wish to restrict this to accept only :post or :delete requests you should do:
|
82
|
+
#
|
83
|
+
# devise_for :users, :sign_out_via => [ :post, :delete ]
|
84
|
+
#
|
85
|
+
# You need to make sure that your sign_out controls trigger a request with a matching HTTP method.
|
86
|
+
#
|
87
|
+
# * :module => the namespace to find controlers. By default, devise will access devise/sessions,
|
88
|
+
# devise/registrations and so on. If you want to namespace all at once, use module:
|
89
|
+
#
|
90
|
+
# devise_for :users, :module => "users"
|
91
|
+
#
|
92
|
+
# Notice that whenever you use namespace in the router DSL, it automatically sets the module.
|
93
|
+
# So the following setup:
|
94
|
+
#
|
95
|
+
# namespace :publisher
|
96
|
+
# devise_for :account
|
97
|
+
# end
|
98
|
+
#
|
99
|
+
# Will use publisher/sessions controller instead of devise/sessions controller. You can revert
|
100
|
+
# this by providing the :module option to devise_for.
|
101
|
+
#
|
102
|
+
# Also pay attention that when you use a namespace it will affect all the helpers and methods for controllers
|
103
|
+
# and views. For example, using the above setup you'll end with following methods:
|
104
|
+
# current_publisher_account, authenticate_publisher_account!, publisher_account_signed_in, etc.
|
105
|
+
#
|
106
|
+
# * :skip => tell which controller you want to skip routes from being created:
|
107
|
+
#
|
108
|
+
# devise_for :users, :skip => :sessions
|
109
|
+
#
|
110
|
+
# * :only => the opposite of :skip, tell which controllers only to generate routes to:
|
111
|
+
#
|
112
|
+
# devise_for :users, :only => :sessions
|
113
|
+
#
|
114
|
+
# * :skip_helpers => skip generating Devise url helpers like new_session_path(@user).
|
115
|
+
# This is useful to avoid conflicts with previous routes and is false by default.
|
116
|
+
# It accepts true as option, meaning it will skip all the helpers for the controllers
|
117
|
+
# given in :skip but it also accepts specific helpers to be skipped:
|
118
|
+
#
|
119
|
+
# devise_for :users, :skip => [:registrations, :confirmations], :skip_helpers => true
|
120
|
+
# devise_for :users, :skip_helpers => [:registrations, :confirmations]
|
121
|
+
#
|
122
|
+
# * :format => include "(.:format)" in the generated routes? true by default, set to false to disable:
|
123
|
+
#
|
124
|
+
# devise_for :users, :format => false
|
125
|
+
#
|
126
|
+
# * :constraints => works the same as Rails' contraints
|
127
|
+
#
|
128
|
+
# * :defaults => works the same as Rails' defaults
|
129
|
+
#
|
130
|
+
# ==== Scoping
|
131
|
+
#
|
132
|
+
# Following Rails 3 routes DSL, you can nest devise_for calls inside a scope:
|
133
|
+
#
|
134
|
+
# scope "/my" do
|
135
|
+
# devise_for :users
|
136
|
+
# end
|
137
|
+
#
|
138
|
+
# However, since Devise uses the request path to retrieve the current user, it has one caveats.
|
139
|
+
# If you are using a dynamic segment, as below:
|
140
|
+
#
|
141
|
+
# scope ":locale" do
|
142
|
+
# devise_for :users
|
143
|
+
# end
|
144
|
+
#
|
145
|
+
# You are required to configure default_url_options in your ApplicationController class level, so
|
146
|
+
# Devise can pick it:
|
147
|
+
#
|
148
|
+
# class ApplicationController < ActionController::Base
|
149
|
+
# def self.default_url_options
|
150
|
+
# { :locale => I18n.locale }
|
151
|
+
# end
|
152
|
+
# end
|
153
|
+
#
|
154
|
+
# ==== Adding custom actions to override controllers
|
155
|
+
#
|
156
|
+
# You can pass a block to devise_for that will add any routes defined in the block to Devise's
|
157
|
+
# list of known actions. This is important if you add a custom action to a controller that
|
158
|
+
# overrides an out of the box Devise controller.
|
159
|
+
# For example:
|
160
|
+
#
|
161
|
+
# class RegistrationsController < Devise::RegistrationsController
|
162
|
+
# def update
|
163
|
+
# # do something different here
|
164
|
+
# end
|
165
|
+
#
|
166
|
+
# def deactivate
|
167
|
+
# # not a standard action
|
168
|
+
# # deactivate code here
|
169
|
+
# end
|
170
|
+
# end
|
171
|
+
#
|
172
|
+
# In order to get Devise to recognize the deactivate action, your devise_for entry should look like this,
|
173
|
+
#
|
174
|
+
# devise_for :owners, :controllers => { :registrations => "registrations" } do
|
175
|
+
# post "deactivate", :to => "registrations#deactivate", :as => "deactivate_registration"
|
176
|
+
# end
|
177
|
+
#
|
178
|
+
def devise_for(*resources)
|
179
|
+
@devise_finalized = false
|
180
|
+
options = resources.extract_options!
|
181
|
+
|
182
|
+
options[:as] ||= @scope[:as] if @scope[:as].present?
|
183
|
+
options[:module] ||= @scope[:module] if @scope[:module].present?
|
184
|
+
options[:path_prefix] ||= @scope[:path] if @scope[:path].present?
|
185
|
+
options[:path_names] = (@scope[:path_names] || {}).merge(options[:path_names] || {})
|
186
|
+
options[:constraints] = (@scope[:constraints] || {}).merge(options[:constraints] || {})
|
187
|
+
options[:defaults] = (@scope[:defaults] || {}).merge(options[:defaults] || {})
|
188
|
+
@scope[:options] = (@scope[:options] || {}).merge({:format => false}) if options[:format] == false
|
189
|
+
|
190
|
+
resources.map!(&:to_sym)
|
191
|
+
|
192
|
+
resources.each do |resource|
|
193
|
+
mapping = Devise.add_mapping(resource, options)
|
194
|
+
|
195
|
+
begin
|
196
|
+
raise_no_devise_method_error!(mapping.class_name) unless mapping.to.respond_to?(:devise)
|
197
|
+
rescue NameError => e
|
198
|
+
raise unless mapping.class_name == resource.to_s.classify
|
199
|
+
warn "[WARNING] You provided devise_for #{resource.inspect} but there is " <<
|
200
|
+
"no model #{mapping.class_name} defined in your application"
|
201
|
+
next
|
202
|
+
rescue NoMethodError => e
|
203
|
+
raise unless e.message.include?("undefined method `devise'")
|
204
|
+
raise_no_devise_method_error!(mapping.class_name)
|
205
|
+
end
|
206
|
+
|
207
|
+
routes = mapping.used_routes
|
208
|
+
|
209
|
+
devise_scope mapping.name do
|
210
|
+
yield if block_given?
|
211
|
+
with_devise_exclusive_scope mapping.fullpath, mapping.name, mapping.constraints, mapping.defaults do
|
212
|
+
routes.each { |mod| send("devise_#{mod}", mapping, mapping.controllers) }
|
213
|
+
end
|
214
|
+
end
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
218
|
+
# Allow you to add authentication request from the router:
|
219
|
+
#
|
220
|
+
# authenticate do
|
221
|
+
# resources :post
|
222
|
+
# end
|
223
|
+
#
|
224
|
+
# authenticate(:admin) do
|
225
|
+
# resources :users
|
226
|
+
# end
|
227
|
+
#
|
228
|
+
def authenticate(scope=nil)
|
229
|
+
constraint = lambda do |request|
|
230
|
+
request.env["warden"].authenticate!(:scope => scope)
|
231
|
+
end
|
232
|
+
|
233
|
+
constraints(constraint) do
|
234
|
+
yield
|
235
|
+
end
|
236
|
+
end
|
237
|
+
|
238
|
+
# Allow you to route based on whether a scope is authenticated. You
|
239
|
+
# can optionally specify which scope.
|
240
|
+
#
|
241
|
+
# authenticated :admin do
|
242
|
+
# root :to => 'admin/dashboard#show'
|
243
|
+
# end
|
244
|
+
#
|
245
|
+
# authenticated do
|
246
|
+
# root :to => 'dashboard#show'
|
247
|
+
# end
|
248
|
+
#
|
249
|
+
# root :to => 'landing#show'
|
250
|
+
#
|
251
|
+
def authenticated(scope=nil)
|
252
|
+
constraint = lambda do |request|
|
253
|
+
request.env["warden"].authenticate? :scope => scope
|
254
|
+
end
|
255
|
+
|
256
|
+
constraints(constraint) do
|
257
|
+
yield
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
# Allow you to route based on whether a scope is *not* authenticated.
|
262
|
+
# You can optionally specify which scope.
|
263
|
+
#
|
264
|
+
# unauthenticated do
|
265
|
+
# as :user do
|
266
|
+
# root :to => 'devise/registrations#new'
|
267
|
+
# end
|
268
|
+
# end
|
269
|
+
#
|
270
|
+
# root :to => 'dashboard#show'
|
271
|
+
#
|
272
|
+
def unauthenticated(scope=nil)
|
273
|
+
constraint = lambda do |request|
|
274
|
+
not request.env["warden"].authenticate? :scope => scope
|
275
|
+
end
|
276
|
+
|
277
|
+
constraints(constraint) do
|
278
|
+
yield
|
279
|
+
end
|
280
|
+
end
|
281
|
+
|
282
|
+
# Sets the devise scope to be used in the controller. If you have custom routes,
|
283
|
+
# you are required to call this method (also aliased as :as) in order to specify
|
284
|
+
# to which controller it is targetted.
|
285
|
+
#
|
286
|
+
# as :user do
|
287
|
+
# get "sign_in", :to => "devise/sessions#new"
|
288
|
+
# end
|
289
|
+
#
|
290
|
+
# Notice you cannot have two scopes mapping to the same URL. And remember, if
|
291
|
+
# you try to access a devise controller without specifying a scope, it will
|
292
|
+
# raise ActionNotFound error.
|
293
|
+
#
|
294
|
+
# Also be aware of that 'devise_scope' and 'as' use the singular form of the
|
295
|
+
# noun where other devise route commands expect the plural form. This would be a
|
296
|
+
# good and working example.
|
297
|
+
#
|
298
|
+
# devise_scope :user do
|
299
|
+
# match "/some/route" => "some_devise_controller"
|
300
|
+
# end
|
301
|
+
# devise_for :users
|
302
|
+
#
|
303
|
+
# Notice and be aware of the differences above between :user and :users
|
304
|
+
def devise_scope(scope)
|
305
|
+
constraint = lambda do |request|
|
306
|
+
request.env["devise.mapping"] = Devise.mappings[scope]
|
307
|
+
true
|
308
|
+
end
|
309
|
+
|
310
|
+
constraints(constraint) do
|
311
|
+
yield
|
312
|
+
end
|
313
|
+
end
|
314
|
+
alias :as :devise_scope
|
315
|
+
|
316
|
+
protected
|
317
|
+
|
318
|
+
def devise_session(mapping, controllers) #:nodoc:
|
319
|
+
resource :session, :only => [], :controller => controllers[:sessions], :path => "" do
|
320
|
+
get :new, :path => mapping.path_names[:sign_in], :as => "new"
|
321
|
+
post :create, :path => mapping.path_names[:sign_in]
|
322
|
+
match :destroy, :path => mapping.path_names[:sign_out], :as => "destroy", :via => mapping.sign_out_via
|
323
|
+
end
|
324
|
+
end
|
325
|
+
|
326
|
+
def devise_password(mapping, controllers) #:nodoc:
|
327
|
+
resource :password, :only => [:new, :create, :edit, :update],
|
328
|
+
:path => mapping.path_names[:password], :controller => controllers[:passwords]
|
329
|
+
end
|
330
|
+
|
331
|
+
def devise_confirmation(mapping, controllers) #:nodoc:
|
332
|
+
resource :confirmation, :only => [:new, :create, :show],
|
333
|
+
:path => mapping.path_names[:confirmation], :controller => controllers[:confirmations]
|
334
|
+
end
|
335
|
+
|
336
|
+
def devise_unlock(mapping, controllers) #:nodoc:
|
337
|
+
if mapping.to.unlock_strategy_enabled?(:email)
|
338
|
+
resource :unlock, :only => [:new, :create, :show],
|
339
|
+
:path => mapping.path_names[:unlock], :controller => controllers[:unlocks]
|
340
|
+
end
|
341
|
+
end
|
342
|
+
|
343
|
+
def devise_registration(mapping, controllers) #:nodoc:
|
344
|
+
path_names = {
|
345
|
+
:new => mapping.path_names[:sign_up],
|
346
|
+
:cancel => mapping.path_names[:cancel]
|
347
|
+
}
|
348
|
+
|
349
|
+
resource :registration, :only => [:new, :create, :edit, :update, :destroy], :path => mapping.path_names[:registration],
|
350
|
+
:path_names => path_names, :controller => controllers[:registrations] do
|
351
|
+
get :cancel
|
352
|
+
end
|
353
|
+
end
|
354
|
+
|
355
|
+
def devise_omniauth_callback(mapping, controllers) #:nodoc:
|
356
|
+
path, @scope[:path] = @scope[:path], nil
|
357
|
+
path_prefix = "/#{mapping.path}/auth".squeeze("/")
|
358
|
+
|
359
|
+
if ::OmniAuth.config.path_prefix && ::OmniAuth.config.path_prefix != path_prefix
|
360
|
+
raise "You can only add :omniauthable behavior to one Devise model"
|
361
|
+
else
|
362
|
+
::OmniAuth.config.path_prefix = path_prefix
|
363
|
+
end
|
364
|
+
|
365
|
+
match "#{path_prefix}/:action/callback", :constraints => { :action => Regexp.union(mapping.to.omniauth_providers.map(&:to_s)) },
|
366
|
+
:to => controllers[:omniauth_callbacks], :as => :omniauth_callback
|
367
|
+
ensure
|
368
|
+
@scope[:path] = path
|
369
|
+
end
|
370
|
+
|
371
|
+
def with_devise_exclusive_scope(new_path, new_as, new_constraints, new_defaults) #:nodoc:
|
372
|
+
old_as, old_path, old_module, old_constraints, old_defaults = @scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults]
|
373
|
+
@scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults] = new_as, new_path, nil, new_constraints, new_defaults
|
374
|
+
yield
|
375
|
+
ensure
|
376
|
+
@scope[:as], @scope[:path], @scope[:module], @scope[:constraints], @scope[:defaults] = old_as, old_path, old_module, old_constraints, old_defaults
|
377
|
+
end
|
378
|
+
|
379
|
+
def raise_no_devise_method_error!(klass) #:nodoc:
|
380
|
+
raise "#{klass} does not respond to 'devise' method. This usually means you haven't " \
|
381
|
+
"loaded your ORM file or it's being loaded too late. To fix it, be sure to require 'devise/orm/YOUR_ORM' " \
|
382
|
+
"inside 'config/initializers/devise.rb' or before your application definition in 'config/application.rb'"
|
383
|
+
end
|
384
|
+
end
|
385
|
+
end
|
@@ -0,0 +1,120 @@
|
|
1
|
+
module Warden::Mixins::Common
|
2
|
+
def request
|
3
|
+
@request ||= ActionDispatch::Request.new(env)
|
4
|
+
end
|
5
|
+
|
6
|
+
# This is called internally by Warden on logout
|
7
|
+
def reset_session!
|
8
|
+
request.reset_session
|
9
|
+
end
|
10
|
+
|
11
|
+
def cookies
|
12
|
+
request.cookie_jar
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Warden::SessionSerializer
|
17
|
+
def serialize(record)
|
18
|
+
klass = record.class
|
19
|
+
array = klass.serialize_into_session(record)
|
20
|
+
array.unshift(klass.name)
|
21
|
+
end
|
22
|
+
|
23
|
+
def deserialize(keys)
|
24
|
+
klass, *args = keys
|
25
|
+
|
26
|
+
begin
|
27
|
+
ActiveSupport::Inflector.constantize(klass).serialize_from_session(*args)
|
28
|
+
rescue NameError => e
|
29
|
+
if e.message =~ /uninitialized constant/
|
30
|
+
Rails.logger.debug "[Devise] Trying to deserialize invalid class #{klass}"
|
31
|
+
nil
|
32
|
+
else
|
33
|
+
raise
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
39
|
+
unless Devise.rack_session?
|
40
|
+
# We cannot use Rails Indifferent Hash because it messes up the flash object.
|
41
|
+
class Devise::IndifferentHash < Hash
|
42
|
+
alias_method :regular_writer, :[]= unless method_defined?(:regular_writer)
|
43
|
+
alias_method :regular_update, :update unless method_defined?(:regular_update)
|
44
|
+
|
45
|
+
def [](key)
|
46
|
+
super(convert_key(key))
|
47
|
+
end
|
48
|
+
|
49
|
+
def []=(key, value)
|
50
|
+
regular_writer(convert_key(key), value)
|
51
|
+
end
|
52
|
+
|
53
|
+
alias_method :store, :[]=
|
54
|
+
|
55
|
+
def update(other_hash)
|
56
|
+
other_hash.each_pair { |key, value| regular_writer(convert_key(key), value) }
|
57
|
+
self
|
58
|
+
end
|
59
|
+
|
60
|
+
alias_method :merge!, :update
|
61
|
+
|
62
|
+
def key?(key)
|
63
|
+
super(convert_key(key))
|
64
|
+
end
|
65
|
+
|
66
|
+
alias_method :include?, :key?
|
67
|
+
alias_method :has_key?, :key?
|
68
|
+
alias_method :member?, :key?
|
69
|
+
|
70
|
+
def fetch(key, *extras)
|
71
|
+
super(convert_key(key), *extras)
|
72
|
+
end
|
73
|
+
|
74
|
+
def values_at(*indices)
|
75
|
+
indices.collect {|key| self[convert_key(key)]}
|
76
|
+
end
|
77
|
+
|
78
|
+
def merge(hash)
|
79
|
+
self.dup.update(hash)
|
80
|
+
end
|
81
|
+
|
82
|
+
def delete(key)
|
83
|
+
super(convert_key(key))
|
84
|
+
end
|
85
|
+
|
86
|
+
def stringify_keys!; self end
|
87
|
+
def stringify_keys; dup end
|
88
|
+
|
89
|
+
undef :symbolize_keys!
|
90
|
+
def symbolize_keys; to_hash.symbolize_keys end
|
91
|
+
|
92
|
+
def to_options!; self end
|
93
|
+
def to_hash; Hash.new.update(self) end
|
94
|
+
|
95
|
+
protected
|
96
|
+
|
97
|
+
def convert_key(key)
|
98
|
+
key.kind_of?(Symbol) ? key.to_s : key
|
99
|
+
end
|
100
|
+
end
|
101
|
+
|
102
|
+
class ActionDispatch::Request
|
103
|
+
def reset_session
|
104
|
+
session.destroy if session && session.respond_to?(:destroy)
|
105
|
+
self.session = {}
|
106
|
+
@env['action_dispatch.request.flash_hash'] = nil
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
Warden::Manager.after_set_user :event => [:set_user, :authentication] do |record, warden, options|
|
111
|
+
if options[:scope] && warden.authenticated?(options[:scope])
|
112
|
+
request, flash = warden.request, warden.env['action_dispatch.request.flash_hash']
|
113
|
+
backup = request.session.to_hash
|
114
|
+
backup.delete("session_id")
|
115
|
+
request.reset_session
|
116
|
+
warden.env['action_dispatch.request.flash_hash'] = flash
|
117
|
+
request.session = Devise::IndifferentHash.new.update(backup)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|