diff --git a/doc/api/users.md b/doc/api/users.md index 333998dffb2f4955679ebd0cd8d8e1226cf3bae1..a76539c375b1d5f17420b8cbc32e35fad2a038cc 100644 --- a/doc/api/users.md +++ b/doc/api/users.md @@ -106,7 +106,8 @@ GET /users?without_project_bots=true ### For administrators **(FREE SELF)** -> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `created_by` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/93092) in GitLab 15.6. ```plaintext GET /users @@ -161,7 +162,8 @@ GET /users "private_profile": false, "current_sign_in_ip": "196.165.1.102", "last_sign_in_ip": "172.127.2.22", - "namespace_id": 1 + "namespace_id": 1, + "created_by": null }, { "id": 2, @@ -196,7 +198,8 @@ GET /users "private_profile": false, "current_sign_in_ip": "10.165.1.102", "last_sign_in_ip": "172.127.2.22", - "namespace_id": 2 + "namespace_id": 2, + "created_by": null } ] ``` @@ -269,6 +272,13 @@ You can include the users' [custom attributes](custom_attributes.md) in the resp GET /users?with_custom_attributes=true ``` +You can use the `created_by` parameter to see if a user account was created: + +- [Manually by an administrator](../user/profile/account/create_accounts.md#create-users-in-admin-area). +- As a [project bot user](../user/project/settings/project_access_tokens.md#bot-users-for-projects). + +If the returned value is `null`, the account was created by a user who registered an account themselves. + ## Single user Get a single user. @@ -315,7 +325,8 @@ Parameters: ### For administrators **(FREE SELF)** -> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `created_by` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/93092) in GitLab 15.6. ```plaintext GET /users/:id @@ -378,7 +389,8 @@ Example Responses: "plan": "gold", "trial": true, "sign_in_count": 1337, - "namespace_id": 1 + "namespace_id": 1, + "created_by": null } ``` @@ -419,6 +431,13 @@ see the `group_saml` option and `provisioned_by_group_id` parameter: } ``` +Administrators can use the `created_by` parameter to see if a user account was created: + +- [Manually by an administrator](../user/profile/account/create_accounts.md#create-users-in-admin-area). +- As a [project bot user](../user/project/settings/project_access_tokens.md#bot-users-for-projects). + +If the returned value is `null`, the account was created by a user who registered an account themselves. + You can include the user's [custom attributes](custom_attributes.md) in the response with: ```plaintext @@ -630,7 +649,8 @@ Users on [GitLab Premium or higher](https://about.gitlab.com/pricing/) also see ### For administrators **(FREE SELF)** -> The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `namespace_id` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/82045) in GitLab 14.10. +> - The `created_by` field in the response was [introduced](https://gitlab.com/gitlab-org/gitlab/-/merge_requests/93092) in GitLab 15.6. ```plaintext GET /user @@ -683,6 +703,7 @@ Parameters: "current_sign_in_ip": "196.165.1.102", "last_sign_in_ip": "172.127.2.22", "namespace_id": 1, + "created_by": null, "note": null } ``` diff --git a/lib/api/entities/user_with_admin.rb b/lib/api/entities/user_with_admin.rb index f9c1a646a4fa98dbb750781958f7761458cf31c8..53fef7a46e2f3fb1a0fa659d9ea01d38a507438e 100644 --- a/lib/api/entities/user_with_admin.rb +++ b/lib/api/entities/user_with_admin.rb @@ -6,6 +6,7 @@ class UserWithAdmin < UserPublic expose :admin?, as: :is_admin expose :note expose :namespace_id + expose :created_by, with: UserBasic end end end diff --git a/spec/requests/api/users_spec.rb b/spec/requests/api/users_spec.rb index 96e23337411995fc5481a2e7cfe44b0d8532a447..66a18b9783856fbcdfcf81107b203a01efde7b23 100644 --- a/spec/requests/api/users_spec.rb +++ b/spec/requests/api/users_spec.rb @@ -165,6 +165,7 @@ expect(json_response.first).not_to have_key('note') expect(json_response.first).not_to have_key('namespace_id') + expect(json_response.first).not_to have_key('created_by') end end @@ -175,6 +176,7 @@ expect(json_response.first).not_to have_key('note') expect(json_response.first).not_to have_key('namespace_id') + expect(json_response.first).not_to have_key('created_by') end end @@ -186,6 +188,26 @@ expect(json_response.first).to have_key('note') expect(json_response.first['note']).to eq '2018-11-05 | 2FA removed | user requested | www.gitlab.com' end + + context 'with `created_by` details' do + it 'has created_by as nil with a self-registered account' do + get api("/users", admin), params: { username: user.username } + + expect(response).to have_gitlab_http_status(:success) + expect(json_response.first).to have_key('created_by') + expect(json_response.first['created_by']).to eq(nil) + end + + it 'is created_by a user and has those details' do + created = create(:user, created_by_id: user.id) + + get api("/users", admin), params: { username: created.username } + + expect(response).to have_gitlab_http_status(:success) + expect(json_response.first['created_by'].symbolize_keys) + .to eq(API::Entities::UserBasic.new(user).as_json) + end + end end context 'N+1 queries' do