mirror of https://github.com/docusealco/docuseal
parent
84edb6dbda
commit
2542f26d96
@ -0,0 +1,22 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class WebhookPreferencesController < ApplicationController
|
||||
load_and_authorize_resource :webhook_url, parent: false
|
||||
|
||||
def update
|
||||
webhook_preferences_params[:events].each do |event, val|
|
||||
@webhook_url.events.delete(event) if val == '0'
|
||||
@webhook_url.events.push(event) if val == '1' && @webhook_url.events.exclude?(event)
|
||||
end
|
||||
|
||||
@webhook_url.save!
|
||||
|
||||
head :ok
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def webhook_preferences_params
|
||||
params.require(:webhook_url).permit(events: {})
|
||||
end
|
||||
end
|
||||
@ -1,7 +1,21 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
class AddSecretToWebhookUrls < ActiveRecord::Migration[7.2]
|
||||
class MigrationWebhookUrl < ApplicationRecord
|
||||
self.table_name = 'webhook_urls'
|
||||
|
||||
serialize :secret, coder: JSON
|
||||
|
||||
encrypts :url, :secret
|
||||
end
|
||||
|
||||
def change
|
||||
add_column :webhook_urls, :secret, :text
|
||||
|
||||
MigrationWebhookUrl.all.each do |url|
|
||||
url.update_columns(secret: {})
|
||||
end
|
||||
|
||||
change_column_null :webhook_urls, :secret, false
|
||||
end
|
||||
end
|
||||
|
||||
@ -0,0 +1,27 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
module WebhookUrls
|
||||
module_function
|
||||
|
||||
def for_account_id(account_id, events)
|
||||
events = Array.wrap(events)
|
||||
|
||||
rel = WebhookUrl.where(account_id:)
|
||||
|
||||
event_arel = events.map { |event| Arel::Table.new(:webhook_urls)[:events].matches("%\"#{event}\"%") }.reduce(:or)
|
||||
|
||||
if Docuseal.multitenant?
|
||||
rel.where(event_arel)
|
||||
else
|
||||
linked_account_rel =
|
||||
AccountLinkedAccount.where(linked_account_id: account_id).where.not(account_type: :testing).select(:account_id)
|
||||
|
||||
webhook_urls = rel.or(WebhookUrl.where(account_id: linked_account_rel).where(event_arel))
|
||||
|
||||
account_urls, linked_urls = webhook_urls.partition { |w| w.account_id == account_id }
|
||||
|
||||
account_urls.select { |w| w.events.intersect?(events) }.presence ||
|
||||
(account_urls.present? ? WebhookUrl.none : linked_urls)
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Reference in new issue