summaryrefslogtreecommitdiff
path: root/lib/api/helpers.rb
diff options
context:
space:
mode:
authorAlex Denisov <1101.debian@gmail.com>2012-09-20 10:25:29 +0300
committerAlex Denisov <1101.debian@gmail.com>2012-09-20 10:25:29 +0300
commite6ce47291b3f08ebe18c2450fc4f21a2a3a2b8a9 (patch)
tree0e5fa008658b3e890cc447477f533f35c45aafa5 /lib/api/helpers.rb
parent77bfc591bf5836892be26059d92411f9fbf04be9 (diff)
parent6104942438c14ec7bd21c6cd5bd995272b3faff6 (diff)
downloadgitlab-ce-e6ce47291b3f08ebe18c2450fc4f21a2a3a2b8a9.tar.gz
master merged
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r--lib/api/helpers.rb39
1 files changed, 36 insertions, 3 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb
index c0ba874790a..9a08b995800 100644
--- a/lib/api/helpers.rb
+++ b/lib/api/helpers.rb
@@ -8,7 +8,7 @@ module Gitlab
if @project ||= current_user.projects.find_by_id(params[:id]) ||
current_user.projects.find_by_code(params[:id])
else
- error!({'message' => '404 Not found'}, 404)
+ not_found!
end
@project
@@ -19,15 +19,48 @@ module Gitlab
end
def authenticate!
- error!({'message' => '401 Unauthorized'}, 401) unless current_user
+ unauthorized! unless current_user
end
def authorize! action, subject
unless abilities.allowed?(current_user, action, subject)
- error!({'message' => '403 Forbidden'}, 403)
+ forbidden!
end
end
+ def attributes_for_keys(keys)
+ attrs = {}
+ keys.each do |key|
+ attrs[key] = params[key] if params[key].present?
+ end
+ attrs
+ end
+
+ # error helpers
+
+ def forbidden!
+ render_api_error!('403 Forbidden', 403)
+ end
+
+ def not_found!(resource = nil)
+ message = ["404"]
+ message << resource if resource
+ message << "Not Found"
+ render_api_error!(message.join(' '), 404)
+ end
+
+ def unauthorized!
+ render_api_error!('401 Unauthorized', 401)
+ end
+
+ def not_allowed!
+ render_api_error!('Method Not Allowed', 405)
+ end
+
+ def render_api_error!(message, status)
+ error!({'message' => message}, status)
+ end
+
private
def abilities