Skip to content
Snippets Groups Projects
application_controller.rb 1.46 KiB
Newer Older
Mateus Rambo Strey's avatar
Mateus Rambo Strey committed
class ApplicationController < ActionController::API
  include DeviseTokenAuth::Concerns::SetUserByToken
  include PublicActivity::StoreController
  # tracking user in papertrail
  # check if client application is allowed to consumes the API.
  before_action :allow_client_application, if: -> { Feature.active?(:allow_client_application) }
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
Mateus Rambo Strey's avatar
Mateus Rambo Strey committed
  # protect_from_forgery with: :null_session
  before_action :configure_permitted_parameters, if: :devise_controller?
  rescue_from Pundit::NotAuthorizedError, with: :user_not_authorized

  # ensure *current_user* can be called in PublicActivity models
  # helper_method :current_user
  # hide_action :current_user
  protected

  def configure_permitted_parameters
    registration_params = [:name, :email, :description, :avatar, :password, :password_confirmation, :current_password, :terms_of_service]
    devise_parameter_sanitizer.permit :sign_up, keys: registration_params
    devise_parameter_sanitizer.permit :account_update, keys: registration_params
  def allow_client_application
    app = Application.find_or_initialize_by(application_id: request.headers["PortalMEC-AppID"].to_s)
    user_not_authorized if app.domain != request.domain
    render status: :unauthorized