feat: (rbac) search role api / search user api
This commit is contained in:
parent
cd81b909b5
commit
f0b7c1a8dd
|
@ -8,7 +8,6 @@ type ConsolePermisson struct {
|
||||||
Name string `json:"name"`
|
Name string `json:"name"`
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
|
||||||
func ListConsolePermisson() (list []ConsolePermisson, err error) {
|
func ListConsolePermisson() (list []ConsolePermisson, err error) {
|
||||||
list = []ConsolePermisson{
|
list = []ConsolePermisson{
|
||||||
{
|
{
|
||||||
|
|
|
@ -5,6 +5,7 @@ import (
|
||||||
"infini.sh/console/model/rbac"
|
"infini.sh/console/model/rbac"
|
||||||
"infini.sh/console/plugin/api/rbac/dto"
|
"infini.sh/console/plugin/api/rbac/dto"
|
||||||
"infini.sh/framework/core/util"
|
"infini.sh/framework/core/util"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
|
@ -19,8 +20,11 @@ func CreateRole(req dto.CreateRole) (id string, err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if result.Total > 0 {
|
||||||
|
err = fmt.Errorf("role name %s already exists", req.Name)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
fmt.Println(string(result.Raw))
|
|
||||||
role := &rbac.Role{
|
role := &rbac.Role{
|
||||||
Name: req.Name,
|
Name: req.Name,
|
||||||
Description: req.Description,
|
Description: req.Description,
|
||||||
|
@ -31,6 +35,7 @@ func CreateRole(req dto.CreateRole) (id string, err error) {
|
||||||
role.Created = time.Now()
|
role.Created = time.Now()
|
||||||
role.Updated = time.Now()
|
role.Updated = time.Now()
|
||||||
err = orm.Save(role)
|
err = orm.Save(role)
|
||||||
|
id = role.ID
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
func DeleteRole(id string) (err error) {
|
func DeleteRole(id string) (err error) {
|
||||||
|
@ -66,6 +71,19 @@ func GetRole(id string) (role rbac.Role, err error) {
|
||||||
|
|
||||||
return
|
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
|
return
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,6 +6,7 @@ import (
|
||||||
"infini.sh/console/plugin/api/rbac/dto"
|
"infini.sh/console/plugin/api/rbac/dto"
|
||||||
"infini.sh/framework/core/orm"
|
"infini.sh/framework/core/orm"
|
||||||
"infini.sh/framework/core/util"
|
"infini.sh/framework/core/util"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -24,11 +25,15 @@ func CreateUser(req dto.CreateUser) (id string, err error) {
|
||||||
q := orm.Query{Size: 1000}
|
q := orm.Query{Size: 1000}
|
||||||
q.Conds = orm.And(orm.Eq("name", req.Name))
|
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 {
|
if err != nil {
|
||||||
return
|
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)
|
roles := make([]rbac.UserRole, 0)
|
||||||
for _, v := range req.Roles {
|
for _, v := range req.Roles {
|
||||||
roles = append(roles, rbac.UserRole{
|
roles = append(roles, rbac.UserRole{
|
||||||
|
@ -98,6 +103,20 @@ func GetUser(id string) (user rbac.User, err error) {
|
||||||
return
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -60,6 +60,7 @@ func init() {
|
||||||
}
|
}
|
||||||
|
|
||||||
type Response struct {
|
type Response struct {
|
||||||
|
Total int64 `json:"total,omitempty"`
|
||||||
Hit interface{} `json:"hit,omitempty"`
|
Hit interface{} `json:"hit,omitempty"`
|
||||||
Id string `json:"_id,omitempty"`
|
Id string `json:"_id,omitempty"`
|
||||||
Result string `json:"result,omitempty"`
|
Result string `json:"result,omitempty"`
|
||||||
|
@ -86,7 +87,7 @@ func DeleteResponse(id string) Response {
|
||||||
}
|
}
|
||||||
func NotFoundResponse(id string) Response {
|
func NotFoundResponse(id string) Response {
|
||||||
return Response{
|
return Response{
|
||||||
Id: id,
|
|
||||||
Found: false,
|
Found: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func (h Rbac) SearchRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
|
||||||
roleType := ps.MustGetParameter("type")
|
var (
|
||||||
err := validateRoleType(roleType)
|
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 {
|
if err != nil {
|
||||||
_ = log.Error(err.Error())
|
log.Error(err)
|
||||||
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
h.WriteError(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
h.WriteJSON(w, Response{Hit: res.Result, Total: res.Total}, http.StatusOK)
|
||||||
return
|
return
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
func (h Rbac) GetRole(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
|
||||||
|
|
|
@ -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) {
|
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
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue