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
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Léo-Paul Géneau
gitlab-ce
Commits
28ef8cc5
Commit
28ef8cc5
authored
Feb 06, 2018
by
Dmitriy Zaporozhets
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add sorting options for /users API (admin only)
Signed-off-by:
Dmitriy Zaporozhets
<
dmitriy.zaporozhets@gmail.com
>
parent
1a430836
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
46 additions
and
1 deletion
+46
-1
changelogs/unreleased/42669-allow-order_by-users-in-gitlab-api.yml
...s/unreleased/42669-allow-order_by-users-in-gitlab-api.yml
+5
-0
doc/api/users.md
doc/api/users.md
+5
-0
lib/api/users.rb
lib/api/users.rb
+18
-1
spec/requests/api/users_spec.rb
spec/requests/api/users_spec.rb
+18
-0
No files found.
changelogs/unreleased/42669-allow-order_by-users-in-gitlab-api.yml
0 → 100644
View file @
28ef8cc5
---
title
:
Add sorting options for /users API (admin only)
merge_request
:
16945
author
:
type
:
added
doc/api/users.md
View file @
28ef8cc5
...
...
@@ -51,6 +51,11 @@ GET /users?blocked=true
GET /users
```
| Attribute | Type | Required | Description |
| --------- | ---- | -------- | ----------- |
|
`order_by`
| string | no | Return projects ordered by
`id`
,
`name`
,
`username`
,
`created_at`
, or
`updated_at`
fields. Default is
`id`
|
|
`sort`
| string | no | Return projects sorted in
`asc`
or
`desc`
order. Default is
`desc`
|
```
json
[
{
...
...
lib/api/users.rb
View file @
28ef8cc5
...
...
@@ -18,6 +18,14 @@ module API
User
.
find_by
(
id:
id
)
||
not_found!
(
'User'
)
end
def
reorder_users
(
users
)
if
params
[
:order_by
]
&&
params
[
:sort
]
users
.
reorder
(
params
[
:order_by
]
=>
params
[
:sort
])
else
users
end
end
params
:optional_attributes
do
optional
:skype
,
type:
String
,
desc:
'The Skype username'
optional
:linkedin
,
type:
String
,
desc:
'The LinkedIn username'
...
...
@@ -35,6 +43,13 @@ module API
optional
:avatar
,
type:
File
,
desc:
'Avatar image for user'
all_or_none_of
:extern_uid
,
:provider
end
params
:sort_params
do
optional
:order_by
,
type:
String
,
values:
%w[id name username created_at updated_at]
,
default:
'id'
,
desc:
'Return users ordered by a field'
optional
:sort
,
type:
String
,
values:
%w[asc desc]
,
default:
'desc'
,
desc:
'Return users sorted in ascending and descending order'
end
end
desc
'Get the list of users'
do
...
...
@@ -53,16 +68,18 @@ module API
optional
:created_before
,
type:
DateTime
,
desc:
'Return users created before the specified time'
all_or_none_of
:extern_uid
,
:provider
use
:sort_params
use
:pagination
end
get
do
authenticated_as_admin!
if
params
[
:external
].
present?
||
(
params
[
:extern_uid
].
present?
&&
params
[
:provider
].
present?
)
unless
current_user
&
.
admin?
params
.
except!
(
:created_after
,
:created_before
)
params
.
except!
(
:created_after
,
:created_before
,
:order_by
,
:sort
)
end
users
=
UsersFinder
.
new
(
current_user
,
params
).
execute
users
=
reorder_users
(
users
)
authorized
=
can?
(
current_user
,
:read_users_list
)
...
...
spec/requests/api/users_spec.rb
View file @
28ef8cc5
...
...
@@ -199,6 +199,24 @@ describe API::Users do
expect
(
json_response
.
size
).
to
eq
(
1
)
expect
(
json_response
.
first
[
'username'
]).
to
eq
(
user
.
username
)
end
it
'returns the correct order when sorted by id'
do
admin
user
get
api
(
'/users'
,
admin
),
{
order_by:
'id'
,
sort:
'asc'
}
expect
(
response
).
to
match_response_schema
(
'public_api/v4/user/admins'
)
expect
(
json_response
.
size
).
to
eq
(
2
)
expect
(
json_response
.
first
[
'id'
]).
to
eq
(
admin
.
id
)
expect
(
json_response
.
last
[
'id'
]).
to
eq
(
user
.
id
)
end
it
'returns 400 when provided incorrect sort params'
do
get
api
(
'/users'
,
admin
),
{
order_by:
'magic'
,
sort:
'asc'
}
expect
(
response
).
to
have_gitlab_http_status
(
400
)
end
end
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