diff --git a/app/controllers/invoices_controller.rb b/app/controllers/invoices_controller.rb index b74ec4d28..298489a3d 100644 --- a/app/controllers/invoices_controller.rb +++ b/app/controllers/invoices_controller.rb @@ -15,14 +15,11 @@ def index def show @invoice = invoice + token_based_access = !integer_id?(params[:id]) respond_to do |format| format.html - format.pdf do - render pdf: "Factuur #{@invoice.human_id}", - template: 'invoices/show.html.erb', - lowquality: true - end + format.pdf { render_invoice_pdf(token_based_access) } end end @@ -73,6 +70,13 @@ def send_invoice private + def integer_id?(id) + Integer(id) + true + rescue ArgumentError + false + end + def invoice @invoice = Invoice.find(Integer(params[:id])) authorize @invoice @@ -83,4 +87,14 @@ def invoice def permitted_attributes params.require(:invoice).permit(%i[user_id activity_id name_override email_override rows], rows_attributes: %i[name amount price]) end + + def render_invoice_pdf(token_based_access) + authorize @invoice, :download? unless token_based_access + + render pdf: "Factuur #{@invoice.human_id}", + template: 'invoices/show', + formats: [:html], + layout: 'pdf', + lowquality: true + end end diff --git a/app/policies/invoice_policy.rb b/app/policies/invoice_policy.rb index bbc1f1f13..f10316385 100644 --- a/app/policies/invoice_policy.rb +++ b/app/policies/invoice_policy.rb @@ -7,6 +7,10 @@ def send_invoice? user&.treasurer? end + def download? + user&.treasurer? + end + def pay? show? end diff --git a/app/views/invoices/index.html.erb b/app/views/invoices/index.html.erb index 24d47db7a..ffa778d96 100644 --- a/app/views/invoices/index.html.erb +++ b/app/views/invoices/index.html.erb @@ -35,6 +35,9 @@ <% if policy(Invoice).send_invoice? %>