4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger# Base controller for all API controllers
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornbergerclass Api::V1::Base < ApplicationController
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase API_KEY_HEADER = 'Ontohub-API-Key'
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase API_KEY_SYMBOL = :api_key
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger respond_to :json
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger protected
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger def check_write_permission
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger authorize! :write, resource
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornberger end
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase def current_user
383165a6d7903b0dd3ff5ebaf52f604171ce03bdTim Reddehase ApiKey.valid.where(key: api_key).first.try(:user) if api_key
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase end
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase def api_key
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase @api_key_in_request ||=
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase if params[API_KEY_SYMBOL].present?
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase params[API_KEY_SYMBOL]
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase elsif request.headers[API_KEY_HEADER].present?
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase request.headers[API_KEY_HEADER]
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase end
3e53de144f37b10a1cf8034b8e13c9574503d014Tim Reddehase end
4b8c0f56a6a49c41dd0e9fdd28a27c4f35e2a208Julian Kornbergerend