Project

General

Profile

Feature #29664 » Add-configuration-option-to-enable-or-disable-webhooks.patch

Katsuya HIDAKA, 2025-10-10 08:06

View differences:

app/controllers/webhooks_controller.rb
self.main_menu = false
before_action :require_login
before_action :check_enabled
before_action :authorize
before_action :find_webhook, only: [:edit, :update, :destroy]
......
def authorize
deny_access unless User.current.allowed_to?(:use_webhooks, nil, global: true)
end
def check_enabled
render_404 unless Webhook.enabled?
end
end
app/models/webhook.rb
before_validation ->(hook){ hook.projects = hook.projects.to_a & hook.setable_projects }
def self.enabled?
Redmine::Configuration['webhooks']
end
# Triggers the given event for the given object, scheduling qualifying hooks
# to be called.
def self.trigger(event, object)
return unless enabled?
hooks_for(event, object).each do |hook|
payload = hook.payload(event, object)
WebhookJob.perform_later(hook.id, payload.to_json)
app/views/my/account.html.erb
<div class="contextual">
<%= additional_emails_link(@user) %>
<%= link_to(sprite_icon('key', l(:button_change_password)), { :action => 'password'}, :class => 'icon icon-passwd') if @user.change_password_allowed? %>
<%= link_to sprite_icon('webhook', l(:label_webhook_plural)), webhooks_path, class: 'icon icon-webhook' if @user.allowed_to?(:use_webhooks, nil, global: true) %>
<%= link_to sprite_icon('webhook', l(:label_webhook_plural)), webhooks_path, class: 'icon icon-webhook' if Webhook.enabled? && @user.allowed_to?(:use_webhooks, nil, global: true) %>
<%= link_to(sprite_icon('apps', l('label_oauth_authorized_application_plural')), oauth_authorized_applications_path, :class => 'icon icon-applications') if Setting.rest_api_enabled? %>
<%= call_hook(:view_my_account_contextual, :user => @user)%>
</div>
config/configuration.yml.example
# Webhooks
#
# Enables or disables the Webhooks feature (enabled by default).
# When disabled, all related UI will be hidden, and no webhooks will be triggered,
# but existing webhook data will be retained.
# webhooks: true
#
# An optional list of hosts and/or IP addresses and/or IP networks which
# should NOT be valid as webhook targets. You can add your internal IPs and
# hostnames here to avoid possible SSRF attacks.
lib/redmine/configuration.rb
'email_delivery' => nil,
'max_concurrent_ajax_uploads' => 2,
'common_mark_enable_hardbreaks' => true,
'thumbnails_generation_timeout' => 10
'thumbnails_generation_timeout' => 10,
'webhooks' => true
}
@config = nil
lib/redmine/preparation.rb
map.permission :save_queries, {:queries => [:new, :create, :edit, :update, :destroy]}, :require => :loggedin
# Webhooks
map.permission :use_webhooks, {}, :require => :member
map.permission :use_webhooks, {}, :require => :member if Redmine::Configuration['webhooks']
map.project_module :issue_tracking do |map|
# Issues
test/functional/my_controller_test.rb
assert_select 'select[name=?]', 'user[language]'
end
def test_my_account_should_toggle_webhook_link_with_configuration
User.find(2).roles.first.add_permission!(:use_webhooks)
get :account
assert_response :success
assert_select 'a.icon-webhook', 1
Redmine::Configuration.with 'webhooks' => false do
get :account
assert_response :success
assert_select 'a.icon-webhook', 0
end
end
def test_my_account_with_avatar_enabled_should_link_to_edit_avatar
with_settings :gravatar_enabled => '1' do
Redmine::Configuration.with 'avatar_server_url' => 'https://gravatar.com' do
test/functional/webhooks_controller_test.rb
assert_select 'td', text: @other_hook.url, count: 0
end
test "should return not found when disabled" do
Redmine::Configuration.with 'webhooks' => false do
get :index
assert_response :not_found
get :new
assert_response :not_found
end
end
test "should get new" do
get :new
assert_response :success
test/unit/webhook_test.rb
end
end
test "enabled? should follow configuration flag" do
assert Webhook.enabled?
Redmine::Configuration.with 'webhooks' => false do
assert_not Webhook.enabled?
end
Redmine::Configuration.with 'webhooks' => true do
assert Webhook.enabled?
end
end
test "trigger should not enqueue jobs when disabled" do
create_hook
Redmine::Configuration.with 'webhooks' => false do
assert_no_enqueued_jobs do
Webhook.trigger('issue.created', @issue)
end
end
end
test "should compute payload" do
hook = create_hook
payload = hook.payload('issue.created', @issue)
(9-9/17)