fix: (rbac)

This commit is contained in:
xushuhui 2022-04-24 09:37:07 +08:00
parent 0c7d037e07
commit a6f3c2203f
7 changed files with 47 additions and 46 deletions

View File

@ -22,9 +22,10 @@ type UserClaims struct {
*User *User
} }
type User struct { type User struct {
Username string `json:"username"` Username string `json:"username"`
UserId string `json:"user_id"` UserId string `json:"user_id"`
Roles []string `json:"roles"` Roles []string `json:"roles"`
Privilege []string `json:"privilege"`
} }
type Account struct { type Account struct {
ID string `json:"id,omitempty" ` ID string `json:"id,omitempty" `

View File

@ -24,12 +24,11 @@ func init() {
UserMenu := Menu{ UserMenu := Menu{
Id: "system_user", Id: "system_user",
Name: "用户管理",
Privilege: "all", Privilege: "all",
} }
RoleMenu := Menu{ RoleMenu := Menu{
Id: "system_role", Id: "system_role",
Name: "角色管理",
Privilege: "all", Privilege: "all",
} }
AdminMenu := []Menu{ AdminMenu := []Menu{

View File

@ -1,7 +1,7 @@
package enum package enum
type Menu struct { type Menu struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"`
Privilege string `json:"privilege,omitempty"` Privilege string `json:"privilege,omitempty"`
} }

View File

@ -21,18 +21,16 @@ const (
) )
type IRole interface { type IRole interface {
ListPermission() interface{}
Create(localUser *User) (id string, err error) Create(localUser *User) (id string, err error)
//Delete(localUser *User, id string) (err error)
} }
type ConsoleRole struct { type ConsoleRole struct {
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description" ` Description string `json:"description" `
RoleType string `json:"type" ` RoleType string `json:"type" `
Permission Permission `json:"permission"` Platform []string `json:"platform,omitempty"`
}
type Permission struct {
Menu []MenuPermission `json:"menu"`
} }
type MenuPermission struct { type MenuPermission struct {
Id string `json:"id"` Id string `json:"id"`
Privilege string `json:"privilege"` Privilege string `json:"privilege"`
@ -82,7 +80,7 @@ func (role ConsoleRole) Create(localUser *User) (id string, err error) {
Name: role.Name, Name: role.Name,
Description: role.Description, Description: role.Description,
RoleType: role.RoleType, RoleType: role.RoleType,
Permission: role.Permission, Platform: role.Platform,
} }
newRole.ID = util.GetUUID() newRole.ID = util.GetUUID()
newRole.Created = time.Now() newRole.Created = time.Now()
@ -101,7 +99,7 @@ func (role ConsoleRole) Create(localUser *User) (id string, err error) {
"id": id, "id": id,
"name": role.Name, "name": role.Name,
"description": role.Description, "description": role.Description,
"permission": role.Permission, "platform": role.Platform,
"type": role.RoleType, "type": role.RoleType,
"created": newRole.Created.Format("2006-01-02 15:04:05"), "created": newRole.Created.Format("2006-01-02 15:04:05"),
"updated": newRole.Updated.Format("2006-01-02 15:04:05"), "updated": newRole.Updated.Format("2006-01-02 15:04:05"),
@ -136,7 +134,7 @@ func (role ElasticsearchRole) Create(localUser *User) (id string, err error) {
return return
} }
newRole := rbac.Role{ newRole := rbac.ElasticRole{
Name: role.Name, Name: role.Name,
Description: role.Description, Description: role.Description,
RoleType: role.RoleType, RoleType: role.RoleType,
@ -205,7 +203,7 @@ func DeleteRole(localUser *User, id string) (err error) {
"id": id, "id": id,
"name": role.Name, "name": role.Name,
"description": role.Description, "description": role.Description,
"permission": role.Permission, "platform": role.Platform,
"type": role.RoleType, "type": role.RoleType,
"created": role.Created.Format("2006-01-02 15:04:05"), "created": role.Created.Format("2006-01-02 15:04:05"),
"updated": role.Updated.Format("2006-01-02 15:04:05"), "updated": role.Updated.Format("2006-01-02 15:04:05"),
@ -224,7 +222,7 @@ func UpdateRole(localUser *User, id string, req dto.UpdateConsoleRole) (err erro
} }
changeLog, _ := util.DiffTwoObject(role, req) changeLog, _ := util.DiffTwoObject(role, req)
role.Description = req.Description role.Description = req.Description
role.Permission = req.Permission role.Platform = req.Platform
role.Updated = time.Now() role.Updated = time.Now()
err = orm.Save(role) err = orm.Save(role)
if err != nil { if err != nil {
@ -238,7 +236,7 @@ func UpdateRole(localUser *User, id string, req dto.UpdateConsoleRole) (err erro
Labels: util.MapStr{ Labels: util.MapStr{
"id": id, "id": id,
"description": role.Description, "description": role.Description,
"permission": role.Permission, "platform": role.Platform,
"updated": role.Updated, "updated": role.Updated,
}, },
User: util.MapStr{ User: util.MapStr{

View File

@ -10,8 +10,8 @@ type Menu struct {
Privilege string `json:"privilege"` Privilege string `json:"privilege"`
} }
type UpdateConsoleRole struct { type UpdateConsoleRole struct {
Description string `json:"description" ` Description string `json:"description" `
Permission RolePermission `json:"permission"` Platform []string `json:"platform"`
} }
type CreateEsRole struct { type CreateEsRole struct {
Name string `json:"name"` Name string `json:"name"`

View File

@ -6,12 +6,12 @@ import (
type Role struct { type Role struct {
orm.ORMObjectBase orm.ORMObjectBase
Name string `json:"name" elastic_mapping:"name:{type:keyword}"` Name string `json:"name" elastic_mapping:"name:{type:keyword}"`
Description string `json:"description" elastic_mapping:"description:{type:text}"` Description string `json:"description" elastic_mapping:"description:{type:text}"`
RoleType string `json:"type" elastic_mapping:"type:{type:keyword}"` RoleType string `json:"type" elastic_mapping:"type:{type:keyword}"`
Permission interface{} `json:"permission,omitempty" elastic_mapping:"permission:{type:object}"` Platform []string `json:"platform,omitempty" `
BuiltIn bool `json:"builtin" elastic_mapping:"builtin:{type:boolean}"` //是否内置 BuiltIn bool `json:"builtin" elastic_mapping:"builtin:{type:boolean}"` //是否内置
ElasticRole
} }
type ConsolePermission struct { type ConsolePermission struct {
Api []string `json:"api"` Api []string `json:"api"`
@ -25,7 +25,12 @@ type Menu struct {
} }
type ElasticRole struct { type ElasticRole struct {
Cluster []struct { orm.ORMObjectBase
Name string `json:"name" elastic_mapping:"name:{type:keyword}"`
Description string `json:"description" elastic_mapping:"description:{type:text}"`
RoleType string `json:"type" elastic_mapping:"type:{type:keyword}"`
BuiltIn bool `json:"builtin" elastic_mapping:"builtin:{type:boolean}"` //是否内置
Cluster []struct {
Id string `json:"id"` Id string `json:"id"`
Name string `json:"name"` Name string `json:"name"`
} `json:"cluster,omitempty"` } `json:"cluster,omitempty"`

View File

@ -1,8 +1,6 @@
package rbac package rbac
import ( import (
log "github.com/cihub/seelog"
"infini.sh/console/internal/biz"
httprouter "infini.sh/framework/core/api/router" httprouter "infini.sh/framework/core/api/router"
"net/http" "net/http"
) )
@ -10,19 +8,19 @@ import (
func (h Rbac) ListPermission(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { func (h Rbac) ListPermission(w http.ResponseWriter, r *http.Request, ps httprouter.Params) {
typ := ps.MustGetParameter("type") typ := ps.MustGetParameter("type")
err := biz.IsAllowRoleType(typ) //err := biz.IsAllowRoleType(typ)
if err != nil { //if err != nil {
h.Error400(w, err.Error()) // h.Error400(w, err.Error())
return // return
} //}
role, err := biz.NewRole(typ) //role, err := biz.NewRole(typ)
//
//if err != nil {
// _ = log.Error(err.Error())
// h.Error(w, err)
// return
//}
if err != nil { h.WriteOKJSON(w, typ)
_ = log.Error(err.Error())
h.Error(w, err)
return
}
permissions := role.ListPermission()
h.WriteOKJSON(w, permissions)
return return
} }