class AdminController < ApplicationController
  before_action :login_required
  layout "application"

  include ApplicationHelper

  def pg_dump
    raise ActionController::UnknownAction
    if theUser(session).is_global_data_admin?
      config   = Rails::Configuration.new
      host     = config.database_configuration[RAILS_ENV]["host"]
      port     = config.database_configuration[RAILS_ENV]["port"]
      database = config.database_configuration[RAILS_ENV]["database"]
      username = config.database_configuration[RAILS_ENV]["username"]
      password = config.database_configuration[RAILS_ENV]["password"]
      puts `echo '#{host}:#{port}:*:#{username}:#{password}' > ~/.pgpass`
      puts `chmod 600 ~/.pgpass`
      send_data `pg_dump -h #{host} -U #{username} -F c -b -v #{database}`,
        :type => 'application/octet-stream',
        :filename => 'e_kommu'+Time.now.strftime("%Y-%m-%d")+'.backup'
    else
      render :text => "Sie haben keine Berechtigung zu diesem Bereich"
    end
  end

end
