changeset 822:8926c413db21

Started implementation of test mail end point. This is intended to test the sending of system notification emails. TODO: more sensible content.
author Sascha Wilde <wilde@intevation.de>
date Thu, 27 Sep 2018 18:52:41 +0200
parents d32516051d08
children d7ae7338872d f3adc0f3a20a
files pkg/controllers/routes.go pkg/controllers/user.go
diffstat 2 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/pkg/controllers/routes.go	Thu Sep 27 18:32:55 2018 +0200
+++ b/pkg/controllers/routes.go	Thu Sep 27 18:52:41 2018 +0200
@@ -43,6 +43,11 @@
 		Handle: deleteUser,
 	})).Methods(http.MethodDelete)
 
+	// System notifications
+	api.Handle("/testmail/{user}", sysAdmin(&JSONHandler{
+		Handle: sendTestMail,
+	})).Methods(http.MethodGet)
+
 	// System Management
 	api.Handle("/system/log/{service}/{file}", sysAdmin(&JSONHandler{
 		Handle: showSystemLog,
--- a/pkg/controllers/user.go	Thu Sep 27 18:32:55 2018 +0200
+++ b/pkg/controllers/user.go	Thu Sep 27 18:52:41 2018 +0200
@@ -8,6 +8,7 @@
 	"github.com/gorilla/mux"
 
 	"gemma.intevation.de/gemma/pkg/auth"
+	"gemma.intevation.de/gemma/pkg/misc"
 	"gemma.intevation.de/gemma/pkg/models"
 )
 
@@ -296,3 +297,64 @@
 	jr.Result = result
 	return
 }
+
+func sendTestMail(
+	_ interface{},
+	req *http.Request,
+	db *sql.Conn,
+) (jr JSONResult, err error) {
+
+	user := models.UserName(mux.Vars(req)["user"])
+	if !user.IsValid() {
+		err = JSONError{http.StatusBadRequest, "error: user invalid"}
+		return
+	}
+
+	userData := &models.User{
+		User:   user,
+		Extent: &models.BoundingBox{},
+	}
+
+	err = db.QueryRowContext(req.Context(), listUserSQL, user).Scan(
+		&userData.Role,
+		&userData.Country,
+		&userData.Email,
+		&userData.Extent.X1, &userData.Extent.Y1,
+		&userData.Extent.X2, &userData.Extent.Y2,
+	)
+
+	switch {
+	case err == sql.ErrNoRows:
+		err = JSONError{
+			Code:    http.StatusNotFound,
+			Message: fmt.Sprintf("Cannot find user %s.", user),
+		}
+		return
+	case err != nil:
+		return
+	}
+
+	var body string
+
+	if userData.Role == "sys_admin" {
+		body = "Test for Sys Admin Notification"
+	} else if userData.Role == "waterway_admin" {
+		body = "Test for Water Way Admin Notification"
+	} else {
+		err = JSONError{
+			Code:    http.StatusBadRequest,
+			Message: "Test mails can only be generated for admin roles.",
+		}
+		return
+	}
+	err = misc.SendMail(string(userData.Email), "Test Email", body)
+	if err != nil {
+		return
+	}
+
+	jr.Result = &struct {
+		Message string `json:"message"`
+	}{"Sending test mail."}
+
+	return
+}