Commit 7a56075e authored by Izaak Alpert's avatar Izaak Alpert

Some of the requested updates, rebase on master

Change-Id: I305266fe9acbbb5136adeeb52e7e4e1d6629a30a
parent e86e8818
......@@ -190,6 +190,14 @@ class User < ActiveRecord::Base
def search query
where("name LIKE :query OR email LIKE :query OR username LIKE :query", query: "%#{query}%")
end
def by_username_or_id(name_or_id)
if (name_or_id.is_a?(Integer))
User.find_by_id(name_or_id)
else
User.find_by_username(name_or_id)
end
end
end
#
......
......@@ -12,25 +12,18 @@ module API
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?
begin
if (identifier.is_a?(Integer))
user = User.find_by_id(identifier)
else
user = User.find_by_username(identifier)
end
if user.nil?
not_found!("No user id or username for: #{identifier}")
end
@current_user = user
@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?
end
@current_user
end
def sudo_identifier()
identifier = params[SUDO_PARAM] == nil ? env[SUDO_HEADER] : params[SUDO_PARAM]
identifier ||= params[SUDO_PARAM] ||= env[SUDO_HEADER]
# Regex for integers
if (!!(identifier =~ /^[0-9]+$/))
identifier.to_i
else
......
......@@ -208,4 +208,14 @@ describe User do
user.can_create_group.should == false
end
end
describe 'by_username_or_id' do
let(:user1){create(:user, username: 'foo')}
it "should get the correct user" do
User.by_username_or_id(user1.id).should == user1
User.by_username_or_id('foo').should == user1
User.by_username_or_id(-1).should be_nil
User.by_username_or_id('bar').should be_nil
end
end
end
require 'spec_helper'
describe Gitlab::API do
include Gitlab::APIHelpers
describe API do
include API::APIHelpers
include ApiHelpers
let(:user) { create(:user) }
let(:admin) { create(:admin) }
......@@ -13,27 +13,27 @@ describe Gitlab::API do
def set_env(token_usr, identifier)
clear_env
clear_param
env[Gitlab::APIHelpers::PRIVATE_TOKEN_HEADER] = token_usr.private_token
env[Gitlab::APIHelpers::SUDO_HEADER] = identifier
env[API::APIHelpers::PRIVATE_TOKEN_HEADER] = token_usr.private_token
env[API::APIHelpers::SUDO_HEADER] = identifier
end
def set_param(token_usr, identifier)
clear_env
clear_param
params[Gitlab::APIHelpers::PRIVATE_TOKEN_PARAM] = token_usr.private_token
params[Gitlab::APIHelpers::SUDO_PARAM] = identifier
params[API::APIHelpers::PRIVATE_TOKEN_PARAM] = token_usr.private_token
params[API::APIHelpers::SUDO_PARAM] = identifier
end
def clear_env
env.delete(Gitlab::APIHelpers::PRIVATE_TOKEN_HEADER)
env.delete(Gitlab::APIHelpers::SUDO_HEADER)
env.delete(API::APIHelpers::PRIVATE_TOKEN_HEADER)
env.delete(API::APIHelpers::SUDO_HEADER)
end
def clear_param
params.delete(Gitlab::APIHelpers::PRIVATE_TOKEN_PARAM)
params.delete(Gitlab::APIHelpers::SUDO_PARAM)
params.delete(API::APIHelpers::PRIVATE_TOKEN_PARAM)
params.delete(API::APIHelpers::SUDO_PARAM)
end
def error!(message, status)
......@@ -42,10 +42,10 @@ describe Gitlab::API do
describe ".current_user" do
it "should leave user as is when sudo not specified" do
env[Gitlab::APIHelpers::PRIVATE_TOKEN_HEADER] = user.private_token
env[API::APIHelpers::PRIVATE_TOKEN_HEADER] = user.private_token
current_user.should == user
clear_env
params[Gitlab::APIHelpers::PRIVATE_TOKEN_PARAM] = user.private_token
params[API::APIHelpers::PRIVATE_TOKEN_PARAM] = user.private_token
current_user.should == user
end
......
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