Commit 12420a22 authored by Nihad Abbasov's avatar Nihad Abbasov

refactor by_username_or_id

parent e09cc195
...@@ -199,11 +199,7 @@ class User < ActiveRecord::Base ...@@ -199,11 +199,7 @@ class User < ActiveRecord::Base
end end
def by_username_or_id(name_or_id) def by_username_or_id(name_or_id)
if (name_or_id.is_a?(Integer)) where('username = ? OR id = ?', name_or_id, name_or_id).first
User.find_by_id(name_or_id)
else
User.find_by_username(name_or_id)
end
end end
def build_user(attrs = {}, options= {}) def build_user(attrs = {}, options= {})
......
...@@ -11,11 +11,7 @@ module API ...@@ -11,11 +11,7 @@ module API
# If the sudo is the current user do nothing # If the sudo is the current user do nothing
if (identifier && !(@current_user.id == identifier || @current_user.username == identifier)) if (identifier && !(@current_user.id == identifier || @current_user.username == identifier))
render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin? render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin?
begin @current_user = User.by_username_or_id(identifier)
@current_user = User.by_username_or_id(identifier)
rescue => ex
not_found!("No user id or username for: #{identifier}")
end
not_found!("No user id or username for: #{identifier}") if @current_user.nil? not_found!("No user id or username for: #{identifier}") if @current_user.nil?
end end
@current_user @current_user
......
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