diff options
author | Douwe Maan <douwe@gitlab.com> | 2015-03-24 14:53:30 +0100 |
---|---|---|
committer | Douwe Maan <douwe@gitlab.com> | 2015-03-27 11:09:29 +0100 |
commit | 3f7531d6f2974ea75ab8e67bc93049f674ddb672 (patch) | |
tree | 5340386a05e4970e7064392741c18f053157fccc | |
parent | 28592ae46767443dc0f3723bd4f05f360bab8f41 (diff) | |
download | gitlab-ce-3f7531d6f2974ea75ab8e67bc93049f674ddb672.tar.gz |
Move User.cleanup_username to Namespace.cleanup_path.
-rw-r--r-- | app/models/namespace.rb | 43 | ||||
-rw-r--r-- | app/models/user.rb | 16 | ||||
-rw-r--r-- | lib/gitlab/oauth/user.rb | 2 | ||||
-rw-r--r-- | spec/models/namespace_spec.rb | 10 | ||||
-rw-r--r-- | spec/models/user_spec.rb | 10 |
5 files changed, 42 insertions, 39 deletions
diff --git a/app/models/namespace.rb b/app/models/namespace.rb index 35280889a86..0c0252ed8ee 100644 --- a/app/models/namespace.rb +++ b/app/models/namespace.rb @@ -44,21 +44,40 @@ class Namespace < ActiveRecord::Base scope :root, -> { where('type IS NULL') } - def self.by_path(path) - where('lower(path) = :value', value: path.downcase).first - end + class << self + def by_path(path) + where('lower(path) = :value', value: path.downcase).first + end - # Case insensetive search for namespace by path or name - def self.find_by_path_or_name(path) - find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) - end + # Case insensetive search for namespace by path or name + def find_by_path_or_name(path) + find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase) + end - def self.search(query) - where("name LIKE :query OR path LIKE :query", query: "%#{query}%") - end + def search(query) + where("name LIKE :query OR path LIKE :query", query: "%#{query}%") + end - def self.global_id - 'GLN' + def global_id + 'GLN' + end + + def clean_path(path) + path.gsub!(/@.*\z/, "") + path.gsub!(/\.git\z/, "") + path.gsub!(/\A-/, "") + path.gsub!(/\z./, "") + path.gsub!(/[^a-zA-Z0-9_\-\.]/, "") + + counter = 0 + base = path + while Namespace.by_path(path).present? + counter += 1 + path = "#{base}#{counter}" + end + + path + end end def to_param diff --git a/app/models/user.rb b/app/models/user.rb index 979150b4d68..3f3a8394d1f 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -229,22 +229,6 @@ class User < ActiveRecord::Base def build_user(attrs = {}) User.new(attrs) end - - def clean_username(username) - username.gsub!(/@.*\z/, "") - username.gsub!(/\.git\z/, "") - username.gsub!(/\A-/, "") - username.gsub!(/[^a-zA-Z0-9_\-\.]/, "") - - counter = 0 - base = username - while User.by_login(username).present? || Namespace.by_path(username).present? - counter += 1 - username = "#{base}#{counter}" - end - - username - end end # diff --git a/lib/gitlab/oauth/user.rb b/lib/gitlab/oauth/user.rb index c023d275703..2f5c217d764 100644 --- a/lib/gitlab/oauth/user.rb +++ b/lib/gitlab/oauth/user.rb @@ -86,7 +86,7 @@ module Gitlab def user_attributes { name: auth_hash.name, - username: ::User.clean_username(auth_hash.username), + username: ::Namespace.clean_path(auth_hash.username), email: auth_hash.email, password: auth_hash.password, password_confirmation: auth_hash.password, diff --git a/spec/models/namespace_spec.rb b/spec/models/namespace_spec.rb index ed6845c82cc..48a3ab9c5a2 100644 --- a/spec/models/namespace_spec.rb +++ b/spec/models/namespace_spec.rb @@ -85,4 +85,14 @@ describe Namespace do it { expect(Namespace.find_by_path_or_name('WOW')).to eq(@namespace) } it { expect(Namespace.find_by_path_or_name('unknown')).to eq(nil) } end + + describe ".clean_path" do + + let!(:user) { create(:user, username: "johngitlab-etc") } + let!(:namespace) { create(:namespace, path: "JohnGitLab-etc1") } + + it "cleans the path and makes sure it's available" do + expect(Namespace.clean_path("-john+gitlab-ETC%.git@gmail.com")).to eq("johngitlab-ETC2") + end + end end diff --git a/spec/models/user_spec.rb b/spec/models/user_spec.rb index 10e90cae143..24384e8bf22 100644 --- a/spec/models/user_spec.rb +++ b/spec/models/user_spec.rb @@ -307,16 +307,6 @@ describe User do end end - describe ".clean_username" do - - 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") - end - end - describe 'all_ssh_keys' do it { is_expected.to have_many(:keys).dependent(:destroy) } |