Merge branch 'master' into 2919-provenance-graph-cutoff
[arvados.git] / services / api / app / mailers / admin_notifier.rb
index d291e86f9a746890c6fdfe5d4b0fea7c5cc03dbf..e17f4a14dab2d05f16780b09a76844401ad3cbff 100644 (file)
@@ -1,41 +1,40 @@
 class AdminNotifier < ActionMailer::Base
+  include AbstractController::Callbacks
+
   default from: Rails.configuration.admin_notifier_email_from
+  before_filter :load_variables
 
-  def after_create(model, *args)
-    self.generic_callback('after_create', model, *args)
+  def new_user(user)
+    @user = user
+    if not Rails.configuration.new_user_notification_recipients.empty? then
+      @recipients = Rails.configuration.new_user_notification_recipients
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.email_subject_prefix}New user notification"
+          )
+    end
   end
 
   def new_inactive_user(user)
     @user = user
     if not Rails.configuration.new_inactive_user_notification_recipients.empty? then
-      mail(to: Rails.configuration.new_inactive_user_notification_recipients, subject: 'New inactive user notification')
+      @recipients = Rails.configuration.new_inactive_user_notification_recipients
+      logger.info "Sending mail to #{@recipients} about new user #{@user.uuid} (#{@user.full_name} <#{@user.email}>)"
+      mail(to: @recipients,
+           subject: "#{Rails.configuration.email_subject_prefix}New inactive user notification"
+          )
     end
   end
 
-  protected
-
-  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
+private
+  def load_variables
+    if Rails.configuration.respond_to?('workbench_address') and
+       not Rails.configuration.workbench_address.nil? and
+       not Rails.configuration.workbench_address.empty? then
+      @wb_address = Rails.configuration.workbench_address.sub(/\/$/,'') + '/users'
+    else
+      @wb_address = ''
     end
   end
 
-  def all_admin_emails()
-    User.
-      where(is_admin: true).
-      collect(&:email).
-      compact.
-      uniq.
-      select { |e| e.match /\@/ }
-  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