2016-03-11 06:16:04 +00:00
|
|
|
class API::RestfulController < ActionController::Base
|
2016-03-11 22:37:18 +00:00
|
|
|
include Pundit
|
|
|
|
include PunditExtra
|
|
|
|
|
2016-03-11 06:16:04 +00:00
|
|
|
snorlax_used_rest!
|
|
|
|
|
2016-03-12 01:26:23 +00:00
|
|
|
load_and_authorize_resource only: [:show, :update, :destroy]
|
2016-03-11 22:37:18 +00:00
|
|
|
|
2016-03-11 06:26:54 +00:00
|
|
|
def create
|
2016-03-25 02:26:32 +00:00
|
|
|
instantiate_resource
|
2016-03-11 06:26:54 +00:00
|
|
|
resource.user = current_user
|
2016-03-25 06:33:26 +00:00
|
|
|
authorize resource
|
2016-03-11 06:26:54 +00:00
|
|
|
create_action
|
|
|
|
respond_with_resource
|
|
|
|
end
|
|
|
|
|
2016-03-11 06:16:04 +00:00
|
|
|
private
|
|
|
|
|
2016-03-11 23:05:42 +00:00
|
|
|
def resource_serializer
|
|
|
|
"new_#{resource_name}_serializer".camelize.constantize
|
|
|
|
end
|
|
|
|
|
2016-03-11 22:37:18 +00:00
|
|
|
def accessible_records
|
|
|
|
if current_user
|
|
|
|
visible_records
|
|
|
|
else
|
|
|
|
public_records
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-03-11 06:16:04 +00:00
|
|
|
def current_user
|
2016-03-25 00:16:27 +00:00
|
|
|
super || token_user || doorkeeper_user || nil
|
2016-03-11 06:16:04 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def token_user
|
2016-03-25 00:16:27 +00:00
|
|
|
token = params[:access_token]
|
|
|
|
access_token = Token.find_by_token(token)
|
2016-07-26 00:14:23 +00:00
|
|
|
@token_user ||= access_token.user if access_token
|
2016-03-11 06:16:04 +00:00
|
|
|
end
|
|
|
|
|
2016-03-25 00:16:27 +00:00
|
|
|
def doorkeeper_user
|
|
|
|
return unless doorkeeper_token.present?
|
|
|
|
doorkeeper_render_error unless valid_doorkeeper_token?
|
|
|
|
@doorkeeper_user ||= User.find(doorkeeper_token.resource_owner_id)
|
|
|
|
end
|
|
|
|
|
2016-03-11 06:16:04 +00:00
|
|
|
def permitted_params
|
|
|
|
@permitted_params ||= PermittedParams.new(params)
|
|
|
|
end
|
|
|
|
end
|