mirror of
https://github.com/thomiceli/opengist.git
synced 2025-06-13 05:47:12 +02:00
Change username setting (#190)
This commit is contained in:
@ -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")
|
||||
{
|
||||
|
@ -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")
|
||||
}
|
||||
|
Reference in New Issue
Block a user