BigW Consortium Gitlab

Commit 7a56075e by Izaak Alpert

Some of the requested updates, rebase on master

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