mirror of
https://github.com/rickbarrette/redmine_qbo.git
synced 2026-11-21 02:50:36 -05:00
Any logged-in user could open any estimate or invoice PDF by id. Now: - estimates: View customers or Write estimates, or an estimate attached to an issue the user can see (the issue page links to it); users without the permission don't trigger a sync of uncached estimates - invoices: View customers, Write invoices, View/Receive payments or Issue refunds, or invoices attached to issues the user can see - share-link customers are unchanged (their own issue's documents) - the sidebar's estimate search only shows to those who can use it The issue PDF merges an estimate PDF with optional content, as it already did for the issue's own PDF, instead of failing. Removed dead code: the JSON branches of CustomersController#create (Redmine refuses JSON there) and the scaffold views for qbo#authenticate, #oauth_callback and #webhook, which those actions never render. An invalid customer now shows the form with a 422. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
64 lines
2.9 KiB
Ruby
64 lines
2.9 KiB
Ruby
#The MIT License (MIT)
|
|
#
|
|
#Copyright (c) 2016 - 2026 rick barrette
|
|
#
|
|
#Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
#
|
|
#The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
#
|
|
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
module AuthHelper
|
|
|
|
# The active customer share token stored in the session by customers#view, if any.
|
|
def customer_token
|
|
return @customer_token if defined?(@customer_token)
|
|
|
|
@customer_token = session[:token].present? ? CustomerToken.active.find_by(token: session[:token]) : nil
|
|
end
|
|
|
|
# True when the visitor is a customer using a share link rather than a logged-in user.
|
|
def customer_token_only?
|
|
!User.current.logged? && customer_token.present?
|
|
end
|
|
|
|
# Logged-in users pass. Customers with an active share link pass too (each controller then limits them
|
|
# to their own issue's documents). Everyone else is sent to the login page.
|
|
def require_login_or_customer_token
|
|
return true if User.current.logged?
|
|
return true if customer_token
|
|
|
|
require_login
|
|
end
|
|
|
|
# A logged-in user may open the estimate's PDF with a billing permission (RedmineQboBilling.estimate_viewer?),
|
|
# or when it's attached to an issue they can see (the issue page links to it)
|
|
def estimate_pdf_allowed?(estimate)
|
|
RedmineQboBilling.estimate_viewer? || Issue.visible.where(estimate_id: estimate.id).exists?
|
|
end
|
|
|
|
# The same for invoices: every one of them must be attached to an issue the user can see
|
|
def invoice_pdfs_allowed?(invoice_ids)
|
|
return true if RedmineQboBilling.invoice_viewer?
|
|
|
|
ids = Array(invoice_ids).map(&:to_s).uniq
|
|
visible = Invoice.joins(:issues).merge(Issue.visible).where(id: ids).distinct.pluck(:id).map(&:to_s)
|
|
ids.any? && (ids - visible).empty?
|
|
end
|
|
|
|
def global_check_permission(permission)
|
|
if !globaly_allowed_to?(permission)
|
|
flash[:error] = t :notice_forbidden
|
|
render_403
|
|
end
|
|
end
|
|
|
|
# True if the user has the permission through any of their roles (or is an admin)
|
|
def globaly_allowed_to?(action)
|
|
return false if User.current.nil?
|
|
|
|
User.current.allowed_to?(action, nil, global: true)
|
|
end
|
|
|
|
end
|