MCPcopy
hub / github.com/songquanpeng/one-api / CreateRootAccountIfNeed

Function CreateRootAccountIfNeed

model/main.go:24–65  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

22var LOG_DB *gorm.DB
23
24func CreateRootAccountIfNeed() error {
25 var user User
26 //if user.Status != util.UserStatusEnabled {
27 if err := DB.First(&user).Error; err != nil {
28 logger.SysLog("no user exists, creating a root user for you: username is root, password is 123456")
29 hashedPassword, err := common.Password2Hash("123456")
30 if err != nil {
31 return err
32 }
33 accessToken := random.GetUUID()
34 if config.InitialRootAccessToken != "" {
35 accessToken = config.InitialRootAccessToken
36 }
37 rootUser := User{
38 Username: "root",
39 Password: hashedPassword,
40 Role: RoleRootUser,
41 Status: UserStatusEnabled,
42 DisplayName: "Root User",
43 AccessToken: accessToken,
44 Quota: 500000000000000,
45 }
46 DB.Create(&rootUser)
47 if config.InitialRootToken != "" {
48 logger.SysLog("creating initial root token as requested")
49 token := Token{
50 Id: 1,
51 UserId: rootUser.Id,
52 Key: config.InitialRootToken,
53 Status: TokenStatusEnabled,
54 Name: "Initial Root Token",
55 CreatedTime: helper.GetTimestamp(),
56 AccessedTime: helper.GetTimestamp(),
57 ExpiredTime: -1,
58 RemainQuota: 500000000000000,
59 UnlimitedQuota: true,
60 }
61 DB.Create(&token)
62 }
63 }
64 return nil
65}
66
67func chooseDB(envName string) (*gorm.DB, error) {
68 dsn := os.Getenv(envName)

Callers 1

mainFunction · 0.92

Calls 4

SysLogFunction · 0.92
Password2HashFunction · 0.92
GetUUIDFunction · 0.92
GetTimestampFunction · 0.92

Tested by

no test coverage detected