diff options
author | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-02-13 18:08:53 +0000 |
---|---|---|
committer | Dmitriy Zaporozhets <dzaporozhets@gitlab.com> | 2015-02-13 18:08:53 +0000 |
commit | 4d9756c1e8e8bb856dac608bc094c809966099ea (patch) | |
tree | 81c0cc20c24f8ef93a374660ef0097f94cae7d79 | |
parent | 6d523db7f609c02f7ccbbc2837a3c38f5ff6445a (diff) | |
parent | 161d15541a65ba167830f9a9bf5d181d0c5f4d77 (diff) | |
download | gitlab-ce-4d9756c1e8e8bb856dac608bc094c809966099ea.tar.gz |
Merge branch 'username-namespace-clash' into 'master'
Prevent autogenerated OAuth username to clash with existing namespace.
If the OAuth username already exists as a group rather than a user, automatic account creation would still fail.
See merge request !1511
-rw-r--r-- | app/models/namespace.rb | 4 | ||||
-rw-r--r-- | app/models/user.rb | 5 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 4 |
3 files changed, 9 insertions, 4 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index ba0b2b71cf9..2c7ed376265 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -44,6 +44,10 @@ class Namespace < ActiveRecord::Base scope :root, -> { where('type IS NULL') } + def self.by_path(path) + where('lower(path) = :value', value: path.downcase).first + end + def self.search(query) where("name LIKE :query OR path LIKE :query", query: "%#{query}%") end diff --git a/app/models/user.rb b/app/models/user.rb index d7f688ec138..a97678999bc 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -252,7 +252,7 @@ class User < ActiveRecord::Base counter = 0 base = username - while by_login(username).present? + while User.by_login(username).present? || Namespace.by_path(username).present? counter += 1 username = "#{base}#{counter}" end @@ -290,7 +290,8 @@ class User < ActiveRecord::Base def namespace_uniq namespace_name = self.username - if Namespace.find_by(path: namespace_name) + existing_namespace = Namespace.by_path(namespace_name) + if existing_namespace && existing_namespace != self.namespace self.errors.add :username, "already exists" end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 6102b2e30be..c015a1d2687 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -303,8 +303,8 @@ describe User do describe ".clean_username" do - let!(:user1) { create(:user, username: "johngitlab-etc") } - let!(:user2) { create(:user, username: "JohnGitLab-etc1") } + let!(:user) { create(:user, username: "johngitlab-etc") } + let!(:namespace) { create(:namespace, path: "JohnGitLab-etc1") } it "cleans a username and makes sure it's available" do expect(User.clean_username("-john+gitlab-ETC%.git@gmail.com")).to eq("johngitlab-ETC2") |