diff --git a/internal/biz/account.go b/internal/biz/account.go index 5e76374a..eee13089 100644 --- a/internal/biz/account.go +++ b/internal/biz/account.go @@ -22,9 +22,10 @@ type UserClaims struct { *User } type User struct { - Username string `json:"username"` - UserId string `json:"user_id"` - Roles []string `json:"roles"` + Username string `json:"username"` + UserId string `json:"user_id"` + Roles []string `json:"roles"` + Privilege []string `json:"privilege"` } type Account struct { ID string `json:"id,omitempty" ` diff --git a/internal/biz/enum/const.go b/internal/biz/enum/const.go index d2b08c17..6c657966 100644 --- a/internal/biz/enum/const.go +++ b/internal/biz/enum/const.go @@ -24,12 +24,11 @@ func init() { UserMenu := Menu{ Id: "system_user", - Name: "用户管理", Privilege: "all", } RoleMenu := Menu{ - Id: "system_role", - Name: "角色管理", + Id: "system_role", + Privilege: "all", } AdminMenu := []Menu{ diff --git a/internal/biz/enum/menu.go b/internal/biz/enum/menu.go index f12602f7..3d75ee6e 100644 --- a/internal/biz/enum/menu.go +++ b/internal/biz/enum/menu.go @@ -1,7 +1,7 @@ package enum type Menu struct { - Id string `json:"id"` - Name string `json:"name"` + Id string `json:"id"` + Privilege string `json:"privilege,omitempty"` } diff --git a/internal/biz/role.go b/internal/biz/role.go index 22a065b4..0bd19446 100644 --- a/internal/biz/role.go +++ b/internal/biz/role.go @@ -21,18 +21,16 @@ const ( ) type IRole interface { - ListPermission() interface{} Create(localUser *User) (id string, err error) + //Delete(localUser *User, id string) (err error) } type ConsoleRole struct { - Name string `json:"name"` - Description string `json:"description" ` - RoleType string `json:"type" ` - Permission Permission `json:"permission"` -} -type Permission struct { - Menu []MenuPermission `json:"menu"` + Name string `json:"name"` + Description string `json:"description" ` + RoleType string `json:"type" ` + Platform []string `json:"platform,omitempty"` } + type MenuPermission struct { Id string `json:"id"` Privilege string `json:"privilege"` @@ -82,7 +80,7 @@ func (role ConsoleRole) Create(localUser *User) (id string, err error) { Name: role.Name, Description: role.Description, RoleType: role.RoleType, - Permission: role.Permission, + Platform: role.Platform, } newRole.ID = util.GetUUID() newRole.Created = time.Now() @@ -101,7 +99,7 @@ func (role ConsoleRole) Create(localUser *User) (id string, err error) { "id": id, "name": role.Name, "description": role.Description, - "permission": role.Permission, + "platform": role.Platform, "type": role.RoleType, "created": newRole.Created.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 } - newRole := rbac.Role{ + newRole := rbac.ElasticRole{ Name: role.Name, Description: role.Description, RoleType: role.RoleType, @@ -205,7 +203,7 @@ func DeleteRole(localUser *User, id string) (err error) { "id": id, "name": role.Name, "description": role.Description, - "permission": role.Permission, + "platform": role.Platform, "type": role.RoleType, "created": role.Created.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) role.Description = req.Description - role.Permission = req.Permission + role.Platform = req.Platform role.Updated = time.Now() err = orm.Save(role) if err != nil { @@ -238,7 +236,7 @@ func UpdateRole(localUser *User, id string, req dto.UpdateConsoleRole) (err erro Labels: util.MapStr{ "id": id, "description": role.Description, - "permission": role.Permission, + "platform": role.Platform, "updated": role.Updated, }, User: util.MapStr{ diff --git a/internal/dto/role.go b/internal/dto/role.go index de97414d..9ab2e858 100644 --- a/internal/dto/role.go +++ b/internal/dto/role.go @@ -10,8 +10,8 @@ type Menu struct { Privilege string `json:"privilege"` } type UpdateConsoleRole struct { - Description string `json:"description" ` - Permission RolePermission `json:"permission"` + Description string `json:"description" ` + Platform []string `json:"platform"` } type CreateEsRole struct { Name string `json:"name"` diff --git a/model/rbac/role.go b/model/rbac/role.go index ae0b2520..012219fe 100644 --- a/model/rbac/role.go +++ b/model/rbac/role.go @@ -6,12 +6,12 @@ import ( type Role 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}"` - Permission interface{} `json:"permission,omitempty" elastic_mapping:"permission:{type:object}"` - BuiltIn bool `json:"builtin" elastic_mapping:"builtin:{type:boolean}"` //是否内置 - ElasticRole + 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}"` + Platform []string `json:"platform,omitempty" ` + BuiltIn bool `json:"builtin" elastic_mapping:"builtin:{type:boolean}"` //是否内置 + } type ConsolePermission struct { Api []string `json:"api"` @@ -25,7 +25,12 @@ type Menu 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"` Name string `json:"name"` } `json:"cluster,omitempty"` diff --git a/plugin/api/rbac/permission.go b/plugin/api/rbac/permission.go index 7a356cc8..165b9398 100644 --- a/plugin/api/rbac/permission.go +++ b/plugin/api/rbac/permission.go @@ -1,8 +1,6 @@ package rbac import ( - log "github.com/cihub/seelog" - "infini.sh/console/internal/biz" httprouter "infini.sh/framework/core/api/router" "net/http" ) @@ -10,19 +8,19 @@ import ( func (h Rbac) ListPermission(w http.ResponseWriter, r *http.Request, ps httprouter.Params) { typ := ps.MustGetParameter("type") - err := biz.IsAllowRoleType(typ) - if err != nil { - h.Error400(w, err.Error()) - return - } - role, err := biz.NewRole(typ) + //err := biz.IsAllowRoleType(typ) + //if err != nil { + // h.Error400(w, err.Error()) + // return + //} + //role, err := biz.NewRole(typ) + // + //if err != nil { + // _ = log.Error(err.Error()) + // h.Error(w, err) + // return + //} - if err != nil { - _ = log.Error(err.Error()) - h.Error(w, err) - return - } - permissions := role.ListPermission() - h.WriteOKJSON(w, permissions) + h.WriteOKJSON(w, typ) return }