refactor: (rbac) move biz and dto to internal dir

fix: orm get not found error
This commit is contained in:
xushuhui 2022-04-16 17:10:54 +08:00
parent f0b7c1a8dd
commit dc8bd1cd1b
10 changed files with 95 additions and 50 deletions

View File

@ -2,8 +2,9 @@ package biz
import (
"fmt"
"infini.sh/console/internal/dto"
"infini.sh/console/model/rbac"
"infini.sh/console/plugin/api/rbac/dto"
"infini.sh/framework/core/util"
"strings"
"time"
@ -43,6 +44,7 @@ func DeleteRole(id string) (err error) {
role.ID = id
_, err = orm.Get(&role)
if err != nil {
err = ErrNotFound
return
}
return orm.Delete(role)
@ -53,6 +55,7 @@ func UpdateRole(id string, req dto.UpdateRole) (err error) {
role.ID = id
_, err = orm.Get(&role)
if err != nil {
err = ErrNotFound
return
}
role.Description = req.Description
@ -66,9 +69,9 @@ func GetRole(id string) (role rbac.Role, err error) {
role.ID = id
_, err = orm.Get(&role)
if err != nil {
err = ErrNotFound
return
}
return
}
func SearchRole(keyword string, from, size int) (roles orm.Result, err error) {

View File

@ -2,20 +2,24 @@ package biz
import (
"fmt"
"infini.sh/console/internal/dto"
"infini.sh/console/model/rbac"
"infini.sh/console/plugin/api/rbac/dto"
"infini.sh/framework/core/orm"
"infini.sh/framework/core/util"
"strings"
"time"
)
var ErrNotFound = fmt.Errorf("not found")
func DeleteUser(id string) (err error) {
user := rbac.User{}
user.ID = id
_, err = orm.Get(&user)
if err != nil {
err = ErrNotFound
return
}
return orm.Delete(user)
@ -23,7 +27,7 @@ func DeleteUser(id string) (err error) {
}
func CreateUser(req dto.CreateUser) (id string, err error) {
q := orm.Query{Size: 1000}
q.Conds = orm.And(orm.Eq("name", req.Name))
q.Conds = orm.And(orm.Eq("username", req.Username))
err, result := orm.Search(rbac.User{}, &q)
if err != nil {
@ -48,6 +52,7 @@ func CreateUser(req dto.CreateUser) (id string, err error) {
Email: req.Email,
Phone: req.Phone,
Roles: roles,
Tags: req.Tags,
}
user.ID = util.GetUUID()
user.Created = time.Now()
@ -64,11 +69,13 @@ func UpdateUser(id string, req dto.UpdateUser) (err error) {
user.ID = id
_, err = orm.Get(&user)
if err != nil {
err = ErrNotFound
return
}
user.Name = req.Name
user.Email = req.Email
user.Phone = req.Phone
user.Tags = req.Tags
user.Updated = time.Now()
err = orm.Save(user)
return
@ -78,6 +85,7 @@ func UpdateUserRole(id string, req dto.UpdateUserRole) (err error) {
user.ID = id
_, err = orm.Get(&user)
if err != nil {
err = ErrNotFound
return
}
roles := make([]rbac.UserRole, 0)

View File

@ -17,22 +17,23 @@ type ElasticsearchPermission struct {
IndexPrivilege []string `json:"index_privilege" `
}
type CreateUser struct {
Username string `json:"username"`
Password string `json:"password"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Roles []Role `json:"roles"`
Username string `json:"username"`
Password string `json:"password"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Roles []Role `json:"roles"`
Tags []string `json:"tags"`
}
type Role struct {
Id string `json:"id"`
Name string `json:"name"`
}
type UpdateUser struct {
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
// Roles []Role `json:"roles"`
Name string `json:"name"`
Email string `json:"email"`
Phone string `json:"phone"`
Tags []string `json:"tags"`
}
type UpdateUserRole struct {
Roles []Role `json:"roles"`

6
internal/dto/user.go Normal file
View File

@ -0,0 +1,6 @@
package dto
type Login struct {
Username string `json:"username"`
Password string `json:"password"`
}

View File

@ -0,0 +1,8 @@
package middleware
func LoginRequired() {
}
func PermissionRequired() {
}

View File

@ -2,7 +2,8 @@ package rbac
import (
"encoding/json"
"infini.sh/console/plugin/api/rbac/biz"
"infini.sh/console/internal/biz"
"infini.sh/framework/core/api"
"infini.sh/framework/core/util"
"os"
@ -64,7 +65,10 @@ type Response struct {
Hit interface{} `json:"hit,omitempty"`
Id string `json:"_id,omitempty"`
Result string `json:"result,omitempty"`
Found bool `json:"found,omitempty"`
}
type NotFoundResp struct {
Found bool `json:"found"`
Id string `json:"_id,omitempty"`
}
func CreateResponse(id string) Response {
@ -85,9 +89,9 @@ func DeleteResponse(id string) Response {
Result: "deleted",
}
}
func NotFoundResponse(id string) Response {
return Response{
func NotFoundResponse(id string) NotFoundResp {
return NotFoundResp{
Id: id,
Found: false,
}
}

View File

@ -3,7 +3,8 @@ package rbac
import (
log "github.com/cihub/seelog"
"github.com/pkg/errors"
"infini.sh/console/plugin/api/rbac/biz"
"infini.sh/console/internal/biz"
httprouter "infini.sh/framework/core/api/router"
"net/http"
)
@ -26,7 +27,7 @@ func (h Rbac) ListPermission(w http.ResponseWriter, req *http.Request, ps httpro
err := validateRoleType(typ)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
var permissons interface{}
@ -39,7 +40,7 @@ func (h Rbac) ListPermission(w http.ResponseWriter, req *http.Request, ps httpro
}
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}

View File

@ -2,8 +2,9 @@ package rbac
import (
log "github.com/cihub/seelog"
"infini.sh/console/plugin/api/rbac/biz"
"infini.sh/console/plugin/api/rbac/dto"
"infini.sh/console/internal/biz"
"infini.sh/console/internal/dto"
httprouter "infini.sh/framework/core/api/router"
"net/http"
)
@ -14,14 +15,14 @@ func (h Rbac) CreateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
err = validateRoleType(roleType)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
var req dto.CreateRole
err = h.DecodeJSON(r, &req)
if err != nil {
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
req.RoleType = roleType
@ -30,7 +31,7 @@ func (h Rbac) CreateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
id, err = biz.CreateRole(req)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, CreateResponse(id), http.StatusOK)
@ -49,7 +50,7 @@ func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
res, err := biz.SearchRole(keyword, from, size)
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
@ -64,7 +65,7 @@ func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Para
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
h.WriteJSON(w, Response{Hit: role}, http.StatusOK)
@ -74,9 +75,10 @@ func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Para
func (h Rbac) DeleteRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("id")
err := biz.DeleteRole(id)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, DeleteResponse(id), http.StatusOK)
@ -88,14 +90,14 @@ func (h Rbac) UpdateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P
var req dto.UpdateRole
err := h.DecodeJSON(r, &req)
if err != nil {
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
err = biz.UpdateRole(id, req)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, UpdateResponse(id), http.StatusOK)

View File

@ -1,19 +1,22 @@
package rbac
import (
"infini.sh/console/plugin/api/rbac/biz"
"infini.sh/console/plugin/api/rbac/dto"
"errors"
"infini.sh/console/internal/biz"
"infini.sh/console/internal/dto"
httprouter "infini.sh/framework/core/api/router"
"infini.sh/framework/modules/elastic"
"net/http"
log "src/github.com/cihub/seelog"
)
type CreateUserReq struct {
Username string `json:"username" `
Password string `json:"password" `
Name string `json:"name" `
Phone string `json:"phone" `
Email string `json:"email" `
Username string `json:"username" `
Password string `json:"password" `
Name string `json:"name" `
Phone string `json:"phone" `
Email string `json:"email" `
Tags []string `json:"tags"`
}
func (h Rbac) CreateUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
@ -21,13 +24,13 @@ 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.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
id, err := biz.CreateUser(req)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, CreateResponse(id), http.StatusOK)
@ -38,9 +41,14 @@ func (h Rbac) CreateUser(w http.ResponseWriter, r *http.Request, ps httprouter.P
func (h Rbac) GetUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("id")
user, err := biz.GetUser(id)
if errors.Is(err, elastic.ErrNotFound) {
h.WriteJSON(w, NotFoundResponse(id), http.StatusNotFound)
return
}
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
h.WriteJSON(w, Response{Hit: user}, http.StatusOK)
@ -53,13 +61,14 @@ 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.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
err = biz.UpdateUser(id, req)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, UpdateResponse(id), http.StatusOK)
@ -72,14 +81,14 @@ 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.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
err = biz.UpdateUserRole(id, req)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, UpdateResponse(id), http.StatusOK)
@ -89,10 +98,13 @@ func (h Rbac) UpdateUserRole(w http.ResponseWriter, r *http.Request, ps httprout
func (h Rbac) DeleteUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
id := ps.MustGetParameter("id")
err := biz.DeleteUser(id)
if errors.Is(err, elastic.ErrNotFound) {
h.WriteJSON(w, NotFoundResponse(id), http.StatusNotFound)
return
}
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
h.Error(w, err)
return
}
_ = h.WriteJSON(w, DeleteResponse(id), http.StatusOK)
@ -108,8 +120,8 @@ func (h Rbac) SearchUser(w http.ResponseWriter, r *http.Request, ps httprouter.P
res, err := biz.SearchUser(keyword, from, size)
if err != nil {
log.Error(err)
h.WriteError(w, err.Error(), http.StatusInternalServerError)
log.Error(err.Error())
h.Error(w, err)
return
}