mirror of
https://github.com/heartcombo/devise.git
synced 2026-01-09 23:58:06 -05:00
Compare commits
33 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
23568bda82 | ||
|
|
ee7f5270fc | ||
|
|
f294700723 | ||
|
|
c86ce298dc | ||
|
|
b0ff0d46dd | ||
|
|
187ef5c452 | ||
|
|
6d29bcc467 | ||
|
|
ee87ec398a | ||
|
|
3e37fe8d4d | ||
|
|
48a94cdece | ||
|
|
bdacffab58 | ||
|
|
085b12a710 | ||
|
|
3435c53725 | ||
|
|
01dec7fc78 | ||
|
|
4bfbeea7e6 | ||
|
|
2a9e8dca73 | ||
|
|
1b6f1b9752 | ||
|
|
732e31528e | ||
|
|
d7db5b1eea | ||
|
|
2761a75437 | ||
|
|
8a15ac6e4a | ||
|
|
9798ad7455 | ||
|
|
54cd2cc0e8 | ||
|
|
445070f6ec | ||
|
|
9856646fac | ||
|
|
60fd9d26ea | ||
|
|
1cf4dc798d | ||
|
|
2f441fb60b | ||
|
|
49d1978863 | ||
|
|
658059f31a | ||
|
|
21359fb433 | ||
|
|
60714cd449 | ||
|
|
6b837cb285 |
@@ -1,3 +1,32 @@
|
||||
== 1.0.2
|
||||
|
||||
* enhancements
|
||||
* Allows you set mailer content type (by github.com/glennr)
|
||||
|
||||
* bug fix
|
||||
* Uses the same content type as request on http authenticatable 401 responses
|
||||
|
||||
== 1.0.1
|
||||
|
||||
* enhancements
|
||||
* HttpAuthenticatable is not added by default automatically.
|
||||
* Avoid mass assignment error messages with current password.
|
||||
|
||||
* bug fix
|
||||
* Fixed encryptors autoload
|
||||
|
||||
== 1.0.0
|
||||
|
||||
* deprecation
|
||||
* :old_password in update_with_password is deprecated, use :current_password instead
|
||||
|
||||
* enhancements
|
||||
* Added Registerable
|
||||
* Added Http Basic Authentication support
|
||||
* Allow scoped_views to be customized per controller/mailer class
|
||||
* [#99] Allow authenticatable to used in change_table statements
|
||||
* Add mailer_content_type configuration parameter (by github.com/glennr)
|
||||
|
||||
== 0.9.2
|
||||
|
||||
* bug fix
|
||||
|
||||
23
README.rdoc
23
README.rdoc
@@ -7,15 +7,18 @@ Devise is a flexible authentication solution for Rails based on Warden. It:
|
||||
* Allows you to have multiple roles (or models/scopes) signed in at the same time;
|
||||
* Is based on a modularity concept: use just what you really need.
|
||||
|
||||
Right now it's composed of nine modules:
|
||||
Right now it's composed of 12 modules:
|
||||
|
||||
* Authenticatable: responsible for encrypting password and validating authenticity of a user while signing in.
|
||||
* Token Authenticatable: validates authenticity of a user while signing in using an authentication token (also known as "single access token").
|
||||
* HttpAuthenticatable: sign in users using basic HTTP authentication.
|
||||
* Confirmable: responsible for verifying whether an account is already confirmed to sign in, and to send emails with confirmation instructions.
|
||||
* Recoverable: takes care of reseting the user password and send reset instructions.
|
||||
* Registerable: handles signing up users through a registration process.
|
||||
* Rememberable: manages generating and clearing token for remember the user from a saved cookie.
|
||||
* Trackable: tracks sign in count, timestamps and ip.
|
||||
* Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.
|
||||
* Timeoutable: expires sessions without activity in a certain period of time.
|
||||
* Validatable: creates all needed validations for email and password. It's totally optional, so you're able to to customize validations by yourself.
|
||||
* Lockable: takes care of locking an account based on the number of failed sign in attempts. Handles unlock via expire and email.
|
||||
* Activatable: if you need to activate accounts by other means, which are not through confirmation, use this module.
|
||||
|
||||
@@ -27,17 +30,13 @@ Devise is based on Warden (http://github.com/hassox/warden), a Rack Authenticati
|
||||
|
||||
== Installation
|
||||
|
||||
All gems are on gemcutter, so you need to add gemcutter to your sources if you haven't yet:
|
||||
|
||||
sudo gem sources -a http://gemcutter.org/
|
||||
|
||||
Install warden gem if you don't have it installed (requires 0.6.4 or higher):
|
||||
Install warden gem if you don't have it installed:
|
||||
|
||||
sudo gem install warden
|
||||
|
||||
Install devise gem:
|
||||
|
||||
sudo gem install devise
|
||||
sudo gem install devise --version=1.0.1
|
||||
|
||||
Configure warden and devise gems inside your app:
|
||||
|
||||
@@ -52,6 +51,10 @@ And you're ready to go. The generator will install an initializer which describe
|
||||
|
||||
http://rdoc.info/projects/plataformatec/devise
|
||||
|
||||
If you want to use Devise with bundler on Rails 2.3, you need to follow the instructions here:
|
||||
|
||||
http://github.com/carlhuda/bundler/issues/issue/83
|
||||
|
||||
== Basic Usage
|
||||
|
||||
This is a walkthrough with all steps you need to setup a devise resource, including model, migration, route files, and optional configuration. You MUST also check out the *Generators* section below to help you start.
|
||||
@@ -170,10 +173,12 @@ Since devise is an engine, it has all default views inside the gem. They are goo
|
||||
|
||||
ruby script/generate devise_views
|
||||
|
||||
By default Devise will use the same views for all roles you have. But what if you need so different views to each of them? Devise also has an easy way to accomplish it: just setup config.scoped_views to true inside "config/initializers/devise.rb".
|
||||
By default Devise will use the same views for all roles you have. But what if you need so different views to each of them? Devise also has an easy way to accomplish it: just setup config.scoped_views to true inside "config/initializers/devise.rb".
|
||||
|
||||
After doing so you will be able to have views based on the scope like 'sessions/users/new' and 'sessions/admin/new'. If no view is found within the scope, Devise will fallback to the default view.
|
||||
|
||||
Devise uses flash messages to let users know if their login is successful or not. Devise expects your application to call 'flash[:notice]' and 'flash[:alert]' as appropriate.
|
||||
|
||||
== I18n
|
||||
|
||||
Devise uses flash messages with I18n with the flash keys :success and :failure. To customize your app, you can setup your locale file this way:
|
||||
|
||||
2
Rakefile
2
Rakefile
@@ -44,7 +44,7 @@ begin
|
||||
s.description = "Flexible authentication solution for Rails with Warden"
|
||||
s.authors = ['José Valim', 'Carlos Antônio']
|
||||
s.files = FileList["[A-Z]*", "{app,config,generators,lib}/**/*", "init.rb"]
|
||||
s.add_dependency("warden", "~> 0.9.0")
|
||||
s.add_dependency("warden", "~> 0.9.3")
|
||||
end
|
||||
|
||||
Jeweler::GemcutterTasks.new
|
||||
|
||||
4
TODO
4
TODO
@@ -1,4 +1,2 @@
|
||||
* Make test run with DataMapper
|
||||
* Add Registerable support
|
||||
* Add http authentication support
|
||||
* Extract Activatable tests from Confirmable
|
||||
* Extract Activatable tests from Confirmable
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
class ConfirmationsController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
include Devise::Controllers::Common
|
||||
|
||||
# GET /resource/confirmation/new
|
||||
def new
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/confirmation
|
||||
def create
|
||||
self.resource = resource_class.send_confirmation_instructions(params[resource_name])
|
||||
|
||||
if resource.errors.empty?
|
||||
set_flash_message :notice, :send_instructions
|
||||
redirect_to new_session_path(resource_name)
|
||||
else
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
# GET /resource/confirmation?confirmation_token=abcdef
|
||||
def show
|
||||
@@ -13,10 +30,4 @@ class ConfirmationsController < ApplicationController
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def send_instructions_with
|
||||
:send_confirmation_instructions
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,9 +1,26 @@
|
||||
class PasswordsController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
include Devise::Controllers::Common
|
||||
|
||||
before_filter :require_no_authentication
|
||||
|
||||
# GET /resource/password/new
|
||||
def new
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/password
|
||||
def create
|
||||
self.resource = resource_class.send_reset_password_instructions(params[resource_name])
|
||||
|
||||
if resource.errors.empty?
|
||||
set_flash_message :notice, :send_instructions
|
||||
redirect_to new_session_path(resource_name)
|
||||
else
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
# GET /resource/password/edit?reset_password_token=abcdef
|
||||
def edit
|
||||
self.resource = resource_class.new
|
||||
@@ -22,10 +39,4 @@ class PasswordsController < ApplicationController
|
||||
render_with_scope :edit
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def send_instructions_with
|
||||
:send_reset_password_instructions
|
||||
end
|
||||
end
|
||||
|
||||
55
app/controllers/registrations_controller.rb
Normal file
55
app/controllers/registrations_controller.rb
Normal file
@@ -0,0 +1,55 @@
|
||||
class RegistrationsController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
|
||||
before_filter :require_no_authentication, :only => [ :new, :create ]
|
||||
before_filter :authenticate_scope!, :only => [:edit, :update, :destroy]
|
||||
|
||||
# GET /resource/sign_in
|
||||
def new
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/sign_up
|
||||
def create
|
||||
build_resource
|
||||
|
||||
if resource.save
|
||||
flash[:"#{resource_name}_signed_up"] = true
|
||||
set_flash_message :notice, :signed_up
|
||||
sign_in_and_redirect(resource_name, resource)
|
||||
else
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
# GET /resource/edit
|
||||
def edit
|
||||
render_with_scope :edit
|
||||
end
|
||||
|
||||
# PUT /resource
|
||||
def update
|
||||
if self.resource.update_with_password(params[resource_name])
|
||||
set_flash_message :notice, :updated
|
||||
redirect_to after_sign_in_path_for(self.resource)
|
||||
else
|
||||
render_with_scope :edit
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /resource
|
||||
def destroy
|
||||
self.resource.destroy
|
||||
set_flash_message :notice, :destroyed
|
||||
sign_out_and_redirect(self.resource)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Authenticates the current scope and dup the resource
|
||||
def authenticate_scope!
|
||||
send(:"authenticate_#{resource_name}!")
|
||||
self.resource = send(:"current_#{resource_name}").dup
|
||||
end
|
||||
end
|
||||
@@ -1,15 +1,18 @@
|
||||
class SessionsController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
include Devise::Controllers::Common
|
||||
|
||||
before_filter :require_no_authentication, :only => [ :new, :create ]
|
||||
|
||||
# GET /resource/sign_in
|
||||
def new
|
||||
Devise::FLASH_MESSAGES.each do |message|
|
||||
set_now_flash_message :alert, message if params.try(:[], message) == "true"
|
||||
unless resource_just_signed_up?
|
||||
Devise::FLASH_MESSAGES.each do |message|
|
||||
set_now_flash_message :alert, message if params.try(:[], message) == "true"
|
||||
end
|
||||
end
|
||||
super
|
||||
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/sign_in
|
||||
@@ -19,7 +22,7 @@ class SessionsController < ApplicationController
|
||||
sign_in_and_redirect(resource_name, resource, true)
|
||||
else
|
||||
set_now_flash_message :alert, (warden.message || :invalid)
|
||||
build_resource
|
||||
clean_up_passwords(build_resource)
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
@@ -30,4 +33,13 @@ class SessionsController < ApplicationController
|
||||
sign_out_and_redirect(resource_name)
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def resource_just_signed_up?
|
||||
flash[:"#{resource_name}_signed_up"]
|
||||
end
|
||||
|
||||
def clean_up_passwords(object)
|
||||
object.clean_up_passwords if object.respond_to?(:clean_up_passwords)
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,6 +1,23 @@
|
||||
class UnlocksController < ApplicationController
|
||||
include Devise::Controllers::InternalHelpers
|
||||
include Devise::Controllers::Common
|
||||
|
||||
# GET /resource/unlock/new
|
||||
def new
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/unlock
|
||||
def create
|
||||
self.resource = resource_class.send_unlock_instructions(params[resource_name])
|
||||
|
||||
if resource.errors.empty?
|
||||
set_flash_message :notice, :send_instructions
|
||||
redirect_to new_session_path(resource_name)
|
||||
else
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
# GET /resource/unlock?unlock_token=abcdef
|
||||
def show
|
||||
@@ -13,10 +30,4 @@ class UnlocksController < ApplicationController
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def send_instructions_with
|
||||
:send_unlock_instructions
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
class DeviseMailer < ::ActionMailer::Base
|
||||
extend Devise::Controllers::InternalHelpers::ScopedViews
|
||||
|
||||
# Deliver confirmation instructions when the user is created or its email is
|
||||
# updated, and also when confirmation is manually requested
|
||||
@@ -26,12 +27,12 @@ class DeviseMailer < ::ActionMailer::Base
|
||||
from mailer_sender(mapping)
|
||||
recipients record.email
|
||||
sent_on Time.now
|
||||
content_type 'text/html'
|
||||
content_type Devise.mailer_content_type
|
||||
body render_with_scope(key, mapping, mapping.name => record, :resource => record)
|
||||
end
|
||||
|
||||
def render_with_scope(key, mapping, assigns)
|
||||
if Devise.scoped_views
|
||||
if self.class.scoped_views
|
||||
begin
|
||||
render :file => "devise_mailer/#{mapping.as}/#{key}", :body => assigns
|
||||
rescue ActionView::MissingTemplate
|
||||
@@ -45,7 +46,7 @@ class DeviseMailer < ::ActionMailer::Base
|
||||
def mailer_sender(mapping)
|
||||
if Devise.mailer_sender.is_a?(Proc)
|
||||
block_args = mapping.name if Devise.mailer_sender.arity > 0
|
||||
Devise.mailer_sender.call(*block_args)
|
||||
Devise.mailer_sender.call(block_args)
|
||||
else
|
||||
Devise.mailer_sender
|
||||
end
|
||||
|
||||
25
app/views/registrations/edit.html.erb
Normal file
25
app/views/registrations/edit.html.erb
Normal file
@@ -0,0 +1,25 @@
|
||||
<h2>Edit <%= resource_name.to_s.humanize %></h2>
|
||||
|
||||
<% form_for resource_name, resource, :url => registration_path(resource_name), :html => { :method => :put } do |f| -%>
|
||||
<%= f.error_messages %>
|
||||
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %> <i>(leave blank if you don't want to change it)</i></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.label :current_password %> <i>(we need your current password to confirm your changes)</i></p>
|
||||
<p><%= f.password_field :current_password %></p>
|
||||
|
||||
<p><%= f.submit "Update" %></p>
|
||||
<% end -%>
|
||||
|
||||
<h3>Cancel my account</h3>
|
||||
|
||||
<p>Unhappy? <%= link_to "Cancel my account", registration_path(resource_name), :confirm => "Are you sure?", :method => :delete %>.</p>
|
||||
|
||||
<%= render :partial => "shared/devise_links" %>
|
||||
17
app/views/registrations/new.html.erb
Normal file
17
app/views/registrations/new.html.erb
Normal file
@@ -0,0 +1,17 @@
|
||||
<h2>Sign up</h2>
|
||||
|
||||
<% form_for resource_name, resource, :url => registration_path(resource_name) do |f| -%>
|
||||
<%= f.error_messages %>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<p><%= f.label :password_confirmation %></p>
|
||||
<p><%= f.password_field :password_confirmation %></p>
|
||||
|
||||
<p><%= f.submit "Sign up" %></p>
|
||||
<% end -%>
|
||||
|
||||
<%= render :partial => "shared/devise_links" %>
|
||||
@@ -1,19 +1,17 @@
|
||||
<h2>Sign in</h2>
|
||||
|
||||
<%- if devise_mapping.authenticatable? %>
|
||||
<% form_for resource_name, resource, :url => session_path(resource_name) do |f| -%>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
<% form_for resource_name, resource, :url => session_path(resource_name) do |f| -%>
|
||||
<p><%= f.label :email %></p>
|
||||
<p><%= f.text_field :email %></p>
|
||||
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
<p><%= f.label :password %></p>
|
||||
<p><%= f.password_field :password %></p>
|
||||
|
||||
<% if devise_mapping.rememberable? -%>
|
||||
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
|
||||
<% end -%>
|
||||
|
||||
<p><%= f.submit "Sign in" %></p>
|
||||
<% if devise_mapping.rememberable? -%>
|
||||
<p><%= f.check_box :remember_me %> <%= f.label :remember_me %></p>
|
||||
<% end -%>
|
||||
<% end%>
|
||||
|
||||
<p><%= f.submit "Sign in" %></p>
|
||||
<% end -%>
|
||||
|
||||
<%= render :partial => "shared/devise_links" %>
|
||||
@@ -2,6 +2,10 @@
|
||||
<%= link_to t('devise.sessions.link'), new_session_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.registerable? && controller_name != 'registrations' %>
|
||||
<%= link_to t('devise.registrations.link'), new_registration_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
|
||||
<%- if devise_mapping.recoverable? && controller_name != 'passwords' %>
|
||||
<%= link_to t('devise.passwords.link'), new_password_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
@@ -12,4 +16,4 @@
|
||||
|
||||
<%- if devise_mapping.lockable? && controller_name != 'unlocks' %>
|
||||
<%= link_to t('devise.unlocks.link'), new_unlock_path(resource_name) %><br />
|
||||
<% end -%>
|
||||
<% end -%>
|
||||
|
||||
@@ -5,11 +5,11 @@
|
||||
|
||||
Gem::Specification.new do |s|
|
||||
s.name = %q{devise}
|
||||
s.version = "0.9.1"
|
||||
s.version = "1.0.2"
|
||||
|
||||
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
||||
s.authors = ["Jos\303\251 Valim", "Carlos Ant\303\264nio"]
|
||||
s.date = %q{2010-02-01}
|
||||
s.date = %q{2010-02-17}
|
||||
s.description = %q{Flexible authentication solution for Rails with Warden}
|
||||
s.email = %q{contact@plataformatec.com.br}
|
||||
s.extra_rdoc_files = [
|
||||
@@ -24,6 +24,7 @@ Gem::Specification.new do |s|
|
||||
"TODO",
|
||||
"app/controllers/confirmations_controller.rb",
|
||||
"app/controllers/passwords_controller.rb",
|
||||
"app/controllers/registrations_controller.rb",
|
||||
"app/controllers/sessions_controller.rb",
|
||||
"app/controllers/unlocks_controller.rb",
|
||||
"app/models/devise_mailer.rb",
|
||||
@@ -33,6 +34,8 @@ Gem::Specification.new do |s|
|
||||
"app/views/devise_mailer/unlock_instructions.html.erb",
|
||||
"app/views/passwords/edit.html.erb",
|
||||
"app/views/passwords/new.html.erb",
|
||||
"app/views/registrations/edit.html.erb",
|
||||
"app/views/registrations/new.html.erb",
|
||||
"app/views/sessions/new.html.erb",
|
||||
"app/views/shared/_devise_links.erb",
|
||||
"app/views/unlocks/new.html.erb",
|
||||
@@ -49,7 +52,6 @@ Gem::Specification.new do |s|
|
||||
"generators/devise_views/devise_views_generator.rb",
|
||||
"init.rb",
|
||||
"lib/devise.rb",
|
||||
"lib/devise/controllers/common.rb",
|
||||
"lib/devise/controllers/helpers.rb",
|
||||
"lib/devise/controllers/internal_helpers.rb",
|
||||
"lib/devise/controllers/url_helpers.rb",
|
||||
@@ -71,10 +73,13 @@ Gem::Specification.new do |s|
|
||||
"lib/devise/models/activatable.rb",
|
||||
"lib/devise/models/authenticatable.rb",
|
||||
"lib/devise/models/confirmable.rb",
|
||||
"lib/devise/models/http_authenticatable.rb",
|
||||
"lib/devise/models/lockable.rb",
|
||||
"lib/devise/models/recoverable.rb",
|
||||
"lib/devise/models/registerable.rb",
|
||||
"lib/devise/models/rememberable.rb",
|
||||
"lib/devise/models/timeoutable.rb",
|
||||
"lib/devise/models/token_authenticatable.rb",
|
||||
"lib/devise/models/trackable.rb",
|
||||
"lib/devise/models/validatable.rb",
|
||||
"lib/devise/orm/active_record.rb",
|
||||
@@ -86,7 +91,9 @@ Gem::Specification.new do |s|
|
||||
"lib/devise/schema.rb",
|
||||
"lib/devise/strategies/authenticatable.rb",
|
||||
"lib/devise/strategies/base.rb",
|
||||
"lib/devise/strategies/http_authenticatable.rb",
|
||||
"lib/devise/strategies/rememberable.rb",
|
||||
"lib/devise/strategies/token_authenticatable.rb",
|
||||
"lib/devise/test_helpers.rb",
|
||||
"lib/devise/version.rb"
|
||||
]
|
||||
@@ -104,10 +111,13 @@ Gem::Specification.new do |s|
|
||||
"test/failure_app_test.rb",
|
||||
"test/integration/authenticatable_test.rb",
|
||||
"test/integration/confirmable_test.rb",
|
||||
"test/integration/http_authenticatable_test.rb",
|
||||
"test/integration/lockable_test.rb",
|
||||
"test/integration/recoverable_test.rb",
|
||||
"test/integration/registerable_test.rb",
|
||||
"test/integration/rememberable_test.rb",
|
||||
"test/integration/timeoutable_test.rb",
|
||||
"test/integration/token_authenticatable_test.rb",
|
||||
"test/integration/trackable_test.rb",
|
||||
"test/mailers/confirmation_instructions_test.rb",
|
||||
"test/mailers/reset_password_instructions_test.rb",
|
||||
@@ -119,6 +129,7 @@ Gem::Specification.new do |s|
|
||||
"test/models/recoverable_test.rb",
|
||||
"test/models/rememberable_test.rb",
|
||||
"test/models/timeoutable_test.rb",
|
||||
"test/models/token_authenticatable_test.rb",
|
||||
"test/models/trackable_test.rb",
|
||||
"test/models/validatable_test.rb",
|
||||
"test/models_test.rb",
|
||||
@@ -146,8 +157,8 @@ Gem::Specification.new do |s|
|
||||
"test/routes_test.rb",
|
||||
"test/support/assertions_helper.rb",
|
||||
"test/support/integration_tests_helper.rb",
|
||||
"test/support/model_tests_helper.rb",
|
||||
"test/support/test_silencer.rb",
|
||||
"test/support/tests_helper.rb",
|
||||
"test/test_helper.rb",
|
||||
"test/test_helpers_test.rb"
|
||||
]
|
||||
@@ -157,12 +168,12 @@ Gem::Specification.new do |s|
|
||||
s.specification_version = 3
|
||||
|
||||
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
||||
s.add_runtime_dependency(%q<warden>, ["~> 0.9.0"])
|
||||
s.add_runtime_dependency(%q<warden>, ["~> 0.9.3"])
|
||||
else
|
||||
s.add_dependency(%q<warden>, ["~> 0.9.0"])
|
||||
s.add_dependency(%q<warden>, ["~> 0.9.3"])
|
||||
end
|
||||
else
|
||||
s.add_dependency(%q<warden>, ["~> 0.9.0"])
|
||||
s.add_dependency(%q<warden>, ["~> 0.9.3"])
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class <%= class_name %> < ActiveRecord::Base
|
||||
# Include default devise modules.
|
||||
# Others available are :lockable, :timeoutable and :activatable.
|
||||
devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable, :validatable
|
||||
# Include default devise modules. Others available are:
|
||||
# :http_authenticatable, :token_authenticatable, :lockable, :timeoutable and :activatable
|
||||
devise :registerable, :authenticatable, :confirmable, :recoverable,
|
||||
:rememberable, :trackable, :validatable
|
||||
|
||||
# Setup accessible (or protected) attributes for your model
|
||||
attr_accessible :email, :password, :password_confirmation
|
||||
|
||||
@@ -3,6 +3,9 @@
|
||||
Devise.setup do |config|
|
||||
# Configure the e-mail address which will be shown in DeviseMailer.
|
||||
config.mailer_sender = "please-change-me@config-initializers-devise.com"
|
||||
|
||||
# Configure the content type of DeviseMailer mails (defaults to text/html")
|
||||
# config.mailer_content_type = "text/plain"
|
||||
|
||||
# ==> Configuration for :authenticatable
|
||||
# Invoke `rake secret` and use the printed value to setup a pepper to generate
|
||||
@@ -26,6 +29,9 @@ Devise.setup do |config|
|
||||
# session. If you need permissions, you should implement that in a before filter.
|
||||
# config.authentication_keys = [ :email ]
|
||||
|
||||
# The realm used in Http Basic Authentication
|
||||
# config.http_authentication_realm = "Application"
|
||||
|
||||
# ==> Configuration for :confirmable
|
||||
# The time you want give to your user to confirm his account. During this time
|
||||
# he will be able to access your application without confirming. Default is nil.
|
||||
@@ -93,7 +99,6 @@ Devise.setup do |config|
|
||||
|
||||
# Configure default_url_options if you are using dynamic segments in :path_prefix
|
||||
# for devise_for.
|
||||
#
|
||||
# config.default_url_options do
|
||||
# { :locale => I18n.locale }
|
||||
# end
|
||||
|
||||
@@ -4,7 +4,6 @@ module Devise
|
||||
autoload :TestHelpers, 'devise/test_helpers'
|
||||
|
||||
module Controllers
|
||||
autoload :Common, 'devise/controllers/common'
|
||||
autoload :Helpers, 'devise/controllers/helpers'
|
||||
autoload :InternalHelpers, 'devise/controllers/internal_helpers'
|
||||
autoload :UrlHelpers, 'devise/controllers/url_helpers'
|
||||
@@ -14,7 +13,7 @@ module Devise
|
||||
autoload :Base, 'devise/encryptors/base'
|
||||
autoload :Bcrypt, 'devise/encryptors/bcrypt'
|
||||
autoload :AuthlogicSha512, 'devise/encryptors/authlogic_sha512'
|
||||
autoload :AuthlogicSha1, 'devise/encryptors/authlogic_sha1'
|
||||
autoload :ClearanceSha1, 'devise/encryptors/clearance_sha1'
|
||||
autoload :RestfulAuthenticationSha1, 'devise/encryptors/restful_authentication_sha1'
|
||||
autoload :Sha512, 'devise/encryptors/sha512'
|
||||
autoload :Sha1, 'devise/encryptors/sha1'
|
||||
@@ -29,10 +28,10 @@ module Devise
|
||||
ALL = []
|
||||
|
||||
# Authentication ones first
|
||||
ALL.push :authenticatable, :token_authenticatable, :rememberable
|
||||
ALL.push :authenticatable, :http_authenticatable, :token_authenticatable, :rememberable
|
||||
|
||||
# Misc after
|
||||
ALL.push :recoverable, :validatable
|
||||
ALL.push :recoverable, :registerable, :validatable
|
||||
|
||||
# The ones which can sign out after
|
||||
ALL.push :activatable, :confirmable, :lockable, :timeoutable
|
||||
@@ -40,20 +39,24 @@ module Devise
|
||||
# Stats for last, so we make sure the user is really signed in
|
||||
ALL.push :trackable
|
||||
|
||||
# Maps controller names to devise modules
|
||||
# Maps controller names to devise modules.
|
||||
CONTROLLERS = {
|
||||
:sessions => [:authenticatable, :token_authenticatable],
|
||||
:passwords => [:recoverable],
|
||||
:confirmations => [:confirmable],
|
||||
:registrations => [:registerable],
|
||||
:unlocks => [:lockable]
|
||||
}
|
||||
|
||||
STRATEGIES = [:rememberable, :token_authenticatable, :authenticatable]
|
||||
# Routes for generating url helpers.
|
||||
ROUTES = [:session, :password, :confirmation, :registration, :unlock]
|
||||
|
||||
STRATEGIES = [:rememberable, :http_authenticatable, :token_authenticatable, :authenticatable]
|
||||
|
||||
TRUE_VALUES = [true, 1, '1', 't', 'T', 'true', 'TRUE']
|
||||
|
||||
# Maps the messages types that are used in flash message.
|
||||
FLASH_MESSAGES = [ :unauthenticated, :unconfirmed, :invalid, :invalid_token, :timeout, :inactive, :locked ]
|
||||
FLASH_MESSAGES = [:unauthenticated, :unconfirmed, :invalid, :invalid_token, :timeout, :inactive, :locked]
|
||||
|
||||
# Declare encryptors length which are used in migrations.
|
||||
ENCRYPTORS_LENGTH = {
|
||||
@@ -133,7 +136,7 @@ module Devise
|
||||
|
||||
# Tell when to use the default scope, if one cannot be found from routes.
|
||||
mattr_accessor :use_default_scope
|
||||
@@use_default_scope
|
||||
@@use_default_scope = false
|
||||
|
||||
# The default scope which is used by warden.
|
||||
mattr_accessor :default_scope
|
||||
@@ -141,12 +144,20 @@ module Devise
|
||||
|
||||
# Address which sends Devise e-mails.
|
||||
mattr_accessor :mailer_sender
|
||||
@@mailer_sender
|
||||
@@mailer_sender = nil
|
||||
|
||||
# Content Type of Devise e-mails.
|
||||
mattr_accessor :mailer_content_type
|
||||
@@mailer_content_type = 'text/html'
|
||||
|
||||
# Authentication token params key name of choice. E.g. /users/sign_in?some_key=...
|
||||
mattr_accessor :token_authentication_key
|
||||
@@token_authentication_key = :auth_token
|
||||
|
||||
# The realm used in Http Basic Authentication
|
||||
mattr_accessor :http_authentication_realm
|
||||
@@http_authentication_realm = "Application"
|
||||
|
||||
class << self
|
||||
# Default way to setup Devise. Run script/generate devise_install to create
|
||||
# a fresh initializer with all configuration values.
|
||||
@@ -242,4 +253,4 @@ rescue
|
||||
end
|
||||
|
||||
require 'devise/mapping'
|
||||
require 'devise/rails'
|
||||
require 'devise/rails'
|
||||
|
||||
@@ -1,24 +0,0 @@
|
||||
module Devise
|
||||
module Controllers
|
||||
# Common actions shared between Devise controllers
|
||||
module Common #:nodoc:
|
||||
# GET /resource/controller/new
|
||||
def new
|
||||
build_resource
|
||||
render_with_scope :new
|
||||
end
|
||||
|
||||
# POST /resource/controller
|
||||
def create
|
||||
self.resource = resource_class.send(send_instructions_with, params[resource_name])
|
||||
|
||||
if resource.errors.empty?
|
||||
set_flash_message :notice, :send_instructions
|
||||
redirect_to new_session_path(resource_name)
|
||||
else
|
||||
render_with_scope :new
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -176,7 +176,7 @@ module Devise
|
||||
# before_filter :authenticate_admin! # Tell devise to use :admin map
|
||||
#
|
||||
Devise.mappings.each_key do |mapping|
|
||||
class_eval <<-METHODS, __FILE__, __LINE__
|
||||
class_eval <<-METHODS, __FILE__, __LINE__ + 1
|
||||
def authenticate_#{mapping}!
|
||||
warden.authenticate!(:scope => :#{mapping})
|
||||
end
|
||||
|
||||
@@ -7,6 +7,7 @@ module Devise
|
||||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
extend ScopedViews
|
||||
unloadable
|
||||
|
||||
helper_method :resource, :scope_name, :resource_name, :resource_class, :devise_mapping, :devise_controller?
|
||||
@@ -17,6 +18,16 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
module ScopedViews
|
||||
def scoped_views
|
||||
defined?(@scoped_views) ? @scoped_views : Devise.scoped_views
|
||||
end
|
||||
|
||||
def scoped_views=(value)
|
||||
@scoped_views = value
|
||||
end
|
||||
end
|
||||
|
||||
# Gets the actual resource stored in the instance variable
|
||||
def resource
|
||||
instance_variable_get(:"@#{resource_name}")
|
||||
@@ -59,12 +70,9 @@ module Devise
|
||||
instance_variable_set(:"@#{resource_name}", new_resource)
|
||||
end
|
||||
|
||||
# Build a devise resource without setting password and password confirmation fields.
|
||||
# Build a devise resource.
|
||||
def build_resource
|
||||
self.resource ||= begin
|
||||
attributes = params[resource_name].try(:except, :password, :password_confirmation)
|
||||
resource_class.new(attributes || {})
|
||||
end
|
||||
self.resource ||= resource_class.new(params[resource_name] || {})
|
||||
end
|
||||
|
||||
# Helper for use in before_filters where no authentication is required.
|
||||
@@ -104,7 +112,8 @@ module Devise
|
||||
# Accepts just :controller as option.
|
||||
def render_with_scope(action, options={})
|
||||
controller_name = options.delete(:controller) || self.controller_name
|
||||
if Devise.scoped_views
|
||||
|
||||
if self.class.scoped_views
|
||||
begin
|
||||
render :template => "#{controller_name}/#{devise_mapping.as}/#{action}"
|
||||
rescue ActionView::MissingTemplate
|
||||
|
||||
@@ -19,17 +19,17 @@ module Devise
|
||||
# Those helpers are added to your ApplicationController.
|
||||
module UrlHelpers
|
||||
|
||||
[:session, :password, :confirmation, :unlock].each do |module_name|
|
||||
Devise::ROUTES.each do |module_name|
|
||||
[:path, :url].each do |path_or_url|
|
||||
actions = [ nil, :new_ ]
|
||||
actions << :edit_ if module_name == :password
|
||||
actions << :destroy_ if module_name == :session
|
||||
actions << :edit_ if [:password, :registration].include?(module_name)
|
||||
actions << :destroy_ if [:session].include?(module_name)
|
||||
|
||||
actions.each do |action|
|
||||
class_eval <<-URL_HELPERS
|
||||
def #{action}#{module_name}_#{path_or_url}(resource, *args)
|
||||
resource = Devise::Mapping.find_scope!(resource)
|
||||
send("#{action}\#{resource}_#{module_name}_#{path_or_url}", *args)
|
||||
class_eval <<-URL_HELPERS, __FILE__, __LINE__ + 1
|
||||
def #{action}#{module_name}_#{path_or_url}(resource_or_scope, *args)
|
||||
scope = Devise::Mapping.find_scope!(resource_or_scope)
|
||||
send("#{action}\#{scope}_#{module_name}_#{path_or_url}", *args)
|
||||
end
|
||||
URL_HELPERS
|
||||
end
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
# that specific user and adds a cookie with this user info to sign in this user
|
||||
# automatically without asking for credentials. Refer to rememberable strategy
|
||||
# for more info.
|
||||
Warden::Manager.after_authentication do |record, warden, options|
|
||||
Warden::Manager.prepend_after_authentication do |record, warden, options|
|
||||
scope = options[:scope]
|
||||
remember_me = warden.params[scope].try(:fetch, :remember_me, nil)
|
||||
|
||||
|
||||
@@ -19,6 +19,11 @@ en:
|
||||
link: "Didn't receive confirmation instructions?"
|
||||
send_instructions: 'You will receive an email with instructions about how to confirm your account in a few minutes.'
|
||||
confirmed: 'Your account was successfully confirmed. You are now signed in.'
|
||||
registrations:
|
||||
link: 'Sign up'
|
||||
signed_up: 'You have signed up successfully.'
|
||||
updated: 'You updated your account successfully.'
|
||||
destroyed: 'Bye! Your account was successfully cancelled. We hope to see you again soon.'
|
||||
unlocks:
|
||||
link: "Didn't receive unlock instructions?"
|
||||
send_instructions: 'You will receive an email with instructions about how to unlock your account in a few minutes.'
|
||||
@@ -27,3 +32,4 @@ en:
|
||||
confirmation_instructions: 'Confirmation instructions'
|
||||
reset_password_instructions: 'Reset password instructions'
|
||||
unlock_instructions: 'Unlock Instructions'
|
||||
|
||||
|
||||
@@ -36,7 +36,10 @@ module Devise
|
||||
|
||||
# Find a mapping by a given class. It takes into account single table inheritance as well.
|
||||
def self.find_by_class(klass)
|
||||
Devise.mappings.values.find { |m| return m if klass <= m.to }
|
||||
Devise.mappings.each_value do |mapping|
|
||||
return mapping if klass <= mapping.to
|
||||
end
|
||||
nil
|
||||
end
|
||||
|
||||
# Receives an object and find a scope for it. If a scope cannot be found,
|
||||
@@ -62,11 +65,12 @@ module Devise
|
||||
@as = (options.delete(:as) || name).to_sym
|
||||
@klass = (options.delete(:class_name) || name.to_s.classify).to_s
|
||||
@name = (options.delete(:scope) || name.to_s.singularize).to_sym
|
||||
@path_names = options.delete(:path_names) || {}
|
||||
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
|
||||
|
||||
@path_prefix = "/#{options.delete(:path_prefix)}/".squeeze("/")
|
||||
@route_options = options || {}
|
||||
|
||||
setup_path_names
|
||||
@path_names = Hash.new { |h,k| h[k] = k.to_s }
|
||||
@path_names.merge!(options.delete(:path_names) || {})
|
||||
end
|
||||
|
||||
# Return modules for the mapping.
|
||||
@@ -115,7 +119,7 @@ module Devise
|
||||
#
|
||||
def self.register(*modules)
|
||||
modules.each do |m|
|
||||
class_eval <<-METHOD, __FILE__, __LINE__
|
||||
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
def #{m}?
|
||||
self.for.include?(:#{m})
|
||||
end
|
||||
@@ -123,15 +127,5 @@ module Devise
|
||||
end
|
||||
end
|
||||
Devise::Mapping.register *ALL
|
||||
|
||||
private
|
||||
|
||||
# Configure default path names, allowing the user overwrite defaults by
|
||||
# passing a hash in :path_names.
|
||||
def setup_path_names
|
||||
[:sign_in, :sign_out, :password, :confirmation].each do |path_name|
|
||||
@path_names[path_name] ||= path_name.to_s
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,6 +6,7 @@ module Devise
|
||||
autoload :Lockable, 'devise/models/lockable'
|
||||
autoload :Recoverable, 'devise/models/recoverable'
|
||||
autoload :Rememberable, 'devise/models/rememberable'
|
||||
autoload :Registerable, 'devise/models/registerable'
|
||||
autoload :Timeoutable, 'devise/models/timeoutable'
|
||||
autoload :Trackable, 'devise/models/trackable'
|
||||
autoload :Validatable, 'devise/models/validatable'
|
||||
@@ -28,7 +29,7 @@ module Devise
|
||||
#
|
||||
def self.config(mod, *accessors) #:nodoc:
|
||||
accessors.each do |accessor|
|
||||
mod.class_eval <<-METHOD, __FILE__, __LINE__
|
||||
mod.class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
||||
def #{accessor}
|
||||
if defined?(@#{accessor})
|
||||
@#{accessor}
|
||||
@@ -50,26 +51,18 @@ module Devise
|
||||
#
|
||||
# devise :authenticatable, :confirmable, :recoverable
|
||||
#
|
||||
# You can also give the following configuration values in a hash: :pepper,
|
||||
# :stretches, :confirm_within and :remember_for. Please check your Devise
|
||||
# initialiazer for a complete description on those values.
|
||||
# You can also give any of the devise configuration values in form of a hash,
|
||||
# with specific values for this model. Please check your Devise initializer
|
||||
# for a complete description on those values.
|
||||
#
|
||||
def devise(*modules)
|
||||
raise "You need to give at least one Devise module" if modules.empty?
|
||||
options = modules.extract_options!
|
||||
|
||||
# TODO Remove me
|
||||
if modules.delete(:all)
|
||||
ActiveSupport::Deprecation.warn "devise :all is deprecated. List your modules instead", caller
|
||||
modules += Devise.all
|
||||
end
|
||||
@devise_modules = Devise::ALL & modules.map(&:to_sym).uniq
|
||||
|
||||
modules -= Array(options.delete(:except))
|
||||
modules = Devise::ALL & modules.uniq
|
||||
|
||||
Devise.orm_class.included_modules_hook(self, modules) do
|
||||
modules.each do |m|
|
||||
devise_modules << m.to_sym
|
||||
Devise.orm_class.included_modules_hook(self) do
|
||||
devise_modules.each do |m|
|
||||
include Devise::Models.const_get(m.to_s.classify)
|
||||
end
|
||||
|
||||
@@ -116,4 +109,4 @@ module Devise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -31,11 +31,17 @@ module Devise
|
||||
base.class_eval do
|
||||
extend ClassMethods
|
||||
|
||||
attr_reader :password, :old_password
|
||||
attr_reader :password, :current_password
|
||||
attr_accessor :password_confirmation
|
||||
end
|
||||
end
|
||||
|
||||
# TODO Remove me in next release
|
||||
def old_password
|
||||
ActiveSupport::Deprecation.warn "old_password is deprecated, please use current_password instead", caller
|
||||
@old_password
|
||||
end
|
||||
|
||||
# Regenerates password salt and encrypted password each time password is set,
|
||||
# and then trigger any "after_changed_password"-callbacks.
|
||||
def password=(new_password)
|
||||
@@ -63,15 +69,36 @@ module Devise
|
||||
valid_password?(attributes[:password])
|
||||
end
|
||||
|
||||
# Update record attributes when :old_password matches, otherwise returns
|
||||
# error on :old_password.
|
||||
# Set password and password confirmation to nil
|
||||
def clean_up_passwords
|
||||
self.password = self.password_confirmation = nil
|
||||
end
|
||||
|
||||
# Update record attributes when :current_password matches, otherwise returns
|
||||
# error on :current_password. It also automatically rejects :password and
|
||||
# :password_confirmation if they are blank.
|
||||
def update_with_password(params={})
|
||||
if valid_password?(params[:old_password])
|
||||
# TODO Remove me in next release
|
||||
if params[:old_password].present?
|
||||
params[:current_password] ||= params[:old_password]
|
||||
ActiveSupport::Deprecation.warn "old_password is deprecated, please use current_password instead", caller
|
||||
end
|
||||
|
||||
params.delete(:password) if params[:password].blank?
|
||||
params.delete(:password_confirmation) if params[:password_confirmation].blank?
|
||||
current_password = params.delete(:current_password)
|
||||
|
||||
result = if valid_password?(current_password)
|
||||
update_attributes(params)
|
||||
else
|
||||
self.class.add_error_on(self, :old_password, :invalid, false)
|
||||
message = current_password.blank? ? :blank : :invalid
|
||||
self.class.add_error_on(self, :current_password, message, false)
|
||||
self.attributes = params
|
||||
false
|
||||
end
|
||||
|
||||
clean_up_passwords unless result
|
||||
result
|
||||
end
|
||||
|
||||
protected
|
||||
@@ -82,12 +109,10 @@ module Devise
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
|
||||
Devise::Models.config(self, :pepper, :stretches, :encryptor, :authentication_keys)
|
||||
|
||||
# Authenticate a user based on configured attribute keys. Returns the
|
||||
# authenticated user if it's valid or nil. Attributes are by default
|
||||
# :email and :password, but the latter is always required.
|
||||
# authenticated user if it's valid or nil.
|
||||
def authenticate(attributes={})
|
||||
return unless authentication_keys.all? { |k| attributes[k].present? }
|
||||
conditions = attributes.slice(*authentication_keys)
|
||||
@@ -115,7 +140,6 @@ module Devise
|
||||
def find_for_authentication(conditions)
|
||||
find(:first, :conditions => conditions)
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
21
lib/devise/models/http_authenticatable.rb
Normal file
21
lib/devise/models/http_authenticatable.rb
Normal file
@@ -0,0 +1,21 @@
|
||||
require 'devise/strategies/http_authenticatable'
|
||||
|
||||
module Devise
|
||||
module Models
|
||||
# Adds HttpAuthenticatable behavior to your model. It expects that your
|
||||
# model class responds to authenticate and authentication_keys methods
|
||||
# (which for example are defined in authenticatable).
|
||||
module HttpAuthenticatable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Authenticate an user using http.
|
||||
def authenticate_with_http(username, password)
|
||||
authenticate(authentication_keys.first => username, :password => password)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -3,9 +3,22 @@ require 'devise/models/activatable'
|
||||
module Devise
|
||||
module Models
|
||||
|
||||
# Handles blocking a user access after a certain number of attempts.
|
||||
# Lockable accepts two different strategies to unlock a user after it's
|
||||
# blocked: email and time. The former will send an email to the user when
|
||||
# the lock happens, containing a link to unlock it's account. The second
|
||||
# will unlock the user automatically after some configured time (ie 2.hours).
|
||||
# It's also possible to setup lockable to use both email and time strategies.
|
||||
#
|
||||
# Configuration:
|
||||
#
|
||||
# maximum_attempts: how many attempts should be accepted before blocking the user.
|
||||
# unlock_strategy: unlock the user account by :time, :email or :both.
|
||||
# unlock_in: the time you want to lock the user after to lock happens. Only
|
||||
# available when unlock_strategy is :time or :both.
|
||||
#
|
||||
module Lockable
|
||||
include Devise::Models::Activatable
|
||||
include Devise::Models::Authenticatable
|
||||
|
||||
def self.included(base)
|
||||
base.class_eval do
|
||||
@@ -16,19 +29,19 @@ module Devise
|
||||
# Lock an user setting it's locked_at to actual time.
|
||||
def lock
|
||||
self.locked_at = Time.now
|
||||
if [:both, :email].include?(self.class.unlock_strategy)
|
||||
if unlock_strategy_enabled?(:email)
|
||||
generate_unlock_token
|
||||
self.send_unlock_instructions
|
||||
send_unlock_instructions
|
||||
end
|
||||
end
|
||||
|
||||
# calls lock and save the model
|
||||
# Lock an user also saving the record.
|
||||
def lock!
|
||||
self.lock
|
||||
lock
|
||||
save(false)
|
||||
end
|
||||
|
||||
# Unlock an user by cleaning locket_at and failed_attempts
|
||||
# Unlock an user by cleaning locket_at and failed_attempts.
|
||||
def unlock!
|
||||
if_locked do
|
||||
self.locked_at = nil
|
||||
@@ -38,9 +51,9 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
# Verifies whether a user is locked or not
|
||||
# Verifies whether a user is locked or not.
|
||||
def locked?
|
||||
self.locked_at && !lock_expired?
|
||||
locked_at && !lock_expired?
|
||||
end
|
||||
|
||||
# Send unlock instructions by email
|
||||
@@ -48,10 +61,10 @@ module Devise
|
||||
::DeviseMailer.deliver_unlock_instructions(self)
|
||||
end
|
||||
|
||||
# Resend the unlock instructions if the user is locked
|
||||
# Resend the unlock instructions if the user is locked.
|
||||
def resend_unlock!
|
||||
if_locked do
|
||||
generate_unlock_token unless self.unlock_token.present?
|
||||
generate_unlock_token unless unlock_token.present?
|
||||
save(false)
|
||||
send_unlock_instructions
|
||||
end
|
||||
@@ -63,20 +76,6 @@ module Devise
|
||||
super && !locked?
|
||||
end
|
||||
|
||||
# Overwrites valid_for_authentication? from Devise::Models::Authenticatable
|
||||
# for verifying whether an user is allowed to sign in or not. If the user
|
||||
# is locked, it should never be allowed.
|
||||
def valid_for_authentication?(attributes)
|
||||
if result = super
|
||||
self.failed_attempts = 0
|
||||
else
|
||||
self.failed_attempts += 1
|
||||
self.lock if self.failed_attempts > self.class.maximum_attempts
|
||||
end
|
||||
save(false) if changed?
|
||||
result
|
||||
end
|
||||
|
||||
# Overwrites invalid_message from Devise::Models::Authenticatable to define
|
||||
# the correct reason for blocking the sign in.
|
||||
def inactive_message
|
||||
@@ -87,6 +86,20 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
# Overwrites valid_for_authentication? from Devise::Models::Authenticatable
|
||||
# for verifying whether an user is allowed to sign in or not. If the user
|
||||
# is locked, it should never be allowed.
|
||||
def valid_for_authentication?(attributes)
|
||||
if result = super
|
||||
self.failed_attempts = 0
|
||||
else
|
||||
self.failed_attempts += 1
|
||||
lock if failed_attempts > self.class.maximum_attempts
|
||||
end
|
||||
save(false) if changed?
|
||||
result
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
# Generates unlock token
|
||||
@@ -96,8 +109,8 @@ module Devise
|
||||
|
||||
# Tells if the lock is expired if :time unlock strategy is active
|
||||
def lock_expired?
|
||||
if [:both, :time].include?(self.class.unlock_strategy)
|
||||
self.locked_at && self.locked_at < self.class.unlock_in.ago
|
||||
if unlock_strategy_enabled?(:time)
|
||||
locked_at && locked_at < self.class.unlock_in.ago
|
||||
else
|
||||
false
|
||||
end
|
||||
@@ -114,6 +127,11 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
# Is the unlock enabled for the given unlock strategy?
|
||||
def unlock_strategy_enabled?(strategy)
|
||||
[:both, strategy].include?(self.class.unlock_strategy)
|
||||
end
|
||||
|
||||
module ClassMethods
|
||||
# Attempt to find a user by it's email. If a record is found, send new
|
||||
# unlock instructions to it. If not user is found, returns a new user
|
||||
@@ -139,4 +157,4 @@ module Devise
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
8
lib/devise/models/registerable.rb
Normal file
8
lib/devise/models/registerable.rb
Normal file
@@ -0,0 +1,8 @@
|
||||
module Devise
|
||||
module Models
|
||||
# Registerable is responsible for everything related to registering a new
|
||||
# resource (ie user sign up).
|
||||
module Registerable
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -9,7 +9,7 @@ module Devise
|
||||
#
|
||||
# Configuration:
|
||||
#
|
||||
# timeout: the time you want to timeout the user session without activity.
|
||||
# timeout_in: the time you want to timeout the user session without activity.
|
||||
module Timeoutable
|
||||
def self.included(base)
|
||||
base.extend ClassMethods
|
||||
|
||||
@@ -3,7 +3,7 @@ require 'devise/strategies/token_authenticatable'
|
||||
module Devise
|
||||
module Models
|
||||
# Token Authenticatable Module, responsible for generate authentication token and validating
|
||||
# authenticity of a user while signing in using a authentication token (say follows an URL).
|
||||
# authenticity of a user while signing in using an authentication token (say follows an URL).
|
||||
#
|
||||
# == Configuration:
|
||||
#
|
||||
|
||||
@@ -20,7 +20,7 @@ module Devise
|
||||
#
|
||||
module ActiveRecord
|
||||
# Required ORM hook. Just yield the given block in ActiveRecord.
|
||||
def self.included_modules_hook(klass, modules)
|
||||
def self.included_modules_hook(klass)
|
||||
yield
|
||||
end
|
||||
|
||||
@@ -36,5 +36,6 @@ end
|
||||
|
||||
if defined?(ActiveRecord)
|
||||
ActiveRecord::Base.extend Devise::Models
|
||||
ActiveRecord::ConnectionAdapters::Table.send :include, Devise::Orm::ActiveRecord
|
||||
ActiveRecord::ConnectionAdapters::TableDefinition.send :include, Devise::Orm::ActiveRecord
|
||||
end
|
||||
end
|
||||
@@ -11,13 +11,13 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
def self.included_modules_hook(klass, modules)
|
||||
def self.included_modules_hook(klass)
|
||||
klass.send :extend, self
|
||||
klass.send :include, InstanceMethods
|
||||
|
||||
yield
|
||||
|
||||
modules.each do |mod|
|
||||
klass.devise_modules.each do |mod|
|
||||
klass.send(mod) if klass.respond_to?(mod)
|
||||
end
|
||||
end
|
||||
@@ -80,4 +80,4 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
DataMapper::Model.send(:include, Devise::Models)
|
||||
DataMapper::Model.send(:include, Devise::Models)
|
||||
|
||||
@@ -11,12 +11,12 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
def self.included_modules_hook(klass, modules)
|
||||
def self.included_modules_hook(klass)
|
||||
klass.send :extend, self
|
||||
klass.send :include, InstanceMethods
|
||||
yield
|
||||
|
||||
modules.each do |mod|
|
||||
klass.devise_modules.each do |mod|
|
||||
klass.send(mod) if klass.respond_to?(mod)
|
||||
end
|
||||
end
|
||||
@@ -47,4 +47,4 @@ module Devise
|
||||
end
|
||||
|
||||
MongoMapper::Document::ClassMethods.send(:include, Devise::Models)
|
||||
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Devise::Models)
|
||||
MongoMapper::EmbeddedDocument::ClassMethods.send(:include, Devise::Models)
|
||||
|
||||
@@ -88,8 +88,8 @@ module ActionController::Routing
|
||||
route_options = mapping.route_options.merge(:path_prefix => mapping.raw_path, :name_prefix => "#{mapping.name}_")
|
||||
|
||||
with_options(route_options) do |routes|
|
||||
mapping.for.each do |strategy|
|
||||
send(strategy, routes, mapping) if self.respond_to?(strategy, true)
|
||||
mapping.for.each do |mod|
|
||||
send(mod, routes, mapping) if self.respond_to?(mod, true)
|
||||
end
|
||||
end
|
||||
end
|
||||
@@ -105,10 +105,6 @@ module ActionController::Routing
|
||||
end
|
||||
end
|
||||
|
||||
def recoverable(routes, mapping)
|
||||
routes.resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password]
|
||||
end
|
||||
|
||||
def confirmable(routes, mapping)
|
||||
routes.resource :confirmation, :only => [:new, :create, :show], :as => mapping.path_names[:confirmation]
|
||||
end
|
||||
@@ -117,6 +113,13 @@ module ActionController::Routing
|
||||
routes.resource :unlock, :only => [:new, :create, :show], :as => mapping.path_names[:unlock]
|
||||
end
|
||||
|
||||
def recoverable(routes, mapping)
|
||||
routes.resource :password, :only => [:new, :create, :edit, :update], :as => mapping.path_names[:password]
|
||||
end
|
||||
|
||||
def registerable(routes, mapping)
|
||||
routes.resource :registration, :only => [:new, :create, :edit, :update, :destroy], :as => mapping.raw_path[1..-1], :path_prefix => nil, :path_names => { :new => mapping.path_names[:sign_up] }
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -6,7 +6,7 @@ module Devise
|
||||
# Redirects to sign_in page if it's not authenticated
|
||||
class Authenticatable < Base
|
||||
def valid?
|
||||
super && params[scope] && params[scope][:password].present?
|
||||
valid_controller? && valid_params? && mapping.to.respond_to?(:authenticate)
|
||||
end
|
||||
|
||||
# Authenticate a user based on email and password params, returning to warden
|
||||
@@ -19,6 +19,16 @@ module Devise
|
||||
fail!(:invalid)
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def valid_controller?
|
||||
params[:controller] == 'sessions'
|
||||
end
|
||||
|
||||
def valid_params?
|
||||
params[scope] && params[scope][:password].present?
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,22 +2,14 @@ module Devise
|
||||
module Strategies
|
||||
# Base strategy for Devise. Responsible for verifying correct scope and mapping.
|
||||
class Base < ::Warden::Strategies::Base
|
||||
# Validate strategy. By default will raise an error if no scope or an
|
||||
# invalid mapping is found.
|
||||
def valid?
|
||||
raise "Could not find mapping for #{scope}" unless mapping
|
||||
mapping.for.include?(klass_type)
|
||||
end
|
||||
|
||||
# Checks if a valid scope was given for devise and find mapping based on
|
||||
# this scope.
|
||||
def mapping
|
||||
Devise.mappings[scope]
|
||||
end
|
||||
|
||||
# Store this class type.
|
||||
def klass_type
|
||||
@klass_type ||= self.class.name.split("::").last.underscore.to_sym
|
||||
@mapping ||= begin
|
||||
mapping = Devise.mappings[scope]
|
||||
raise "Could not find mapping for #{scope}" unless mapping
|
||||
mapping
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
49
lib/devise/strategies/http_authenticatable.rb
Normal file
49
lib/devise/strategies/http_authenticatable.rb
Normal file
@@ -0,0 +1,49 @@
|
||||
require 'devise/strategies/base'
|
||||
|
||||
module Devise
|
||||
module Strategies
|
||||
# Sign in an user using HTTP authentication.
|
||||
class HttpAuthenticatable < Base
|
||||
def valid?
|
||||
http_authentication? && mapping.to.respond_to?(:authenticate_with_http)
|
||||
end
|
||||
|
||||
def authenticate!
|
||||
username, password = username_and_password
|
||||
|
||||
if resource = mapping.to.authenticate_with_http(username, password)
|
||||
success!(resource)
|
||||
else
|
||||
custom!([401, custom_headers, ["HTTP Basic: Access denied.\n"]])
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def username_and_password
|
||||
decode_credentials(request).split(/:/, 2)
|
||||
end
|
||||
|
||||
def http_authentication
|
||||
request.env['HTTP_AUTHORIZATION'] ||
|
||||
request.env['X-HTTP_AUTHORIZATION'] ||
|
||||
request.env['X_HTTP_AUTHORIZATION'] ||
|
||||
request.env['REDIRECT_X_HTTP_AUTHORIZATION']
|
||||
end
|
||||
alias :http_authentication? :http_authentication
|
||||
|
||||
def decode_credentials(request)
|
||||
ActiveSupport::Base64.decode64(http_authentication.split(' ', 2).last || '')
|
||||
end
|
||||
|
||||
def custom_headers
|
||||
{
|
||||
"Content-Type" => Mime::Type.lookup_by_extension(request.template_format.to_s).to_s,
|
||||
"WWW-Authenticate" => %(Basic realm="#{Devise.http_authentication_realm.gsub(/"/, "")}")
|
||||
}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Warden::Strategies.add(:http_authenticatable, Devise::Strategies::HttpAuthenticatable)
|
||||
@@ -10,7 +10,7 @@ module Devise
|
||||
|
||||
# A valid strategy for rememberable needs a remember token in the cookies.
|
||||
def valid?
|
||||
super && remember_me_cookie.present?
|
||||
remember_me_cookie.present? && mapping.to.respond_to?(:serialize_from_cookie)
|
||||
end
|
||||
|
||||
# To authenticate a user we deserialize the cookie and attempt finding
|
||||
|
||||
@@ -6,7 +6,7 @@ module Devise
|
||||
# Redirects to sign_in page if it's not authenticated.
|
||||
class TokenAuthenticatable < Base
|
||||
def valid?
|
||||
super && authentication_token(scope).present?
|
||||
mapping.to.respond_to?(:authenticate_with_token) && authentication_token(scope).present?
|
||||
end
|
||||
|
||||
# Authenticate a user based on authenticatable token params, returning to warden
|
||||
@@ -20,17 +20,16 @@ module Devise
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
private
|
||||
|
||||
# Detect authentication token in params: scoped or not.
|
||||
def authentication_token(scope)
|
||||
if params[scope]
|
||||
params[scope][mapping.to.token_authentication_key]
|
||||
else
|
||||
params[mapping.to.token_authentication_key]
|
||||
end
|
||||
# Detect authentication token in params: scoped or not.
|
||||
def authentication_token(scope)
|
||||
if params[scope]
|
||||
params[scope][mapping.to.token_authentication_key]
|
||||
else
|
||||
params[mapping.to.token_authentication_key]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,3 +1,3 @@
|
||||
module Devise
|
||||
VERSION = "0.9.2".freeze
|
||||
VERSION = "1.0.2".freeze
|
||||
end
|
||||
|
||||
@@ -25,7 +25,7 @@ class DeviseTest < ActiveSupport::TestCase
|
||||
Devise.configure_warden(config)
|
||||
|
||||
assert_equal Devise::FailureApp, config.failure_app
|
||||
assert_equal [:rememberable, :token_authenticatable, :authenticatable], config.default_strategies
|
||||
assert_equal [:rememberable, :http_authenticatable, :token_authenticatable, :authenticatable], config.default_strategies
|
||||
assert_equal :user, config.default_scope
|
||||
assert config.silence_missing_strategies?
|
||||
end
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
require 'test/test_helper'
|
||||
|
||||
class AuthenticationTest < ActionController::IntegrationTest
|
||||
|
||||
test 'home should be accessible without signed in' do
|
||||
class AuthenticationSanityTest < ActionController::IntegrationTest
|
||||
test 'home should be accessible without sign in' do
|
||||
visit '/'
|
||||
assert_response :success
|
||||
assert_template 'home/index'
|
||||
@@ -76,43 +75,6 @@ class AuthenticationTest < ActionController::IntegrationTest
|
||||
assert_contain 'Welcome Admin'
|
||||
end
|
||||
|
||||
test 'sign in as user should not authenticate if not using proper authentication keys' do
|
||||
swap Devise, :authentication_keys => [:username] do
|
||||
sign_in_as_user
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'admin signing in with invalid email should return to sign in form with error message' do
|
||||
sign_in_as_admin do
|
||||
fill_in 'email', :with => 'wrongemail@test.com'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid email or password'
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'admin signing in with invalid pasword should return to sign in form with error message' do
|
||||
sign_in_as_admin do
|
||||
fill_in 'password', :with => 'abcdef'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid email or password'
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'error message is configurable by resource name' do
|
||||
store_translations :en, :devise => {
|
||||
:sessions => { :admin => { :invalid => "Invalid credentials" } }
|
||||
} do
|
||||
sign_in_as_admin do
|
||||
fill_in 'password', :with => 'abcdef'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid credentials'
|
||||
end
|
||||
end
|
||||
|
||||
test 'authenticated admin should not be able to sign as admin again' do
|
||||
sign_in_as_admin
|
||||
get new_admin_session_path
|
||||
@@ -143,6 +105,45 @@ class AuthenticationTest < ActionController::IntegrationTest
|
||||
get root_path
|
||||
assert_not_contain 'Signed out successfully'
|
||||
end
|
||||
end
|
||||
|
||||
class AuthenticationTest < ActionController::IntegrationTest
|
||||
test 'sign in should not authenticate if not using proper authentication keys' do
|
||||
swap Devise, :authentication_keys => [:username] do
|
||||
sign_in_as_user
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
test 'sign in with invalid email should return to sign in form with error message' do
|
||||
sign_in_as_admin do
|
||||
fill_in 'email', :with => 'wrongemail@test.com'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid email or password'
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'sign in with invalid pasword should return to sign in form with error message' do
|
||||
sign_in_as_admin do
|
||||
fill_in 'password', :with => 'abcdef'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid email or password'
|
||||
assert_not warden.authenticated?(:admin)
|
||||
end
|
||||
|
||||
test 'error message is configurable by resource name' do
|
||||
store_translations :en, :devise => {
|
||||
:sessions => { :admin => { :invalid => "Invalid credentials" } }
|
||||
} do
|
||||
sign_in_as_admin do
|
||||
fill_in 'password', :with => 'abcdef'
|
||||
end
|
||||
|
||||
assert_contain 'Invalid credentials'
|
||||
end
|
||||
end
|
||||
|
||||
test 'redirect from warden shows sign in or sign up message' do
|
||||
get admins_path
|
||||
@@ -194,20 +195,21 @@ class AuthenticationTest < ActionController::IntegrationTest
|
||||
assert_equal "/admin_area/home", @request.path
|
||||
end
|
||||
|
||||
test 'destroyed account is signed out' do
|
||||
sign_in_as_user
|
||||
visit 'users/index'
|
||||
|
||||
User.destroy_all
|
||||
visit 'users/index'
|
||||
assert_redirected_to '/users/sign_in?unauthenticated=true'
|
||||
end
|
||||
|
||||
test 'allows session to be set by a given scope' do
|
||||
sign_in_as_user
|
||||
visit 'users/index'
|
||||
assert_equal "Cart", @controller.user_session[:cart]
|
||||
end
|
||||
|
||||
test 'destroyed account is logged out' do
|
||||
sign_in_as_user
|
||||
visit 'users/index'
|
||||
User.destroy_all
|
||||
visit 'users/index'
|
||||
assert_redirected_to '/users/sign_in?unauthenticated=true'
|
||||
end
|
||||
|
||||
test 'renders the scoped view if turned on and view is available' do
|
||||
swap Devise, :scoped_views => true do
|
||||
assert_raise Webrat::NotFoundError do
|
||||
@@ -217,6 +219,20 @@ class AuthenticationTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'renders the scoped view if turned on in an specific controller' do
|
||||
begin
|
||||
SessionsController.scoped_views = true
|
||||
assert_raise Webrat::NotFoundError do
|
||||
sign_in_as_user
|
||||
end
|
||||
|
||||
assert_match /Special user view/, response.body
|
||||
assert !PasswordsController.scoped_views
|
||||
ensure
|
||||
SessionsController.send :remove_instance_variable, :@scoped_views
|
||||
end
|
||||
end
|
||||
|
||||
test 'does not render the scoped view if turned off' do
|
||||
swap Devise, :scoped_views => false do
|
||||
assert_nothing_raised do
|
||||
|
||||
50
test/integration/http_authenticatable_test.rb
Normal file
50
test/integration/http_authenticatable_test.rb
Normal file
@@ -0,0 +1,50 @@
|
||||
require 'test/test_helper'
|
||||
|
||||
class HttpAuthenticationTest < ActionController::IntegrationTest
|
||||
|
||||
test 'sign in should authenticate with http' do
|
||||
sign_in_as_new_user_with_http
|
||||
assert_response :success
|
||||
assert_template 'users/index'
|
||||
assert_contain 'Welcome'
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'returns a custom response with www-authenticate header on failures' do
|
||||
sign_in_as_new_user_with_http("unknown")
|
||||
assert_equal 401, status
|
||||
assert_equal 'Basic realm="Application"', headers["WWW-Authenticate"]
|
||||
end
|
||||
|
||||
test 'uses the request format as response content type' do
|
||||
sign_in_as_new_user_with_http("unknown", "123456", :xml)
|
||||
assert_equal 401, status
|
||||
assert_equal "application/xml", headers["Content-Type"]
|
||||
end
|
||||
|
||||
test 'returns a custom response with www-authenticate and chosen realm' do
|
||||
swap Devise, :http_authentication_realm => "MyApp" do
|
||||
sign_in_as_new_user_with_http("unknown")
|
||||
assert_equal 401, status
|
||||
assert_equal 'Basic realm="MyApp"', headers["WWW-Authenticate"]
|
||||
end
|
||||
end
|
||||
|
||||
test 'sign in should authenticate with http even with specific authentication keys' do
|
||||
swap Devise, :authentication_keys => [:username] do
|
||||
sign_in_as_new_user_with_http "usertest"
|
||||
assert_response :success
|
||||
assert_template 'users/index'
|
||||
assert_contain 'Welcome'
|
||||
assert warden.authenticated?(:user)
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def sign_in_as_new_user_with_http(username="user@test.com", password="123456", format=:html)
|
||||
user = create_user
|
||||
get users_path(:format => format), {}, :authorization => "Basic #{ActiveSupport::Base64.encode64("#{username}:#{password}")}"
|
||||
user
|
||||
end
|
||||
end
|
||||
130
test/integration/registerable_test.rb
Normal file
130
test/integration/registerable_test.rb
Normal file
@@ -0,0 +1,130 @@
|
||||
require 'test/test_helper'
|
||||
|
||||
class RegistrationTest < ActionController::IntegrationTest
|
||||
|
||||
test 'a guest admin should be able to sign in successfully' do
|
||||
visit new_admin_session_path
|
||||
click_link 'Sign up'
|
||||
|
||||
assert_template 'registrations/new'
|
||||
|
||||
fill_in 'email', :with => 'new_user@test.com'
|
||||
fill_in 'password', :with => 'new_user123'
|
||||
fill_in 'password confirmation', :with => 'new_user123'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_contain 'You have signed up successfully.'
|
||||
assert warden.authenticated?(:admin)
|
||||
|
||||
admin = Admin.last
|
||||
assert_equal admin.email, 'new_user@test.com'
|
||||
end
|
||||
|
||||
test 'a guest user should be able to sign up successfully and be blocked by confirmation' do
|
||||
visit new_user_registration_path
|
||||
|
||||
fill_in 'email', :with => 'new_user@test.com'
|
||||
fill_in 'password', :with => 'new_user123'
|
||||
fill_in 'password confirmation', :with => 'new_user123'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_equal true, @controller.send(:flash)[:"user_signed_up"]
|
||||
assert_equal "You have signed up successfully.", @controller.send(:flash)[:notice]
|
||||
|
||||
# For some reason flash is not being set correctly, so instead of getting the
|
||||
# "signed_up" message we get the unconfirmed one. Seems to be an issue with
|
||||
# the internal redirect by the hook and the tests.
|
||||
# follow_redirect!
|
||||
# assert_contain 'You have signed up successfully.'
|
||||
# assert_not_contain 'confirm your account'
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
|
||||
user = User.last
|
||||
assert_equal user.email, 'new_user@test.com'
|
||||
assert_not user.confirmed?
|
||||
end
|
||||
|
||||
test 'a guest user cannot sign up with invalid information' do
|
||||
visit new_user_registration_path
|
||||
|
||||
fill_in 'email', :with => 'invalid_email'
|
||||
fill_in 'password', :with => 'new_user123'
|
||||
fill_in 'password confirmation', :with => 'new_user321'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_template 'registrations/new'
|
||||
assert_have_selector '#errorExplanation'
|
||||
assert_contain "Email is invalid"
|
||||
assert_contain "Password doesn't match confirmation"
|
||||
assert_nil User.first
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'a guest should not sign up with email/password that already exists' do
|
||||
user = create_user
|
||||
visit new_user_registration_path
|
||||
|
||||
fill_in 'email', :with => 'user@test.com'
|
||||
fill_in 'password', :with => '123456'
|
||||
fill_in 'password confirmation', :with => '123456'
|
||||
click_button 'Sign up'
|
||||
|
||||
assert_template 'registrations/new'
|
||||
assert_contain 'Email has already been taken'
|
||||
|
||||
assert_not warden.authenticated?(:user)
|
||||
end
|
||||
|
||||
test 'a guest should not be able to change account' do
|
||||
visit edit_user_registration_path
|
||||
follow_redirect!
|
||||
assert_template 'sessions/new'
|
||||
end
|
||||
|
||||
test 'a signed in user should not be able to access sign up' do
|
||||
sign_in_as_user
|
||||
visit new_user_registration_path
|
||||
assert_template 'home/index'
|
||||
end
|
||||
|
||||
test 'a signed in user should be able to edit his account' do
|
||||
sign_in_as_user
|
||||
visit edit_user_registration_path
|
||||
|
||||
fill_in 'email', :with => 'user.new@email.com'
|
||||
fill_in 'current password', :with => '123456'
|
||||
click_button 'Update'
|
||||
|
||||
assert_template 'home/index'
|
||||
assert_contain 'You updated your account successfully.'
|
||||
|
||||
assert_equal "user.new@email.com", User.first.email
|
||||
end
|
||||
|
||||
test 'a signed in user should be able to edit his password' do
|
||||
sign_in_as_user
|
||||
visit edit_user_registration_path
|
||||
|
||||
fill_in 'password', :with => 'pas123'
|
||||
fill_in 'password confirmation', :with => 'pas123'
|
||||
fill_in 'current password', :with => '123456'
|
||||
click_button 'Update'
|
||||
|
||||
assert_template 'home/index'
|
||||
assert_contain 'You updated your account successfully.'
|
||||
|
||||
assert User.first.valid_password?('pas123')
|
||||
end
|
||||
|
||||
test 'a signed in user should be able to cancel his account' do
|
||||
sign_in_as_user
|
||||
visit edit_user_registration_path
|
||||
|
||||
click_link "Cancel my account"
|
||||
assert_contain "Bye! Your account was successfully cancelled. We hope to see you again soon."
|
||||
|
||||
assert User.all.empty?
|
||||
end
|
||||
end
|
||||
@@ -2,7 +2,7 @@ require 'test/test_helper'
|
||||
|
||||
class TokenAuthenticationTest < ActionController::IntegrationTest
|
||||
|
||||
test 'sign in user should authenticate with valid authentication token and proper authentication token key' do
|
||||
test 'sign in should authenticate with valid authentication token and proper authentication token key' do
|
||||
swap Devise, :token_authentication_key => :secret_token do
|
||||
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
||||
|
||||
@@ -13,7 +13,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'user signing in with valid authentication token - but improper authentication token key - return to sign in form with error message' do
|
||||
test 'signing in with valid authentication token - but improper authentication token key - return to sign in form with error message' do
|
||||
swap Devise, :token_authentication_key => :donald_duck_token do
|
||||
sign_in_as_new_user_with_token(:auth_token_key => :secret_token)
|
||||
assert_redirected_to new_user_session_path(:unauthenticated => true)
|
||||
@@ -25,7 +25,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
|
||||
end
|
||||
end
|
||||
|
||||
test 'user signing in with invalid authentication token should return to sign in form with error message' do
|
||||
test 'signing in with invalid authentication token should return to sign in form with error message' do
|
||||
store_translations :en, :devise => {:sessions => {:invalid_token => 'LOL, that was not a single character correct.'}} do
|
||||
sign_in_as_new_user_with_token(:auth_token => '*** INVALID TOKEN ***')
|
||||
assert_redirected_to new_user_session_path(:invalid_token => true)
|
||||
@@ -40,7 +40,7 @@ class TokenAuthenticationTest < ActionController::IntegrationTest
|
||||
|
||||
private
|
||||
|
||||
def sign_in_as_new_user_with_token(options = {}, &block)
|
||||
def sign_in_as_new_user_with_token(options = {})
|
||||
options[:auth_token_key] ||= Devise.token_authentication_key
|
||||
options[:auth_token] ||= VALID_AUTHENTICATION_TOKEN
|
||||
|
||||
|
||||
@@ -63,6 +63,21 @@ class ConfirmationInstructionsTest < ActionMailer::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
test 'content type should be set to plain when manually configured' do
|
||||
swap Devise, :mailer_content_type => "text/plain" do
|
||||
assert_equal "text/plain", mail.content_type
|
||||
end
|
||||
end
|
||||
|
||||
test 'renders a scoped if scoped_views is set in the mailer class' do
|
||||
begin
|
||||
DeviseMailer.scoped_views = true
|
||||
assert_equal user.email, mail.body
|
||||
ensure
|
||||
DeviseMailer.send :remove_instance_variable, :@scoped_views
|
||||
end
|
||||
end
|
||||
|
||||
test 'mailer sender accepts a proc' do
|
||||
swap Devise, :mailer_sender => lambda { "another@example.com" } do
|
||||
assert_equal ['another@example.com'], mail.from
|
||||
|
||||
@@ -63,18 +63,22 @@ class MappingTest < ActiveSupport::TestCase
|
||||
|
||||
test 'return default path names' do
|
||||
mapping = Devise.mappings[:user]
|
||||
assert_equal 'sign_in', mapping.path_names[:sign_in]
|
||||
assert_equal 'sign_out', mapping.path_names[:sign_out]
|
||||
assert_equal 'password', mapping.path_names[:password]
|
||||
assert_equal 'sign_in', mapping.path_names[:sign_in]
|
||||
assert_equal 'sign_out', mapping.path_names[:sign_out]
|
||||
assert_equal 'password', mapping.path_names[:password]
|
||||
assert_equal 'confirmation', mapping.path_names[:confirmation]
|
||||
assert_equal 'sign_up', mapping.path_names[:sign_up]
|
||||
assert_equal 'unlock', mapping.path_names[:unlock]
|
||||
end
|
||||
|
||||
test 'allow custom path names to be given' do
|
||||
mapping = Devise.mappings[:manager]
|
||||
assert_equal 'login', mapping.path_names[:sign_in]
|
||||
assert_equal 'logout', mapping.path_names[:sign_out]
|
||||
assert_equal 'secret', mapping.path_names[:password]
|
||||
assert_equal 'login', mapping.path_names[:sign_in]
|
||||
assert_equal 'logout', mapping.path_names[:sign_out]
|
||||
assert_equal 'secret', mapping.path_names[:password]
|
||||
assert_equal 'verification', mapping.path_names[:confirmation]
|
||||
assert_equal 'register', mapping.path_names[:sign_up]
|
||||
assert_equal 'unblock', mapping.path_names[:unlock]
|
||||
end
|
||||
|
||||
test 'has an empty path as default path prefix' do
|
||||
@@ -86,7 +90,7 @@ class MappingTest < ActiveSupport::TestCase
|
||||
mapping = Devise.mappings[:manager]
|
||||
assert_equal '/:locale/', mapping.path_prefix
|
||||
end
|
||||
|
||||
|
||||
test 'retrieve as from the proper position' do
|
||||
assert_equal 1, Devise.mappings[:user].as_position
|
||||
assert_equal 2, Devise.mappings[:manager].as_position
|
||||
@@ -96,13 +100,13 @@ class MappingTest < ActiveSupport::TestCase
|
||||
assert_equal '/users', Devise.mappings[:user].raw_path
|
||||
assert_equal '/:locale/accounts', Devise.mappings[:manager].raw_path
|
||||
end
|
||||
|
||||
|
||||
test 'raw path ignores the relative_url_root' do
|
||||
swap ActionController::Base, :relative_url_root => "/abc" do
|
||||
assert_equal '/users', Devise.mappings[:user].raw_path
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
test 'parsed path is returned' do
|
||||
begin
|
||||
Devise.default_url_options {{ :locale => I18n.locale }}
|
||||
@@ -112,7 +116,7 @@ class MappingTest < ActiveSupport::TestCase
|
||||
Devise.default_url_options {{ }}
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
test 'parsed path adds in the relative_url_root' do
|
||||
swap ActionController::Base, :relative_url_root => '/abc' do
|
||||
assert_equal '/abc/users', Devise.mappings[:user].parsed_path
|
||||
|
||||
@@ -119,7 +119,7 @@ class AuthenticatableTest < ActiveSupport::TestCase
|
||||
|
||||
test 'should use authentication keys to retrieve users' do
|
||||
swap Devise, :authentication_keys => [:username] do
|
||||
user = create_user(:username => "josevalim")
|
||||
user = create_user
|
||||
assert_nil User.authenticate(:email => user.email, :password => user.password)
|
||||
assert_not_nil User.authenticate(:username => user.username, :password => user.password)
|
||||
end
|
||||
@@ -130,29 +130,51 @@ class AuthenticatableTest < ActiveSupport::TestCase
|
||||
assert_not_nil Admin.authenticate(:email => admin.email, :password => admin.password)
|
||||
end
|
||||
|
||||
test 'should respond to old password' do
|
||||
assert new_user.respond_to?(:old_password)
|
||||
test 'should respond to current password' do
|
||||
assert new_user.respond_to?(:current_password)
|
||||
end
|
||||
|
||||
test 'should update password with valid old password' do
|
||||
test 'should update password with valid current password' do
|
||||
user = create_user
|
||||
assert user.update_with_password(:old_password => '123456',
|
||||
assert user.update_with_password(:current_password => '123456',
|
||||
:password => 'pass321', :password_confirmation => 'pass321')
|
||||
assert user.reload.valid_password?('pass321')
|
||||
end
|
||||
|
||||
test 'should add an error to old password when it is invalid' do
|
||||
test 'should add an error to current password when it is invalid' do
|
||||
user = create_user
|
||||
assert_not user.update_with_password(:old_password => 'other',
|
||||
assert_not user.update_with_password(:current_password => 'other',
|
||||
:password => 'pass321', :password_confirmation => 'pass321')
|
||||
assert user.reload.valid_password?('123456')
|
||||
assert_match /invalid/, user.errors[:old_password]
|
||||
assert_match /invalid/, user.errors[:current_password]
|
||||
end
|
||||
|
||||
test 'should add an error to current password when it is blank' do
|
||||
user = create_user
|
||||
assert_not user.update_with_password(:password => 'pass321',
|
||||
:password_confirmation => 'pass321')
|
||||
assert user.reload.valid_password?('123456')
|
||||
assert_match /blank/, user.errors[:current_password]
|
||||
end
|
||||
|
||||
test 'should ignore password and its confirmation if they are blank' do
|
||||
user = create_user
|
||||
assert user.update_with_password(:current_password => '123456', :email => "new@email.com")
|
||||
assert_equal "new@email.com", user.email
|
||||
end
|
||||
|
||||
test 'should not update password with invalid confirmation' do
|
||||
user = create_user
|
||||
assert_not user.update_with_password(:old_password => '123456',
|
||||
assert_not user.update_with_password(:current_password => '123456',
|
||||
:password => 'pass321', :password_confirmation => 'other')
|
||||
assert user.reload.valid_password?('123456')
|
||||
end
|
||||
|
||||
test 'should clean up password fields on failure' do
|
||||
user = create_user
|
||||
assert_not user.update_with_password(:current_password => '123456',
|
||||
:password => 'pass321', :password_confirmation => 'other')
|
||||
assert user.password.blank?
|
||||
assert user.password_confirmation.blank?
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,25 +1,25 @@
|
||||
require 'test/test_helper'
|
||||
|
||||
class LockableTest < ActiveSupport::TestCase
|
||||
|
||||
|
||||
def setup
|
||||
setup_mailer
|
||||
end
|
||||
|
||||
|
||||
test "should increment failed attempts on unsuccessful authentication" do
|
||||
user = create_user
|
||||
assert_equal 0, user.failed_attempts
|
||||
authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword")
|
||||
assert_equal 1, user.reload.failed_attempts
|
||||
end
|
||||
|
||||
|
||||
test "should lock account base on maximum_attempts" do
|
||||
user = create_user
|
||||
attempts = Devise.maximum_attempts + 1
|
||||
attempts.times { authenticated_user = User.authenticate(:email => user.email, :password => "anotherpassword") }
|
||||
assert user.reload.locked?
|
||||
end
|
||||
|
||||
|
||||
test "should respect maximum attempts configuration" do
|
||||
user = create_user
|
||||
swap Devise, :maximum_attempts => 2 do
|
||||
@@ -27,7 +27,7 @@ class LockableTest < ActiveSupport::TestCase
|
||||
assert user.reload.locked?
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
test "should clear failed_attempts on successfull sign in" do
|
||||
user = create_user
|
||||
User.authenticate(:email => user.email, :password => "anotherpassword")
|
||||
@@ -61,8 +61,8 @@ class LockableTest < ActiveSupport::TestCase
|
||||
assert_nil user.reload.unlock_token
|
||||
assert 0, user.reload.failed_attempts
|
||||
end
|
||||
|
||||
test 'should not unlcok an unlocked user' do
|
||||
|
||||
test 'should not unlock an unlocked user' do
|
||||
user = create_user
|
||||
assert_not user.unlock!
|
||||
assert_match /not locked/, user.errors[:email]
|
||||
@@ -199,4 +199,4 @@ class LockableTest < ActiveSupport::TestCase
|
||||
assert_equal 'not locked', user.errors[:email]
|
||||
end
|
||||
|
||||
end
|
||||
end
|
||||
|
||||
@@ -23,7 +23,7 @@ class ActiveRecordTest < ActiveSupport::TestCase
|
||||
end
|
||||
|
||||
test 'add modules cherry pick' do
|
||||
assert_include_modules Admin, :authenticatable, :timeoutable
|
||||
assert_include_modules Admin, :authenticatable, :registerable, :timeoutable
|
||||
end
|
||||
|
||||
test 'set a default value for stretches' do
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
class Admin < ActiveRecord::Base
|
||||
devise :authenticatable, :timeoutable
|
||||
devise :authenticatable, :registerable, :timeoutable
|
||||
|
||||
def self.find_for_authentication(conditions)
|
||||
last(:conditions => conditions)
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
class User < ActiveRecord::Base
|
||||
devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable,
|
||||
:validatable, :timeoutable, :lockable, :token_authenticatable
|
||||
devise :authenticatable, :http_authenticatable, :confirmable, :lockable, :recoverable,
|
||||
:registerable, :rememberable, :timeoutable, :token_authenticatable,
|
||||
:trackable, :validatable
|
||||
|
||||
attr_accessible :username, :email, :password, :password_confirmation
|
||||
end
|
||||
|
||||
@@ -1,7 +1,8 @@
|
||||
class User
|
||||
include MongoMapper::Document
|
||||
key :created_at, DateTime
|
||||
devise :authenticatable, :confirmable, :recoverable, :rememberable, :trackable,
|
||||
:validatable, :timeoutable, :lockable, :token_authenticatable
|
||||
devise :authenticatable, :http_authenticatable, :confirmable, :recoverable,
|
||||
:rememberable, :trackable, :validatable, :timeoutable, :lockable,
|
||||
:token_authenticatable
|
||||
# attr_accessible :username, :email, :password, :password_confirmation
|
||||
end
|
||||
|
||||
@@ -7,9 +7,13 @@
|
||||
<body>
|
||||
<div id="container">
|
||||
<%- flash.each do |name, msg| -%>
|
||||
<%= content_tag :div, msg, :id => "flash_#{name}" %>
|
||||
<%= content_tag :div, msg, :id => "flash_#{name}" if msg.is_a?(String) %>
|
||||
<%- end -%>
|
||||
|
||||
<% if user_signed_in? -%>
|
||||
<p>Hello User! You are signed in!</p>
|
||||
<% end -%>
|
||||
|
||||
<%= yield %>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
@@ -35,6 +35,9 @@ Devise.setup do |config|
|
||||
|
||||
# Configure the e-mail address which will be shown in DeviseMailer.
|
||||
config.mailer_sender = "please-change-me-omg@yourapp.com"
|
||||
|
||||
# Configure the content type of DeviseMailer mails (defaults to text/html")
|
||||
# config.mailer_content_type = "text/plain"
|
||||
|
||||
# Load and configure the ORM. Supports :active_record, :data_mapper and :mongo_mapper.
|
||||
require "devise/orm/#{DEVISE_ORM}"
|
||||
|
||||
@@ -3,8 +3,9 @@ ActionController::Routing::Routes.draw do |map|
|
||||
map.devise_for :admin, :as => 'admin_area'
|
||||
map.devise_for :accounts, :scope => 'manager', :path_prefix => ':locale',
|
||||
:class_name => "User", :requirements => { :extra => 'value' }, :path_names => {
|
||||
:sign_in => 'login', :sign_out => 'logout', :password => 'secret',
|
||||
:confirmation => 'verification', :unlock => 'unblock'
|
||||
:sign_in => 'login', :sign_out => 'logout',
|
||||
:password => 'secret', :confirmation => 'verification',
|
||||
:unlock => 'unblock', :sign_up => 'register'
|
||||
}
|
||||
|
||||
map.resources :users, :only => [:index], :member => { :expire => :get }
|
||||
|
||||
@@ -42,6 +42,38 @@ class MapRoutingTest < ActionController::TestCase
|
||||
assert_recognizes({:controller => 'passwords', :action => 'update'}, {:path => 'users/password', :method => :put})
|
||||
end
|
||||
|
||||
test 'map new user unlock' do
|
||||
assert_recognizes({:controller => 'unlocks', :action => 'new'}, 'users/unlock/new')
|
||||
end
|
||||
|
||||
test 'map create user unlock' do
|
||||
assert_recognizes({:controller => 'unlocks', :action => 'create'}, {:path => 'users/unlock', :method => :post})
|
||||
end
|
||||
|
||||
test 'map show user unlock' do
|
||||
assert_recognizes({:controller => 'unlocks', :action => 'show'}, {:path => 'users/unlock', :method => :get})
|
||||
end
|
||||
|
||||
test 'map new user registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'new'}, 'users/sign_up')
|
||||
end
|
||||
|
||||
test 'map create user registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'create'}, {:path => 'users', :method => :post})
|
||||
end
|
||||
|
||||
test 'map edit user registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'edit'}, {:path => 'users/edit', :method => :get})
|
||||
end
|
||||
|
||||
test 'map update user registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'update'}, {:path => 'users', :method => :put})
|
||||
end
|
||||
|
||||
test 'map destroy user registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'destroy'}, {:path => 'users', :method => :delete})
|
||||
end
|
||||
|
||||
test 'map admin session with :as option' do
|
||||
assert_recognizes({:controller => 'sessions', :action => 'new'}, {:path => 'admin_area/sign_in', :method => :get})
|
||||
end
|
||||
@@ -72,4 +104,7 @@ class MapRoutingTest < ActionController::TestCase
|
||||
assert_recognizes({:controller => 'unlocks', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/unblock/new')
|
||||
end
|
||||
|
||||
test 'map account with custom path name for registration' do
|
||||
assert_recognizes({:controller => 'registrations', :action => 'new', :locale => 'en', :extra => 'value'}, '/en/accounts/register')
|
||||
end
|
||||
end
|
||||
|
||||
@@ -7,7 +7,11 @@ class ActionController::IntegrationTest
|
||||
def create_user(options={})
|
||||
@user ||= begin
|
||||
user = User.create!(
|
||||
:email => 'user@test.com', :password => '123456', :password_confirmation => '123456', :created_at => Time.now.utc
|
||||
:username => 'usertest',
|
||||
:email => 'user@test.com',
|
||||
:password => '123456',
|
||||
:password_confirmation => '123456',
|
||||
:created_at => Time.now.utc
|
||||
)
|
||||
user.confirm! unless options[:confirm] == false
|
||||
user.lock! if options[:locked] == true
|
||||
|
||||
@@ -1,36 +0,0 @@
|
||||
class ActiveSupport::TestCase
|
||||
def setup_mailer
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
def store_translations(locale, translations, &block)
|
||||
begin
|
||||
I18n.backend.store_translations locale, translations
|
||||
yield
|
||||
ensure
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
# Helpers for creating new users
|
||||
#
|
||||
def generate_unique_email
|
||||
@@email_count ||= 0
|
||||
@@email_count += 1
|
||||
"test#{@@email_count}@email.com"
|
||||
end
|
||||
|
||||
def valid_attributes(attributes={})
|
||||
{ :email => generate_unique_email,
|
||||
:password => '123456',
|
||||
:password_confirmation => '123456' }.update(attributes)
|
||||
end
|
||||
|
||||
def new_user(attributes={})
|
||||
User.new(valid_attributes(attributes))
|
||||
end
|
||||
|
||||
def create_user(attributes={})
|
||||
User.create!(valid_attributes(attributes))
|
||||
end
|
||||
end
|
||||
@@ -1,5 +1,39 @@
|
||||
class ActiveSupport::TestCase
|
||||
|
||||
VALID_AUTHENTICATION_TOKEN = 'AbCdEfGhIjKlMnOpQrSt'.freeze
|
||||
|
||||
def setup_mailer
|
||||
ActionMailer::Base.deliveries = []
|
||||
end
|
||||
|
||||
def store_translations(locale, translations, &block)
|
||||
begin
|
||||
I18n.backend.store_translations locale, translations
|
||||
yield
|
||||
ensure
|
||||
I18n.reload!
|
||||
end
|
||||
end
|
||||
|
||||
# Helpers for creating new users
|
||||
#
|
||||
def generate_unique_email
|
||||
@@email_count ||= 0
|
||||
@@email_count += 1
|
||||
"test#{@@email_count}@email.com"
|
||||
end
|
||||
|
||||
def valid_attributes(attributes={})
|
||||
{ :username => "usertest",
|
||||
:email => generate_unique_email,
|
||||
:password => '123456',
|
||||
:password_confirmation => '123456' }.update(attributes)
|
||||
end
|
||||
|
||||
def new_user(attributes={})
|
||||
User.new(valid_attributes(attributes))
|
||||
end
|
||||
|
||||
def create_user(attributes={})
|
||||
User.create!(valid_attributes(attributes))
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user