diff --git a/plugin/api/rbac/biz/permission.go b/plugin/api/rbac/biz/permission.go index 6c0da28d..ed7a1f58 100644 --- a/plugin/api/rbac/biz/permission.go +++ b/plugin/api/rbac/biz/permission.go @@ -8,7 +8,6 @@ type ConsolePermisson struct { Name string `json:"name"` } -// func ListConsolePermisson() (list []ConsolePermisson, err error) { list = []ConsolePermisson{ { diff --git a/plugin/api/rbac/biz/role.go b/plugin/api/rbac/biz/role.go index 56cee3fa..7d330490 100644 --- a/plugin/api/rbac/biz/role.go +++ b/plugin/api/rbac/biz/role.go @@ -5,6 +5,7 @@ import ( "infini.sh/console/model/rbac" "infini.sh/console/plugin/api/rbac/dto" "infini.sh/framework/core/util" + "strings" "time" "infini.sh/framework/core/orm" @@ -19,8 +20,11 @@ func CreateRole(req dto.CreateRole) (id string, err error) { if err != nil { return } + if result.Total > 0 { + err = fmt.Errorf("role name %s already exists", req.Name) + return + } - fmt.Println(string(result.Raw)) role := &rbac.Role{ Name: req.Name, Description: req.Description, @@ -31,6 +35,7 @@ func CreateRole(req dto.CreateRole) (id string, err error) { role.Created = time.Now() role.Updated = time.Now() err = orm.Save(role) + id = role.ID return } func DeleteRole(id string) (err error) { @@ -66,6 +71,19 @@ func GetRole(id string) (role rbac.Role, err error) { return } -func SearchRole() (roles []rbac.Role, err error) { +func SearchRole(keyword string, from, size int) (roles orm.Result, err error) { + query := orm.Query{} + + queryDSL := `{"query":{"bool":{"must":[%s]}}, "from": %d,"size": %d}` + mustBuilder := &strings.Builder{} + + if keyword != "" { + mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) + } + queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), from, size) + query.RawQuery = []byte(queryDSL) + + err, roles = orm.Search(rbac.Role{}, &query) + return } diff --git a/plugin/api/rbac/biz/user.go b/plugin/api/rbac/biz/user.go index b18b63bd..f5dea03c 100644 --- a/plugin/api/rbac/biz/user.go +++ b/plugin/api/rbac/biz/user.go @@ -6,6 +6,7 @@ import ( "infini.sh/console/plugin/api/rbac/dto" "infini.sh/framework/core/orm" "infini.sh/framework/core/util" + "strings" "time" ) @@ -24,11 +25,15 @@ func CreateUser(req dto.CreateUser) (id string, err error) { q := orm.Query{Size: 1000} q.Conds = orm.And(orm.Eq("name", req.Name)) - err, result := orm.Search(rbac.Role{}, &q) + err, result := orm.Search(rbac.User{}, &q) if err != nil { return } - fmt.Println(string(result.Raw)) + if result.Total > 0 { + err = fmt.Errorf("user name %s already exists", req.Name) + return + } + roles := make([]rbac.UserRole, 0) for _, v := range req.Roles { roles = append(roles, rbac.UserRole{ @@ -98,6 +103,20 @@ func GetUser(id string) (user rbac.User, err error) { return } -func SearchUser() { +func SearchUser(keyword string, from, size int) (users orm.Result, err error) { + query := orm.Query{} + + queryDSL := `{"query":{"bool":{"must":[%s]}}, "from": %d,"size": %d}` + mustBuilder := &strings.Builder{} + + if keyword != "" { + mustBuilder.WriteString(fmt.Sprintf(`{"query_string":{"default_field":"*","query": "%s"}}`, keyword)) + } + queryDSL = fmt.Sprintf(queryDSL, mustBuilder.String(), from, size) + query.RawQuery = []byte(queryDSL) + + err, users = orm.Search(rbac.User{}, &query) + + return } diff --git a/plugin/api/rbac/init.go b/plugin/api/rbac/init.go index 1caa0507..f75d5a35 100644 --- a/plugin/api/rbac/init.go +++ b/plugin/api/rbac/init.go @@ -60,6 +60,7 @@ func init() { } type Response struct { + Total int64 `json:"total,omitempty"` Hit interface{} `json:"hit,omitempty"` Id string `json:"_id,omitempty"` Result string `json:"result,omitempty"` @@ -86,7 +87,7 @@ func DeleteResponse(id string) Response { } func NotFoundResponse(id string) Response { return Response{ - Id: id, + Found: false, } } diff --git a/plugin/api/rbac/role.go b/plugin/api/rbac/role.go index 7e82f70c..17eddf16 100644 --- a/plugin/api/rbac/role.go +++ b/plugin/api/rbac/role.go @@ -40,14 +40,22 @@ func (h Rbac) CreateRole(w http.ResponseWriter, r *http.Request, ps httprouter.P func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { - roleType := ps.MustGetParameter("type") - err := validateRoleType(roleType) + var ( + keyword = h.GetParameterOrDefault(r, "keyword", "") + from = h.GetIntOrDefault(r, "from", 0) + size = h.GetIntOrDefault(r, "size", 20) + ) + + res, err := biz.SearchRole(keyword, from, size) if err != nil { - _ = log.Error(err.Error()) - _ = h.WriteError(w, err.Error(), http.StatusInternalServerError) + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) return } + + h.WriteJSON(w, Response{Hit: res.Result, Total: res.Total}, http.StatusOK) return + } func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { diff --git a/plugin/api/rbac/user.go b/plugin/api/rbac/user.go index c81fb2a3..20292c19 100644 --- a/plugin/api/rbac/user.go +++ b/plugin/api/rbac/user.go @@ -100,5 +100,20 @@ func (h Rbac) DeleteUser(w http.ResponseWriter, r *http.Request, ps httprouter.P } func (h Rbac) SearchUser(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { + var ( + keyword = h.GetParameterOrDefault(r, "keyword", "") + from = h.GetIntOrDefault(r, "from", 0) + size = h.GetIntOrDefault(r, "size", 20) + ) + + res, err := biz.SearchUser(keyword, from, size) + if err != nil { + log.Error(err) + h.WriteError(w, err.Error(), http.StatusInternalServerError) + return + } + + h.WriteJSON(w, Response{Hit: res.Result, Total: res.Total}, http.StatusOK) + return }