From 5f93b0e3da9f84f44c5f00af5d3e48c277d68d5b Mon Sep 17 00:00:00 2001 From: Douwe Maan Date: Tue, 24 Mar 2015 15:21:37 +0100 Subject: Don't allow username to end in period. --- ...24133047_remove_periods_at_ends_of_usernames.rb | 39 ++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb (limited to 'db') diff --git a/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb new file mode 100644 index 00000000000..36e3faa53be --- /dev/null +++ b/db/migrate/20150324133047_remove_periods_at_ends_of_usernames.rb @@ -0,0 +1,39 @@ +class RemovePeriodsAtEndsOfUsernames < ActiveRecord::Migration + class Namespace < ActiveRecord::Base + class << self + def by_path(path) + where('lower(path) = :value', value: path.downcase).first + 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 + end + + def up + select_all("SELECT id, username FROM users WHERE username LIKE '%.'").each do |user| + username = quote_string(Namespace.clean_path(user["username"])) + execute "UPDATE users SET username = '#{username}' WHERE id = #{user["id"]}" + execute "UPDATE namespaces SET path = '#{username}', name = '#{username}' WHERE type = NULL AND owner_id = #{user["id"]}" + end + + select_all("SELECT id, path FROM namespaces WHERE type = 'Group' AND path LIKE '%.'").each do |group| + path = quote_string(Namespace.clean_path(group["path"])) + execute "UPDATE namespaces SET path = '#{path}' WHERE id = #{group["id"]}" + end + end +end -- cgit v1.2.3