mirror of
https://github.com/thomiceli/opengist.git
synced 2025-06-12 21:47:11 +02:00
Added email and gravatar to user config
This commit is contained in:
@ -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")
|
||||
}
|
@ -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")
|
||||
{
|
||||
|
@ -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{}{}
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user