Skip to content

Commit

Permalink
update user reset password
Browse files Browse the repository at this point in the history
Signed-off-by: yxxhero <[email protected]>
  • Loading branch information
yxxhero authored and gaius-qi committed Aug 31, 2021
1 parent 1e42522 commit cedf68a
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 5 deletions.
31 changes: 31 additions & 0 deletions manager/handlers/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,37 @@ func (h *Handlers) SignUp(ctx *gin.Context) {
ctx.JSON(http.StatusOK, user)
}

// @Summary Reset Password For User
// @Description reset password for user by json config
// @Tags User
// @Accept json
// @Produce json
// @Param User body types.ResetPasswordRequest true "User"
// @Success 200
// @Failure 400
// @Failure 500
// @Router /users/:id/reset_password [post]
func (h *Handlers) ResetPassword(ctx *gin.Context) {
var params types.UserIDRequest
if err := ctx.ShouldBindUri(&params); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}
var json types.ResetPasswordRequest
if err := ctx.ShouldBindJSON(&json); err != nil {
ctx.JSON(http.StatusUnprocessableEntity, gin.H{"errors": err.Error()})
return
}

err := h.Service.ResetPassword(params.ID, ctx.GetString("userName"), json)
if err != nil {
ctx.Error(err)
return
}

ctx.Status(http.StatusOK)
}

// @Summary Delete Role For User
// @Description Delete Role For User by uri config
// @Tags users
Expand Down
7 changes: 5 additions & 2 deletions manager/router/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,8 +83,11 @@ func Init(console bool, verbose bool, publicPath string, service service.REST, e
ai.POST("/signin", jwt.LoginHandler)
ai.POST("/signout", jwt.LogoutHandler)
ai.POST("/refresh_token", jwt.RefreshHandler)
ai.POST("/:id/roles/:role_name", h.AddRoleToUser)
ai.DELETE("/:id/roles/:role_name", h.DeleteRoleForUser)
ai.POST("/reset_password", h.ResetPassword)

ai.POST("/:id/roles/:role_name", h.AddRoleToUser, jwt.MiddlewareFunc(), rbac)
ai.DELETE("/:id/roles/:role_name", h.DeleteRoleForUser, jwt.MiddlewareFunc(), rbac)

ai.POST("/signup", h.SignUp)

// Scheduler Cluster
Expand Down
5 changes: 3 additions & 2 deletions manager/service/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ import (
)

type REST interface {
SignIn(json types.SignInRequest) (*model.User, error)
SignUp(json types.SignUpRequest) (*model.User, error)
SignIn(types.SignInRequest) (*model.User, error)
SignUp(types.SignUpRequest) (*model.User, error)
ResetPassword(uint, string, types.ResetPasswordRequest) error

AddRoleForUser(uint, string) error
DeleteRoleForUser(uint, string) error
Expand Down
10 changes: 10 additions & 0 deletions manager/service/settings.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package service

import (
"errors"

"d7y.io/dragonfly/v2/manager/model"
"d7y.io/dragonfly/v2/manager/types"
"d7y.io/dragonfly/v2/pkg/util/stringutils"
)

var AvaliableSettings = []string{
"server_domain",
}

func (s *rest) CreateSetting(json types.CreateSettingRequest) (*model.Settings, error) {
if !stringutils.Contains(AvaliableSettings, json.Key) {
return nil, errors.New("invalid setting key")
}
setting := model.Settings{
Key: json.Key,
Value: json.Value,
Expand Down
44 changes: 44 additions & 0 deletions manager/service/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package service

import (
"errors"

"d7y.io/dragonfly/v2/manager/model"
"d7y.io/dragonfly/v2/manager/types"
"golang.org/x/crypto/bcrypt"
Expand All @@ -38,6 +40,48 @@ func (s *rest) SignIn(json types.SignInRequest) (*model.User, error) {
return &user, nil
}

func (s *rest) ResetPassword(uid uint, userName string, json types.ResetPasswordRequest) error {
user := model.User{}
if err := s.db.First(&user, model.User{
Name: userName,
}).Error; err != nil {
return err
}

updatePassword := func(uid uint, password string) error {
user := model.User{}
encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(json.NewPassword), bcrypt.MinCost)
if err != nil {
return err
}
if err := s.db.First(&user, uid).Updates(model.User{
EncryptedPassword: string(encryptedPasswordBytes),
}).Error; err != nil {
return err
}
return nil

}

if user.ID == uid {
err := bcrypt.CompareHashAndPassword([]byte(user.EncryptedPassword), []byte(json.OldPassword))
if err != nil {
return err
}
return updatePassword(uid, json.NewPassword)
} else {
// validate user permission
has, err := s.enforcer.Enforce(userName, "users", "*")
if err != nil {
return err
}
if !has {
return errors.New("permission deny")
}
return updatePassword(uid, json.NewPassword)
}
}

func (s *rest) SignUp(json types.SignUpRequest) (*model.User, error) {
encryptedPasswordBytes, err := bcrypt.GenerateFromPassword([]byte(json.Password), bcrypt.MinCost)
if err != nil {
Expand Down
10 changes: 9 additions & 1 deletion manager/types/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ type SignInRequest struct {
Password string `form:"password" binding:"required,min=8,max=20"`
}

type ResetPasswordRequest struct {
OldPassword string `form:"old_password" binding:"required,min=8,max=20"`
NewPassword string `form:"new_password" binding:"required,min=8,max=20"`
}

type SignUpRequest struct {
SignInRequest
Email string `form:"email" binding:"required,email"`
Expand All @@ -29,8 +34,11 @@ type SignUpRequest struct {
Location string `form:"location" binding:"omitempty"`
BIO string `form:"bio" binding:"omitempty"`
}
type UserIDRequest struct {
ID uint `uri:"id" binding:"required,min=1"`
}

type RoleRequest struct {
UserIDRequest
RoleName string `uri:"role_name" binding:"required,min=1"`
ID uint `uri:"id" binding:"required,min=1"`
}

0 comments on commit cedf68a

Please sign in to comment.