Increase username length limit from 30 to 60 (#2719).

git-svn-id: svn+ssh://rubyforge.org/var/svn/redmine/trunk@8778 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Jean-Philippe Lang 2012-02-05 11:50:53 +00:00
parent 1d4ef8964d
commit a00f04886f
6 changed files with 34 additions and 15 deletions

View File

@ -347,8 +347,8 @@ class MailHandler < ActionMailer::Base
@full_sanitizer ||= HTML::FullSanitizer.new @full_sanitizer ||= HTML::FullSanitizer.new
end end
def self.assign_string_attribute_with_limit(object, attribute, value) def self.assign_string_attribute_with_limit(object, attribute, value, limit=nil)
limit = object.class.columns_hash[attribute.to_s].limit || 255 limit ||= object.class.columns_hash[attribute.to_s].limit || 255
value = value.to_s.slice(0, limit) value = value.to_s.slice(0, limit)
object.send("#{attribute}=", value) object.send("#{attribute}=", value)
end end
@ -359,7 +359,7 @@ class MailHandler < ActionMailer::Base
# Truncating the email address would result in an invalid format # Truncating the email address would result in an invalid format
user.mail = email_address user.mail = email_address
assign_string_attribute_with_limit(user, 'login', email_address) assign_string_attribute_with_limit(user, 'login', email_address, User::LOGIN_LENGTH_LIMIT)
names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split names = fullname.blank? ? email_address.gsub(/@.*$/, '').split('.') : fullname.split
assign_string_attribute_with_limit(user, 'firstname', names.shift) assign_string_attribute_with_limit(user, 'firstname', names.shift)

View File

@ -72,15 +72,18 @@ class User < Principal
# Prevents unauthorized assignments # Prevents unauthorized assignments
attr_protected :login, :admin, :password, :password_confirmation, :hashed_password attr_protected :login, :admin, :password, :password_confirmation, :hashed_password
LOGIN_LENGTH_LIMIT = 60
MAIL_LENGTH_LIMIT = 60
validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) } validates_presence_of :login, :firstname, :lastname, :mail, :if => Proc.new { |user| !user.is_a?(AnonymousUser) }
validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false validates_uniqueness_of :login, :if => Proc.new { |user| !user.login.blank? }, :case_sensitive => false
validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false validates_uniqueness_of :mail, :if => Proc.new { |user| !user.mail.blank? }, :case_sensitive => false
# Login must contain lettres, numbers, underscores only # Login must contain lettres, numbers, underscores only
validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i validates_format_of :login, :with => /^[a-z0-9_\-@\.]*$/i
validates_length_of :login, :maximum => 30 validates_length_of :login, :maximum => LOGIN_LENGTH_LIMIT
validates_length_of :firstname, :lastname, :maximum => 30 validates_length_of :firstname, :lastname, :maximum => 30
validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true validates_format_of :mail, :with => /^([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})$/i, :allow_blank => true
validates_length_of :mail, :maximum => 60, :allow_nil => true validates_length_of :mail, :maximum => MAIL_LENGTH_LIMIT, :allow_nil => true
validates_confirmation_of :password, :allow_nil => true validates_confirmation_of :password, :allow_nil => true
validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true validates_inclusion_of :mail_notification, :in => MAIL_NOTIFICATION_OPTIONS.collect(&:first), :allow_blank => true
validate :validate_password_length validate :validate_password_length

View File

@ -0,0 +1,9 @@
class ChangeUsersLoginLimit < ActiveRecord::Migration
def self.up
change_column :users, :login, :string, :limit => nil, :default => '', :null => false
end
def self.down
change_column :users, :login, :string, :limit => 30, :default => '', :null => false
end
end

View File

@ -231,7 +231,7 @@ namespace :redmine do
u = User.find_by_login(username) u = User.find_by_login(username)
if !u if !u
# Create a new user if not found # Create a new user if not found
mail = username[0,limit_for(User, 'mail')] mail = username[0, User::MAIL_LENGTH_LIMIT]
if mail_attr = TracSessionAttribute.find_by_sid_and_name(username, 'email') if mail_attr = TracSessionAttribute.find_by_sid_and_name(username, 'email')
mail = mail_attr.value mail = mail_attr.value
end end
@ -249,7 +249,7 @@ namespace :redmine do
:firstname => fn[0, limit_for(User, 'firstname')], :firstname => fn[0, limit_for(User, 'firstname')],
:lastname => ln[0, limit_for(User, 'lastname')] :lastname => ln[0, limit_for(User, 'lastname')]
u.login = username[0,limit_for(User, 'login')].gsub(/[^a-z0-9_\-@\.]/i, '-') u.login = username[0, User::LOGIN_LENGTH_LIMIT].gsub(/[^a-z0-9_\-@\.]/i, '-')
u.password = 'trac' u.password = 'trac'
u.admin = true if TracPermission.find_by_username_and_action(username, 'admin') u.admin = true if TracPermission.find_by_username_and_action(username, 'admin')
# finally, a default user is used if the new user is not valid # finally, a default user is used if the new user is not valid

View File

@ -547,14 +547,13 @@ class MailHandlerTest < ActiveSupport::TestCase
['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'], ['jsmith@example.net', 'John Smith'] => ['jsmith@example.net', 'John', 'Smith'],
['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'], ['jsmith@example.net', 'John Paul Smith'] => ['jsmith@example.net', 'John', 'Paul Smith'],
['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'], ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsTheMaximumLength Smith'] => ['jsmith@example.net', 'AVeryLongFirstnameThatExceedsT', 'Smith'],
['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh'], ['jsmith@example.net', 'John AVeryLongLastnameThatExceedsTheMaximumLength'] => ['jsmith@example.net', 'John', 'AVeryLongLastnameThatExceedsTh']
['alongemailaddressthatexceedsloginlength@example.net', 'John Smith'] => ['alongemailaddressthatexceedslo', 'John', 'Smith']
} }
to_test.each do |attrs, expected| to_test.each do |attrs, expected|
user = MailHandler.new_user_from_attributes(attrs.first, attrs.last) user = MailHandler.new_user_from_attributes(attrs.first, attrs.last)
assert user.valid? assert user.valid?, user.errors.full_messages
assert_equal attrs.first, user.mail assert_equal attrs.first, user.mail
assert_equal expected[0], user.login assert_equal expected[0], user.login
assert_equal expected[1], user.firstname assert_equal expected[1], user.firstname
@ -571,12 +570,10 @@ class MailHandlerTest < ActiveSupport::TestCase
end end
def test_new_user_from_attributes_should_use_default_login_if_invalid def test_new_user_from_attributes_should_use_default_login_if_invalid
MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-1@example.net').save! user = MailHandler.new_user_from_attributes('foo+bar@example.net')
# another long address that would result in duplicate login
user = MailHandler.new_user_from_attributes('alongemailaddressthatexceedsloginlength-2@example.net')
assert user.valid? assert user.valid?
assert user.login =~ /^user[a-f0-9]+$/ assert user.login =~ /^user[a-f0-9]+$/
assert_equal 'foo+bar@example.net', user.mail
end end
private private

View File

@ -58,6 +58,16 @@ class UserTest < ActiveSupport::TestCase
u.errors[:mail].to_s u.errors[:mail].to_s
end end
def test_login_length_validation
user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")
user.login = "x" * (User::LOGIN_LENGTH_LIMIT+1)
assert !user.valid?
user.login = "x" * (User::LOGIN_LENGTH_LIMIT)
assert user.valid?
assert user.save
end
def test_create def test_create
user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo") user = User.new(:firstname => "new", :lastname => "user", :mail => "newuser@somenet.foo")