From 25de63972709adc6c7ca9c4b84abab1730551f9a Mon Sep 17 00:00:00 2001
From: Paul Fournet <paul.fournet@cybelangel.com>
Date: Mon, 4 Apr 2022 15:04:07 +0000
Subject: [PATCH] add: Manage template from global settings form

---
 handler/routes.go              |  8 ++++++++
 model/setting.go               |  2 ++
 store/jsondb/jsondb.go         | 13 ++++++++++++-
 templates/global_settings.html | 24 ++++++++++++++++++++++--
 4 files changed, 44 insertions(+), 3 deletions(-)

diff --git a/handler/routes.go b/handler/routes.go
index c86cfb4..2fbea07 100644
--- a/handler/routes.go
+++ b/handler/routes.go
@@ -254,6 +254,13 @@ func EmailClient(db store.IStore, mailer emailer.Emailer, emailSubject, emailCon
 		globalSettings, _ := db.GetGlobalSettings()
 		config := util.BuildClientConfig(*clientData.Client, server, globalSettings)
 
+		if globalSettings.EmailContent != "" {
+			emailContent = globalSettings.EmailContent
+		}
+		if globalSettings.EmailSubject != "" {
+			emailSubject = globalSettings.EmailSubject
+		}
+
 		cfg_att := emailer.Attachment{"wg0.conf", []byte(config)}
 		var attachments []emailer.Attachment
 		if clientData.Client.PrivateKey != "" {
@@ -609,6 +616,7 @@ func GlobalSettingSubmit(db store.IStore) echo.HandlerFunc {
 		}
 
 		globalSettings.UpdatedAt = time.Now().UTC()
+		globalSettings.EmailContent = base64.StdEncoding.EncodeToString([]byte(globalSettings.EmailContent))
 
 		// write config to the database
 		if err := db.SaveGlobalSettings(globalSettings); err != nil {
diff --git a/model/setting.go b/model/setting.go
index a316172..1adc0b7 100644
--- a/model/setting.go
+++ b/model/setting.go
@@ -11,5 +11,7 @@ type GlobalSetting struct {
 	MTU                 int       `json:"mtu,string"`
 	PersistentKeepalive int       `json:"persistent_keepalive,string"`
 	ConfigFilePath      string    `json:"config_file_path"`
+	EmailSubject        string    `json:"email_subject"`
+	EmailContent        string    `json:"email_content"`
 	UpdatedAt           time.Time `json:"updated_at"`
 }
diff --git a/store/jsondb/jsondb.go b/store/jsondb/jsondb.go
index 9088f54..b72aea9 100644
--- a/store/jsondb/jsondb.go
+++ b/store/jsondb/jsondb.go
@@ -115,7 +115,18 @@ func (o *JsonDB) GetUser() (model.User, error) {
 // GetGlobalSettings func to query global settings from the database
 func (o *JsonDB) GetGlobalSettings() (model.GlobalSetting, error) {
 	settings := model.GlobalSetting{}
-	return settings, o.conn.Read("server", "global_settings", &settings)
+	err := o.conn.Read("server", "global_settings", &settings)
+	if err != nil {
+		return settings, err
+	}
+	if settings.EmailContent != "" {
+		str, err := base64.StdEncoding.DecodeString(settings.EmailContent)
+		if err != nil {
+			return settings, err
+		}
+		settings.EmailContent = string(str)
+	}
+	return settings, err
 }
 
 // GetServer func to query Server setting from the database
diff --git a/templates/global_settings.html b/templates/global_settings.html
index 81454dc..13add4e 100644
--- a/templates/global_settings.html
+++ b/templates/global_settings.html
@@ -61,6 +61,16 @@ Global Settings
                                     name="config_file_path" placeholder="E.g. /etc/wireguard/wg0.conf"
                                     value="{{ .globalSettings.ConfigFilePath }}">
                             </div>
+                            <div class="form-group">
+                                <label for="email_subject">Email Subject</label>
+                                <input type="text" class="form-control" id="email_subject"
+                                    name="email_subject" placeholder="Your new wireguard configuration"
+                                    value="{{ .globalSettings.EmailSubject }}">
+                            </div>
+                            <div class="form-group">
+                                <label for="email_content">Email Content</label>
+                                <textarea class="form-control" id="email_content" name="email_content" placeholder="<p>Html email template</p>" rows="3"> {{ .globalSettings.EmailContent }} </textarea>
+                            </div>
                         </div>
                         <!-- /.card-body -->
 
@@ -142,7 +152,17 @@ Global Settings
             const mtu = $("#mtu").val();
             const persistent_keepalive = $("#persistent_keepalive").val();
             const config_file_path = $("#config_file_path").val();
-            const data = {"endpoint_address": endpoint_address, "dns_servers": dns_servers, "mtu": mtu, "persistent_keepalive": persistent_keepalive, "config_file_path": config_file_path};
+            const email_subject = $("#email_subject").val();
+            const email_content = $("#email_content").val();
+            const data = {
+              "endpoint_address": endpoint_address,
+              "dns_servers": dns_servers,
+              "mtu": mtu,
+              "persistent_keepalive": persistent_keepalive,
+              "config_file_path": config_file_path,
+              "email_subject": email_subject,
+              "email_content": email_content
+            };
 
             $.ajax({
                 cache: false,
@@ -255,4 +275,4 @@ Global Settings
             });
         });
     </script>
-{{end}}
\ No newline at end of file
+{{end}}