Commit 3f7531d6 authored by Douwe Maan's avatar Douwe Maan

Move User.cleanup_username to Namespace.cleanup_path.

parent 28592ae4
......@@ -44,23 +44,42 @@ class Namespace < ActiveRecord::Base
scope :root, -> { where('type IS NULL') }
def self.by_path(path)
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)
def find_by_path_or_name(path)
find_by("lower(path) = :path OR lower(name) = :path", path: path.downcase)
end
def self.search(query)
def search(query)
where("name LIKE :query OR path LIKE :query", query: "%#{query}%")
end
def self.global_id
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
path
end
......
......@@ -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
#
......
......@@ -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,
......
......@@ -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
......@@ -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) }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment