comparison pkg/controllers/user.go @ 827:f3adc0f3a20a

Use templating to make somewhat more interesting test notification mails.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 23:24:40 +0200
parents 8926c413db21
children a244b18cb916
comparison
equal deleted inserted replaced
822:8926c413db21 827:f3adc0f3a20a
1 package controllers 1 package controllers
2 2
3 import ( 3 import (
4 "bytes"
4 "database/sql" 5 "database/sql"
5 "fmt" 6 "fmt"
7 "log"
6 "net/http" 8 "net/http"
9 "text/template"
10 "time"
7 11
8 "github.com/gorilla/mux" 12 "github.com/gorilla/mux"
9 13
10 "gemma.intevation.de/gemma/pkg/auth" 14 "gemma.intevation.de/gemma/pkg/auth"
11 "gemma.intevation.de/gemma/pkg/misc" 15 "gemma.intevation.de/gemma/pkg/misc"
49 email_address, 53 email_address,
50 ST_XMin(map_extent), ST_YMin(map_extent), 54 ST_XMin(map_extent), ST_YMin(map_extent),
51 ST_XMax(map_extent), ST_YMax(map_extent) 55 ST_XMax(map_extent), ST_YMax(map_extent)
52 FROM users.list_users 56 FROM users.list_users
53 WHERE username = $1` 57 WHERE username = $1`
58 )
59
60 var (
61 testSysadminNotifyMailTmpl = template.Must(
62 template.New("sysadmin").Parse(`Dear {{ .User }},
63
64 this is a test email for the Gemma System Errors notification service. You
65 recieved this mail, because a System Administrator triggered the test mail
66 sending function at {{ .Timestamp }}.
67
68 When a critical system error is detecte an automated mail will be send to
69 the address: {{ .Email }} with details on the error condition.`))
70
71 testWWAdminNotifyMailTmpl = template.Must(
72 template.New("waterwayadmin").Parse(`Dear {{ .User }},
73
74 this is a test email for the Gemma System Imports notification service. You
75 recieved this mail, because a System Administrator triggered the test mail
76 sending function at {{ .Timestamp }}.
77
78 When the status of an data import managed by you changes an automated mail will
79 be send to the address: {{ .Email }} with details on the new import status
80 (inkluding import errors) and details on the concerned import.`))
54 ) 81 )
55 82
56 func deleteUser( 83 func deleteUser(
57 _ interface{}, req *http.Request, 84 _ interface{}, req *http.Request,
58 db *sql.Conn, 85 db *sql.Conn,
332 return 359 return
333 case err != nil: 360 case err != nil:
334 return 361 return
335 } 362 }
336 363
337 var body string 364 var subject string
338 365
366 var tmplVars = struct {
367 User string
368 Timestamp string
369 Email string
370 }{
371 User: string(user),
372 Timestamp: time.Now().Format("2006-01-02 15:04:05"),
373 Email: string(userData.Email),
374 }
375
376 var bodyTmpl *template.Template
339 if userData.Role == "sys_admin" { 377 if userData.Role == "sys_admin" {
340 body = "Test for Sys Admin Notification" 378 subject = "Sysadmin Notification TEST"
379 bodyTmpl = testSysadminNotifyMailTmpl
341 } else if userData.Role == "waterway_admin" { 380 } else if userData.Role == "waterway_admin" {
342 body = "Test for Water Way Admin Notification" 381 subject = "Waterway Admin Notification TEST"
382 bodyTmpl = testWWAdminNotifyMailTmpl
343 } else { 383 } else {
344 err = JSONError{ 384 err = JSONError{
345 Code: http.StatusBadRequest, 385 Code: http.StatusBadRequest,
346 Message: "Test mails can only be generated for admin roles.", 386 Message: "Test mails can only be generated for admin roles.",
347 } 387 }
348 return 388 return
349 } 389 }
350 err = misc.SendMail(string(userData.Email), "Test Email", body) 390 var buf bytes.Buffer
391 if err := bodyTmpl.Execute(&buf, &tmplVars); err != nil {
392 log.Printf("error: %v\n", err)
393 }
394
395 err = misc.SendMail(string(userData.Email), subject, buf.String())
351 if err != nil { 396 if err != nil {
352 return 397 return
353 } 398 }
354 399
355 jr.Result = &struct { 400 jr.Result = &struct {