Skip to content

Commit

Permalink
feat: cancel 2fa api
Browse files Browse the repository at this point in the history
  • Loading branch information
xhofe committed Aug 7, 2022
1 parent d01958a commit 2b5da3e
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
13 changes: 13 additions & 0 deletions internal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func UpdateUser(u *model.User) error {
return errors.WithStack(db.Save(u).Error)
}

func Cancel2FAByUser(u *model.User) error {
u.OtpSecret = ""
return errors.WithStack(UpdateUser(u))
}

func Cancel2FAById(id uint) error {
user, err := GetUserById(id)
if err != nil {
return err
}
return Cancel2FAByUser(user)
}

func GetUsers(pageIndex, pageSize int) ([]model.User, int64, error) {
userDB := db.Model(&model.User{})
var count int64
Expand Down
17 changes: 17 additions & 0 deletions server/handles/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,9 @@ func UpdateUser(c *gin.Context) {
if req.Password == "" {
req.Password = user.Password
}
if req.OtpSecret == "" {
req.OtpSecret = user.OtpSecret
}
if err := db.UpdateUser(&req); err != nil {
common.ErrorResp(c, err, 500)
} else {
Expand Down Expand Up @@ -99,3 +102,17 @@ func GetUser(c *gin.Context) {
}
common.SuccessResp(c, user)
}

func Cancel2FAById(c *gin.Context) {
idStr := c.Query("id")
id, err := strconv.Atoi(idStr)
if err != nil {
common.ErrorResp(c, err, 400)
return
}
if err := db.Cancel2FAById(uint(id)); err != nil {
common.ErrorResp(c, err, 500)
return
}
common.SuccessResp(c)
}
1 change: 1 addition & 0 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func admin(g *gin.RouterGroup) {
user.GET("/get", handles.GetUser)
user.POST("/create", handles.CreateUser)
user.POST("/update", handles.UpdateUser)
user.POST("/cancel_2fa", handles.Cancel2FAById)
user.POST("/delete", handles.DeleteUser)

storage := g.Group("/storage")
Expand Down

0 comments on commit 2b5da3e

Please sign in to comment.