Browse Source

Moved to new notification factory. Added notification templates to app/views/mailer/*.

Martin Edenhofer 9 years ago
parent
commit
d54bb4fa05

+ 1 - 0
app/assets/javascripts/app/controllers/signup.coffee

@@ -43,6 +43,7 @@ class Index extends App.ControllerContent
     if !@params.login && @params.email
       @params.login = @params.email
 
+    @params.signup = true
     @params.role_ids = [0]
     @log 'notice', 'updateAttributes', @params
     user = new App.User

+ 1 - 1
app/assets/javascripts/app/models/user.coffee

@@ -1,5 +1,5 @@
 class App.User extends App.Model
-  @configure 'User', 'login', 'firstname', 'lastname', 'email', 'web', 'password', 'phone', 'fax', 'mobile', 'street', 'zip', 'city', 'country', 'organization_id', 'department', 'note', 'role_ids', 'group_ids', 'active', 'invite', 'updated_at'
+  @configure 'User', 'login', 'firstname', 'lastname', 'email', 'web', 'password', 'phone', 'fax', 'mobile', 'street', 'zip', 'city', 'country', 'organization_id', 'department', 'note', 'role_ids', 'group_ids', 'active', 'invite', 'signup', 'updated_at'
   @extend Spine.Model.Ajax
   @url: @apiPath + '/users'
 

+ 54 - 41
app/controllers/users_controller.rb

@@ -135,46 +135,28 @@ class UsersController < ApplicationController
 
       # send inviteation if needed / only if session exists
       if params[:invite] && current_user
-
-        # generate token
         token = Token.create(action: 'PasswordReset', user_id: user.id)
+        NotificationFactory.notification(
+          template: 'user_invite',
+          user: user,
+          objects: {
+            token: token,
+            user: user,
+            current_user: current_user,
+          }
+        )
+      end
 
-        # send mail
-        data = {}
-        data[:subject] = 'Invitation to #{config.product_name} at #{config.fqdn}'
-        data[:body]    = 'Hi #{user.firstname},
-
-        I (#{current_user.firstname} #{current_user.lastname}) invite you to #{config.product_name} - the customer support / ticket system platform.
-
-        Click on the following link and set your password:
-
-        #{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
-
-        Enjoy,
-
-        #{current_user.firstname} #{current_user.lastname}
-
-        Your #{config.product_name} Team
-        '
-
-        # prepare subject & body
-        [:subject, :body].each { |key|
-          data[key.to_sym] = NotificationFactory.build(
-            locale: user.preferences[:locale],
-            string: data[key.to_sym],
-            objects: {
-              token: token,
-              user: user,
-              current_user: current_user,
-            }
-          )
-        }
-
-        # send notification
-        NotificationFactory.send(
-          recipient: user,
-          subject: data[:subject],
-          body: data[:body]
+      # send email verify
+      if params[:signup] && !current_user
+        token = Token.create(action: 'EmailVerify', user_id: user.id)
+        NotificationFactory.notification(
+          template: 'signup',
+          user: user,
+          objects: {
+            token: token,
+            user: user,
+          }
         )
       end
       user_new = User.find(user.id)
@@ -434,12 +416,20 @@ curl http://localhost/api/v1/users/password_reset.json -v -u #{login}:#{password
       return
     end
 
-    token = User.password_reset_send(params[:username])
-    if token
+    result = User.password_reset_new_token(params[:username])
+    if result && result[:token]
+
+      # send mail
+      user = result[:user]
+      NotificationFactory.notification(
+        template: 'password_reset',
+        user: user,
+        objects: result
+      )
 
       # only if system is in develop mode, send token back to browser for browser tests
       if Setting.get('developer_mode') == true
-        render json: { message: 'ok', token: token.name }, status: :ok
+        render json: { message: 'ok', token: result[:token].name }, status: :ok
         return
       end
 
@@ -485,6 +475,19 @@ curl http://localhost/api/v1/users/password_reset_verify.json -v -u #{login}:#{p
 
       # set new password with token
       user = User.password_reset_via_token(params[:token], params[:password])
+
+      # send mail
+      if user
+        NotificationFactory.notification(
+          template: 'password_change',
+          user: user,
+          objects: {
+            user: user,
+            current_user: current_user,
+          }
+        )
+      end
+
     else
       user = User.password_reset_check(params[:token])
     end
@@ -543,6 +546,16 @@ curl http://localhost/api/v1/users/password_change.json -v -u #{login}:#{passwor
     end
 
     user.update_attributes(password: params[:password_new])
+
+    NotificationFactory.notification(
+      template: 'password_change',
+      user: user,
+      objects: {
+        user: user,
+        current_user: current_user,
+      }
+    )
+
     render json: { message: 'ok', user_login: user.login }, status: :ok
   end
 

+ 1 - 1
app/models/channel/driver/mail_stdin.rb

@@ -10,7 +10,7 @@ process emails from STDIN
 
 e. g.
 
-cat test/fixtures/mail1.box | rails r 'Channel::Driver::MailStdin.new'
+  cat test/fixtures/mail1.box | rails r 'Channel::Driver::MailStdin.new'
 
 =end
 

+ 18 - 200
app/models/observer/ticket/notification/background_job.rb

@@ -133,7 +133,7 @@ class Observer::Ticket::Notification::BackgroundJob
           created_by_id: ticket.updated_by_id || 1,
           user_id: user.id,
         )
-        Rails.logger.info "send ticket online notifiaction to agent (#{@p[:type]}/#{ticket.id}/#{user.email})"
+        Rails.logger.debug "sent ticket online notifiaction to agent (#{@p[:type]}/#{ticket.id}/#{user.email})"
       end
 
       # ignore email channel notificaiton and empty emails
@@ -147,39 +147,26 @@ class Observer::Ticket::Notification::BackgroundJob
 
       # get user based notification template
       # if create, send create message / block update messages
+      template = nil
       if @p[:type] == 'create'
-        template = template_create(user, ticket, article, changes)
+        template = 'ticket_create'
       elsif @p[:type] == 'update'
-        template = template_update(user, ticket, article, changes)
+        template = 'ticket_update'
       else
         fail "unknown type for notification #{@p[:type]}"
       end
 
-      # prepare subject & body
-      notification = {}
-      [:subject, :body].each { |key|
-        notification[key.to_sym] = NotificationFactory.build(
-          locale: user.preferences[:locale],
-          string: template[key],
-          objects: {
-            ticket: ticket,
-            article: article,
-            recipient: user,
-          }
-        )
-      }
-
-      # rebuild subject
-      notification[:subject] = ticket.subject_build(notification[:subject])
-
-      Rails.logger.info "send ticket email notifiaction to agent (#{@p[:type]}/#{ticket.id}/#{user.email})"
-
-      NotificationFactory.send(
-        recipient: user,
-        subject: notification[:subject],
-        body: notification[:body],
-        content_type: 'text/html',
+      NotificationFactory.notification(
+        template: template,
+        user: user,
+        objects: {
+          ticket: ticket,
+          article: article,
+          recipient: user,
+          changes: changes,
+        }
       )
+      Rails.logger.debug "sent ticket email notifiaction to agent (#{@p[:type]}/#{ticket.id}/#{user.email})"
     end
 
   end
@@ -203,6 +190,7 @@ class Observer::Ticket::Notification::BackgroundJob
   def human_changes(user, record)
 
     return {} if !@p[:changes]
+    locale = user.preferences[:locale] || 'en-us'
 
     # only show allowed attributes
     attribute_list = ObjectManager::Attribute.by_object_as_hash('Ticket', user)
@@ -278,7 +266,9 @@ class Observer::Ticket::Notification::BackgroundJob
         display = object_manager_attribute[:display].to_s
       end
       changes[display] = if object_manager_attribute && object_manager_attribute[:translate]
-                           ["i18n(#{value_str[0]})", "i18n(#{value_str[1]})"]
+                           from = Translation.translate(locale, value_str[0])
+                           to = Translation.translate(locale, value_str[1])
+                           [from, to]
                          else
                            [value_str[0].to_s, value_str[1].to_s]
                          end
@@ -286,176 +276,4 @@ class Observer::Ticket::Notification::BackgroundJob
     changes
   end
 
-  def template_create(user, ticket, article, _ticket_changes)
-    article_content = ''
-    if article
-      article_content = 'i18n(Information):
-<blockquote type="cite">
-#{article.body.text2html}
-</blockquote>
-<br>
-<br>'
-    end
-
-    if user.preferences[:locale] =~ /^de/i
-      subject = 'Neues Ticket (#{ticket.title})'
-      body    = '<div>Hallo #{recipient.firstname.text2html},</div>
-<br>
-<div>
-es wurde ein neues Ticket (#{ticket.title.text2html}) von "<b>#{ticket.updated_by.fullname.text2html}</b>" erstellt.
-</div>
-<br>
-<div>
-i18n(Group): #{ticket.group.name.text2html}<br>
-i18n(Owner): #{ticket.owner.fullname.text2html}<br>
-i18n(State): i18n(#{ticket.state.name.text2html})<br>
-</div>
-<br>
-<div>
-' + article_content + '
-</div>
-'
-    else
-
-      subject = 'New Ticket (#{ticket.title})'
-      body    = '<div>Hi #{recipient.firstname.text2html},</div>
-<br>
-<div>
-a new Ticket (#{ticket.title.text2html}) has been created by "<b>#{ticket.updated_by.fullname.text2html}</b>".
-</div>
-<br>
-<div>
-Group: #{ticket.group.name.text2html}<br>
-Owner: #{ticket.owner.fullname.text2html}<br>
-State: i18n(#{ticket.state.name.text2html})<br>
-</div>
-<br>
-<div>
-' + article_content + '
-</div>
-'
-
-    end
-
-    body = template_header(user) + body
-    body += template_footer(user, ticket, article)
-
-    template = {
-      subject: subject,
-      body: body,
-    }
-    template
-  end
-
-  def template_update(user, ticket, article, ticket_changes)
-    changes = ''
-    ticket_changes.each {|key, value|
-      changes += "i18n(#{key.to_s.text2html}): #{value[0].to_s.text2html} -> #{value[1].to_s.text2html}<br>\n"
-    }
-    article_content = ''
-    if article
-      article_content = 'i18n(Information):
-<blockquote type="cite">
-#{article.body.text2html}
-</blockquote>
-<br>
-<br>'
-    end
-    if user.preferences[:locale] =~ /^de/i
-      subject = 'Ticket aktualisiert (#{ticket.title})'
-      body    = '<div>Hallo #{recipient.firstname.text2html},</div>
-<br>
-<div>
-Ticket (#{ticket.title.text2html}) wurde von "<b>#{ticket.updated_by.fullname.text2html}</b>" aktualisiert.
-</div>
-<br>
-<div>
-i18n(Changes):<br>
-' + changes + '
-</div>
-<br>
-<div>
-' + article_content + '
-</div>
-'
-    else
-      subject = 'Updated Ticket (#{ticket.title})'
-      body    = '<div>Hi #{recipient.firstname.text2html},</div>
-<br>
-<div>
-Ticket (#{ticket.title.text2html}) has been updated by "<b>#{ticket.updated_by.fullname.text2html}</b>".
-</div>
-<br>
-<div>
-i18n(Changes):<br>
-' + changes + '
-</div>
-<br>
-<div>
-' + article_content + '
-</div>
-'
-    end
-
-    body = template_header(user) + body
-    body += template_footer(user, ticket, article)
-
-    template = {
-      subject: subject,
-      body: body,
-    }
-    template
-  end
-
-  def template_header(_user)
-    '
-<style type="text/css">
-  blockquote {
-    border-left: 2px solid blue;
-    padding-left: 1em;
-  }
-  .header {
-    color: #aaaaaa;
-    border-bottom-style:solid;
-    border-bottom-width:1px;
-    border-bottom-color:#aaaaaa;
-    width: 100%;
-    max-width: 600px;
-    padding-bottom: 6px;
-    margin-bottom: 16px;
-    padding-top: 6px;
-    font-size: 16px;
-  }
-  .footer {
-    color: #aaaaaa;
-    border-top-style:solid;
-    border-top-width:1px;
-    border-top-color:#aaaaaa;
-    width: 100%;
-    max-width: 600px;
-    padding-top: 6px;
-    margin-top: 16px;
-    padding-botton: 6px;
-  }
-  .footer a {
-    color: #aaaaaa;
-  }
-</style>
-
-<div class="header">
-  #{config.product_name} i18n(Notification)
-</div>
-'
-  end
-
-  def template_footer(_user, _ticket, _article)
-    '
-<p>
-  <a href="#{config.http_type}://#{config.fqdn}/#ticket/zoom/#{ticket.id}" target="zammad_app">i18n(View this in Zammad)</a>
-</p>
-<div class="footer">
-  <a href="#{config.http_type}://#{config.fqdn}/#profile/notifications">i18n(Manage your notifications settings)</a>
-</div>
-'
-  end
 end

+ 10 - 39
app/models/user.rb

@@ -283,17 +283,20 @@ returns
 
 =begin
 
-send reset password email with token to user
+generate new token for reset password
 
-  result = User.password_reset_send(username)
+  result = User.password_reset_new_token(username)
 
 returns
 
-  result = token
+  result = {
+    token: token,
+    user: user,
+  }
 
 =end
 
-  def self.password_reset_send(username)
+  def self.password_reset_new_token(username)
     return if !username || username == ''
 
     # try to find user based on login
@@ -311,42 +314,10 @@ returns
     # generate token
     token = Token.create(action: 'PasswordReset', user_id: user.id)
 
-    # send mail
-    data = {}
-    data[:subject] = 'Reset your #{config.product_name} password'
-    data[:body]    = 'Forgot your password?
-
-We received a request to reset the password for your #{config.product_name} account (#{user.login}).
-
-If you want to reset your password, click on the link below (or copy and paste the URL into your browser):
-
-#{config.http_type}://#{config.fqdn}/#password_reset_verify/#{token.name}
-
-This link takes you to a page where you can change your password.
-
-If you don\'t want to reset your password, please ignore this message. Your password will not be reset.
-
-Your #{config.product_name} Team'
-
-    # prepare subject & body
-    [:subject, :body].each { |key|
-      data[key.to_sym] = NotificationFactory.build(
-        locale: user.preferences[:locale],
-        string: data[key.to_sym],
-        objects: {
-          token: token,
-          user: user,
-        }
-      )
+    {
+      token: token,
+      user: user,
     }
-
-    # send notification
-    NotificationFactory.send(
-      recipient: user,
-      subject: data[:subject],
-      body: data[:body]
-    )
-    token
   end
 
 =begin

+ 7 - 35
app/models/user_device.rb

@@ -154,41 +154,13 @@ send new user device info
   def send_notification
     user = User.find(user_id)
 
-    # send mail
-    data = {}
-    data[:subject] = '#{config.product_name} signin detected from a new device'
-    data[:body]    = 'Hi #{user.firstname},
-
-it looks like you signed into your #{config.product_name} account using a new device on "#{user_device.created_at}":
-
-Your Location: #{user_device.location}
-Your IP: #{user_device.ip}
-
-Your device has been added to your list of known devices, which you can view here:
-
-#{config.http_type}://#{config.fqdn}/#profile/devices
-
-If this wasn\'t you, remove the device, changing your account password, and contacting your administrator. Somebody might have gained unauthorized access to your account.
-
-Your #{config.product_name} Team'
-
-    # prepare subject & body
-    [:subject, :body].each { |key|
-      data[key.to_sym] = NotificationFactory.build(
-        locale: user.preferences[:locale],
-        string: data[key.to_sym],
-        objects: {
-          user_device: self,
-          user: user,
-        }
-      )
-    }
-
-    # send notification
-    NotificationFactory.send(
-      recipient: user,
-      subject: data[:subject],
-      body: data[:body]
+    NotificationFactory.notification(
+      template: 'user_device',
+      user: user,
+      objects: {
+        user_device: self,
+        user: user,
+      }
     )
   end
 end

+ 42 - 0
app/views/mailer/application.html.erb

@@ -0,0 +1,42 @@
+<style type="text/css">
+  blockquote {
+    border-left: 2px solid blue;
+    padding-left: 1em;
+  }
+  .header {
+    color: #aaaaaa;
+    border-bottom-style:solid;
+    border-bottom-width:1px;
+    border-bottom-color:#aaaaaa;
+    width: 100%;
+    max-width: 600px;
+    padding-bottom: 6px;
+    margin-bottom: 16px;
+    padding-top: 6px;
+    font-size: 16px;
+  }
+  .footer {
+    color: #aaaaaa;
+    border-top-style:solid;
+    border-top-width:1px;
+    border-top-color:#aaaaaa;
+    width: 100%;
+    max-width: 600px;
+    padding-top: 6px;
+    margin-top: 16px;
+    padding-botton: 6px;
+  }
+  .footer a {
+    color: #aaaaaa;
+  }
+</style>
+
+<div class="header">
+  <%= c 'product_name' %> <%= t 'Notification' %>
+</div>
+
+<%= d 'message', false %>
+
+<div class="footer">
+  <a href="<%= c 'http_type' %>://<%= c 'fqdn' %>/#profile/notifications"><%= t 'Manage your notifications settings' %></a> | <%= c 'organization' %>
+</div>

+ 9 - 0
app/views/mailer/password_change/de.html.erb

@@ -0,0 +1,9 @@
+Dein <%= c 'product_name' %> Passwort wurde geändert
+
+<p>Hallo <%= d 'user.firstname' %>,</p>
+<br>
+<p>das Passwort für Dein <%= c 'product_name' %> Account <b><%= d 'user.login' %></b> wurde kürzlich geändert.</p>
+<br>
+<p>Diese Aktivität ist Dir nicht bekannt? In diesen Fall kontaktiere Deinen System-Administrator.</p>
+<br>
+<p>Dein <%= c 'product_name' %> Team</p>

+ 9 - 0
app/views/mailer/password_change/en.html.erb

@@ -0,0 +1,9 @@
+Your <%= c 'product_name' %> password has been changed
+
+<p>Hi <%= d 'user.firstname' %>,</p>
+<br>
+<p>the password for your <%= c 'product_name' %> account <b><%= d 'user.login' %></b> has been changed recently.</p>
+<br>
+<p>This activity is not known to you? If not, contact your system administrator.</p>
+<br>
+<p>Your <%= c 'product_name' %> Team</p>

Some files were not shown because too many files changed in this diff