1
0
Эх сурвалжийг харах

增加命令行修改密码功能

lifei6671 7 жил өмнө
parent
commit
f5737c6a73

+ 3 - 0
commands/command.go

@@ -230,6 +230,9 @@ func ResolveCommand(args []string) {
 	RegisterCache()
 	RegisterModel()
 	RegisterLogger(conf.LogFile)
+
+	ModifyPassword()
+
 }
 
 //注册缓存管道

+ 59 - 1
commands/install.go

@@ -8,6 +8,9 @@ import (
 	"github.com/astaxie/beego/orm"
 	"github.com/lifei6671/mindoc/conf"
 	"github.com/lifei6671/mindoc/models"
+	"flag"
+	"github.com/lifei6671/mindoc/utils"
+	"github.com/astaxie/beego"
 )
 
 //系统安装.
@@ -34,6 +37,61 @@ func Version() {
 	}
 }
 
+//修改用户密码
+func ModifyPassword() {
+	var account,password string
+
+	//账号和密码需要解析参数后才能获取
+	if len(os.Args) >= 2 && os.Args[1] == "password" {
+		flagSet := flag.NewFlagSet("MinDoc command: ", flag.ExitOnError)
+
+		flagSet.StringVar(&account, "account", "", "用户账号.")
+		flagSet.StringVar(&password, "password", "", "用户密码.")
+
+		if err := flagSet.Parse(os.Args[2:]); err != nil {
+			beego.Error("解析参数失败 -> ",err)
+			os.Exit(1)
+		}
+
+		if len(os.Args) < 2 {
+			fmt.Println("Parameter error.")
+			os.Exit(1)
+		}
+
+		if account == "" {
+			fmt.Println("Account cannot be empty.")
+			os.Exit(1)
+		}
+		if password == "" {
+			fmt.Println("Password cannot be empty.")
+			os.Exit(1)
+		}
+		member,err := models.NewMember().FindByAccount(account)
+
+		if err != nil {
+			fmt.Println("Failed to change password:",err)
+			os.Exit(1)
+		}
+		pwd,err := utils.PasswordHash(password)
+
+		if err != nil {
+			fmt.Println("Failed to change password:",err)
+			os.Exit(1)
+		}
+		member.Password = pwd
+
+		err = member.Update("password")
+		if err != nil {
+			fmt.Println("Failed to change password:",err)
+			os.Exit(1)
+		}
+		fmt.Println("Successfully modified.")
+		os.Exit(0)
+	}
+
+
+}
+
 //初始化数据
 func initialization() {
 
@@ -48,7 +106,7 @@ func initialization() {
 	if err == orm.ErrNoRows {
 
 		member.Account = "admin"
-		member.Avatar = "/static/images/headimgurl.jpg"
+		member.Avatar = conf.URLForWithCdnImage("/static/images/headimgurl.jpg")
 		member.Password = "123456"
 		member.AuthMethod = "local"
 		member.Role = 0