summaryrefslogtreecommitdiffstats
path: root/services/user/rename.go
blob: af195d7d76a26069d34dbce0cbbffbd03b692a08 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
// Copyright 2023 The Gitea Authors. All rights reserved.
// SPDX-License-Identifier: MIT

package user

import (
	"context"
	"fmt"
	"strings"

	user_model "code.gitea.io/gitea/models/user"
	"code.gitea.io/gitea/modules/log"
	"code.gitea.io/gitea/services/agit"
	container_service "code.gitea.io/gitea/services/packages/container"
)

func renameUser(ctx context.Context, u *user_model.User, newUserName string) error {
	if u.IsOrganization() {
		return fmt.Errorf("cannot rename organization")
	}

	if err := user_model.ChangeUserName(ctx, u, newUserName); err != nil {
		return err
	}

	if err := agit.UserNameChanged(ctx, u, newUserName); err != nil {
		return err
	}
	if err := container_service.UpdateRepositoryNames(ctx, u, newUserName); err != nil {
		return err
	}

	u.Name = newUserName
	u.LowerName = strings.ToLower(newUserName)
	if err := user_model.UpdateUser(ctx, u, false); err != nil {
		return err
	}

	log.Trace("User name changed: %s -> %s", u.Name, newUserName)
	return nil
}