mirror of
https://github.com/thomiceli/opengist.git
synced 2025-06-12 13:37:13 +02:00
Style preference tab for user (#467)
This commit is contained in:
@ -1,23 +1,25 @@
|
||||
package db
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/thomiceli/opengist/internal/git"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type User struct {
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Username string `gorm:"uniqueIndex,size:191"`
|
||||
Password string
|
||||
IsAdmin bool
|
||||
CreatedAt int64
|
||||
Email string
|
||||
MD5Hash string // for gravatar, if no Email is specified, the value is random
|
||||
AvatarURL string
|
||||
GithubID string
|
||||
GitlabID string
|
||||
GiteaID string
|
||||
OIDCID string `gorm:"column:oidc_id"`
|
||||
ID uint `gorm:"primaryKey"`
|
||||
Username string `gorm:"uniqueIndex,size:191"`
|
||||
Password string
|
||||
IsAdmin bool
|
||||
CreatedAt int64
|
||||
Email string
|
||||
MD5Hash string // for gravatar, if no Email is specified, the value is random
|
||||
AvatarURL string
|
||||
GithubID string
|
||||
GitlabID string
|
||||
GiteaID string
|
||||
OIDCID string `gorm:"column:oidc_id"`
|
||||
StylePreferences string
|
||||
|
||||
Gists []Gist `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
|
||||
SSHKeys []SSHKey `gorm:"constraint:OnUpdate:CASCADE,OnDelete:CASCADE;foreignKey:UserID"`
|
||||
@ -234,6 +236,15 @@ func (user *User) HasMFA() (bool, bool, error) {
|
||||
return webauthn, totp, err
|
||||
}
|
||||
|
||||
func (user *User) GetStyle() *UserStyleDTO {
|
||||
style := new(UserStyleDTO)
|
||||
err := json.Unmarshal([]byte(user.StylePreferences), style)
|
||||
if err != nil {
|
||||
return nil
|
||||
}
|
||||
return style
|
||||
}
|
||||
|
||||
// -- DTO -- //
|
||||
|
||||
type UserDTO struct {
|
||||
@ -251,3 +262,18 @@ func (dto *UserDTO) ToUser() *User {
|
||||
type UserUsernameDTO struct {
|
||||
Username string `form:"username" validate:"required,max=24,alphanumdash,notreserved"`
|
||||
}
|
||||
|
||||
type UserStyleDTO struct {
|
||||
SoftWrap bool `form:"softwrap" json:"soft_wrap"`
|
||||
RemovedLineColor string `form:"removedlinecolor" json:"removed_line_color" validate:"min=0,max=7"`
|
||||
AddedLineColor string `form:"addedlinecolor" json:"added_line_color" validate:"min=0,max=7"`
|
||||
GitLineColor string `form:"gitlinecolor" json:"git_line_color" validate:"min=0,max=7"`
|
||||
}
|
||||
|
||||
func (dto *UserStyleDTO) ToJson() string {
|
||||
data, err := json.Marshal(dto)
|
||||
if err != nil {
|
||||
return "{}"
|
||||
}
|
||||
return string(data)
|
||||
}
|
||||
|
@ -148,6 +148,17 @@ settings.create-password-help: Create your password to login to Opengist via HTT
|
||||
settings.change-password: Change password
|
||||
settings.change-password-help: Change your password to login to Opengist via HTTP
|
||||
settings.password-label-title: Password
|
||||
settings.header.account: Account
|
||||
settings.header.mfa: MFA
|
||||
settings.header.ssh: SSH
|
||||
settings.header.style: Style
|
||||
settings.style.gist-code: Gist code
|
||||
settings.style.no-soft-wrap: No Soft Wrap
|
||||
settings.style.soft-wrap: Soft Wrap
|
||||
settings.style.removed-lines-color: Removed lines color
|
||||
settings.style.added-lines-color: Added lines color
|
||||
settings.style.git-lines-color: Git lines color
|
||||
settings.style.save-style: Save style
|
||||
|
||||
auth.signup-disabled: Administrator has disabled signing up
|
||||
auth.login: Login
|
||||
|
@ -14,7 +14,7 @@ func BeginTotp(ctx *context.Context) error {
|
||||
return ctx.ErrorRes(500, "Cannot check for user MFA", err)
|
||||
} else if hasTotp {
|
||||
ctx.AddFlash(ctx.Tr("auth.totp.already-enabled"), "error")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/mfa")
|
||||
}
|
||||
|
||||
ogUrl, err := url.Parse(ctx.GetData("baseHttpUrl").(string))
|
||||
@ -47,7 +47,7 @@ func FinishTotp(ctx *context.Context) error {
|
||||
return ctx.ErrorRes(500, "Cannot check for user MFA", err)
|
||||
} else if hasTotp {
|
||||
ctx.AddFlash(ctx.Tr("auth.totp.already-enabled"), "error")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/mfa")
|
||||
}
|
||||
|
||||
dto := &db.TOTPDTO{}
|
||||
@ -134,7 +134,7 @@ func AssertTotp(ctx *context.Context) error {
|
||||
}
|
||||
|
||||
ctx.AddFlash(ctx.Tr("auth.totp.code-used", dto.Code), "warning")
|
||||
redirectUrl = "/settings"
|
||||
redirectUrl = "/settings/mfa"
|
||||
}
|
||||
|
||||
sess.Values["user"] = userId
|
||||
@ -157,7 +157,7 @@ func DisableTotp(ctx *context.Context) error {
|
||||
}
|
||||
|
||||
ctx.AddFlash(ctx.Tr("auth.totp.disabled"), "success")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/mfa")
|
||||
}
|
||||
|
||||
func RegenerateTotpRecoveryCodes(ctx *context.Context) error {
|
||||
|
@ -5,13 +5,19 @@ import (
|
||||
"github.com/thomiceli/opengist/internal/web/context"
|
||||
)
|
||||
|
||||
func UserSettings(ctx *context.Context) error {
|
||||
func UserAccount(ctx *context.Context) error {
|
||||
user := ctx.User
|
||||
|
||||
keys, err := db.GetSSHKeysByUserID(user.ID)
|
||||
if err != nil {
|
||||
return ctx.ErrorRes(500, "Cannot get SSH keys", err)
|
||||
}
|
||||
ctx.SetData("email", user.Email)
|
||||
ctx.SetData("hasPassword", user.Password != "")
|
||||
ctx.SetData("disableForm", ctx.GetData("DisableLoginForm"))
|
||||
ctx.SetData("settingsHeaderPage", "account")
|
||||
ctx.SetData("htmlTitle", ctx.TrH("settings"))
|
||||
return ctx.Html("settings_account.html")
|
||||
}
|
||||
|
||||
func UserMFA(ctx *context.Context) error {
|
||||
user := ctx.User
|
||||
|
||||
passkeys, err := db.GetAllCredentialsForUser(user.ID)
|
||||
if err != nil {
|
||||
@ -23,12 +29,48 @@ func UserSettings(ctx *context.Context) error {
|
||||
return ctx.ErrorRes(500, "Cannot get MFA status", err)
|
||||
}
|
||||
|
||||
ctx.SetData("email", user.Email)
|
||||
ctx.SetData("sshKeys", keys)
|
||||
ctx.SetData("passkeys", passkeys)
|
||||
ctx.SetData("hasTotp", hasTotp)
|
||||
ctx.SetData("hasPassword", user.Password != "")
|
||||
ctx.SetData("disableForm", ctx.GetData("DisableLoginForm"))
|
||||
ctx.SetData("settingsHeaderPage", "mfa")
|
||||
ctx.SetData("htmlTitle", ctx.TrH("settings"))
|
||||
return ctx.Html("settings.html")
|
||||
return ctx.Html("settings_mfa.html")
|
||||
}
|
||||
|
||||
func UserSSHKeys(ctx *context.Context) error {
|
||||
user := ctx.User
|
||||
|
||||
keys, err := db.GetSSHKeysByUserID(user.ID)
|
||||
if err != nil {
|
||||
return ctx.ErrorRes(500, "Cannot get SSH keys", err)
|
||||
}
|
||||
|
||||
ctx.SetData("sshKeys", keys)
|
||||
ctx.SetData("settingsHeaderPage", "ssh")
|
||||
ctx.SetData("htmlTitle", ctx.TrH("settings"))
|
||||
return ctx.Html("settings_ssh.html")
|
||||
}
|
||||
|
||||
func UserStyle(ctx *context.Context) error {
|
||||
ctx.SetData("settingsHeaderPage", "style")
|
||||
ctx.SetData("htmlTitle", ctx.TrH("settings"))
|
||||
return ctx.Html("settings_style.html")
|
||||
}
|
||||
|
||||
func ProcessUserStyle(ctx *context.Context) error {
|
||||
styleDto := new(db.UserStyleDTO)
|
||||
if err := ctx.Bind(styleDto); err != nil {
|
||||
return ctx.ErrorRes(400, ctx.Tr("error.cannot-bind-data"), err)
|
||||
}
|
||||
|
||||
if err := ctx.Validate(styleDto); err != nil {
|
||||
return ctx.ErrorRes(400, "Invalid data", err)
|
||||
}
|
||||
user := ctx.User
|
||||
user.StylePreferences = styleDto.ToJson()
|
||||
if err := user.Update(); err != nil {
|
||||
return ctx.ErrorRes(500, "Cannot update user styles", err)
|
||||
}
|
||||
|
||||
ctx.AddFlash("Updated style", "success")
|
||||
return ctx.RedirectTo("/settings/style")
|
||||
}
|
||||
|
@ -20,7 +20,7 @@ func SshKeysProcess(ctx *context.Context) error {
|
||||
|
||||
if err := ctx.Validate(dto); err != nil {
|
||||
ctx.AddFlash(validator.ValidationMessages(&err, ctx.GetData("locale").(*i18n.Locale)), "error")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
key := dto.ToSSHKey()
|
||||
|
||||
@ -29,7 +29,7 @@ func SshKeysProcess(ctx *context.Context) error {
|
||||
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key.Content))
|
||||
if err != nil {
|
||||
ctx.AddFlash(ctx.Tr("flash.user.invalid-ssh-key"), "error")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
key.Content = strings.TrimSpace(string(ssh.MarshalAuthorizedKey(pubKey)))
|
||||
|
||||
@ -38,7 +38,7 @@ func SshKeysProcess(ctx *context.Context) error {
|
||||
return ctx.ErrorRes(500, "Cannot check if SSH key exists", err)
|
||||
}
|
||||
ctx.AddFlash(ctx.Tr("settings.ssh-key-exists"), "error")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
|
||||
if err := key.Create(); err != nil {
|
||||
@ -46,20 +46,20 @@ func SshKeysProcess(ctx *context.Context) error {
|
||||
}
|
||||
|
||||
ctx.AddFlash(ctx.Tr("flash.user.ssh-key-added"), "success")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
|
||||
func SshKeysDelete(ctx *context.Context) error {
|
||||
user := ctx.User
|
||||
keyId, err := strconv.Atoi(ctx.Param("id"))
|
||||
if err != nil {
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
|
||||
key, err := db.GetSSHKeyByID(uint(keyId))
|
||||
|
||||
if err != nil || key.UserID != user.ID {
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
|
||||
if err := key.Delete(); err != nil {
|
||||
@ -67,5 +67,5 @@ func SshKeysDelete(ctx *context.Context) error {
|
||||
}
|
||||
|
||||
ctx.AddFlash(ctx.Tr("flash.user.ssh-key-deleted"), "success")
|
||||
return ctx.RedirectTo("/settings")
|
||||
return ctx.RedirectTo("/settings/ssh")
|
||||
}
|
||||
|
@ -275,6 +275,7 @@ func sessionInit(next Handler) Handler {
|
||||
if user != nil {
|
||||
ctx.User = user
|
||||
ctx.SetData("userLogged", user)
|
||||
ctx.SetData("currentStyle", user.GetStyle())
|
||||
}
|
||||
return next(ctx)
|
||||
}
|
||||
|
@ -186,6 +186,10 @@ func (s *Server) setFuncMap() {
|
||||
}
|
||||
return str
|
||||
},
|
||||
"hexToRgb": func(hex string) string {
|
||||
h, _ := strconv.ParseUint(strings.TrimPrefix(hex, "#"), 16, 32)
|
||||
return fmt.Sprintf("%d, %d, %d,", (h>>16)&0xFF, (h>>8)&0xFF, h&0xFF)
|
||||
},
|
||||
}
|
||||
|
||||
t := template.Must(template.New("t").Funcs(fm).ParseFS(templates.Files, "*/*.html"))
|
||||
|
@ -56,7 +56,11 @@ func (s *Server) registerRoutes() {
|
||||
sA := r.SubGroup("/settings")
|
||||
{
|
||||
sA.Use(logged)
|
||||
sA.GET("", settings.UserSettings)
|
||||
sA.GET("", settings.UserAccount)
|
||||
sA.GET("/mfa", settings.UserMFA)
|
||||
sA.GET("/ssh", settings.UserSSHKeys)
|
||||
sA.GET("/style", settings.UserStyle)
|
||||
sA.POST("/style", settings.ProcessUserStyle)
|
||||
sA.POST("/email", settings.EmailProcess)
|
||||
sA.DELETE("/account", settings.AccountDeleteProcess)
|
||||
sA.POST("/ssh-keys", settings.SshKeysProcess)
|
||||
|
Reference in New Issue
Block a user