feat: (rbac) register role api

This commit is contained in:
xushuhui 2022-04-14 14:24:56 +08:00
parent 844f091a13
commit 61d2c8dea7
4 changed files with 81 additions and 8 deletions

View File

@ -9,14 +9,18 @@ import (
"path" "path"
) )
type Permisson struct { type Rbac struct {
api.Handler api.Handler
} }
func registerRouter() { func registerRouter() {
p := Permisson{} r := Rbac{}
api.HandleAPIMethod(api.GET, "/permission/:type", r.ListPermission)
api.HandleAPIMethod(api.GET, "/permission/:type", p.ListPermission) api.HandleAPIMethod(api.POST, "/role", r.CreateRole)
api.HandleAPIMethod(api.GET, "/role/:id", r.GetRole)
api.HandleAPIMethod(api.DELETE, "/role/:id", r.DeleteRole)
api.HandleAPIMethod(api.PUT, "/role/:id", r.UpdateRole)
api.HandleAPIMethod(api.GET, "/roles", r.ListRole)
} }
func loadJsonConfig() { func loadJsonConfig() {

View File

@ -19,9 +19,20 @@ type Response struct {
Hit interface{} `json:"hit"` Hit interface{} `json:"hit"`
} }
func (h Permisson) ListPermission(w http.ResponseWriter, req *http.Request, ps httprouter.Params) { func validateRoleType(roleType RoleType) (err error) {
if roleType != Console && roleType != Elastisearch {
err = errors.New("unsupport type parmeter " + roleType)
}
return
}
func (h Rbac) ListPermission(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
typ := ps.MustGetParameter("type") typ := ps.MustGetParameter("type")
var err error err := validateRoleType(typ)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
var permissons interface{} var permissons interface{}
switch typ { switch typ {
case Console: case Console:
@ -29,8 +40,6 @@ func (h Permisson) ListPermission(w http.ResponseWriter, req *http.Request, ps h
case Elastisearch: case Elastisearch:
permissons, err = biz.ListElasticsearchPermisson() permissons, err = biz.ListElasticsearchPermisson()
default:
err = errors.New("unsupport type parmeter " + typ)
} }
if err != nil { if err != nil {
_ = log.Error(err.Error()) _ = log.Error(err.Error())

51
plugin/api/rbac/role.go Normal file
View File

@ -0,0 +1,51 @@
package rbac
import (
log "github.com/cihub/seelog"
httprouter "infini.sh/framework/core/api/router"
"net/http"
)
type CreateRoleReq struct {
Name string `json:"name"`
Description string `json:"description" `
RoleType string `json:"type" `
Permission interface{} `json:"permission"`
}
type ElasticsearchPermission struct {
Cluster []string `json:"cluster" `
Index []string `json:"index" `
ClusterPrivilege []string `json:"cluster_privilege" `
IndexPrivilege []string `json:"index_privilege" `
}
func (h Rbac) CreateRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
roleType := ps.MustGetParameter("type")
err := validateRoleType(roleType)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h Rbac) ListRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
roleType := ps.MustGetParameter("type")
err := validateRoleType(roleType)
if err != nil {
_ = log.Error(err.Error())
_ = h.WriteError(w, err.Error(), http.StatusInternalServerError)
return
}
}
func (h Rbac) GetRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
_ = ps.MustGetParameter("id")
}
func (h Rbac) DeleteRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
_ = ps.MustGetParameter("id")
}
func (h Rbac) UpdateRole(w http.ResponseWriter, req *http.Request, ps httprouter.Params) {
_ = ps.MustGetParameter("id")
}

9
plugin/api/rbac/user.go Normal file
View File

@ -0,0 +1,9 @@
package rbac
type CreateUserReq struct {
Username string `json:"username" `
Password string `json:"password" `
Name string `json:"name" `
Phone string `json:"phone" `
Email string `json:"email" `
}