From 7e514dbf3c5eb7638b5ff834d2c2ef294de6bb58 Mon Sep 17 00:00:00 2001 From: Adam Ruzicka Date: Thu, 19 Oct 2023 17:03:23 +0200 Subject: [PATCH] Fixes #36848 - Rework long running task watching setup - enabled for admin-like users on create - enabled for admin-like users on seed when the notifications is first created, excluding hidden admins - delivery is not attempted for disabled users - delivery is not attempted for users without email set - delivery is attempted only for users with mail_enabled set --- app/lib/actions/check_long_running_tasks.rb | 10 ++++------ .../foreman_tasks/concerns/user_extensions.rb | 17 +++++++++++++++++ db/seeds.d/95-mail_notifications.rb | 14 ++++++++++++++ 3 files changed, 35 insertions(+), 6 deletions(-) diff --git a/app/lib/actions/check_long_running_tasks.rb b/app/lib/actions/check_long_running_tasks.rb index 06c4724ec..bdbd97155 100644 --- a/app/lib/actions/check_long_running_tasks.rb +++ b/app/lib/actions/check_long_running_tasks.rb @@ -8,12 +8,10 @@ class CheckLongRunningTasks < ::Actions::EntryAction def plan time = Time.now.utc cutoff = time - INTERVAL - notification = ::ForemanTasks::TasksMailNotification.find_by(name: "long_running_tasks") - org_admin_role = Role.find_by(name: 'Organization admin') - users = User.left_joins(:roles) - .where(id: UserMailNotification.where(mail_notification_id: notification.id).select(:role_id)) - .or(User.where(admin: true)) - .or(User.where(id: UserRole.where(id: [org_admin_role.id] + org_admin_role.cloned_role_ids).select(:owner_id))) + users = User.joins(:mail_notifications) + .where(mail_enabled: true, mail_notifications: { name: 'long_running_tasks' }) + .where.not(mail: [nil, '']) + .where(disabled: [nil, false]) query = "state ^ (#{STATES.join(', ')}) AND state_updated_at <= \"#{cutoff}\"" users.each do |user| diff --git a/app/models/foreman_tasks/concerns/user_extensions.rb b/app/models/foreman_tasks/concerns/user_extensions.rb index 3adcfeb3f..d0af39692 100644 --- a/app/models/foreman_tasks/concerns/user_extensions.rb +++ b/app/models/foreman_tasks/concerns/user_extensions.rb @@ -8,6 +8,23 @@ module UserExtensions has_many :tasks, :dependent => :nullify, :class_name => ::ForemanTasks::Task.name # rubocop:enable Rails/ReflectionClassName + + before_validation :attach_task_mail_notifications, on: :create + end + + def attach_task_mail_notifications + org_admin_role = Role.find_by(name: 'Organization admin') + admin_role_ids = ([org_admin_role.id] + org_admin_role.cloned_role_ids) + role_ids = roles.map(&:id) + + return unless admin || (role_ids & admin_role_ids).any? + + notification = MailNotification.find_by(name: 'long_running_tasks') + return if notification.nil? + + if user_mail_notifications.none? { |n| n.mail_notification_id == notification.id } + user_mail_notifications.build(mail_notification_id: notification.id, interval: 'Subscribe') + end end end end diff --git a/db/seeds.d/95-mail_notifications.rb b/db/seeds.d/95-mail_notifications.rb index 98fb825c3..064e3e752 100644 --- a/db/seeds.d/95-mail_notifications.rb +++ b/db/seeds.d/95-mail_notifications.rb @@ -20,5 +20,19 @@ raise ::Foreman::Exception.new(N_("Unable to create mail notification: %s"), SeedHelper.format_errors(created_notification)) end + + org_admin_role = Role.find_by(name: 'Organization admin') + + users = User.left_joins(:roles) + .joins(:auth_source) + .where(admin: true) + .or(User.where(id: UserRole.where(id: [org_admin_role.id] + org_admin_role.cloned_role_ids).select(:owner_id))) + .where.not(auth_source: { name: 'Hidden' }) + users.each do |user| + mail = UserMailNotification.create(mail_notification_id: created_notification.id, user_id: user.id, interval: 'Subscribe') + if mail.nil? || mail.errors.any? + raise ::Foreman::Exception.new(N_("Unable to enable mail notification to user '%s': %s"), user.login, SeedHelper.format_errors(mail)) + end + end end end