mirror of https://github.com/Xhofe/alist
feat(cmd): set or random new password for admin
parent
75acbcc115
commit
a425392a2b
49
cmd/admin.go
49
cmd/admin.go
|
@ -4,8 +4,10 @@ Copyright © 2022 NAME HERE <EMAIL ADDRESS>
|
||||||
package cmd
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"github.com/alist-org/alist/v3/internal/model"
|
||||||
"github.com/alist-org/alist/v3/internal/op"
|
"github.com/alist-org/alist/v3/internal/op"
|
||||||
"github.com/alist-org/alist/v3/pkg/utils"
|
"github.com/alist-org/alist/v3/pkg/utils"
|
||||||
|
"github.com/alist-org/alist/v3/pkg/utils/random"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -20,14 +22,57 @@ var PasswordCmd = &cobra.Command{
|
||||||
if err != nil {
|
if err != nil {
|
||||||
utils.Log.Errorf("failed get admin user: %+v", err)
|
utils.Log.Errorf("failed get admin user: %+v", err)
|
||||||
} else {
|
} else {
|
||||||
utils.Log.Infof("admin user's info: \nusername: %s\npassword: %s", admin.Username, admin.Password)
|
utils.Log.Infof("Admin user's username: %s", admin.Username)
|
||||||
|
utils.Log.Infof("The password can only be output at the first startup, and then stored as a hash value, which cannot be reversed")
|
||||||
|
utils.Log.Infof("You can reset the password by running 'alist password random' to generate a random password")
|
||||||
|
utils.Log.Infof("You can also set the password by running 'alist password set NEW_PASSWORD' to set a new password")
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var RandomPasswordCmd = &cobra.Command{
|
||||||
|
Use: "random",
|
||||||
|
Short: "Reset admin user's password to a random string",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
newPwd := random.String(8)
|
||||||
|
setAdminPassword(newPwd)
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
var SetPasswordCmd = &cobra.Command{
|
||||||
|
Use: "set",
|
||||||
|
Short: "Set admin user's password",
|
||||||
|
Run: func(cmd *cobra.Command, args []string) {
|
||||||
|
if len(args) == 0 {
|
||||||
|
utils.Log.Errorf("Please enter the new password")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
setAdminPassword(args[0])
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
func setAdminPassword(pwd string) {
|
||||||
|
Init()
|
||||||
|
admin, err := op.GetAdmin()
|
||||||
|
if err != nil {
|
||||||
|
utils.Log.Errorf("failed get admin user: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
admin.PwdHash = model.HashPwd(pwd)
|
||||||
|
if err := op.UpdateUser(admin); err != nil {
|
||||||
|
utils.Log.Errorf("failed update admin user: %+v", err)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
utils.Log.Infof("admin user has been updated:")
|
||||||
|
utils.Log.Infof("username: %s", admin.Username)
|
||||||
|
utils.Log.Infof("password: %s", pwd)
|
||||||
|
DelAdminCacheOnline()
|
||||||
|
}
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
RootCmd.AddCommand(PasswordCmd)
|
RootCmd.AddCommand(PasswordCmd)
|
||||||
|
PasswordCmd.AddCommand(RandomPasswordCmd)
|
||||||
|
PasswordCmd.AddCommand(SetPasswordCmd)
|
||||||
// Here you will define your flags and configuration settings.
|
// Here you will define your flags and configuration settings.
|
||||||
|
|
||||||
// Cobra supports Persistent Flags which will work for this command
|
// Cobra supports Persistent Flags which will work for this command
|
||||||
|
|
|
@ -28,7 +28,7 @@ func DelUserCacheOnline(username string) {
|
||||||
u := fmt.Sprintf("http://localhost:%d/api/admin/user/del_cache", port)
|
u := fmt.Sprintf("http://localhost:%d/api/admin/user/del_cache", port)
|
||||||
if port == -1 {
|
if port == -1 {
|
||||||
if conf.Conf.Scheme.HttpsPort == -1 {
|
if conf.Conf.Scheme.HttpsPort == -1 {
|
||||||
utils.Log.Infof("[del_user_cache] no open port")
|
utils.Log.Warnf("[del_user_cache] no open port")
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
u = fmt.Sprintf("https://localhost:%d/api/admin/user/del_cache", conf.Conf.Scheme.HttpsPort)
|
u = fmt.Sprintf("https://localhost:%d/api/admin/user/del_cache", conf.Conf.Scheme.HttpsPort)
|
||||||
|
@ -48,5 +48,5 @@ func DelUserCacheOnline(username string) {
|
||||||
utils.Log.Errorf("[del_user_cache] del cache error: %s", msg)
|
utils.Log.Errorf("[del_user_cache] del cache error: %s", msg)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
utils.Log.Infof("[del_user_cache] del user [%s] cache success", username)
|
utils.Log.Debugf("[del_user_cache] del user [%s] cache success", username)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue