Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
gitlab-ce
Commits
7a56075e
Commit
7a56075e
authored
Jun 28, 2013
by
Izaak Alpert
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Some of the requested updates, rebase on master
Change-Id: I305266fe9acbbb5136adeeb52e7e4e1d6629a30a
parent
e86e8818
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
38 additions
and
27 deletions
+38
-27
app/models/user.rb
app/models/user.rb
+8
-0
lib/api/helpers.rb
lib/api/helpers.rb
+8
-15
spec/models/user_spec.rb
spec/models/user_spec.rb
+10
-0
spec/requests/api/api_helpers_spec.rb
spec/requests/api/api_helpers_spec.rb
+12
-12
No files found.
app/models/user.rb
View file @
7a56075e
...
...
@@ -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
#
...
...
lib/api/helpers.rb
View file @
7a56075e
...
...
@@ -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
...
...
@@ -129,10 +122,10 @@ module API
def
abilities
@abilities
||=
begin
abilities
=
Six
.
new
abilities
<<
Ability
abilities
end
abilities
=
Six
.
new
abilities
<<
Ability
abilities
end
end
end
end
spec/models/user_spec.rb
View file @
7a56075e
...
...
@@ -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
spec/requests/api/api_helpers_spec.rb
View file @
7a56075e
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
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment