From f5737c6a73ece12b8d5c06e06ef897e1228a6213 Mon Sep 17 00:00:00 2001 From: lifei6671 Date: Tue, 10 Jul 2018 19:13:33 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0=E5=91=BD=E4=BB=A4=E8=A1=8C?= =?UTF-8?q?=E4=BF=AE=E6=94=B9=E5=AF=86=E7=A0=81=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- commands/command.go | 3 +++ commands/install.go | 60 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/commands/command.go b/commands/command.go index f6ff0d51..6726f189 100644 --- a/commands/command.go +++ b/commands/command.go @@ -230,6 +230,9 @@ func ResolveCommand(args []string) { RegisterCache() RegisterModel() RegisterLogger(conf.LogFile) + + ModifyPassword() + } //注册缓存管道 diff --git a/commands/install.go b/commands/install.go index 347f61b5..bd4e5c29 100644 --- a/commands/install.go +++ b/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