Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add 2 new admin actions #191

Merged
merged 3 commits into from
Jan 2, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
Change username
  • Loading branch information
thomiceli committed Jan 1, 2024
commit 624dc76f71c41a8132753d038dd6c146096164fe
1 change: 1 addition & 0 deletions internal/i18n/locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions internal/web/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
{
Expand Down
29 changes: 29 additions & 0 deletions internal/web/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,3 +139,32 @@ 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")
}

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")
}
Loading