Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/internal/bootstrap/data/user.go
1562 views
1
package data
2
3
import (
4
"github.com/alist-org/alist/v3/internal/db"
5
"os"
6
7
"github.com/alist-org/alist/v3/cmd/flags"
8
"github.com/alist-org/alist/v3/internal/model"
9
"github.com/alist-org/alist/v3/internal/op"
10
"github.com/alist-org/alist/v3/pkg/utils"
11
"github.com/alist-org/alist/v3/pkg/utils/random"
12
"github.com/pkg/errors"
13
"gorm.io/gorm"
14
)
15
16
func initUser() {
17
guest, err := op.GetGuest()
18
if err != nil {
19
if errors.Is(err, gorm.ErrRecordNotFound) {
20
salt := random.String(16)
21
guestRole, _ := op.GetRoleByName("guest")
22
guest = &model.User{
23
Username: "guest",
24
PwdHash: model.TwoHashPwd("guest", salt),
25
Salt: salt,
26
Role: model.Roles{int(guestRole.ID)},
27
BasePath: "/",
28
Permission: 0,
29
Disabled: true,
30
Authn: "[]",
31
}
32
if err := db.CreateUser(guest); err != nil {
33
utils.Log.Fatalf("[init user] Failed to create guest user: %v", err)
34
}
35
} else {
36
utils.Log.Fatalf("[init user] Failed to get guest user: %v", err)
37
}
38
}
39
admin, err := op.GetAdmin()
40
adminPassword := random.String(8)
41
envpass := os.Getenv("ALIST_ADMIN_PASSWORD")
42
if flags.Dev {
43
adminPassword = "admin"
44
} else if len(envpass) > 0 {
45
adminPassword = envpass
46
}
47
if err != nil {
48
if errors.Is(err, gorm.ErrRecordNotFound) {
49
salt := random.String(16)
50
adminRole, _ := op.GetRoleByName("admin")
51
admin = &model.User{
52
Username: "admin",
53
Salt: salt,
54
PwdHash: model.TwoHashPwd(adminPassword, salt),
55
Role: model.Roles{int(adminRole.ID)},
56
BasePath: "/",
57
Authn: "[]",
58
// 0(can see hidden) - 7(can remove) & 12(can read archives) - 13(can decompress archives)
59
Permission: 0xFFFF,
60
}
61
if err := op.CreateUser(admin); err != nil {
62
panic(err)
63
} else {
64
utils.Log.Infof("Successfully created the admin user and the initial password is: %s", adminPassword)
65
}
66
} else {
67
utils.Log.Fatalf("[init user] Failed to get admin user: %v", err)
68
}
69
}
70
}
71
72