diff options
author | miks <miks@cubesystems.lv> | 2012-09-10 16:47:31 +0300 |
---|---|---|
committer | miks <miks@cubesystems.lv> | 2012-09-10 16:47:31 +0300 |
commit | 2e34a6d3c40a60ed689de5d7870fe663b1959e88 (patch) | |
tree | d2c1d12930948c11e2c767e8688ee49ac8c79ea4 /lib/api/helpers.rb | |
parent | fdb5c82c331e43dc5d0466d2a4c90ce3e649fc7b (diff) | |
parent | 8674fba173e520a67d60e6b5289dcd1bd648d537 (diff) | |
download | gitlab-ce-2e34a6d3c40a60ed689de5d7870fe663b1959e88.tar.gz |
Merge branch 'master' into project_hooks_api
Diffstat (limited to 'lib/api/helpers.rb')
-rw-r--r-- | lib/api/helpers.rb | 45 |
1 files changed, 43 insertions, 2 deletions
diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index ce7b7b497fc..054eb2d3f70 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,7 +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) + forbidden! + end + 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 + @abilities ||= begin + abilities = Six.new + abilities << Ability + abilities + end end end end |