()
| 15 | ) |
| 16 | |
| 17 | func initUser() { |
| 18 | admin, err := op.GetAdmin() |
| 19 | adminPassword := random.String(8) |
| 20 | envpass := os.Getenv("OPENLIST_ADMIN_PASSWORD") |
| 21 | if flags.Dev { |
| 22 | adminPassword = "admin" |
| 23 | } else if len(envpass) > 0 { |
| 24 | adminPassword = envpass |
| 25 | } |
| 26 | if err != nil { |
| 27 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 28 | salt := random.String(16) |
| 29 | admin = &model.User{ |
| 30 | Username: "admin", |
| 31 | Salt: salt, |
| 32 | PwdHash: model.TwoHashPwd(adminPassword, salt), |
| 33 | Role: model.ADMIN, |
| 34 | BasePath: "/", |
| 35 | Authn: "[]", |
| 36 | // 0(can see hidden) - 8(webdav read) & 12(can read archives) - 14(can share) |
| 37 | Permission: 0x71FF, |
| 38 | } |
| 39 | if err := op.CreateUser(admin); err != nil { |
| 40 | panic(err) |
| 41 | } else { |
| 42 | // DO NOT output the password to log file. Only output to console. |
| 43 | // utils.Log.Infof("Successfully created the admin user and the initial password is: %s", adminPassword) |
| 44 | fmt.Printf("Successfully created the admin user and the initial password is: %s\n", adminPassword) |
| 45 | } |
| 46 | } else { |
| 47 | utils.Log.Fatalf("[init user] Failed to get admin user: %v", err) |
| 48 | } |
| 49 | } |
| 50 | _, err = op.GetGuest() |
| 51 | if err != nil { |
| 52 | if errors.Is(err, gorm.ErrRecordNotFound) { |
| 53 | salt := random.String(16) |
| 54 | guest := &model.User{ |
| 55 | Username: "guest", |
| 56 | PwdHash: model.TwoHashPwd("guest", salt), |
| 57 | Salt: salt, |
| 58 | Role: model.GUEST, |
| 59 | BasePath: "/", |
| 60 | Permission: 0, |
| 61 | Disabled: true, |
| 62 | Authn: "[]", |
| 63 | } |
| 64 | if err := db.CreateUser(guest); err != nil { |
| 65 | utils.Log.Fatalf("[init user] Failed to create guest user: %v", err) |
| 66 | } |
| 67 | } else { |
| 68 | utils.Log.Fatalf("[init user] Failed to get guest user: %v", err) |
| 69 | } |
| 70 | } |
| 71 | } |
no test coverage detected