From 79b5d8261ae8f3a72742144b66952c9580c09c1c Mon Sep 17 00:00:00 2001 From: Braulio Bhavamitra Date: Wed, 12 Dec 2012 21:29:23 +0000 Subject: [PATCH] Login with login OR email --- app/models/user.rb | 3 ++- app/views/account/_signup_form.rhtml | 3 ++- app/views/account/forgot_password.rhtml | 2 +- public/javascripts/application.js | 21 ++++++++++++++++++++- test/unit/user_test.rb | 2 ++ 5 files changed, 27 insertions(+), 4 deletions(-) diff --git a/app/models/user.rb b/app/models/user.rb index d387dbb..e2d869a 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -119,7 +119,8 @@ class User < ActiveRecord::Base # Authenticates a user by their login name and unencrypted password. Returns the user or nil. def self.authenticate(login, password, environment = nil) environment ||= Environment.default - u = first :conditions => ['login = ? AND environment_id = ? AND activated_at IS NOT NULL', login, environment.id] # need to get the salt + u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL', + login, login, environment.id] # need to get the salt u && u.authenticated?(password) ? u : nil end diff --git a/app/views/account/_signup_form.rhtml b/app/views/account/_signup_form.rhtml index 560be5b..f18f56f 100644 --- a/app/views/account/_signup_form.rhtml +++ b/app/views/account/_signup_form.rhtml @@ -13,7 +13,8 @@ <%= environment.default_hostname %>/
- <%= required text_field(:user, :login, :id => 'user_login', :onchange => 'this.value = convToValidLogin(this.value);') %> + <%= required text_field(:user, :login, :id => 'user_login', + :onchange => 'this.value = convToValidUsername(this.value);') %>

 

<%= content_tag(:small, _('Choose your login name carefully! It will be your network access and you will not be able to change it later.'), :id => 'signup-balloon') %> diff --git a/app/views/account/forgot_password.rhtml b/app/views/account/forgot_password.rhtml index 537c9f4..1e3748d 100644 --- a/app/views/account/forgot_password.rhtml +++ b/app/views/account/forgot_password.rhtml @@ -5,7 +5,7 @@ <% labelled_form_for :change_password, @change_password, :url => { :action => 'forgot_password' } do |f| %> <%= f.text_field :login, - :onchange => 'this.value = convToValidLogin( this.value )' %> + :onchange => 'this.value = convToValidUsername( this.value )' %> <%= f.text_field :email %> diff --git a/public/javascripts/application.js b/public/javascripts/application.js index 9c271ee..f2a16b9 100644 --- a/public/javascripts/application.js +++ b/public/javascripts/application.js @@ -29,7 +29,14 @@ function focus_first_field() { /* * * Convert a string to a valid login name * * */ function convToValidLogin( str ) { - return convToValidIdentifier(str, '') + if (str.indexOf('@') == -1) + return convToValidUsername(str); + else + return convToValidEmail(str); +} + +function convToValidUsername( str ) { + return convToValidIdentifier(str, ''); } /* * * Convert a string to a valid login name * * */ @@ -46,6 +53,18 @@ function convToValidIdentifier( str, sep ) { .replace( /[^-_a-z0-9.]+/g, sep ) } +function convToValidEmail( str ) { + return str.toLowerCase() + .replace( /á|à|ã|â/g, "a" ) + .replace( /é|ê/g, "e" ) + .replace( /í/g, "i" ) + .replace( /ó|ô|õ|ö/g, "o" ) + .replace( /ú|ũ|ü/g, "u" ) + .replace( /ñ/g, "n" ) + .replace( /ç/g, "c" ) + .replace( /[^@a-z0-9!#$%&'*+-/=?^_`{|}~.]+/g, '' ) +} + function updateUrlField(name_field, id) { url_field = $(id); old_url_value = url_field.value; diff --git a/test/unit/user_test.rb b/test/unit/user_test.rb index 69fe1a5..833c293 100644 --- a/test/unit/user_test.rb +++ b/test/unit/user_test.rb @@ -53,6 +53,8 @@ class UserTest < ActiveSupport::TestCase def test_should_authenticate_user assert_equal users(:johndoe), User.authenticate('johndoe', 'test') + assert_equal users(:johndoe), User.authenticate('johndoe@localhost.localdomain', 'test') + assert_equal nil, User.authenticate('wrongemail@localhost', 'test') end def test_should_authenticate_user_of_nondefault_environment -- libgit2 0.21.2