Commit 67992b9b authored by Stan Hu's avatar Stan Hu

Make namespace API available to all users

Closes https://github.com/gitlabhq/gitlabhq/issues/9328
parent 06250eef
...@@ -8,6 +8,7 @@ v 7.12.0 (unreleased) ...@@ -8,6 +8,7 @@ v 7.12.0 (unreleased)
- Add file attachment support in Milestone description (Stan Hu) - Add file attachment support in Milestone description (Stan Hu)
- Fix milestone "Browse Issues" button. - Fix milestone "Browse Issues" button.
- Set milestone on new issue when creating issue from index with milestone filter active. - Set milestone on new issue when creating issue from index with milestone filter active.
- Make namespace API available to all users (Stan Hu)
- Add web hook support for note events (Stan Hu) - Add web hook support for note events (Stan Hu)
- Disable "New Issue" and "New Merge Request" buttons when features are disabled in project settings (Stan Hu) - Disable "New Issue" and "New Merge Request" buttons when features are disabled in project settings (Stan Hu)
- Remove Rack Attack monkey patches and bump to version 4.3.0 (Stan Hu) - Remove Rack Attack monkey patches and bump to version 4.3.0 (Stan Hu)
......
...@@ -655,6 +655,12 @@ class User < ActiveRecord::Base ...@@ -655,6 +655,12 @@ class User < ActiveRecord::Base
end end
end end
def namespaces
namespace_ids = groups.pluck(:id)
namespace_ids.push(namespace.id)
Namespace.where(id: namespace_ids)
end
def oauth_authorized_tokens def oauth_authorized_tokens
Doorkeeper::AccessToken.where(resource_owner_id: self.id, revoked_at: nil) Doorkeeper::AccessToken.where(resource_owner_id: self.id, revoked_at: nil)
end end
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
- [Deploy Keys](deploy_keys.md) - [Deploy Keys](deploy_keys.md)
- [System Hooks](system_hooks.md) - [System Hooks](system_hooks.md)
- [Groups](groups.md) - [Groups](groups.md)
- [Namespaces](namespaces.md)
## Clients ## Clients
......
# Namespaces
## List namespaces
Get a list of namespaces. (As user: my namespaces, as admin: all namespaces)
```
GET /namespaces
```
```json
[
{
"id": 1,
"path": "user1",
"kind": "user"
},
{
"id": 2,
"path": "group1",
"kind": "group"
}
]
```
You can search for namespaces by name or path, see below.
## Search for namespace
Get all namespaces that match your string in their name or path.
```
GET /namespaces?search=foobar
```
```json
[
{
"id": 1,
"path": "user1",
"kind": "user"
}
]
```
module API module API
# namespaces API # namespaces API
class Namespaces < Grape::API class Namespaces < Grape::API
before do before { authenticate! }
authenticate!
authenticated_as_admin!
end
resource :namespaces do resource :namespaces do
# Get a namespaces list # Get a namespaces list
...@@ -12,7 +9,11 @@ module API ...@@ -12,7 +9,11 @@ module API
# Example Request: # Example Request:
# GET /namespaces # GET /namespaces
get do get do
@namespaces = Namespace.all @namespaces = if current_user.admin
Namespace.all
else
current_user.namespaces
end
@namespaces = @namespaces.search(params[:search]) if params[:search].present? @namespaces = @namespaces.search(params[:search]) if params[:search].present?
@namespaces = paginate @namespaces @namespaces = paginate @namespaces
......
...@@ -248,6 +248,7 @@ describe User do ...@@ -248,6 +248,7 @@ describe User do
it { expect(@user.several_namespaces?).to be_truthy } it { expect(@user.several_namespaces?).to be_truthy }
it { expect(@user.authorized_groups).to eq([@group]) } it { expect(@user.authorized_groups).to eq([@group]) }
it { expect(@user.owned_groups).to eq([@group]) } it { expect(@user.owned_groups).to eq([@group]) }
it { expect(@user.namespaces).to match_array([@user.namespace, @group]) }
end end
describe 'group multiple owners' do describe 'group multiple owners' do
...@@ -270,6 +271,7 @@ describe User do ...@@ -270,6 +271,7 @@ describe User do
end end
it { expect(@user.several_namespaces?).to be_falsey } it { expect(@user.several_namespaces?).to be_falsey }
it { expect(@user.namespaces).to eq([@user.namespace]) }
end end
describe 'blocking user' do describe 'blocking user' do
......
...@@ -3,6 +3,7 @@ require 'spec_helper' ...@@ -3,6 +3,7 @@ require 'spec_helper'
describe API::API, api: true do describe API::API, api: true do
include ApiHelpers include ApiHelpers
let(:admin) { create(:admin) } let(:admin) { create(:admin) }
let(:user) { create(:user) }
let!(:group1) { create(:group) } let!(:group1) { create(:group) }
let!(:group2) { create(:group) } let!(:group2) { create(:group) }
...@@ -14,7 +15,7 @@ describe API::API, api: true do ...@@ -14,7 +15,7 @@ describe API::API, api: true do
end end
end end
context "when authenticated as admin" do context "when authenticated as admin" do
it "admin: should return an array of all namespaces" do it "admin: should return an array of all namespaces" do
get api("/namespaces", admin) get api("/namespaces", admin)
expect(response.status).to eq(200) expect(response.status).to eq(200)
...@@ -22,6 +23,32 @@ describe API::API, api: true do ...@@ -22,6 +23,32 @@ describe API::API, api: true do
expect(json_response.length).to eq(Namespace.count) expect(json_response.length).to eq(Namespace.count)
end end
it "admin: should return an array of matched namespaces" do
get api("/namespaces?search=#{group1.name}", admin)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
end
context "when authenticated as a regular user" do
it "user: should return an array of namespaces" do
get api("/namespaces", user)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
it "admin: should return an array of matched namespaces" do
get api("/namespaces?search=#{user.username}", user)
expect(response.status).to eq(200)
expect(json_response).to be_an Array
expect(json_response.length).to eq(1)
end
end end
end end
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