Skip to content

Commit

Permalink
添加修改用户名和密码
Browse files Browse the repository at this point in the history
  • Loading branch information
Jrohy committed Apr 6, 2020
1 parent e5abcee commit 2c92815
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 2 deletions.
19 changes: 19 additions & 0 deletions core/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ func (mysql *Mysql) CreateUser(username string, password string) error {
return nil
}

// UpdateUser 更新Trojan用户名和密码
func (mysql *Mysql) UpdateUser(id uint, username string, password string) error {
db := mysql.GetDB()
if db == nil {
return errors.New("can't connect mysql")
}
defer db.Close()
encryPass := sha256.Sum224([]byte(password))
if _, err := db.Exec(fmt.Sprintf("UPDATE users SET username='%s', password='%x' WHERE id=%d;", username, encryPass, id)); err != nil {
fmt.Println(err)
return err
}
if err := SetValue(username+"_pass", password); err != nil {
fmt.Println(err)
return err
}
return nil
}

// DeleteUser 删除用户
func (mysql *Mysql) DeleteUser(id uint) error {
db := mysql.GetDB()
Expand Down
39 changes: 37 additions & 2 deletions web/controller/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package controller

import (
"encoding/base64"
"strconv"
"time"
"trojan/core"
)
Expand Down Expand Up @@ -43,11 +44,11 @@ func CreateUser(username string, password string) *ResponseBody {
responseBody.Msg = "不能创建用户名为admin的用户!"
return &responseBody
}
if _, err := core.GetValue(username + "_pass"); err == nil {
mysql := core.GetMysql()
if user := mysql.GetUserByName(username); user != nil {
responseBody.Msg = "已存在用户名为: " + username + " 的用户!"
return &responseBody
}
mysql := core.GetMysql()
pass, err := base64.StdEncoding.DecodeString(password)
if err != nil {
responseBody.Msg = "Base64解码失败: " + err.Error()
Expand All @@ -59,6 +60,40 @@ func CreateUser(username string, password string) *ResponseBody {
return &responseBody
}

// UpdateUser 更新用户
func UpdateUser(id uint, username string, password string) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
defer TimeCost(time.Now(), &responseBody)
if username == "admin" {
responseBody.Msg = "不能更改用户名为admin的用户!"
return &responseBody
}
mysql := core.GetMysql()
userList := mysql.GetData(strconv.Itoa(int(id)))
if userList == nil {
responseBody.Msg = "can't connect mysql"
return &responseBody
}
if userList[0].Username != username {
if user := mysql.GetUserByName(username); user != nil {
responseBody.Msg = "已存在用户名为: " + username + " 的用户!"
return &responseBody
}
}
if userList[0].Username != "admin" {
_ = core.DelValue(userList[0].Username + "_pass")
}
pass, err := base64.StdEncoding.DecodeString(password)
if err != nil {
responseBody.Msg = "Base64解码失败: " + err.Error()
return &responseBody
}
if err := mysql.UpdateUser(id, username, string(pass)); err != nil {
responseBody.Msg = err.Error()
}
return &responseBody
}

// DelUser 删除用户
func DelUser(id uint) *ResponseBody {
responseBody := ResponseBody{Msg: "success"}
Expand Down
7 changes: 7 additions & 0 deletions web/web.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,13 @@ func userRouter(router *gin.Engine) {
password := c.PostForm("password")
c.JSON(200, controller.CreateUser(username, password))
})
user.POST("/update", func(c *gin.Context) {
sid := c.PostForm("id")
username := c.PostForm("username")
password := c.PostForm("password")
id, _ := strconv.Atoi(sid)
c.JSON(200, controller.UpdateUser(uint(id), username, password))
})
user.DELETE("", func(c *gin.Context) {
stringId := c.Query("id")
id, _ := strconv.Atoi(stringId)
Expand Down

0 comments on commit 2c92815

Please sign in to comment.