From ac20b9f8b79d63861a929231cd2067659d7c3981 Mon Sep 17 00:00:00 2001 From: Bekir Pehlivan Date: Mon, 23 Oct 2023 18:06:02 +0300 Subject: [PATCH] added localization and one click validation --- api/router.go | 2 + controller/admin_controller.go | 2 +- controller/localization.go | 88 ++++++++++++++++++++ controller/login_controller.go | 3 +- controller/root_controller.go | 4 + controller/validation_controller.go | 56 +++++++------ internal/notify.go | 26 ++++++ templates/admin.html | 18 ++++- templates/admin_navbar.html | 2 +- templates/index.html | 75 ++++++++++-------- templates/login.html | 13 ++- templates/login_navbar.html | 2 +- templates/success.html | 4 +- templates/table.html | 2 +- templates/tbody.html | 119 +++++++++++++++++++++++++--- 15 files changed, 334 insertions(+), 82 deletions(-) create mode 100644 controller/localization.go create mode 100644 internal/notify.go diff --git a/api/router.go b/api/router.go index 404d6e2..dc002aa 100644 --- a/api/router.go +++ b/api/router.go @@ -19,6 +19,8 @@ func InitServer() { log.Error(err) } + controller.InitLocalizer() + gin.SetMode(gin.ReleaseMode) router := gin.Default() diff --git a/controller/admin_controller.go b/controller/admin_controller.go index f5874d8..783634d 100644 --- a/controller/admin_controller.go +++ b/controller/admin_controller.go @@ -20,5 +20,5 @@ func HandleAdmin(ctx *gin.Context) { return } - ctx.HTML(200, "admin.html", nil) + ctx.HTML(200, "admin.html", Data["admin"]) } diff --git a/controller/localization.go b/controller/localization.go new file mode 100644 index 0000000..b21347a --- /dev/null +++ b/controller/localization.go @@ -0,0 +1,88 @@ +package controller + +import "flag" + +var ( + lang = flag.String("lang", "en", "Language") + Data map[string]map[string]string +) + +func InitLocalizer() { + + switch *lang { + case "en": + Data = map[string]map[string]string{ + "index": { + "source_details": "Source Details", + "destination_details": "Destination Details", + "server": "Server", + "account": "Account", + "password": "Password", + "validate": "Validate Credentials", + "sync": "Start Synchronization", + "user_queue": "User Table", + }, + "login": { + "sign_in": "Sign In", + "description": "Sign in to access admin panel", + "username": "Username", + "password": "Password", + }, + "admin": { + "index": "Index", + "source_server": "Source Server", + "source_account": "Source Account", + "dest_server": "Destination Server", + "dest_account": "Destination Account", + "status": "Status", + "actions": "Actions", + }, + "table": { + "index": "Index", + "source_server": "Source Server", + "source_account": "Source Account", + "dest_server": "Destination Server", + "dest_account": "Destination Account", + "status": "Status", + "actions": "Actions", + }, + } + case "tr": + Data = map[string]map[string]string{ + "index": { + "source_details": "Kaynak Bilgileri", + "destination_details": "Hedef Bilgileri", + "server": "Sunucu", + "account": "Hesap", + "password": "Parola", + "validate": "Bilgileri Doğrula", + "sync": "Senkronizasyonu Başlat", + "user_queue": "Kullanıcı İşlem Kuyruğu", + }, + "login": { + "sign_in": "Giriş Yap", + "description": "Admin paneline erişebilmek için giriş yapın.", + "username": "Kullanıcı Adı", + "password": "Parola", + }, + "admin": { + "index": "Sıra", + "source_server": "Kaynak Sunucu", + "source_account": "Kaynak Hesap", + "dest_server": "Hedef Sunucu", + "dest_account": "Hedef Hesap", + "status": "Durum", + "actions": "Eylemler", + }, + "table": { + "index": "Sıra", + "source_server": "Kaynak Sunucu", + "source_account": "Kaynak Hesap", + "dest_server": "Hedef Sunucu", + "dest_account": "Hedef Hesap", + "status": "Durum", + "actions": "Eylemler", + }, + } + } +} diff --git a/controller/login_controller.go b/controller/login_controller.go index 27f1d9d..0c6d648 100644 --- a/controller/login_controller.go +++ b/controller/login_controller.go @@ -17,7 +17,8 @@ func HandleLogin(ctx *gin.Context) { ctx.Redirect(http.StatusTemporaryRedirect, "/") return } - ctx.HTML(200, "login.html", nil) + + ctx.HTML(200, "login.html", Data["login"]) } type user struct { diff --git a/controller/root_controller.go b/controller/root_controller.go index 3c3f4fa..5943d91 100644 --- a/controller/root_controller.go +++ b/controller/root_controller.go @@ -34,9 +34,13 @@ func HandleRoot(ctx *gin.Context) { data := struct { SourceDetails internal.Credentials DestinationDetails internal.Credentials + Text map[string]string + Table map[string]string }{ SourceDetails: sourceDetails, DestinationDetails: destinationDetails, + Text: Data["index"], + Table: Data["table"], } ctx.HTML(200, "index.html", data) } diff --git a/controller/validation_controller.go b/controller/validation_controller.go index 8cc0438..2bc4b7f 100644 --- a/controller/validation_controller.go +++ b/controller/validation_controller.go @@ -8,45 +8,55 @@ import ( func HandleValidate(ctx *gin.Context) { - sourceCreds := ctx.PostForm("source_creds") - destCreds := ctx.PostForm("destination_creds") + validate := ctx.PostForm("validate") submitsync := ctx.PostForm("submit_sync") - var Server, Account, Password string - var Source bool + var SServer, SAccount, SPassword string + var DServer, DAccount, DPassword string - if sourceCreds != "" { - Server = ctx.PostForm("source_server") - Account = ctx.PostForm("source_account") - Password = ctx.PostForm("source_password") - Source = true + if validate != "" { + SServer = ctx.PostForm("source_server") + SAccount = ctx.PostForm("source_account") + SPassword = ctx.PostForm("source_password") + DServer = ctx.PostForm("destination_server") + DAccount = ctx.PostForm("destination_account") + DPassword = ctx.PostForm("destination_password") } - if destCreds != "" { - Server = ctx.PostForm("destination_server") - Account = ctx.PostForm("destination_account") - Password = ctx.PostForm("destination_password") - Source = false - } - - if destCreds == "" && sourceCreds == "" && submitsync != "" { + if validate == "" && submitsync != "" { HandleSync(ctx) return } creds := internal.Credentials{ - Server: Server, - Account: Account, - Password: Password, - Source: Source, + Server: SServer, + Account: SAccount, + Password: SPassword, + Source: true, } log.Infof("Validating credentials for: %s", creds.Account) err := internal.ValidateCredentials(creds) if err != nil { - ctx.HTML(200, "error.html", err.Error()) + ctx.HTML(200, "error.html", "Couldn't verify for user: "+SAccount) return } - ctx.HTML(200, "success.html", creds) + + creds = internal.Credentials{ + Server: DServer, + Account: DAccount, + Password: DPassword, + Source: false, + } + + log.Infof("Validating credentials for: %s", creds.Account) + + err = internal.ValidateCredentials(creds) + if err != nil { + ctx.HTML(200, "error.html", "Couldn't verify for user: "+DAccount) + return + } + + ctx.HTML(200, "success.html", nil) } diff --git a/internal/notify.go b/internal/notify.go new file mode 100644 index 0000000..975eed1 --- /dev/null +++ b/internal/notify.go @@ -0,0 +1,26 @@ +package internal + +import ( + "net/smtp" +) + +func Email(creds Credentials) error { + + smtpHost := "" + smtpPort := "" + from := "" + username := "" + password := "" + to := creds.Account + subject := "" + message := "" + + auth := smtp.CRAMMD5Auth(username, password) + + msg := []byte("From: " + from + "\r\n" + + "To: " + to + "\r\n" + + "Subject: [" + "" + "] " + subject + "\r\n\r\n" + + message + "\r\n") + + return smtp.SendMail(smtpHost+":"+smtpPort, auth, from, []string{to}, msg) +} diff --git a/templates/admin.html b/templates/admin.html index bffdaed..44d7231 100644 --- a/templates/admin.html +++ b/templates/admin.html @@ -5,7 +5,7 @@ - imapsync-web-admin + Monomail-Sync Admin