Commit 12420a2256168a122b3a1f5a2c974df52fe802ec
1 parent
e09cc195
Exists in
master
and in
4 other branches
refactor by_username_or_id
Showing
2 changed files
with
2 additions
and
10 deletions
Show diff stats
app/models/user.rb
@@ -199,11 +199,7 @@ class User < ActiveRecord::Base | @@ -199,11 +199,7 @@ class User < ActiveRecord::Base | ||
199 | end | 199 | end |
200 | 200 | ||
201 | def by_username_or_id(name_or_id) | 201 | def by_username_or_id(name_or_id) |
202 | - if (name_or_id.is_a?(Integer)) | ||
203 | - User.find_by_id(name_or_id) | ||
204 | - else | ||
205 | - User.find_by_username(name_or_id) | ||
206 | - end | 202 | + where('username = ? OR id = ?', name_or_id, name_or_id).first |
207 | end | 203 | end |
208 | 204 | ||
209 | def build_user(attrs = {}, options= {}) | 205 | def build_user(attrs = {}, options= {}) |
lib/api/helpers.rb
@@ -11,11 +11,7 @@ module API | @@ -11,11 +11,7 @@ module API | ||
11 | # If the sudo is the current user do nothing | 11 | # If the sudo is the current user do nothing |
12 | if (identifier && !(@current_user.id == identifier || @current_user.username == identifier)) | 12 | if (identifier && !(@current_user.id == identifier || @current_user.username == identifier)) |
13 | render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin? | 13 | render_api_error!('403 Forbidden: Must be admin to use sudo', 403) unless @current_user.is_admin? |
14 | - begin | ||
15 | - @current_user = User.by_username_or_id(identifier) | ||
16 | - rescue => ex | ||
17 | - not_found!("No user id or username for: #{identifier}") | ||
18 | - end | 14 | + @current_user = User.by_username_or_id(identifier) |
19 | not_found!("No user id or username for: #{identifier}") if @current_user.nil? | 15 | not_found!("No user id or username for: #{identifier}") if @current_user.nil? |
20 | end | 16 | end |
21 | @current_user | 17 | @current_user |