Files
docuseal/lib/abilities/template_conditions.rb
T

38 lines
1.2 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
module Abilities
module TemplateConditions
module_function
def collection(user, ability: nil)
2025-05-11 16:11:02 +03:00
templates = Template.where(account_id: user.account_id)
return templates unless user.account.testing?
2024-06-23 12:19:56 +03:00
shared_ids =
2025-05-11 16:11:02 +03:00
TemplateSharing.where({ ability:, account_id: [user.account_id, TemplateSharing::ALL_ID] }.compact)
2024-06-23 12:19:56 +03:00
.select(:template_id)
2025-05-11 16:11:02 +03:00
join_query =
Template.arel_table
.join(Arel::Nodes::TableAlias.new(templates.select(:id).arel.union(shared_ids.arel), 'union_ids'))
.on(Template.arel_table[:id].eq(Arel::Table.new(:union_ids)[:id]))
2024-06-23 12:19:56 +03:00
Template.joins(join_query.join_sources.first)
end
def entity(template, user:, ability: nil)
2025-05-11 16:11:02 +03:00
return true if template.account_id.blank?
return true if template.account_id == user.account_id
2025-05-11 16:11:02 +03:00
return false unless user.account.linked_account_account
return false if template.template_sharings.to_a.blank?
account_ids = [user.account_id, TemplateSharing::ALL_ID]
2025-05-11 16:11:02 +03:00
template.template_sharings.to_a.any? do |e|
2025-01-29 00:56:14 +02:00
e.account_id.in?(account_ids) && (ability.nil? || e.ability == 'manage' || e.ability == ability)
end
end
end
end