summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorTomasz Maczukin <tomasz@maczukin.pl>2017-11-23 14:32:16 +0100
committerTomasz Maczukin <tomasz@maczukin.pl>2017-11-23 17:44:05 +0100
commitdfbfd3c7d7d4677ac99a7f8147a673911e8d4e98 (patch)
tree7dfb3b813f9820fa539eb908dd1ca06719a63348 /lib
parent5c2578e68177e0b5ba48f9d2931602747ad58c91 (diff)
downloadgitlab-ce-dfbfd3c7d7d4677ac99a7f8147a673911e8d4e98.tar.gz
Allow request namespace by ID or path
Diffstat (limited to 'lib')
-rw-r--r--lib/api/helpers.rb22
-rw-r--r--lib/api/namespaces.rb18
2 files changed, 24 insertions, 16 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index b26c61ab8da..52ac416f9ad 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -50,6 +50,10 @@ module API
initial_current_user != current_user
end
+ def user_namespace
+ @user_namespace ||= find_namespace!(params[:id])
+ end
+
def user_group
@group ||= find_group!(params[:id])
end
@@ -112,6 +116,24 @@ module API
end
end
+ def find_namespace(id)
+ if id.to_s =~ /^\d+$/
+ Namespace.find_by(id: id)
+ else
+ Namespace.find_by_full_path(id)
+ end
+ end
+
+ def find_namespace!(id)
+ namespace = find_namespace(id)
+
+ if can?(current_user, :admin_namespace, namespace)
+ namespace
+ else
+ not_found!('Namespace')
+ end
+ end
+
def find_project_label(id)
label = available_labels.find_by_id(id) || available_labels.find_by_title(id)
label || not_found!('Label')
diff --git a/lib/api/namespaces.rb b/lib/api/namespaces.rb
index 21dc5009d0e..32b77aedba8 100644
--- a/lib/api/namespaces.rb
+++ b/lib/api/namespaces.rb
@@ -24,24 +24,10 @@ module API
success Entities::Namespace
end
params do
- requires :id, type: Integer, desc: "Namespace's ID"
+ requires :id, type: String, desc: "Namespace's ID or path"
end
get ':id' do
- namespace = Namespace.find(params[:id])
- authenticate_get_namespace!(namespace)
-
- present namespace, with: Entities::Namespace, current_user: current_user
- end
- end
-
- helpers do
- def authenticate_get_namespace!(namespace)
- return if current_user.admin?
- forbidden!('No access granted') unless user_can_access_namespace?(namespace)
- end
-
- def user_can_access_namespace?(namespace)
- namespace.has_owner?(current_user)
+ present user_namespace, with: Entities::Namespace, current_user: current_user
end
end
end