Commit 79b5d8261ae8f3a72742144b66952c9580c09c1c

Authored by Braulio Bhavamitra
1 parent 82ae16fc

Login with login OR email

app/models/user.rb
... ... @@ -119,7 +119,8 @@ class User < ActiveRecord::Base
119 119 # Authenticates a user by their login name and unencrypted password. Returns the user or nil.
120 120 def self.authenticate(login, password, environment = nil)
121 121 environment ||= Environment.default
122   - u = first :conditions => ['login = ? AND environment_id = ? AND activated_at IS NOT NULL', login, environment.id] # need to get the salt
  122 + u = self.first :conditions => ['(login = ? OR email = ?) AND environment_id = ? AND activated_at IS NOT NULL',
  123 + login, login, environment.id] # need to get the salt
123 124 u && u.authenticated?(password) ? u : nil
124 125 end
125 126  
... ...
app/views/account/_signup_form.rhtml
... ... @@ -13,7 +13,8 @@
13 13 <span id="signup-domain"><%= environment.default_hostname %>/</span>
14 14 <div id='signup-login'>
15 15 <div id='signup-login-field' class='formfield'>
16   - <%= required text_field(:user, :login, :id => 'user_login', :onchange => 'this.value = convToValidLogin(this.value);') %>
  16 + <%= required text_field(:user, :login, :id => 'user_login',
  17 + :onchange => 'this.value = convToValidUsername(this.value);') %>
17 18 <div id='url-check'><p>&nbsp;</p></div>
18 19 </div>
19 20 <%= 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') %>
... ...
app/views/account/forgot_password.rhtml
... ... @@ -5,7 +5,7 @@
5 5 <% labelled_form_for :change_password, @change_password, :url => { :action => 'forgot_password' } do |f| %>
6 6  
7 7 <%= f.text_field :login,
8   - :onchange => 'this.value = convToValidLogin( this.value )' %>
  8 + :onchange => 'this.value = convToValidUsername( this.value )' %>
9 9  
10 10 <%= f.text_field :email %>
11 11  
... ...
public/javascripts/application.js
... ... @@ -29,7 +29,14 @@ function focus_first_field() {
29 29  
30 30 /* * * Convert a string to a valid login name * * */
31 31 function convToValidLogin( str ) {
32   - return convToValidIdentifier(str, '')
  32 + if (str.indexOf('@') == -1)
  33 + return convToValidUsername(str);
  34 + else
  35 + return convToValidEmail(str);
  36 +}
  37 +
  38 +function convToValidUsername( str ) {
  39 + return convToValidIdentifier(str, '');
33 40 }
34 41  
35 42 /* * * Convert a string to a valid login name * * */
... ... @@ -46,6 +53,18 @@ function convToValidIdentifier( str, sep ) {
46 53 .replace( /[^-_a-z0-9.]+/g, sep )
47 54 }
48 55  
  56 +function convToValidEmail( str ) {
  57 + return str.toLowerCase()
  58 + .replace( /á|à|ã|â/g, "a" )
  59 + .replace( /é|ê/g, "e" )
  60 + .replace( /í/g, "i" )
  61 + .replace( /ó|ô|õ|ö/g, "o" )
  62 + .replace( /ú|ũ|ü/g, "u" )
  63 + .replace( /ñ/g, "n" )
  64 + .replace( /ç/g, "c" )
  65 + .replace( /[^@a-z0-9!#$%&'*+-/=?^_`{|}~.]+/g, '' )
  66 +}
  67 +
49 68 function updateUrlField(name_field, id) {
50 69 url_field = $(id);
51 70 old_url_value = url_field.value;
... ...
test/unit/user_test.rb
... ... @@ -53,6 +53,8 @@ class UserTest &lt; ActiveSupport::TestCase
53 53  
54 54 def test_should_authenticate_user
55 55 assert_equal users(:johndoe), User.authenticate('johndoe', 'test')
  56 + assert_equal users(:johndoe), User.authenticate('johndoe@localhost.localdomain', 'test')
  57 + assert_equal nil, User.authenticate('wrongemail@localhost', 'test')
56 58 end
57 59  
58 60 def test_should_authenticate_user_of_nondefault_environment
... ...