Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
alist-org
GitHub Repository: alist-org/alist
Path: blob/main/cmd/admin.go
1541 views
1
/*
2
Copyright © 2022 NAME HERE <EMAIL ADDRESS>
3
*/
4
package cmd
5
6
import (
7
"github.com/alist-org/alist/v3/internal/conf"
8
"github.com/alist-org/alist/v3/internal/op"
9
"github.com/alist-org/alist/v3/internal/setting"
10
"github.com/alist-org/alist/v3/pkg/utils"
11
"github.com/alist-org/alist/v3/pkg/utils/random"
12
"github.com/spf13/cobra"
13
)
14
15
// AdminCmd represents the password command
16
var AdminCmd = &cobra.Command{
17
Use: "admin",
18
Aliases: []string{"password"},
19
Short: "Show admin user's info and some operations about admin user's password",
20
Run: func(cmd *cobra.Command, args []string) {
21
Init()
22
defer Release()
23
admin, err := op.GetAdmin()
24
if err != nil {
25
utils.Log.Errorf("failed get admin user: %+v", err)
26
} else {
27
utils.Log.Infof("Admin user's username: %s", admin.Username)
28
utils.Log.Infof("The password can only be output at the first startup, and then stored as a hash value, which cannot be reversed")
29
utils.Log.Infof("You can reset the password with a random string by running [alist admin random]")
30
utils.Log.Infof("You can also set a new password by running [alist admin set NEW_PASSWORD]")
31
}
32
},
33
}
34
35
var RandomPasswordCmd = &cobra.Command{
36
Use: "random",
37
Short: "Reset admin user's password to a random string",
38
Run: func(cmd *cobra.Command, args []string) {
39
newPwd := random.String(8)
40
setAdminPassword(newPwd)
41
},
42
}
43
44
var SetPasswordCmd = &cobra.Command{
45
Use: "set",
46
Short: "Set admin user's password",
47
Run: func(cmd *cobra.Command, args []string) {
48
if len(args) == 0 {
49
utils.Log.Errorf("Please enter the new password")
50
return
51
}
52
setAdminPassword(args[0])
53
},
54
}
55
56
var ShowTokenCmd = &cobra.Command{
57
Use: "token",
58
Short: "Show admin token",
59
Run: func(cmd *cobra.Command, args []string) {
60
Init()
61
defer Release()
62
token := setting.GetStr(conf.Token)
63
utils.Log.Infof("Admin token: %s", token)
64
},
65
}
66
67
func setAdminPassword(pwd string) {
68
Init()
69
defer Release()
70
admin, err := op.GetAdmin()
71
if err != nil {
72
utils.Log.Errorf("failed get admin user: %+v", err)
73
return
74
}
75
admin.SetPassword(pwd)
76
if err := op.UpdateUser(admin); err != nil {
77
utils.Log.Errorf("failed update admin user: %+v", err)
78
return
79
}
80
utils.Log.Infof("admin user has been updated:")
81
utils.Log.Infof("username: %s", admin.Username)
82
utils.Log.Infof("password: %s", pwd)
83
DelAdminCacheOnline()
84
}
85
86
func init() {
87
RootCmd.AddCommand(AdminCmd)
88
AdminCmd.AddCommand(RandomPasswordCmd)
89
AdminCmd.AddCommand(SetPasswordCmd)
90
AdminCmd.AddCommand(ShowTokenCmd)
91
// Here you will define your flags and configuration settings.
92
93
// Cobra supports Persistent Flags which will work for this command
94
// and all subcommands, e.g.:
95
// passwordCmd.PersistentFlags().String("foo", "", "A help for foo")
96
97
// Cobra supports local flags which will only run when this command
98
// is called directly, e.g.:
99
// passwordCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
100
}
101
102