mirror of
https://github.com/thomiceli/opengist.git
synced 2025-07-14 03:41:51 +02:00
Change username setting (#190)
This commit is contained in:
@ -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