From 16821ed736cb277c9e05a24275b70b1e0f8ded9c Mon Sep 17 00:00:00 2001 From: xushuhui Date: Wed, 20 Apr 2022 15:25:54 +0800 Subject: [PATCH] feat: (rbac) core response --- internal/biz/account.go | 29 +++++++++++++++++----- internal/biz/user.go | 4 ++-- internal/core/response.go | 45 +++++++++++++++++++++++++++++++++++ internal/dto/user.go | 4 ++-- plugin/api/account/account.go | 15 +++++++++++- plugin/api/rbac/init.go | 36 ---------------------------- plugin/api/rbac/permission.go | 3 ++- plugin/api/rbac/role.go | 11 +++++---- plugin/api/rbac/user.go | 17 ++++++------- 9 files changed, 103 insertions(+), 61 deletions(-) create mode 100644 internal/core/response.go diff --git a/internal/biz/account.go b/internal/biz/account.go index 253516f3..1ee4df90 100644 --- a/internal/biz/account.go +++ b/internal/biz/account.go @@ -4,6 +4,7 @@ import ( "errors" "fmt" "github.com/golang-jwt/jwt" + "github.com/mitchellh/mapstructure" "golang.org/x/crypto/bcrypt" "infini.sh/console/internal/dto" "infini.sh/console/model/rbac" @@ -11,6 +12,7 @@ import ( "infini.sh/framework/core/global" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" + "strings" "time" ) @@ -24,10 +26,21 @@ type User struct { UserId string `json:"user_id"` Roles []string `json:"roles"` } +type Account struct { + ID string `json:"id,omitempty" ` + Created string `json:"created,omitempty" ` + Updated string `json:"updated,omitempty" ` + Username string `json:"username" elastic_mapping:"username:{type:keyword}"` + Password string `json:"password" elastic_mapping:"password:{type:text}"` + Name string `json:"name" elastic_mapping:"name:{type:keyword}"` + Phone string `json:"phone" elastic_mapping:"phone:{type:keyword}"` + Email string `json:"email" elastic_mapping:"email:{type:keyword}"` + Tags []string `json:"tags" elastic_mapping:"tags:{type:text}"` +} const Secret = "console" -func authenticateUser(username string, password string) (user rbac.User, err error) { +func authenticateUser(username string, password string) (user Account, err error) { err, result := orm.GetBy("username", username, rbac.User{}) if err != nil { @@ -38,7 +51,11 @@ func authenticateUser(username string, password string) (user rbac.User, err err err = errors.New("user not found") return } - user = result.Result[0].(rbac.User) + + err = mapstructure.Decode(result.Result[0], &user) + if err != nil { + return + } err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(password)) if err == bcrypt.ErrMismatchedHashAndPassword { err = errors.New("password incorrect") @@ -47,7 +64,7 @@ func authenticateUser(username string, password string) (user rbac.User, err err return } -func authenticateAdmin(username string, password string) (user rbac.User, err error) { +func authenticateAdmin(username string, password string) (user Account, err error) { u, _ := global.Env().GetConfig("bootstrap.username", "admin") p, _ := global.Env().GetConfig("bootstrap.password", "admin") @@ -60,7 +77,7 @@ func authenticateAdmin(username string, password string) (user rbac.User, err er user.Username = username return user, nil } -func authorize(user rbac.User) (m map[string]interface{}, err error) { +func authorize(user Account) (m map[string]interface{}, err error) { token := jwt.NewWithClaims(jwt.SigningMethodHS256, UserClaims{ User: &User{ Username: user.Username, @@ -86,7 +103,7 @@ func authorize(user rbac.User) (m map[string]interface{}, err error) { return } func Login(username string, password string) (m map[string]interface{}, err error) { - var user rbac.User + var user Account if username == "admin" { user, err = authenticateAdmin(username, password) if err != nil { @@ -128,7 +145,7 @@ func UpdatePassword(localUser *User, req dto.UpdatePassword) (err error) { err = ErrNotFound return } - err = bcrypt.CompareHashAndPassword([]byte(req.OldPassword), []byte(user.Password)) + err = bcrypt.CompareHashAndPassword([]byte(user.Password), []byte(req.OldPassword)) if err == bcrypt.ErrMismatchedHashAndPassword { err = errors.New("old password is not correct") return diff --git a/internal/biz/user.go b/internal/biz/user.go index 5c4c67c0..66903c5f 100644 --- a/internal/biz/user.go +++ b/internal/biz/user.go @@ -135,7 +135,7 @@ func UpdateUser(localUser *User, id string, req dto.UpdateUser) (err error) { user.Phone = req.Phone user.Tags = req.Tags user.Updated = time.Now() - err = orm.Save(user) + err = orm.Save(&user) if err != nil { return } @@ -177,7 +177,7 @@ func UpdateUserRole(localUser *User, id string, req dto.UpdateUserRole) (err err } user.Roles = roles user.Updated = time.Now() - err = orm.Save(user) + err = orm.Save(&user) if err != nil { return } diff --git a/internal/core/response.go b/internal/core/response.go new file mode 100644 index 00000000..0804e91c --- /dev/null +++ b/internal/core/response.go @@ -0,0 +1,45 @@ +package core + +type Response struct { + Total int64 `json:"total,omitempty"` + Hit interface{} `json:"hit,omitempty"` + Id string `json:"_id,omitempty"` + Result string `json:"result,omitempty"` +} +type FoundResp struct { + Found bool `json:"found"` + Id string `json:"_id,omitempty"` + Source interface{} `json:"_source,omitempty"` +} + +func CreateResponse(id string) Response { + return Response{ + Id: id, + Result: "created", + } +} +func UpdateResponse(id string) Response { + return Response{ + Id: id, + Result: "updated", + } +} +func DeleteResponse(id string) Response { + return Response{ + Id: id, + Result: "deleted", + } +} +func NotFoundResponse(id string) FoundResp { + return FoundResp{ + Id: id, + Found: false, + } +} +func FoundResponse(id string, data interface{}) FoundResp { + return FoundResp{ + Id: id, + Found: true, + Source: data, + } +} diff --git a/internal/dto/user.go b/internal/dto/user.go index 67a98696..a9b4dde7 100644 --- a/internal/dto/user.go +++ b/internal/dto/user.go @@ -5,6 +5,6 @@ type Login struct { Password string `json:"password"` } type UpdatePassword struct { - OldPassword string `json:"oldPassword"` - NewPassword string `json:"newPassword"` + OldPassword string `json:"old_password"` + NewPassword string `json:"new_password"` } diff --git a/plugin/api/account/account.go b/plugin/api/account/account.go index f7de9fe5..c5203a99 100644 --- a/plugin/api/account/account.go +++ b/plugin/api/account/account.go @@ -2,6 +2,7 @@ package account import ( "infini.sh/console/internal/biz" + "infini.sh/console/internal/core" "infini.sh/console/internal/dto" m "infini.sh/console/internal/middleware" "infini.sh/framework/core/api" @@ -99,7 +100,19 @@ func (h Account) Profile(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - h.WriteJSON(w, reqUser, 200) + user, err := biz.GetUser(reqUser.UserId) + if err != nil { + h.Error(w, err) + return + } + u := util.MapStr{ + "id": user.ID, + "username": user.Username, + "email": user.Email, + "phone": user.Phone, + "name": user.Name, + } + h.WriteOKJSON(w, core.FoundResponse(reqUser.UserId, u)) return } func (h Account) UpdatePassword(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { diff --git a/plugin/api/rbac/init.go b/plugin/api/rbac/init.go index bbb2d2e1..d9abb13b 100644 --- a/plugin/api/rbac/init.go +++ b/plugin/api/rbac/init.go @@ -65,39 +65,3 @@ func init() { loadJsonConfig() loadRolePermission() } - -type Response struct { - Total int64 `json:"total,omitempty"` - Hit interface{} `json:"hit,omitempty"` - Id string `json:"_id,omitempty"` - Result string `json:"result,omitempty"` -} -type NotFoundResp struct { - Found bool `json:"found"` - Id string `json:"_id,omitempty"` -} - -func CreateResponse(id string) Response { - return Response{ - Id: id, - Result: "created", - } -} -func UpdateResponse(id string) Response { - return Response{ - Id: id, - Result: "updated", - } -} -func DeleteResponse(id string) Response { - return Response{ - Id: id, - Result: "deleted", - } -} -func NotFoundResponse(id string) NotFoundResp { - return NotFoundResp{ - Id: id, - Found: false, - } -} diff --git a/plugin/api/rbac/permission.go b/plugin/api/rbac/permission.go index 1f480a53..a6ff6e7b 100644 --- a/plugin/api/rbac/permission.go +++ b/plugin/api/rbac/permission.go @@ -3,6 +3,7 @@ package rbac import ( log "github.com/cihub/seelog" "infini.sh/console/internal/biz" + "infini.sh/console/internal/core" httprouter "infini.sh/framework/core/api/router" "net/http" @@ -18,7 +19,7 @@ func (h Rbac) ListPermission(w http.ResponseWriter, r *http.Request, ps httprout return } permissions := role.ListPermission() - h.WriteOKJSON(w, Response{ + h.WriteOKJSON(w, core.Response{ Hit: permissions, }) return diff --git a/plugin/api/rbac/role.go b/plugin/api/rbac/role.go index cda8dd91..8e996f6c 100644 --- a/plugin/api/rbac/role.go +++ b/plugin/api/rbac/role.go @@ -4,6 +4,7 @@ import ( log "github.com/cihub/seelog" "infini.sh/console/internal/biz" "infini.sh/console/internal/biz/enum" + "infini.sh/console/internal/core" "infini.sh/console/internal/dto" httprouter "infini.sh/framework/core/api/router" "net/http" @@ -35,7 +36,7 @@ func (h Rbac) CreateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P return } - _ = h.WriteOKJSON(w, CreateResponse(id)) + _ = h.WriteOKJSON(w, core.CreateResponse(id)) return } @@ -62,7 +63,7 @@ func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.P roles = append(roles, v) } - h.WriteOKJSON(w, Response{Hit: roles, Total: res.Total + int64(len(enum.BuildRoles))}) + h.WriteOKJSON(w, core.Response{Hit: roles, Total: res.Total + int64(len(enum.BuildRoles))}) return } @@ -76,7 +77,7 @@ func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Para h.Error(w, err) return } - h.WriteOKJSON(w, Response{Hit: role}) + h.WriteOKJSON(w, core.Response{Hit: role}) return } @@ -96,7 +97,7 @@ func (h Rbac) DeleteRole(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - _ = h.WriteOKJSON(w, DeleteResponse(id)) + _ = h.WriteOKJSON(w, core.DeleteResponse(id)) return } @@ -122,6 +123,6 @@ func (h Rbac) UpdateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - _ = h.WriteOKJSON(w, UpdateResponse(id)) + _ = h.WriteOKJSON(w, core.UpdateResponse(id)) return } diff --git a/plugin/api/rbac/user.go b/plugin/api/rbac/user.go index d86a16ff..5b8c1de2 100644 --- a/plugin/api/rbac/user.go +++ b/plugin/api/rbac/user.go @@ -3,6 +3,7 @@ package rbac import ( "errors" "infini.sh/console/internal/biz" + "infini.sh/console/internal/core" "infini.sh/console/internal/dto" httprouter "infini.sh/framework/core/api/router" "infini.sh/framework/modules/elastic" @@ -39,7 +40,7 @@ func (h Rbac) CreateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - _ = h.WriteOKJSON(w, CreateResponse(id)) + _ = h.WriteOKJSON(w, core.CreateResponse(id)) return } @@ -48,7 +49,7 @@ func (h Rbac) GetUser(w http.ResponseWriter, r *http.Request, ps httprouter.Para id := ps.MustGetParameter("id") user, err := biz.GetUser(id) if errors.Is(err, elastic.ErrNotFound) { - h.WriteJSON(w, NotFoundResponse(id), http.StatusNotFound) + h.WriteJSON(w, core.NotFoundResponse(id), http.StatusNotFound) return } @@ -57,7 +58,7 @@ func (h Rbac) GetUser(w http.ResponseWriter, r *http.Request, ps httprouter.Para h.Error(w, err) return } - h.WriteOKJSON(w, Response{Hit: user}) + h.WriteOKJSON(w, core.FoundResponse(id, user)) return } @@ -83,7 +84,7 @@ func (h Rbac) UpdateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - _ = h.WriteOKJSON(w, UpdateResponse(id)) + _ = h.WriteOKJSON(w, core.UpdateResponse(id)) return } @@ -109,7 +110,7 @@ func (h Rbac) UpdateUserRole(w http.ResponseWriter, r *http.Request, ps httprout h.Error(w, err) return } - _ = h.WriteOKJSON(w, UpdateResponse(id)) + _ = h.WriteOKJSON(w, core.UpdateResponse(id)) return } @@ -123,7 +124,7 @@ func (h Rbac) DeleteUser(w http.ResponseWriter, r *http.Request, ps httprouter.P } err = biz.DeleteUser(localUser, id) if errors.Is(err, elastic.ErrNotFound) { - h.WriteJSON(w, NotFoundResponse(id), http.StatusNotFound) + h.WriteJSON(w, core.NotFoundResponse(id), http.StatusNotFound) return } if err != nil { @@ -131,7 +132,7 @@ func (h Rbac) DeleteUser(w http.ResponseWriter, r *http.Request, ps httprouter.P h.Error(w, err) return } - _ = h.WriteOKJSON(w, DeleteResponse(id)) + _ = h.WriteOKJSON(w, core.DeleteResponse(id)) return } @@ -149,7 +150,7 @@ func (h Rbac) SearchUser(w http.ResponseWriter, r *http.Request, ps httprouter.P return } - h.WriteOKJSON(w, Response{Hit: res.Result, Total: res.Total}) + h.WriteOKJSON(w, core.Response{Hit: res.Result, Total: res.Total}) return }