summaryrefslogtreecommitdiff
path: root/lib/api
diff options
context:
space:
mode:
authorWilliam George <code@williamgeorge.co.uk>2018-10-18 09:06:44 +0000
committerSean McGivern <sean@mcgivern.me.uk>2018-10-18 09:06:44 +0000
commit1b153d497b6948932b0de2f0088fe7192eb0994a (patch)
treea4f93a1c3a12314b54b2486d5b471c929d4e7003 /lib/api
parentc5d8e7fcee6bb15376902e8f1336f1ed368b9da8 (diff)
downloadgitlab-ce-1b153d497b6948932b0de2f0088fe7192eb0994a.tar.gz
Make getting a user by the username case insensitive
Diffstat (limited to 'lib/api')
-rw-r--r--lib/api/features.rb2
-rw-r--r--lib/api/helpers.rb8
-rw-r--r--lib/api/internal.rb4
-rw-r--r--lib/api/users.rb14
4 files changed, 10 insertions, 18 deletions
diff --git a/lib/api/features.rb b/lib/api/features.rb
index 6f2422af13a..1331248699f 100644
--- a/lib/api/features.rb
+++ b/lib/api/features.rb
@@ -20,7 +20,7 @@ module API
def gate_targets(params)
targets = []
targets << Feature.group(params[:feature_group]) if params[:feature_group]
- targets << User.find_by_username(params[:user]) if params[:user]
+ targets << UserFinder.new(params[:user]).find_by_username if params[:user]
targets
end
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index a7ba8066233..60bf977f0e4 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -96,15 +96,9 @@ module API
LabelsFinder.new(current_user, search_params).execute
end
- # rubocop: disable CodeReuse/ActiveRecord
def find_user(id)
- if id =~ /^\d+$/
- User.find_by(id: id)
- else
- User.find_by(username: id)
- end
+ UserFinder.new(id).find_by_id_or_username
end
- # rubocop: enable CodeReuse/ActiveRecord
# rubocop: disable CodeReuse/ActiveRecord
def find_project(id)
diff --git a/lib/api/internal.rb b/lib/api/internal.rb
index 6a264c4cc6d..4dd6b19e353 100644
--- a/lib/api/internal.rb
+++ b/lib/api/internal.rb
@@ -40,7 +40,7 @@ module API
elsif params[:user_id]
User.find_by(id: params[:user_id])
elsif params[:username]
- User.find_by_username(params[:username])
+ UserFinder.new(params[:username]).find_by_username
end
protocol = params[:protocol]
@@ -154,7 +154,7 @@ module API
elsif params[:user_id]
user = User.find_by(id: params[:user_id])
elsif params[:username]
- user = User.find_by(username: params[:username])
+ user = UserFinder.new(params[:username]).find_by_username
end
present user, with: Entities::UserSafe
diff --git a/lib/api/users.rb b/lib/api/users.rb
index 501c5cf1df3..47382b09207 100644
--- a/lib/api/users.rb
+++ b/lib/api/users.rb
@@ -155,7 +155,6 @@ module API
requires :username, type: String, desc: 'The username of the user'
use :optional_attributes
end
- # rubocop: disable CodeReuse/ActiveRecord
post do
authenticated_as_admin!
@@ -166,17 +165,16 @@ module API
present user, with: Entities::UserPublic, current_user: current_user
else
conflict!('Email has already been taken') if User
- .where(email: user.email)
- .count > 0
+ .by_any_email(user.email.downcase)
+ .any?
conflict!('Username has already been taken') if User
- .where(username: user.username)
- .count > 0
+ .by_username(user.username)
+ .any?
render_validation_error!(user)
end
end
- # rubocop: enable CodeReuse/ActiveRecord
desc 'Update a user. Available only for admins.' do
success Entities::UserPublic
@@ -198,11 +196,11 @@ module API
not_found!('User') unless user
conflict!('Email has already been taken') if params[:email] &&
- User.where(email: params[:email])
+ User.by_any_email(params[:email].downcase)
.where.not(id: user.id).count > 0
conflict!('Username has already been taken') if params[:username] &&
- User.where(username: params[:username])
+ User.by_username(params[:username])
.where.not(id: user.id).count > 0
user_params = declared_params(include_missing: false)