diff --git a/internal/i18n/locales/en-US.yml b/internal/i18n/locales/en-US.yml index 1b5bbb9..628fd64 100644 --- a/internal/i18n/locales/en-US.yml +++ b/internal/i18n/locales/en-US.yml @@ -109,6 +109,7 @@ settings.delete-ssh-key-confirm: Confirm deletion of SSH key settings.ssh-key-added-at: Added settings.ssh-key-never-used: Never used settings.ssh-key-last-used: Last used +settings.change-username: Change username settings.create-password: Create password settings.create-password-help: Create your password to login to Opengist via HTTP settings.change-password: Change password diff --git a/internal/web/server.go b/internal/web/server.go index 3edd7e0..0fdd0e3 100644 --- a/internal/web/server.go +++ b/internal/web/server.go @@ -222,6 +222,7 @@ func NewServer(isDev bool) *Server { g1.POST("/settings/ssh-keys", sshKeysProcess, logged) g1.DELETE("/settings/ssh-keys/:id", sshKeysDelete, logged) g1.PUT("/settings/password", passwordProcess, logged) + g1.PUT("/settings/username", usernameProcess, logged) g2 := g1.Group("/admin-panel") { diff --git a/internal/web/settings.go b/internal/web/settings.go index 457324d..1007f77 100644 --- a/internal/web/settings.go +++ b/internal/web/settings.go @@ -3,6 +3,9 @@ package web import ( "crypto/md5" "fmt" + "github.com/thomiceli/opengist/internal/config" + "os" + "path/filepath" "strconv" "strings" "time" @@ -139,3 +142,39 @@ func passwordProcess(ctx echo.Context) error { addFlash(ctx, "Password updated", "success") return redirect(ctx, "/settings") } + +func usernameProcess(ctx echo.Context) error { + user := getUserLogged(ctx) + + dto := new(db.UserDTO) + if err := ctx.Bind(dto); err != nil { + return errorRes(400, "Cannot bind data", err) + } + dto.Password = user.Password + + if err := ctx.Validate(dto); err != nil { + addFlash(ctx, validationMessages(&err), "error") + return redirect(ctx, "/settings") + } + + if exists, err := db.UserExists(dto.Username); err != nil || exists { + addFlash(ctx, "Username already exists", "error") + return redirect(ctx, "/settings") + } + + err := os.Rename( + filepath.Join(config.C.OpengistHome, "repos", user.Username), + filepath.Join(config.C.OpengistHome, "repos", dto.Username)) + if err != nil { + return errorRes(500, "Cannot rename user directory", err) + } + + user.Username = dto.Username + + if err := user.Update(); err != nil { + return errorRes(500, "Cannot update username", err) + } + + addFlash(ctx, "Username updated", "success") + return redirect(ctx, "/settings") +} diff --git a/templates/pages/settings.html b/templates/pages/settings.html index 2592616..f2b2c83 100644 --- a/templates/pages/settings.html +++ b/templates/pages/settings.html @@ -6,104 +6,28 @@
-
-
+
+
-
+

- {{ .locale.Tr "settings.email" }} + {{ .locale.Tr "settings.change-username" }}

-

- {{ .locale.Tr "settings.email-help" }} -

-
+
- +
- + + + {{ .csrfHtml }}
- {{ if or .githubOauth .gitlabOauth .giteaOauth .oidcOauth }} -
-
-

- {{ .locale.Tr "settings.link-accounts" }} -

-
- - {{ if .githubOauth }} - {{ if .userLogged.GithubID }} - - {{ .locale.Tr "settings.unlink-github-account" }} - - {{ else }} - - {{ .locale.Tr "settings.link-github-account" }} - - {{ end }} - {{ end }} - - {{ if .gitlabOauth }} - {{ if .userLogged.GitlabID }} - - {{ .locale.Tr "settings.unlink-gitlab-account" }} - - {{ else }} - - {{ .locale.Tr "settings.link-gitlab-account" }} - - {{ end }} - {{ end }} - - {{ if .giteaOauth }} - {{ if .userLogged.GiteaID }} - - {{ .locale.Tr "settings.unlink-gitea-account" }} - - {{ else }} - - {{ .locale.Tr "settings.link-gitea-account" }} - - {{ end }} - {{ end }} - {{ if .oidcOauth }} - {{ if .userLogged.OIDCID }} - - Unlink OpenID account - - {{ else }} - - Link OpenID account - - {{ end }} - {{ end }} - -
-
-
- {{ end }} -
-
-

- {{ .locale.Tr "settings.delete-account" }} -

-
- - - {{ .csrfHtml }} -
-
-
-
-

@@ -140,6 +64,89 @@

+
+
+
+

+ {{ .locale.Tr "settings.email" }} +

+

+ {{ .locale.Tr "settings.email-help" }} +

+
+
+
+ +
+
+ + {{ .csrfHtml }} +
+
+
+ {{ if or .githubOauth .gitlabOauth .giteaOauth .oidcOauth }} +
+
+

+ {{ .locale.Tr "settings.link-accounts" }} +

+
+ + {{ if .githubOauth }} + {{ if .userLogged.GithubID }} + + {{ .locale.Tr "settings.unlink-github-account" }} + + {{ else }} + + {{ .locale.Tr "settings.link-github-account" }} + + {{ end }} + {{ end }} + + {{ if .gitlabOauth }} + {{ if .userLogged.GitlabID }} + + {{ .locale.Tr "settings.unlink-gitlab-account" }} + + {{ else }} + + {{ .locale.Tr "settings.link-gitlab-account" }} + + {{ end }} + {{ end }} + + {{ if .giteaOauth }} + {{ if .userLogged.GiteaID }} + + {{ .locale.Tr "settings.unlink-gitea-account" }} + + {{ else }} + + {{ .locale.Tr "settings.link-gitea-account" }} + + {{ end }} + {{ end }} + {{ if .oidcOauth }} + {{ if .userLogged.OIDCID }} + + Unlink OpenID account + + {{ else }} + + Link OpenID account + + {{ end }} + {{ end }} +
+
+
+ {{ end }} +

@@ -201,6 +208,18 @@

+
+
+

+ {{ .locale.Tr "settings.delete-account" }} +

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