Commit 95c6c218 authored by Peter Leitzen's avatar Peter Leitzen

Merge branch 'feature/get-user-api-namespace-id' into 'master'

Expose `namespace_id` field for user APIs, when accessed by admin

See merge request gitlab-org/gitlab!82045
parents 17f33fda 70ba12d1
...@@ -99,6 +99,8 @@ GET /users?exclude_external=true ...@@ -99,6 +99,8 @@ GET /users?exclude_external=true
### For admins ### For admins
> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10.
```plaintext ```plaintext
GET /users GET /users
``` ```
...@@ -151,7 +153,8 @@ GET /users ...@@ -151,7 +153,8 @@ GET /users
"external": false, "external": false,
"private_profile": false, "private_profile": false,
"current_sign_in_ip": "196.165.1.102", "current_sign_in_ip": "196.165.1.102",
"last_sign_in_ip": "172.127.2.22" "last_sign_in_ip": "172.127.2.22",
"namespace_id": 1
}, },
{ {
"id": 2, "id": 2,
...@@ -185,7 +188,8 @@ GET /users ...@@ -185,7 +188,8 @@ GET /users
"external": false, "external": false,
"private_profile": false, "private_profile": false,
"current_sign_in_ip": "10.165.1.102", "current_sign_in_ip": "10.165.1.102",
"last_sign_in_ip": "172.127.2.22" "last_sign_in_ip": "172.127.2.22",
"namespace_id": 2
} }
] ]
``` ```
...@@ -300,6 +304,8 @@ Parameters: ...@@ -300,6 +304,8 @@ Parameters:
### For admin ### For admin
> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10.
```plaintext ```plaintext
GET /users/:id GET /users/:id
``` ```
...@@ -355,7 +361,8 @@ Example Responses: ...@@ -355,7 +361,8 @@ Example Responses:
"last_sign_in_ip": "172.127.2.22", "last_sign_in_ip": "172.127.2.22",
"plan": "gold", "plan": "gold",
"trial": true, "trial": true,
"sign_in_count": 1337 "sign_in_count": 1337,
"namespace_id": 1
} }
``` ```
...@@ -404,6 +411,8 @@ GET /users/:id?with_custom_attributes=true ...@@ -404,6 +411,8 @@ GET /users/:id?with_custom_attributes=true
## User creation ## User creation
> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10.
Creates a new user. Note only administrators can create new Creates a new user. Note only administrators can create new
users. Either `password`, `reset_password`, or `force_random_password` users. Either `password`, `reset_password`, or `force_random_password`
must be specified. If `reset_password` and `force_random_password` are must be specified. If `reset_password` and `force_random_password` are
...@@ -459,6 +468,8 @@ Parameters: ...@@ -459,6 +468,8 @@ Parameters:
## User modification ## User modification
> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10.
Modifies an existing user. Only administrators can change attributes of a user. Modifies an existing user. Only administrators can change attributes of a user.
```plaintext ```plaintext
...@@ -583,6 +594,8 @@ GET /user ...@@ -583,6 +594,8 @@ GET /user
## List current user (for admins) ## List current user (for admins)
> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10.
```plaintext ```plaintext
GET /user GET /user
``` ```
...@@ -632,7 +645,8 @@ Parameters: ...@@ -632,7 +645,8 @@ Parameters:
"private_profile": false, "private_profile": false,
"commit_email": "john-codes@example.com", "commit_email": "john-codes@example.com",
"current_sign_in_ip": "196.165.1.102", "current_sign_in_ip": "196.165.1.102",
"last_sign_in_ip": "172.127.2.22" "last_sign_in_ip": "172.127.2.22",
"namespace_id": 1
} }
``` ```
......
...@@ -5,6 +5,7 @@ module API ...@@ -5,6 +5,7 @@ module API
class UserWithAdmin < UserPublic class UserWithAdmin < UserPublic
expose :admin?, as: :is_admin expose :admin?, as: :is_admin
expose :note expose :note
expose :namespace_id
end end
end end
end end
......
...@@ -120,8 +120,11 @@ module API ...@@ -120,8 +120,11 @@ module API
users = reorder_users(users) users = reorder_users(users)
entity = current_user&.admin? ? Entities::UserWithAdmin : Entities::UserBasic entity = current_user&.admin? ? Entities::UserWithAdmin : Entities::UserBasic
users = users.preload(:identities, :u2f_registrations) if entity == Entities::UserWithAdmin
users = users.preload(:identities, :webauthn_registrations) if entity == Entities::UserWithAdmin if entity == Entities::UserWithAdmin
users = users.preload(:identities, :u2f_registrations, :webauthn_registrations, :namespace)
end
users, options = with_custom_attributes(users, { with: entity, current_user: current_user }) users, options = with_custom_attributes(users, { with: entity, current_user: current_user })
users = users.preload(:user_detail) users = users.preload(:user_detail)
......
...@@ -26,7 +26,8 @@ ...@@ -26,7 +26,8 @@
"can_create_group", "can_create_group",
"can_create_project", "can_create_project",
"two_factor_enabled", "two_factor_enabled",
"external" "external",
"namespace_id"
], ],
"properties": { "properties": {
"$ref": "full.json" "$ref": "full.json"
......
...@@ -83,19 +83,21 @@ RSpec.describe API::Users do ...@@ -83,19 +83,21 @@ RSpec.describe API::Users do
describe 'GET /users/' do describe 'GET /users/' do
context 'when unauthenticated' do context 'when unauthenticated' do
it "does not contain the note of users" do it "does not contain certain fields" do
get api("/users"), params: { username: user.username } get api("/users"), params: { username: user.username }
expect(json_response.first).not_to have_key('note') expect(json_response.first).not_to have_key('note')
expect(json_response.first).not_to have_key('namespace_id')
end end
end end
context 'when authenticated' do context 'when authenticated' do
context 'as a regular user' do context 'as a regular user' do
it 'does not contain the note of users' do it 'does not contain certain fields' do
get api("/users", user), params: { username: user.username } get api("/users", user), params: { username: user.username }
expect(json_response.first).not_to have_key('note') expect(json_response.first).not_to have_key('note')
expect(json_response.first).not_to have_key('namespace_id')
end end
end end
...@@ -154,6 +156,7 @@ RSpec.describe API::Users do ...@@ -154,6 +156,7 @@ RSpec.describe API::Users do
get api("/user", user) get api("/user", user)
expect(json_response).not_to have_key('note') expect(json_response).not_to have_key('note')
expect(json_response).not_to have_key('namespace_id')
end end
end end
end end
...@@ -384,6 +387,15 @@ RSpec.describe API::Users do ...@@ -384,6 +387,15 @@ RSpec.describe API::Users do
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
end end
it "users contain the `namespace_id` field" do
get api("/users", admin)
expect(response).to have_gitlab_http_status(:success)
expect(response).to match_response_schema('public_api/v4/user/admins')
expect(json_response.size).to eq(2)
expect(json_response.map { |u| u['namespace_id'] }).to include(user.namespace_id, admin.namespace_id)
end
it "returns an array of external users" do it "returns an array of external users" do
create(:user, external: true) create(:user, external: true)
...@@ -697,6 +709,14 @@ RSpec.describe API::Users do ...@@ -697,6 +709,14 @@ RSpec.describe API::Users do
expect(json_response['highest_role']).to be(0) expect(json_response['highest_role']).to be(0)
end end
it 'includes the `namespace_id` field' do
get api("/users/#{user.id}", admin)
expect(response).to have_gitlab_http_status(:success)
expect(response).to match_response_schema('public_api/v4/user/admin')
expect(json_response['namespace_id']).to eq(user.namespace_id)
end
if Gitlab.ee? if Gitlab.ee?
it 'does not include values for plan or trial' do it 'does not include values for plan or trial' do
get api("/users/#{user.id}", admin) get api("/users/#{user.id}", admin)
......
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