ramon-devise 0.4.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.
- data/CHANGELOG.rdoc +109 -0
- data/MIT-LICENSE +20 -0
- data/README.rdoc +243 -0
- data/Rakefile +45 -0
- data/TODO +8 -0
- data/app/controllers/confirmations_controller.rb +33 -0
- data/app/controllers/passwords_controller.rb +41 -0
- data/app/controllers/sessions_controller.rb +33 -0
- data/app/models/devise_mailer.rb +53 -0
- data/app/views/confirmations/new.html.erb +16 -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/passwords/edit.html.erb +20 -0
- data/app/views/passwords/new.html.erb +16 -0
- data/app/views/sessions/new.html.erb +23 -0
- data/generators/devise/USAGE +5 -0
- data/generators/devise/devise_generator.rb +25 -0
- data/generators/devise/lib/route_devise.rb +32 -0
- data/generators/devise/templates/README +22 -0
- data/generators/devise/templates/migration.rb +20 -0
- data/generators/devise/templates/model.rb +5 -0
- data/generators/devise_install/USAGE +3 -0
- data/generators/devise_install/devise_install_generator.rb +9 -0
- data/generators/devise_install/templates/devise.rb +40 -0
- data/generators/devise_views/USAGE +3 -0
- data/generators/devise_views/devise_views_generator.rb +24 -0
- data/init.rb +2 -0
- data/lib/devise.rb +79 -0
- data/lib/devise/controllers/filters.rb +111 -0
- data/lib/devise/controllers/helpers.rb +130 -0
- data/lib/devise/controllers/url_helpers.rb +49 -0
- data/lib/devise/failure.rb +38 -0
- data/lib/devise/hooks/confirmable.rb +11 -0
- data/lib/devise/hooks/rememberable.rb +27 -0
- data/lib/devise/locales/en.yml +18 -0
- data/lib/devise/mapping.rb +120 -0
- data/lib/devise/migrations.rb +51 -0
- data/lib/devise/models.rb +105 -0
- data/lib/devise/models/authenticatable.rb +97 -0
- data/lib/devise/models/confirmable.rb +156 -0
- data/lib/devise/models/recoverable.rb +88 -0
- data/lib/devise/models/rememberable.rb +95 -0
- data/lib/devise/models/validatable.rb +36 -0
- data/lib/devise/rails.rb +17 -0
- data/lib/devise/rails/routes.rb +109 -0
- data/lib/devise/rails/warden_compat.rb +26 -0
- data/lib/devise/strategies/authenticatable.rb +46 -0
- data/lib/devise/strategies/base.rb +24 -0
- data/lib/devise/strategies/rememberable.rb +35 -0
- data/lib/devise/version.rb +3 -0
- data/lib/devise/warden.rb +24 -0
- data/test/controllers/filters_test.rb +103 -0
- data/test/controllers/helpers_test.rb +55 -0
- data/test/controllers/url_helpers_test.rb +47 -0
- data/test/devise_test.rb +72 -0
- data/test/failure_test.rb +34 -0
- data/test/integration/authenticatable_test.rb +187 -0
- data/test/integration/confirmable_test.rb +89 -0
- data/test/integration/recoverable_test.rb +131 -0
- data/test/integration/rememberable_test.rb +65 -0
- data/test/mailers/confirmation_instructions_test.rb +59 -0
- data/test/mailers/reset_password_instructions_test.rb +62 -0
- data/test/mapping_test.rb +101 -0
- data/test/models/authenticatable_test.rb +118 -0
- data/test/models/confirmable_test.rb +237 -0
- data/test/models/recoverable_test.rb +141 -0
- data/test/models/rememberable_test.rb +130 -0
- data/test/models/validatable_test.rb +99 -0
- data/test/models_test.rb +111 -0
- data/test/rails_app/app/controllers/admins_controller.rb +6 -0
- data/test/rails_app/app/controllers/application_controller.rb +10 -0
- data/test/rails_app/app/controllers/home_controller.rb +4 -0
- data/test/rails_app/app/controllers/users_controller.rb +7 -0
- data/test/rails_app/app/helpers/application_helper.rb +3 -0
- data/test/rails_app/app/models/account.rb +3 -0
- data/test/rails_app/app/models/admin.rb +3 -0
- data/test/rails_app/app/models/organizer.rb +3 -0
- data/test/rails_app/app/models/user.rb +3 -0
- data/test/rails_app/config/boot.rb +110 -0
- data/test/rails_app/config/environment.rb +41 -0
- data/test/rails_app/config/environments/development.rb +17 -0
- data/test/rails_app/config/environments/production.rb +28 -0
- data/test/rails_app/config/environments/test.rb +28 -0
- data/test/rails_app/config/initializers/new_rails_defaults.rb +21 -0
- data/test/rails_app/config/initializers/session_store.rb +15 -0
- data/test/rails_app/config/routes.rb +18 -0
- data/test/routes_test.rb +79 -0
- data/test/support/assertions_helper.rb +22 -0
- data/test/support/integration_tests_helper.rb +66 -0
- data/test/support/model_tests_helper.rb +51 -0
- data/test/test_helper.rb +40 -0
- metadata +154 -0
@@ -0,0 +1,95 @@
|
|
1
|
+
require 'digest/sha1'
|
2
|
+
require 'devise/hooks/rememberable'
|
3
|
+
require 'devise/strategies/rememberable'
|
4
|
+
|
5
|
+
module Devise
|
6
|
+
module Models
|
7
|
+
|
8
|
+
# Rememberable manages generating and clearing token for remember the user
|
9
|
+
# from a saved cookie. Rememberable also has utility methods for dealing
|
10
|
+
# with serializing the user into the cookie and back from the cookie, trying
|
11
|
+
# to lookup the record based on the saved information.
|
12
|
+
# You probably wouldn't use rememberable methods directly, they are used
|
13
|
+
# mostly internally for handling the remember token.
|
14
|
+
#
|
15
|
+
# Configuration:
|
16
|
+
#
|
17
|
+
# remember_for: the time you want the user will be remembered without
|
18
|
+
# asking for credentials. After this time the user will be
|
19
|
+
# blocked and will have to enter his credentials again.
|
20
|
+
# This configuration is also used to calculate the expires
|
21
|
+
# time for the cookie created to remember the user.
|
22
|
+
# By default remember_for is 2.weeks.
|
23
|
+
#
|
24
|
+
# Examples:
|
25
|
+
#
|
26
|
+
# User.find(1).remember_me! # regenerating the token
|
27
|
+
# User.find(1).forget_me! # clearing the token
|
28
|
+
#
|
29
|
+
# # generating info to put into cookies
|
30
|
+
# User.serialize_into_cookie(user)
|
31
|
+
#
|
32
|
+
# # lookup the user based on the incoming cookie information
|
33
|
+
# User.serialize_from_cookie(cookie_string)
|
34
|
+
module Rememberable
|
35
|
+
|
36
|
+
def self.included(base)
|
37
|
+
base.class_eval do
|
38
|
+
extend ClassMethods
|
39
|
+
|
40
|
+
# Remember me option available in after_authentication hook.
|
41
|
+
attr_accessor :remember_me
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
# Generate a new remember token and save the record without validations.
|
46
|
+
def remember_me!
|
47
|
+
self.remember_token = friendly_token
|
48
|
+
self.remember_created_at = Time.now.utc
|
49
|
+
save(false)
|
50
|
+
end
|
51
|
+
|
52
|
+
# Removes the remember token only if it exists, and save the record
|
53
|
+
# without validations.
|
54
|
+
def forget_me!
|
55
|
+
if remember_token?
|
56
|
+
self.remember_token = nil
|
57
|
+
self.remember_created_at = nil
|
58
|
+
save(false)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
# Checks whether the incoming token matches or not with the record token.
|
63
|
+
def valid_remember_token?(token)
|
64
|
+
remember_token? && !remember_expired? && remember_token == token
|
65
|
+
end
|
66
|
+
|
67
|
+
# Remember token should be expired if expiration time not overpass now.
|
68
|
+
def remember_expired?
|
69
|
+
remember_expires_at <= Time.now.utc
|
70
|
+
end
|
71
|
+
|
72
|
+
# Remember token expires at created time + remember_for configuration
|
73
|
+
def remember_expires_at
|
74
|
+
remember_created_at + remember_for
|
75
|
+
end
|
76
|
+
|
77
|
+
module ClassMethods
|
78
|
+
|
79
|
+
# Create the cookie key using the record id and remember_token
|
80
|
+
def serialize_into_cookie(rememberable)
|
81
|
+
"#{rememberable.id}::#{rememberable.remember_token}"
|
82
|
+
end
|
83
|
+
|
84
|
+
# Recreate the user based on the stored cookie
|
85
|
+
def serialize_from_cookie(cookie)
|
86
|
+
rememberable_id, remember_token = cookie.split('::')
|
87
|
+
rememberable = find_by_id(rememberable_id) if rememberable_id
|
88
|
+
rememberable if rememberable.try(:valid_remember_token?, remember_token)
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
Devise::Models.config(self, :remember_for, 2.weeks)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,36 @@
|
|
1
|
+
module Devise
|
2
|
+
module Models
|
3
|
+
|
4
|
+
# Validatable creates all needed validations for a user email and password.
|
5
|
+
# It's optional, given you may want to create the validations by yourself.
|
6
|
+
# Automatically validate if the email is present, unique and it's format is
|
7
|
+
# valid. Also tests presence of password, confirmation and length
|
8
|
+
module Validatable
|
9
|
+
|
10
|
+
# Email regex used to validate email formats. Retrieved from authlogic.
|
11
|
+
EMAIL_REGEX = /\A[\w\.%\+\-]+@(?:[A-Z0-9\-]+\.)+(?:[A-Z]{2,4}|museum|travel)\z/i
|
12
|
+
|
13
|
+
def self.included(base)
|
14
|
+
base.class_eval do
|
15
|
+
|
16
|
+
validates_presence_of :email
|
17
|
+
validates_uniqueness_of :email, :allow_blank => true
|
18
|
+
validates_format_of :email, :with => EMAIL_REGEX, :allow_blank => true
|
19
|
+
|
20
|
+
validates_presence_of :password, :if => :password_required?
|
21
|
+
validates_confirmation_of :password, :if => :password_required?
|
22
|
+
validates_length_of :password, :within => 6..20, :allow_blank => true, :if => :password_required?
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
protected
|
27
|
+
|
28
|
+
# Checks whether a password is needed or not. For validations only.
|
29
|
+
# Passwords are always required if it's a new record, or if the password
|
30
|
+
# or confirmation are being set somewhere.
|
31
|
+
def password_required?
|
32
|
+
new_record? || !password.nil? || !password_confirmation.nil?
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
data/lib/devise/rails.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'devise/rails/routes'
|
2
|
+
require 'devise/rails/warden_compat'
|
3
|
+
|
4
|
+
Rails.configuration.after_initialize do
|
5
|
+
if defined?(ActiveRecord)
|
6
|
+
ActiveRecord::Base.extend Devise::Models
|
7
|
+
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Migrations
|
8
|
+
end
|
9
|
+
|
10
|
+
# Adds Warden Manager to Rails middleware stack, configuring default devise
|
11
|
+
# strategy and also the failure app.
|
12
|
+
Rails.configuration.middleware.use Warden::Manager do |manager|
|
13
|
+
Devise.configure_warden_manager(manager)
|
14
|
+
end
|
15
|
+
|
16
|
+
I18n.load_path.unshift File.expand_path(File.join(File.dirname(__FILE__), 'locales', 'en.yml'))
|
17
|
+
end
|
@@ -0,0 +1,109 @@
|
|
1
|
+
module ActionController::Routing
|
2
|
+
class RouteSet #:nodoc:
|
3
|
+
|
4
|
+
# Ensure Devise modules are included only after loading routes, because we
|
5
|
+
# need devise_for mappings already declared to create magic filters and
|
6
|
+
# helpers.
|
7
|
+
def load_routes_with_devise!
|
8
|
+
load_routes_without_devise!
|
9
|
+
|
10
|
+
ActionController::Base.send :include, Devise::Controllers::Filters
|
11
|
+
ActionController::Base.send :include, Devise::Controllers::UrlHelpers
|
12
|
+
|
13
|
+
ActionView::Base.send :include, Devise::Controllers::UrlHelpers
|
14
|
+
end
|
15
|
+
alias_method_chain :load_routes!, :devise
|
16
|
+
|
17
|
+
class Mapper #:doc:
|
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
|
+
# Examples: Let's say you have an User model configured to use
|
22
|
+
# authenticatable, confirmable and recoverable modules. After creating this
|
23
|
+
# inside your routes:
|
24
|
+
#
|
25
|
+
# map.devise_for :users
|
26
|
+
#
|
27
|
+
# this method is going to look inside your User model and create the
|
28
|
+
# needed routes:
|
29
|
+
#
|
30
|
+
# # Session routes for Authenticatable (default)
|
31
|
+
# new_user_session GET /users/sign_in {:controller=>"sessions", :action=>"new"}
|
32
|
+
# user_session POST /users/sign_in {:controller=>"sessions", :action=>"create"}
|
33
|
+
# destroy_user_session GET /users/sign_out {:controller=>"sessions", :action=>"destroy"}
|
34
|
+
#
|
35
|
+
# # Password routes for Recoverable, if User model has :recoverable configured
|
36
|
+
# new_user_password GET /users/password/new(.:format) {:controller=>"passwords", :action=>"new"}
|
37
|
+
# edit_user_password GET /users/password/edit(.:format) {:controller=>"passwords", :action=>"edit"}
|
38
|
+
# user_password PUT /users/password(.:format) {:controller=>"passwords", :action=>"update"}
|
39
|
+
# POST /users/password(.:format) {:controller=>"passwords", :action=>"create"}
|
40
|
+
#
|
41
|
+
# # Confirmation routes for Confirmable, if User model has :confirmable configured
|
42
|
+
# new_user_confirmation GET /users/confirmation/new(.:format) {:controller=>"confirmations", :action=>"new"}
|
43
|
+
# user_confirmation GET /users/confirmation(.:format) {:controller=>"confirmations", :action=>"show"}
|
44
|
+
# POST /users/confirmation(.:format) {:controller=>"confirmations", :action=>"create"}
|
45
|
+
#
|
46
|
+
# You can configure your routes with some options:
|
47
|
+
# * :class_name => setup a different class to be looked up by devise, if it cannot be correctly find by the route name.
|
48
|
+
#
|
49
|
+
# map.devise_for :users, :class_name => 'Account'
|
50
|
+
#
|
51
|
+
# * :as => allows you to setup path name that will be used, as rails routes does. The following route configuration would setup your route as /accounts instead of /users:
|
52
|
+
#
|
53
|
+
# map.devise_for :users, :as => 'accounts'
|
54
|
+
#
|
55
|
+
# * :singular => setup the name used to create named routes. By default, for a :users key, it is going to be the singularized version, :user. To configure a named route like account_session_path instead of user_session_path just do:
|
56
|
+
#
|
57
|
+
# map.devise_for :users, :singular => :account
|
58
|
+
#
|
59
|
+
# * :path_names => configure different path names to overwrite defaults :sign_in, :sign_out, :password and :confirmation.
|
60
|
+
#
|
61
|
+
# map.devise_for :users, :path_names => { :sign_in => 'login', :sign_out => 'logout', :password => 'secret', :confirmation => 'verification' }
|
62
|
+
#
|
63
|
+
# * :path_prefix => the path prefix to be used in all routes.
|
64
|
+
#
|
65
|
+
# map.devise_for :users, :path_prefix => "/:locale"
|
66
|
+
#
|
67
|
+
# If you are using a dynamic prefix, like :locale above, you need to configure default_url_options through Devise. You can do that in config/initializers/devise.rb or setting a Devise.default_url_options:
|
68
|
+
#
|
69
|
+
# Devise.default_url_options do
|
70
|
+
# { :locale => I18n.locale }
|
71
|
+
# end
|
72
|
+
#
|
73
|
+
def devise_for(*resources)
|
74
|
+
options = resources.extract_options!
|
75
|
+
|
76
|
+
resources.map!(&:to_sym)
|
77
|
+
resources.each do |resource|
|
78
|
+
mapping = Devise::Mapping.new(resource, options)
|
79
|
+
Devise.mappings[mapping.name] = mapping
|
80
|
+
|
81
|
+
with_options(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_") do |routes|
|
82
|
+
mapping.for.each do |strategy|
|
83
|
+
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
88
|
+
|
89
|
+
protected
|
90
|
+
|
91
|
+
def authenticatable(routes, mapping)
|
92
|
+
routes.with_options(:controller => 'sessions', :name_prefix => nil) do |session|
|
93
|
+
session.send(:"new_#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'new', :conditions => { :method => :get })
|
94
|
+
session.send(:"#{mapping.name}_session", mapping.path_names[:sign_in], :action => 'create', :conditions => { :method => :post })
|
95
|
+
session.send(:"destroy_#{mapping.name}_session", mapping.path_names[:sign_out], :action => 'destroy', :conditions => { :method => :get })
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
def recoverable(routes, mapping)
|
100
|
+
routes.resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password]
|
101
|
+
end
|
102
|
+
|
103
|
+
def confirmable(routes, mapping)
|
104
|
+
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# Taken from RailsWarden, thanks to Hassox. http://github.com/hassox/rails_warden
|
2
|
+
module Warden::Mixins::Common
|
3
|
+
# Gets the rails request object by default if it's available
|
4
|
+
def request
|
5
|
+
return @request if @request
|
6
|
+
if env['action_controller.rescue.request']
|
7
|
+
@request = env['action_controller.rescue.request']
|
8
|
+
else
|
9
|
+
Rack::Request.new(env)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def raw_session
|
14
|
+
request.session
|
15
|
+
end
|
16
|
+
|
17
|
+
def reset_session!
|
18
|
+
raw_session.inspect # why do I have to inspect it to get it to clear?
|
19
|
+
raw_session.clear
|
20
|
+
end
|
21
|
+
|
22
|
+
# Proxy to request cookies
|
23
|
+
def cookies
|
24
|
+
request.cookies
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
module Devise
|
2
|
+
module Strategies
|
3
|
+
# Default strategy for signing in a user, based on his email and password.
|
4
|
+
# Redirects to sign_in page if it's not authenticated
|
5
|
+
class Authenticatable < Devise::Strategies::Base
|
6
|
+
|
7
|
+
# Authenticate a user based on email and password params, returning to warden
|
8
|
+
# success and the authenticated user if everything is okay. Otherwise redirect
|
9
|
+
# to sign in page.
|
10
|
+
#
|
11
|
+
# Please notice the semantic difference between calling fail! and throw :warden.
|
12
|
+
# The first does not perform any action when calling authenticate, just
|
13
|
+
# when authenticate! is invoked. The second always perform the action.
|
14
|
+
def authenticate!
|
15
|
+
if valid_attributes? && resource = mapping.to.authenticate(attributes)
|
16
|
+
success!(resource)
|
17
|
+
else
|
18
|
+
store_location
|
19
|
+
fail!(:unauthenticated)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
private
|
24
|
+
|
25
|
+
# Find the attributes for the current mapping.
|
26
|
+
def attributes
|
27
|
+
@attributes ||= params[scope]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Check for the right keys.
|
31
|
+
def valid_attributes?
|
32
|
+
attributes && attributes[:email].present? && attributes[:password].present?
|
33
|
+
end
|
34
|
+
|
35
|
+
# Stores requested uri to redirect the user after signing in. We cannot use
|
36
|
+
# scoped session provided by warden here, since the user is not authenticated
|
37
|
+
# yet, but we still need to store the uri based on scope, so different scopes
|
38
|
+
# would never use the same uri to redirect.
|
39
|
+
def store_location
|
40
|
+
session[:"#{mapping.name}.return_to"] = request.request_uri if request.get?
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
Warden::Strategies.add(:authenticatable, Devise::Strategies::Authenticatable)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
module Devise
|
2
|
+
module Strategies
|
3
|
+
# Base strategy for Devise. Responsible for verifying correct scope and
|
4
|
+
# mapping.
|
5
|
+
class Base < Warden::Strategies::Base
|
6
|
+
|
7
|
+
# Validate strategy. By default will raise an error if no scope or an
|
8
|
+
# invalid mapping is found.
|
9
|
+
def valid?
|
10
|
+
mapping.for.include?(self.class.name.split("::").last.underscore.to_sym)
|
11
|
+
end
|
12
|
+
|
13
|
+
# Checks if a valid scope was given for devise and find mapping based on
|
14
|
+
# this scope.
|
15
|
+
def mapping
|
16
|
+
@mapping ||= begin
|
17
|
+
raise "You need to give a scope for Devise authentication" unless scope
|
18
|
+
raise "You need to give a valid Devise mapping" unless mapping = Devise.mappings[scope]
|
19
|
+
mapping
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,35 @@
|
|
1
|
+
module Devise
|
2
|
+
module Strategies
|
3
|
+
# Remember the user through the remember token. This strategy is responsible
|
4
|
+
# to verify whether there is a cookie with the remember token, and to
|
5
|
+
# recreate the user from this cookie if it exists. Must be called *before*
|
6
|
+
# authenticatable.
|
7
|
+
class Rememberable < Devise::Strategies::Base
|
8
|
+
|
9
|
+
# A valid strategy for rememberable needs a remember token in the cookies.
|
10
|
+
def valid?
|
11
|
+
super && remember_me_cookie.present?
|
12
|
+
end
|
13
|
+
|
14
|
+
# To authenticate a user we deserialize the cookie and attempt finding
|
15
|
+
# the record in the database. If the attempt fails, we pass to another
|
16
|
+
# strategy handle the authentication.
|
17
|
+
def authenticate!
|
18
|
+
if resource = mapping.to.serialize_from_cookie(remember_me_cookie)
|
19
|
+
success!(resource)
|
20
|
+
else
|
21
|
+
pass
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
# Accessor for remember cookie
|
28
|
+
def remember_me_cookie
|
29
|
+
cookies['remember_token']
|
30
|
+
end
|
31
|
+
end
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
Warden::Strategies.add(:rememberable, Devise::Strategies::Rememberable)
|
@@ -0,0 +1,24 @@
|
|
1
|
+
begin
|
2
|
+
require 'warden'
|
3
|
+
rescue
|
4
|
+
gem 'warden'
|
5
|
+
require 'warden'
|
6
|
+
end
|
7
|
+
|
8
|
+
# Session Serialization in. This block determines how the user will be stored
|
9
|
+
# in the session. If you're using a complex object like an ActiveRecord model,
|
10
|
+
# it is not a good idea to store the complete object. An ID is sufficient.
|
11
|
+
Warden::Manager.serialize_into_session{ |user| [user.class, user.id] }
|
12
|
+
|
13
|
+
# Session Serialization out. This block gets the user out of the session.
|
14
|
+
# It should be the reverse of serializing the object into the session
|
15
|
+
Warden::Manager.serialize_from_session do |klass, id|
|
16
|
+
begin
|
17
|
+
klass.find(id)
|
18
|
+
rescue ActiveRecord::RecordNotFound
|
19
|
+
throw(:warden)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Setup devise strategies for Warden
|
24
|
+
require 'devise/strategies/base'
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require 'test/test_helper'
|
2
|
+
require 'ostruct'
|
3
|
+
|
4
|
+
class MockController < ApplicationController
|
5
|
+
attr_accessor :env
|
6
|
+
|
7
|
+
def request
|
8
|
+
self
|
9
|
+
end
|
10
|
+
|
11
|
+
def path
|
12
|
+
''
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class ControllerAuthenticableTest < ActionController::TestCase
|
17
|
+
|
18
|
+
def setup
|
19
|
+
@controller = MockController.new
|
20
|
+
@mock_warden = OpenStruct.new
|
21
|
+
@controller.env = { 'warden' => @mock_warden }
|
22
|
+
end
|
23
|
+
|
24
|
+
test 'setup warden' do
|
25
|
+
assert_not_nil @controller.warden
|
26
|
+
end
|
27
|
+
|
28
|
+
test 'provide access to warden instance' do
|
29
|
+
assert_equal @controller.warden, @controller.env['warden']
|
30
|
+
end
|
31
|
+
|
32
|
+
test 'run authenticate? with scope on warden' do
|
33
|
+
@mock_warden.expects(:authenticated?).with(:my_scope)
|
34
|
+
@controller.signed_in?(:my_scope)
|
35
|
+
end
|
36
|
+
|
37
|
+
test 'proxy signed_in? to authenticated' do
|
38
|
+
@mock_warden.expects(:authenticated?).with(:my_scope)
|
39
|
+
@controller.signed_in?(:my_scope)
|
40
|
+
end
|
41
|
+
|
42
|
+
test 'run user with scope on warden' do
|
43
|
+
@mock_warden.expects(:user).with(:admin).returns(true)
|
44
|
+
@controller.current_admin
|
45
|
+
|
46
|
+
@mock_warden.expects(:user).with(:user).returns(true)
|
47
|
+
@controller.current_user
|
48
|
+
end
|
49
|
+
|
50
|
+
test 'proxy logout to warden' do
|
51
|
+
@mock_warden.expects(:user).with(:user).returns(true)
|
52
|
+
@mock_warden.expects(:logout).with(:user).returns(true)
|
53
|
+
@controller.sign_out(:user)
|
54
|
+
end
|
55
|
+
|
56
|
+
test 'proxy user_authenticate! to authenticate with user scope' do
|
57
|
+
@mock_warden.expects(:authenticate!).with(:scope => :user)
|
58
|
+
@controller.authenticate_user!
|
59
|
+
end
|
60
|
+
|
61
|
+
test 'proxy admin_authenticate! to authenticate with admin scope' do
|
62
|
+
@mock_warden.expects(:authenticate!).with(:scope => :admin)
|
63
|
+
@controller.authenticate_admin!
|
64
|
+
end
|
65
|
+
|
66
|
+
test 'proxy user_authenticated? to authenticate with user scope' do
|
67
|
+
@mock_warden.expects(:authenticated?).with(:user)
|
68
|
+
@controller.user_signed_in?
|
69
|
+
end
|
70
|
+
|
71
|
+
test 'proxy admin_authenticated? to authenticate with admin scope' do
|
72
|
+
@mock_warden.expects(:authenticated?).with(:admin)
|
73
|
+
@controller.admin_signed_in?
|
74
|
+
end
|
75
|
+
|
76
|
+
test 'proxy user_session to session scope in warden' do
|
77
|
+
@mock_warden.expects(:session).with(:user).returns({})
|
78
|
+
@controller.user_session
|
79
|
+
end
|
80
|
+
|
81
|
+
test 'proxy admin_session to session scope in warden' do
|
82
|
+
@mock_warden.expects(:session).with(:admin).returns({})
|
83
|
+
@controller.admin_session
|
84
|
+
end
|
85
|
+
|
86
|
+
test 'sign in automatically proxy to set user on warden' do
|
87
|
+
@mock_warden.expects(:set_user).with(user = mock, :scope => :user).returns(true)
|
88
|
+
@controller.sign_in(:user, user)
|
89
|
+
end
|
90
|
+
|
91
|
+
test 'is not a devise controller' do
|
92
|
+
assert_not @controller.devise_controller?
|
93
|
+
end
|
94
|
+
|
95
|
+
test 'default url options are retrieved from devise' do
|
96
|
+
begin
|
97
|
+
Devise.default_url_options {{ :locale => I18n.locale }}
|
98
|
+
assert_equal({ :locale => :en }, @controller.send(:default_url_options))
|
99
|
+
ensure
|
100
|
+
Devise.default_url_options {{ }}
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|