From 7b27db88496edcfc73a68e6d419488bed666ec29 Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Sun, 19 Mar 2023 16:29:14 +0100 Subject: [PATCH] Added email and gravatar to user config --- internal/models/user.go | 6 ++ internal/web/{ssh.go => config.go} | 60 +++++++++++++++---- internal/web/run.go | 11 +++- internal/web/util.go | 2 +- templates/base/base_header.html | 4 +- .../pages/{ssh_keys.html => settings.html} | 47 +++++++++++++-- 6 files changed, 107 insertions(+), 23 deletions(-) rename internal/web/{ssh.go => config.go} (54%) rename templates/pages/{ssh_keys.html => settings.html} (61%) diff --git a/internal/models/user.go b/internal/models/user.go index 6333389..ce336fe 100644 --- a/internal/models/user.go +++ b/internal/models/user.go @@ -10,6 +10,8 @@ type User struct { Password string IsAdmin bool CreatedAt int64 + Email string + MD5Hash string // for gravatar, if no Email is specified, the value is random Gists []Gist `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"` SSHKeys []SSHKey `gorm:"foreignKey:UserID"` @@ -92,6 +94,10 @@ func (user *User) Create() error { return db.Create(&user).Error } +func (user *User) Update() error { + return db.Save(&user).Error +} + func (user *User) Delete() error { return db.Delete(&user).Error } diff --git a/internal/web/ssh.go b/internal/web/config.go similarity index 54% rename from internal/web/ssh.go rename to internal/web/config.go index 60cf3bf..042d796 100644 --- a/internal/web/ssh.go +++ b/internal/web/config.go @@ -1,15 +1,19 @@ package web import ( + "crypto/md5" "crypto/sha256" "encoding/base64" + "fmt" "github.com/labstack/echo/v4" "golang.org/x/crypto/ssh" "opengist/internal/models" "strconv" + "strings" + "time" ) -func sshKeys(ctx echo.Context) error { +func userSettings(ctx echo.Context) error { user := getUserLogged(ctx) keys, err := models.GetSSHKeysByUserID(user.ID) @@ -17,14 +21,48 @@ func sshKeys(ctx echo.Context) error { return errorRes(500, "Cannot get SSH keys", err) } + setData(ctx, "email", user.Email) setData(ctx, "sshKeys", keys) - setData(ctx, "htmlTitle", "Manage SSH keys") - return html(ctx, "ssh_keys.html") + setData(ctx, "htmlTitle", "Settings") + return html(ctx, "settings.html") +} + +func emailProcess(ctx echo.Context) error { + user := getUserLogged(ctx) + email := ctx.FormValue("email") + var hash string + + fmt.Println() + + if email == "" { + // generate random md5 string + hash = fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()))) + } else { + hash = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email))))) + } + + user.Email = email + user.MD5Hash = hash + + if err := user.Update(); err != nil { + return errorRes(500, "Cannot update email", err) + } + + addFlash(ctx, "Email updated", "success") + return redirect(ctx, "/settings") +} + +func accountDeleteProcess(ctx echo.Context) error { + user := getUserLogged(ctx) + + if err := user.Delete(); err != nil { + return errorRes(500, "Cannot delete this user", err) + } + + return redirect(ctx, "/all") } func sshKeysProcess(ctx echo.Context) error { - setData(ctx, "htmlTitle", "Manage SSH keys") - user := getUserLogged(ctx) var dto = new(models.SSHKeyDTO) @@ -34,7 +72,7 @@ func sshKeysProcess(ctx echo.Context) error { if err := ctx.Validate(dto); err != nil { addFlash(ctx, validationMessages(&err), "error") - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } key := dto.ToSSHKey() @@ -43,7 +81,7 @@ func sshKeysProcess(ctx echo.Context) error { pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key.Content)) if err != nil { addFlash(ctx, "Invalid SSH key", "error") - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } sha := sha256.Sum256(pubKey.Marshal()) @@ -54,7 +92,7 @@ func sshKeysProcess(ctx echo.Context) error { } addFlash(ctx, "SSH key added", "success") - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } func sshKeysDelete(ctx echo.Context) error { @@ -62,13 +100,13 @@ func sshKeysDelete(ctx echo.Context) error { keyId, err := strconv.Atoi(ctx.Param("id")) if err != nil { - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } key, err := models.GetSSHKeyByID(uint(keyId)) if err != nil || key.UserID != user.ID { - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } if err := key.Delete(); err != nil { @@ -76,5 +114,5 @@ func sshKeysDelete(ctx echo.Context) error { } addFlash(ctx, "SSH key deleted", "success") - return redirect(ctx, "/ssh-keys") + return redirect(ctx, "/settings") } diff --git a/internal/web/run.go b/internal/web/run.go index 8ddbd44..96e6c5e 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -100,6 +100,9 @@ func Start() { "slug": func(s string) string { return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-") }, + "avatarUrl": func(user *models.User) string { + return "https://www.gravatar.com/avatar/" + user.MD5Hash + "?d=identicon&s=200" + }, }).ParseGlob("templates/*/*.html")), } @@ -144,9 +147,11 @@ func Start() { g1.POST("/login", processLogin) g1.GET("/logout", logout) - g1.GET("/ssh-keys", sshKeys, logged) - g1.POST("/ssh-keys", sshKeysProcess, logged) - g1.DELETE("/ssh-keys/:id", sshKeysDelete, logged) + g1.GET("/settings", userSettings, logged) + g1.POST("/settings/email", emailProcess, logged) + g1.DELETE("/settings/account", accountDeleteProcess, logged) + g1.POST("/settings/ssh-keys", sshKeysProcess, logged) + g1.DELETE("/settings/ssh-keys/:id", sshKeysDelete, logged) g2 := g1.Group("/admin") { diff --git a/internal/web/util.go b/internal/web/util.go index baee85d..ae79df3 100644 --- a/internal/web/util.go +++ b/internal/web/util.go @@ -148,7 +148,7 @@ func validateReservedKeywords(fl validator.FieldLevel) bool { name := fl.Field().String() restrictedNames := map[string]struct{}{} - for _, restrictedName := range []string{"register", "login", "logout", "ssh-keys", "admin", "all"} { + for _, restrictedName := range []string{"register", "login", "logout", "config", "admin", "all"} { restrictedNames[restrictedName] = struct{}{} } diff --git a/templates/base/base_header.html b/templates/base/base_header.html index a05d952..f937a0e 100644 --- a/templates/base/base_header.html +++ b/templates/base/base_header.html @@ -58,7 +58,7 @@ {{ if .userLogged.IsAdmin }} {{ end }} - +

{{ .userLogged.Username }}

@@ -90,7 +90,7 @@
All {{ if .userLogged }} My gists - SSH Keys + Config {{ if .userLogged.IsAdmin }} Admin diff --git a/templates/pages/ssh_keys.html b/templates/pages/settings.html similarity index 61% rename from templates/pages/ssh_keys.html rename to templates/pages/settings.html index 308cdbf..730ba25 100644 --- a/templates/pages/ssh_keys.html +++ b/templates/pages/settings.html @@ -2,19 +2,54 @@
-

SSH Keys

-

Used only to push gists using Git via SSH

+

Settings

-
+
-

+

+ Email +

+

+ Used for commits and Gravatar +

+
+
+
+ +
+
+ + {{ .csrfHtml }} +
+
+
+
+
+

+ Delete account +

+
+ + + {{ .csrfHtml }} +
+
+
+
+
+
+
+

Add SSH Key

-
+

+ Used only to push gists using Git via SSH +

+
@@ -53,7 +88,7 @@

Last used {{ .LastUsedAt }}

{{ end }}
- + {{ $.csrfHtml }}