From 66b9995b076b44d9cd29395edb6c993d131c253f Mon Sep 17 00:00:00 2001 From: xushuhui Date: Thu, 21 Apr 2022 10:13:49 +0800 Subject: [PATCH] fix: (rbac) create user password require --- internal/biz/user.go | 2 +- plugin/api/rbac/role.go | 2 +- plugin/api/rbac/user.go | 11 ++++++++--- 3 files changed, 10 insertions(+), 5 deletions(-) diff --git a/internal/biz/user.go b/internal/biz/user.go index 66903c5f..45c43a1a 100644 --- a/internal/biz/user.go +++ b/internal/biz/user.go @@ -74,7 +74,7 @@ func CreateUser(localUser *User, req dto.CreateUser) (id string, err error) { Name: v.Name, }) } - hash, err := bcrypt.GenerateFromPassword([]byte("123456"), bcrypt.DefaultCost) + hash, err := bcrypt.GenerateFromPassword([]byte(req.Password), bcrypt.DefaultCost) if err != nil { return diff --git a/plugin/api/rbac/role.go b/plugin/api/rbac/role.go index 3bc0fac1..c9c2d65e 100644 --- a/plugin/api/rbac/role.go +++ b/plugin/api/rbac/role.go @@ -19,7 +19,7 @@ func (h Rbac) CreateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P var req dto.CreateRole err = h.DecodeJSON(r, &req) if err != nil { - h.Error(w, err) + h.Error400(w, err.Error()) return } req.RoleType = roleType diff --git a/plugin/api/rbac/user.go b/plugin/api/rbac/user.go index 662ded0a..9bdb7f18 100644 --- a/plugin/api/rbac/user.go +++ b/plugin/api/rbac/user.go @@ -25,7 +25,12 @@ func (h Rbac) CreateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P var req dto.CreateUser err := h.DecodeJSON(r, &req) if err != nil { - h.Error(w, err) + h.Error400(w, err.Error()) + return + } + if req.Username == "" || req.Password == "" { + + h.Error400(w, "username or password require") return } localUser, err := biz.FromUserContext(r.Context()) @@ -68,7 +73,7 @@ func (h Rbac) UpdateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P err := h.DecodeJSON(r, &req) if err != nil { _ = log.Error(err.Error()) - h.Error(w, err) + h.Error400(w, err.Error()) return } localUser, err := biz.FromUserContext(r.Context()) @@ -94,7 +99,7 @@ func (h Rbac) UpdateUserRole(w http.ResponseWriter, r *http.Request, ps httprout err := h.DecodeJSON(r, &req) if err != nil { _ = log.Error(err.Error()) - h.Error(w, err) + h.Error400(w, err.Error()) return } localUser, err := biz.FromUserContext(r.Context())