Commit 0dbb8ea4 authored by Ash McKenzie's avatar Ash McKenzie

Merge branch '29426-add-api-endpoint-to-get-users-without-projects' into 'master'

Add API endpoint to get users without projects

Closes #29426

See merge request gitlab-org/gitlab!29347
parents c5802dcf d64ec096
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
# active: boolean # active: boolean
# blocked: boolean # blocked: boolean
# external: boolean # external: boolean
# without_projects: boolean
# #
class UsersFinder class UsersFinder
include CreatedAtFilter include CreatedAtFilter
...@@ -36,6 +37,7 @@ class UsersFinder ...@@ -36,6 +37,7 @@ class UsersFinder
users = by_external(users) users = by_external(users)
users = by_2fa(users) users = by_2fa(users)
users = by_created_at(users) users = by_created_at(users)
users = by_without_projects(users)
users = by_custom_attributes(users) users = by_custom_attributes(users)
users users
...@@ -94,6 +96,12 @@ class UsersFinder ...@@ -94,6 +96,12 @@ class UsersFinder
users users
end end
end end
def by_without_projects(users)
return users unless params[:without_projects]
users.without_projects
end
end end
UsersFinder.prepend_if_ee('EE::UsersFinder') UsersFinder.prepend_if_ee('EE::UsersFinder')
---
title: Add API endpoint to get users without projects
merge_request: 29347
author:
type: added
...@@ -75,6 +75,7 @@ GET /users ...@@ -75,6 +75,7 @@ GET /users
| `order_by` | string | no | Return users ordered by `id`, `name`, `username`, `created_at`, or `updated_at` fields. Default is `id` | | `order_by` | string | no | Return users ordered by `id`, `name`, `username`, `created_at`, or `updated_at` fields. Default is `id` |
| `sort` | string | no | Return users sorted in `asc` or `desc` order. Default is `desc` | | `sort` | string | no | Return users sorted in `asc` or `desc` order. Default is `desc` |
| `two_factor` | string | no | Filter users by Two-factor authentication. Filter values are `enabled` or `disabled`. By default it returns all users | | `two_factor` | string | no | Filter users by Two-factor authentication. Filter values are `enabled` or `disabled`. By default it returns all users |
| `without_projects` | boolean | no | Filter users without projects. Default is `false` |
```json ```json
[ [
...@@ -207,6 +208,8 @@ You can search users by creation date time range with: ...@@ -207,6 +208,8 @@ You can search users by creation date time range with:
GET /users?created_before=2001-01-02T00:00:00.060Z&created_after=1999-01-02T00:00:00.060 GET /users?created_before=2001-01-02T00:00:00.060Z&created_after=1999-01-02T00:00:00.060
``` ```
You can search for users without projects with: `/users?without_projects=true`
You can filter by [custom attributes](custom_attributes.md) with: You can filter by [custom attributes](custom_attributes.md) with:
```plaintext ```plaintext
......
...@@ -82,6 +82,7 @@ module API ...@@ -82,6 +82,7 @@ module API
optional :blocked, type: Boolean, default: false, desc: 'Filters only blocked users' optional :blocked, type: Boolean, default: false, desc: 'Filters only blocked users'
optional :created_after, type: DateTime, desc: 'Return users created after the specified time' optional :created_after, type: DateTime, desc: 'Return users created after the specified time'
optional :created_before, type: DateTime, desc: 'Return users created before the specified time' optional :created_before, type: DateTime, desc: 'Return users created before the specified time'
optional :without_projects, type: Boolean, default: false, desc: 'Filters only users without projects'
all_or_none_of :extern_uid, :provider all_or_none_of :extern_uid, :provider
use :sort_params use :sort_params
...@@ -94,7 +95,7 @@ module API ...@@ -94,7 +95,7 @@ module API
authenticated_as_admin! if params[:external].present? || (params[:extern_uid].present? && params[:provider].present?) authenticated_as_admin! if params[:external].present? || (params[:extern_uid].present? && params[:provider].present?)
unless current_user&.admin? unless current_user&.admin?
params.except!(:created_after, :created_before, :order_by, :sort, :two_factor) params.except!(:created_after, :created_before, :order_by, :sort, :two_factor, :without_projects)
end end
users = UsersFinder.new(current_user, params).execute users = UsersFinder.new(current_user, params).execute
......
...@@ -280,6 +280,18 @@ describe API::Users, :do_not_mock_admin_mode do ...@@ -280,6 +280,18 @@ describe API::Users, :do_not_mock_admin_mode do
expect(json_response.first['id']).to eq(user_with_2fa.id) expect(json_response.first['id']).to eq(user_with_2fa.id)
end end
it "returns users without projects" do
user_without_projects = create(:user)
create(:project, namespace: user.namespace)
create(:project, namespace: admin.namespace)
get api('/users', admin), params: { without_projects: true }
expect(response).to match_response_schema('public_api/v4/user/admins')
expect(json_response.size).to eq(1)
expect(json_response.first['id']).to eq(user_without_projects.id)
end
it 'returns 400 when provided incorrect sort params' do it 'returns 400 when provided incorrect sort params' do
get api('/users', admin), params: { order_by: 'magic', sort: 'asc' } get api('/users', admin), params: { order_by: 'magic', sort: 'asc' }
......
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