17296: Merge branch 'master'
[arvados.git] / services / api / app / mailers / admin_notifier.rb
index 5de70053dcf0265258244ad252df341936334154..2c39a3924e7a65b3c3b105abff3e01224f25ac5a 100644 (file)
@@ -1,34 +1,38 @@
+# Copyright (C) The Arvados Authors. All rights reserved.
+#
+# SPDX-License-Identifier: AGPL-3.0
+
 class AdminNotifier < ActionMailer::Base
-  default from: Rails.configuration.admin_notifier_email_from
+  include AbstractController::Callbacks
 
-  def after_create(model, *args)
-    self.generic_callback('after_create', model, *args)
-  end
+  default from: Rails.configuration.Users.AdminNotifierEmailFrom
+
+  def new_user(user)
+    @user = user
+    if not Rails.configuration.Users.NewUserNotificationRecipients.empty? then
+      @recipients = Rails.configuration.Users.NewUserNotificationRecipients.keys
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
 
-  protected
+      add_to_subject = ''
+      if Rails.configuration.Users.AutoSetupNewUsers
+        add_to_subject = @user.is_invited ? ' and setup' : ', but not setup'
+      end
 
-  def generic_callback(callback_type, model, *args)
-    model_specific_method = "#{callback_type}_#{model.class.to_s.underscore}".to_sym
-    if self.respond_to? model_specific_method
-      self.send model_specific_method, model, *args
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.Users.EmailSubjectPrefix}New user created#{add_to_subject} notification"
+          )
     end
   end
 
-  def all_admin_emails()
-    User.
-      where(is_admin: true).
-      collect(&:email).
-      compact.
-      uniq.
-      select { |e| e.match /\@/ }
+  def new_inactive_user(user)
+    @user = user
+    if not Rails.configuration.Users.NewInactiveUserNotificationRecipients.empty? then
+      @recipients = Rails.configuration.Users.NewInactiveUserNotificationRecipients.keys
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.Users.EmailSubjectPrefix}New inactive user notification"
+          )
+    end
   end
 
-  def after_create_user(user, *args)
-    @new_user = user
-    logger.info "Sending mail to #{@recipients} about new user #{@new_user.uuid} (#{@new_user.full_name}, #{@new_user.email})"
-    mail({
-           to: self.all_admin_emails,
-           subject: "#{Rails.configuration.email_subject_prefix}New user: #{@new_user.full_name}, #{@new_user.email}"
-         })
-  end
 end