ssl_certificates_controller.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class SSLCertificatesController < ApplicationController
  3. prepend_before_action :authenticate_and_authorize!
  4. def index
  5. certificates = SSLCertificate.all
  6. assets = ApplicationModel::CanAssets.reduce(certificates)
  7. render json: assets
  8. end
  9. def create
  10. cert = SSLCertificate.create!(cert_params)
  11. render json: cert.attributes_with_association_ids, status: :created
  12. end
  13. def destroy
  14. SSLCertificate
  15. .find(params[:id])
  16. .destroy!
  17. render json: {
  18. result: 'ok',
  19. }
  20. end
  21. def download
  22. cert = SSLCertificate.find params[:id]
  23. send_data(
  24. cert.certificate,
  25. filename: "#{cert.fingerprint}.crt",
  26. type: 'text/plain',
  27. disposition: 'attachment'
  28. )
  29. end
  30. private
  31. def cert_params
  32. output = params.permit(:certificate)
  33. if output[:certificate].blank?
  34. output[:certificate] = params[:file]&.read&.force_encoding('utf-8')
  35. end
  36. output
  37. end
  38. end