fix: (rbac) list index names by cluster ids

This commit is contained in:
xushuhui 2022-04-22 18:36:39 +08:00
parent fbacc4ab62
commit 0c7d037e07
5 changed files with 74 additions and 70 deletions

View File

@ -100,7 +100,7 @@ func authorize(user Account) (m map[string]interface{}, err error) {
"expire_in": 86400, "expire_in": 86400,
"roles": []string{"admin"}, "roles": []string{"admin"},
"privilege": []string{ "privilege": []string{
"system_user:all", "system_role:all", "system_cluster:all", "system_command:all", "system.user:all", "system.role:all", "system.cluster:all", "system.command:all",
}, },
} }
return return

View File

@ -6,39 +6,18 @@ var IndexApis = make([]string, 0)
var RolePermission = make(map[string][]string) var RolePermission = make(map[string][]string)
type ConsolePermisson struct { type ConsolePermisson struct {
Menu []Menu `json:"menu"` Platform []Platform `json:"platform"`
} }
type Menu struct { type Platform struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"`
Privilege []string `json:"privilege,omitempty"` Privilege map[string]string `json:"privilege,omitempty"`
Children []Menu `json:"children,omitempty"` Children []Platform `json:"children,omitempty"`
} }
func (role ConsoleRole) ListPermission() interface{} { func (role ConsoleRole) ListPermission() interface{} {
menu := []Menu{
{
Id: "system",
Name: "系统管理",
Children: []Menu{
{
Id: "system_user",
Name: "用户管理",
Privilege: []string{"none", "read", "all"},
},
{
Id: "system_role", p := ConsolePermisson{}
Name: "角色管理",
Privilege: []string{"none", "read", "all"},
},
},
},
}
p := ConsolePermisson{
Menu: menu,
}
return p return p
} }
func (role ElasticsearchRole) ListPermission() interface{} { func (role ElasticsearchRole) ListPermission() interface{} {

View File

@ -31,12 +31,10 @@ type ConsoleRole struct {
Permission Permission `json:"permission"` Permission Permission `json:"permission"`
} }
type Permission struct { type Permission struct {
Api []string `json:"api"`
Menu []MenuPermission `json:"menu"` Menu []MenuPermission `json:"menu"`
} }
type MenuPermission struct { type MenuPermission struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"`
Privilege string `json:"privilege"` Privilege string `json:"privilege"`
} }
type ElasticsearchRole struct { type ElasticsearchRole struct {

View File

@ -1,6 +1,8 @@
package index_management package index_management
import ( import (
"fmt"
"infini.sh/framework/core/elastic"
"net/http" "net/http"
"strconv" "strconv"
"strings" "strings"
@ -116,43 +118,68 @@ func (handler APIHandler) UpdateDictItemAction(w http.ResponseWriter, req *http.
handler.WriteJSON(w, resp, http.StatusOK) handler.WriteJSON(w, resp, http.StatusOK)
} }
func (handler APIHandler) ListIndex(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func (h APIHandler) ListIndex(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
// clusterIds := handler.GetParameterOrDefault(req, "cluster_id", "") clusterIds := h.GetParameterOrDefault(req, "ids", "")
// keyword := handler.GetParameterOrDefault(req, "keyword", "") keyword := h.GetParameterOrDefault(req, "keyword", "")
// Ids := strings.Split(clusterIds, ",") ids := strings.Split(clusterIds, ",")
// var dsl = `{ for i := range ids {
// "_source": ["metadata.index_name"], ids[i] = `"` + ids[i] + `"`
// "collapse": { }
// "field": "metadata.index_name" if len(ids) == 0 {
// }, h.Error400(w, "id is required")
// "size": 100, return
// "query": { }
// "bool": { var dsl = `{
// "must": [ "_source": ["metadata.index_name"],
// { "collapse": {
// "terms": { "field": "metadata.index_name"
// "metadata.cluster_id": [%s] },
// } "size": 100,
// },%s "query": {
// ], "bool": {
// "must_not": [ "must": [
// { {
// "term": { "terms": {
// "metadata.labels.state": { "metadata.cluster_id": %s
// "value": "delete" }
// } }%s
// } ],
// } "must_not": [
// ] {
// } "term": {
// } "metadata.labels.state": {
//}` "value": "delete"
// var likeDsl = `{ }
// "wildcard": { }
// "metadata.index_name": { }
// "value": "*inf*" ]
// } }
// } }
// }` }`
str := &strings.Builder{}
if keyword != "" {
str.WriteString(fmt.Sprintf(`,{"wildcard":{"metadata.index_name":{"value":"*%s*"}}}`, keyword))
}
dsl = fmt.Sprintf(dsl, ids, str)
esClient := elastic.GetClient(h.Config.Elasticsearch)
resp, err := esClient.SearchWithRawQueryDSL(".infini_index", []byte(dsl))
if err != nil {
return
}
list := resp.Hits.Hits
var indexNames []string
for _, v := range list {
m := v.Source["metadata"].(map[string]interface{})
indexNames = append(indexNames, m["index_name"].(string))
}
m := make(map[string]interface{})
m["indexnames"] = indexNames
h.WriteOKJSON(w, m)
return return
} }

View File

@ -58,7 +58,7 @@ func loadRolePermission() {
} }
func init() { func init() {
registerRouter() registerRouter()
loadJsonConfig()
loadRolePermission() loadRolePermission()
} }