Commit a5fc5b14 authored by Oswaldo Ferreira's avatar Oswaldo Ferreira

Rename members_count to members_count_with_descendants and expose only to group admins

parent 6a6c8c82
...@@ -30,9 +30,7 @@ Example response: ...@@ -30,9 +30,7 @@ Example response:
"id": 1, "id": 1,
"path": "user1", "path": "user1",
"kind": "user", "kind": "user",
"full_path": "user1", "full_path": "user1"
"parent_id": "null",
"members_count": "null"
}, },
{ {
"id": 2, "id": 2,
...@@ -40,7 +38,7 @@ Example response: ...@@ -40,7 +38,7 @@ Example response:
"kind": "group" "kind": "group"
"full_path": "group1", "full_path": "group1",
"parent_id": "null", "parent_id": "null",
"members_count": 2 "members_count_with_descendants": 2
}, },
{ {
"id": 3, "id": 3,
...@@ -48,11 +46,13 @@ Example response: ...@@ -48,11 +46,13 @@ Example response:
"kind": "group", "kind": "group",
"full_path": "foo/bar", "full_path": "foo/bar",
"parent_id": "9", "parent_id": "9",
"members_count": 5 "members_count_with_descendants": 5
} }
] ]
``` ```
**Note**: `members_count_with_descendants` are presented only for group masters/owners.
## Search for namespace ## Search for namespace
Get all namespaces that match a string in their name or path. Get all namespaces that match a string in their name or path.
...@@ -81,7 +81,7 @@ Example response: ...@@ -81,7 +81,7 @@ Example response:
"kind": "group", "kind": "group",
"full_path": "twitter", "full_path": "twitter",
"parent_id": "null", "parent_id": "null",
"members_count": 2 "members_count_with_descendants": 2
} }
] ]
``` ```
...@@ -512,8 +512,12 @@ module API ...@@ -512,8 +512,12 @@ module API
class Namespace < Grape::Entity class Namespace < Grape::Entity
expose :id, :name, :path, :kind, :full_path, :parent_id expose :id, :name, :path, :kind, :full_path, :parent_id
expose :members_count do |namespace, _| expose :members_count_with_descendants, if: -> (namespace, opts) { expose_members_count_with_descendants?(namespace, opts) } do |namespace, _|
namespace.users_with_descendants.count if namespace.kind == 'group' namespace.users_with_descendants.count
end
def expose_members_count_with_descendants?(namespace, opts)
namespace.kind == 'group' && Ability.allowed?(opts[:current_user], :admin_group, namespace)
end end
# EE-only # EE-only
......
...@@ -39,6 +39,8 @@ module API ...@@ -39,6 +39,8 @@ module API
else else
render_validation_error!(namespace) render_validation_error!(namespace)
end end
present paginate(namespaces), with: Entities::Namespace, current_user: current_user
end end
end end
end end
......
...@@ -18,10 +18,15 @@ describe API::Namespaces do ...@@ -18,10 +18,15 @@ describe API::Namespaces do
it "returns correct attributes" do it "returns correct attributes" do
get api("/namespaces", admin) get api("/namespaces", admin)
group_kind_json_response = json_response.find { |resource| resource['kind'] == 'group' }
user_kind_json_response = json_response.find { |resource| resource['kind'] == 'user' }
expect(response).to have_http_status(200) expect(response).to have_http_status(200)
expect(response).to include_pagination_headers expect(response).to include_pagination_headers
expect(json_response.first).to include('id', 'name', 'path', 'full_path', 'parent_id', expect(group_kind_json_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
'members_count', 'shared_runners_minutes_limit', 'plan') 'parent_id', 'members_count_with_descendants')
expect(user_kind_json_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
end end
it "admin: returns an array of all namespaces" do it "admin: returns an array of all namespaces" do
...@@ -46,12 +51,25 @@ describe API::Namespaces do ...@@ -46,12 +51,25 @@ describe API::Namespaces do
end end
context "when authenticated as a regular user" do context "when authenticated as a regular user" do
it "returns correct attributes" do it "returns members_count_with_descendants if user can admin group" do
group1.add_owner(user)
get api("/namespaces", user) get api("/namespaces", user)
expect(response).to have_http_status(200) owned_group_response = json_response.find { |resource| resource['id'] == group1.id }
expect(response).to include_pagination_headers
expect(json_response.first).to include('id', 'name', 'path', 'full_path', 'parent_id', 'members_count') expect(owned_group_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path',
'parent_id', 'members_count_with_descendants')
end
it "does not returns members_count_with_descendants if user cannot admin group" do
group1.add_guest(user)
get api("/namespaces", user)
guest_group_response = json_response.find { |resource| resource['id'] == group1.id }
expect(guest_group_response.keys).to contain_exactly('id', 'kind', 'name', 'path', 'full_path', 'parent_id')
end end
it "user: returns an array of namespaces" do it "user: returns an array of namespaces" do
......
...@@ -748,9 +748,7 @@ describe API::Projects do ...@@ -748,9 +748,7 @@ describe API::Projects do
'name' => user.namespace.name, 'name' => user.namespace.name,
'path' => user.namespace.path, 'path' => user.namespace.path,
'kind' => user.namespace.kind, 'kind' => user.namespace.kind,
'full_path' => user.namespace.full_path, 'full_path' => user.namespace.full_path
'parent_id' => nil,
'members_count' => nil
}) })
end end
......
...@@ -785,9 +785,7 @@ describe API::V3::Projects do ...@@ -785,9 +785,7 @@ describe API::V3::Projects do
'name' => user.namespace.name, 'name' => user.namespace.name,
'path' => user.namespace.path, 'path' => user.namespace.path,
'kind' => user.namespace.kind, 'kind' => user.namespace.kind,
'full_path' => user.namespace.full_path, 'full_path' => user.namespace.full_path
'parent_id' => nil,
'members_count' => nil
}) })
end end
......
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