Merge pull request #1645 from consul/login-by-username
Allow users to login using username
This commit is contained in:
@@ -234,7 +234,7 @@ GEM
|
|||||||
railties (>= 3.2)
|
railties (>= 3.2)
|
||||||
loofah (2.0.3)
|
loofah (2.0.3)
|
||||||
nokogiri (>= 1.5.9)
|
nokogiri (>= 1.5.9)
|
||||||
mail (2.6.6.rc1)
|
mail (2.6.6)
|
||||||
mime-types (>= 1.16, < 4)
|
mime-types (>= 1.16, < 4)
|
||||||
mime-types (3.1)
|
mime-types (3.1)
|
||||||
mime-types-data (~> 3.2015)
|
mime-types-data (~> 3.2015)
|
||||||
@@ -551,4 +551,4 @@ DEPENDENCIES
|
|||||||
whenever
|
whenever
|
||||||
|
|
||||||
BUNDLED WITH
|
BUNDLED WITH
|
||||||
1.15.0
|
1.15.1
|
||||||
|
|||||||
@@ -3,7 +3,8 @@ class User < ActiveRecord::Base
|
|||||||
include Verification
|
include Verification
|
||||||
|
|
||||||
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable,
|
devise :database_authenticatable, :registerable, :confirmable, :recoverable, :rememberable,
|
||||||
:trackable, :validatable, :omniauthable, :async, :password_expirable, :secure_validatable
|
:trackable, :validatable, :omniauthable, :async, :password_expirable, :secure_validatable,
|
||||||
|
authentication_keys: [:login]
|
||||||
|
|
||||||
acts_as_voter
|
acts_as_voter
|
||||||
acts_as_paranoid column: :hidden_at
|
acts_as_paranoid column: :hidden_at
|
||||||
@@ -48,6 +49,7 @@ class User < ActiveRecord::Base
|
|||||||
|
|
||||||
attr_accessor :skip_password_validation
|
attr_accessor :skip_password_validation
|
||||||
attr_accessor :use_redeemable_code
|
attr_accessor :use_redeemable_code
|
||||||
|
attr_accessor :login
|
||||||
|
|
||||||
scope :administrators, -> { joins(:administrators) }
|
scope :administrators, -> { joins(:administrators) }
|
||||||
scope :moderators, -> { joins(:moderator) }
|
scope :moderators, -> { joins(:moderator) }
|
||||||
@@ -286,6 +288,14 @@ class User < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
delegate :can?, :cannot?, to: :ability
|
delegate :can?, :cannot?, to: :ability
|
||||||
|
|
||||||
|
# overwritting of Devise method to allow login using email OR username
|
||||||
|
def self.find_for_database_authentication(warden_conditions)
|
||||||
|
conditions = warden_conditions.dup
|
||||||
|
login = conditions.delete(:login)
|
||||||
|
where(conditions.to_hash).where(["lower(email) = ?", login.downcase]).first ||
|
||||||
|
where(conditions.to_hash).where(["username = ?", login]).first
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def clean_document_number
|
def clean_document_number
|
||||||
|
|||||||
@@ -11,7 +11,7 @@
|
|||||||
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
|
<%= form_for(resource, as: resource_name, url: session_path(resource_name)) do |f| %>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<div class="small-12 column">
|
<div class="small-12 column">
|
||||||
<%= f.email_field :email, autofocus: true, placeholder: t("devise_views.sessions.new.email_label") %>
|
<%= f.text_field :login, autofocus: true, placeholder: t("devise_views.sessions.new.login_label") %>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="small-12 column">
|
<div class="small-12 column">
|
||||||
|
|||||||
@@ -113,6 +113,7 @@ en:
|
|||||||
description: "Description"
|
description: "Description"
|
||||||
terms_of_service: "Terms of service"
|
terms_of_service: "Terms of service"
|
||||||
user:
|
user:
|
||||||
|
login: "Email or username"
|
||||||
email: "Email"
|
email: "Email"
|
||||||
username: "Username"
|
username: "Username"
|
||||||
password_confirmation: "Password confirmation"
|
password_confirmation: "Password confirmation"
|
||||||
|
|||||||
@@ -108,6 +108,7 @@ es:
|
|||||||
description: "Descripción"
|
description: "Descripción"
|
||||||
terms_of_service: "Términos de servicio"
|
terms_of_service: "Términos de servicio"
|
||||||
user:
|
user:
|
||||||
|
login: Email o nombre de usuario
|
||||||
email: "Correo electrónico"
|
email: "Correo electrónico"
|
||||||
username: "Nombre de usuario"
|
username: "Nombre de usuario"
|
||||||
password_confirmation: "Confirmación de contraseña"
|
password_confirmation: "Confirmación de contraseña"
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ en:
|
|||||||
title: Forgotten password?
|
title: Forgotten password?
|
||||||
sessions:
|
sessions:
|
||||||
new:
|
new:
|
||||||
email_label: Email
|
login_label: Email or username
|
||||||
password_label: Password
|
password_label: Password
|
||||||
remember_me: Remember me
|
remember_me: Remember me
|
||||||
submit: Enter
|
submit: Enter
|
||||||
|
|||||||
@@ -68,7 +68,7 @@ es:
|
|||||||
title: "¿Has olvidado tu contraseña?"
|
title: "¿Has olvidado tu contraseña?"
|
||||||
sessions:
|
sessions:
|
||||||
new:
|
new:
|
||||||
email_label: Email
|
login_label: Email o nombre de usuario
|
||||||
password_label: Contraseña
|
password_label: Contraseña
|
||||||
remember_me: Recordarme
|
remember_me: Recordarme
|
||||||
submit: Entrar
|
submit: Entrar
|
||||||
|
|||||||
@@ -138,6 +138,6 @@ feature 'Account' do
|
|||||||
|
|
||||||
login_through_form_as(@user)
|
login_through_form_as(@user)
|
||||||
|
|
||||||
expect(page).to have_content "Invalid email or password"
|
expect(page).to have_content "Invalid login or password"
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -42,11 +42,11 @@ feature 'Moderate users' do
|
|||||||
visit root_path
|
visit root_path
|
||||||
|
|
||||||
click_link 'Sign in'
|
click_link 'Sign in'
|
||||||
fill_in 'user_email', with: citizen.email
|
fill_in 'user_login', with: citizen.email
|
||||||
fill_in 'user_password', with: citizen.password
|
fill_in 'user_password', with: citizen.password
|
||||||
click_button 'Enter'
|
click_button 'Enter'
|
||||||
|
|
||||||
expect(page).to have_content 'Invalid email or password'
|
expect(page).to have_content 'Invalid login or password'
|
||||||
expect(current_path).to eq(new_user_session_path)
|
expect(current_path).to eq(new_user_session_path)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,9 @@ require 'rails_helper'
|
|||||||
feature 'Users' do
|
feature 'Users' do
|
||||||
|
|
||||||
context 'Regular authentication' do
|
context 'Regular authentication' do
|
||||||
scenario 'Sign up' do
|
context 'Sign up' do
|
||||||
|
|
||||||
|
scenario 'Success' do
|
||||||
visit '/'
|
visit '/'
|
||||||
click_link 'Register'
|
click_link 'Register'
|
||||||
|
|
||||||
@@ -30,17 +32,74 @@ feature 'Users' do
|
|||||||
expect(page).to have_content error_message
|
expect(page).to have_content error_message
|
||||||
end
|
end
|
||||||
|
|
||||||
scenario 'Sign in' do
|
end
|
||||||
|
|
||||||
|
context 'Sign in' do
|
||||||
|
|
||||||
|
scenario 'sign in with email' do
|
||||||
create(:user, email: 'manuela@consul.dev', password: 'judgementday')
|
create(:user, email: 'manuela@consul.dev', password: 'judgementday')
|
||||||
|
|
||||||
visit '/'
|
visit '/'
|
||||||
click_link 'Sign in'
|
click_link 'Sign in'
|
||||||
fill_in 'user_email', with: 'manuela@consul.dev'
|
fill_in 'user_login', with: 'manuela@consul.dev'
|
||||||
fill_in 'user_password', with: 'judgementday'
|
fill_in 'user_password', with: 'judgementday'
|
||||||
click_button 'Enter'
|
click_button 'Enter'
|
||||||
|
|
||||||
expect(page).to have_content 'You have been signed in successfully.'
|
expect(page).to have_content 'You have been signed in successfully.'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
scenario 'Sign in with username' do
|
||||||
|
create(:user, username: '👻👽👾🤖', email: 'ash@nostromo.dev', password: 'xenomorph')
|
||||||
|
|
||||||
|
visit '/'
|
||||||
|
click_link 'Sign in'
|
||||||
|
fill_in 'user_login', with: '👻👽👾🤖'
|
||||||
|
fill_in 'user_password', with: 'xenomorph'
|
||||||
|
click_button 'Enter'
|
||||||
|
|
||||||
|
expect(page).to have_content 'You have been signed in successfully.'
|
||||||
|
end
|
||||||
|
|
||||||
|
scenario 'Avoid username-email collisions' do
|
||||||
|
u1 = create(:user, username: 'Spidey', email: 'peter@nyc.dev', password: 'greatpower')
|
||||||
|
u2 = create(:user, username: 'peter@nyc.dev', email: 'venom@nyc.dev', password: 'symbiote')
|
||||||
|
|
||||||
|
visit '/'
|
||||||
|
click_link 'Sign in'
|
||||||
|
fill_in 'user_login', with: 'peter@nyc.dev'
|
||||||
|
fill_in 'user_password', with: 'greatpower'
|
||||||
|
click_button 'Enter'
|
||||||
|
|
||||||
|
expect(page).to have_content 'You have been signed in successfully.'
|
||||||
|
|
||||||
|
visit account_path
|
||||||
|
|
||||||
|
expect(page).to have_link 'My activity', href: user_path(u1)
|
||||||
|
|
||||||
|
visit '/'
|
||||||
|
click_link 'Sign out'
|
||||||
|
|
||||||
|
expect(page).to have_content 'You have been signed out successfully.'
|
||||||
|
|
||||||
|
click_link 'Sign in'
|
||||||
|
fill_in 'user_login', with: 'peter@nyc.dev'
|
||||||
|
fill_in 'user_password', with: 'symbiote'
|
||||||
|
click_button 'Enter'
|
||||||
|
|
||||||
|
expect(page).to_not have_content 'You have been signed in successfully.'
|
||||||
|
expect(page).to have_content 'Invalid login or password.'
|
||||||
|
|
||||||
|
fill_in 'user_login', with: 'venom@nyc.dev'
|
||||||
|
fill_in 'user_password', with: 'symbiote'
|
||||||
|
click_button 'Enter'
|
||||||
|
|
||||||
|
expect(page).to have_content 'You have been signed in successfully.'
|
||||||
|
|
||||||
|
visit account_path
|
||||||
|
|
||||||
|
expect(page).to have_link 'My activity', href: user_path(u2)
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'OAuth authentication' do
|
context 'OAuth authentication' do
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ feature "Welcome screen" do
|
|||||||
|
|
||||||
visit email_path(email_verification_token: encrypted)
|
visit email_path(email_verification_token: encrypted)
|
||||||
|
|
||||||
fill_in 'user_email', with: user.email
|
fill_in 'user_login', with: user.email
|
||||||
fill_in 'user_password', with: user.password
|
fill_in 'user_password', with: user.password
|
||||||
|
|
||||||
click_button 'Enter'
|
click_button 'Enter'
|
||||||
|
|||||||
@@ -18,7 +18,7 @@ module CommonActions
|
|||||||
visit root_path
|
visit root_path
|
||||||
click_link 'Sign in'
|
click_link 'Sign in'
|
||||||
|
|
||||||
fill_in 'user_email', with: user.email
|
fill_in 'user_login', with: user.email
|
||||||
fill_in 'user_password', with: user.password
|
fill_in 'user_password', with: user.password
|
||||||
|
|
||||||
click_button 'Enter'
|
click_button 'Enter'
|
||||||
|
|||||||
Reference in New Issue
Block a user