class ApplicationController < ActionController::Base
  # Prevent CSRF attacks by raising an exception.
  # For APIs, you may want to use :null_session instead.
  protect_from_forgery with: :exception

  # Do not mention passwords in the log file
  # filter_parameter_logging :password

  before_action :moderate_packages

  private

  # If the current user is an administrator then show a second bar below
  # the navigation bar that contains a paginator of packages that contain
  # screenshots that require moderation.
  def moderate_packages
    if can? :approve, Screenshot and Package.need_moderation.any?
      @moderate_packages = Package.need_moderation
    end
  end

  # Define where the user is redirected to after a successful login.
  def after_sign_in_path_for(resource)
    my_profile_path
  end

  # Overwriting the sign_out redirect path method
  def after_sign_out_path_for(resource_or_scope)
    root_path
  end

  # Write a message into the audit log in the database
  def auditlog(message, level: 'info', screenshot: nil, package: nil)
    Log.log(message, ip: request.remote_ip, user: current_user,
      screenshot: screenshot, package: package)
  end
end
