gitlabhq/lib/api/helpers.rb

63 lines
1.3 KiB
Ruby
Raw Normal View History

2012-06-27 13:32:56 +02:00
module Gitlab
module APIHelpers
def current_user
@current_user ||= User.find_by_authentication_token(params[:private_token])
end
2012-07-06 15:08:17 +02:00
def user_project
2012-07-25 14:24:28 +02:00
if @project ||= current_user.projects.find_by_id(params[:id]) ||
current_user.projects.find_by_code(params[:id])
else
2012-09-10 09:41:46 +02:00
not_found!
2012-07-25 14:24:28 +02:00
end
@project
2012-07-06 15:08:17 +02:00
end
2012-09-03 13:46:29 +02:00
def paginate(object)
object.page(params[:page]).per(params[:per_page].to_i)
end
2012-06-27 13:32:56 +02:00
def authenticate!
2012-09-10 09:41:46 +02:00
unauthorized! unless current_user
2012-06-27 13:32:56 +02:00
end
2012-09-10 08:06:11 +02:00
def authorize! action, subject
unless abilities.allowed?(current_user, action, subject)
2012-09-10 09:41:46 +02:00
forbidden!
2012-09-10 08:06:11 +02:00
end
end
2012-09-10 09:41:46 +02:00
# error helpers
def forbidden!
error!({'message' => '403 Forbidden'}, 403)
end
def not_found!(resource = nil)
message = ["404"]
message << resource if resource
message << "Not Found"
error!({'message' => message.join(' ')}, 404)
end
def unauthorized!
error!({'message' => '401 Unauthorized'}, 401)
end
def not_allowed!
error!({'message' => 'method not allowed'}, 405)
end
2012-09-10 08:06:11 +02:00
private
def abilities
@abilities ||= begin
abilities = Six.new
abilities << Ability
abilities
end
end
2012-06-27 13:32:56 +02:00
end
end