wip: rbac - 01
This commit is contained in:
@ -1,9 +1,10 @@
|
||||
package model
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
// platform:module:class:action
|
||||
// pro:*:*:*
|
||||
// pro:content:*:*
|
||||
// pro:content:secret_news:*
|
||||
// pro:content:secret_news:upload
|
||||
|
||||
type Privilege struct {
|
||||
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
|
||||
@ -15,38 +16,6 @@ type Privilege struct {
|
||||
Scope string `json:"scope" gorm:"column:scope"`
|
||||
}
|
||||
|
||||
func (u *Urbac) newPrivilege(ctx context.Context, code, label string, parentId uint64, scope string) (*Privilege, error) {
|
||||
p := &Privilege{Code: code, Label: label, ParentId: parentId, Scope: scope}
|
||||
|
||||
codes := strings.SplitN(code, ":", 4)
|
||||
if len(codes) != 4 {
|
||||
return nil, fmt.Errorf("invalid code format")
|
||||
}
|
||||
|
||||
wailcard := false
|
||||
for _, item := range codes {
|
||||
if item == "*" {
|
||||
wailcard = true
|
||||
}
|
||||
|
||||
if wailcard && item != "*" {
|
||||
return nil, fmt.Errorf("invalid code format")
|
||||
}
|
||||
|
||||
if len(item) > 8 {
|
||||
return nil, fmt.Errorf("invalid code format: code snippet too long")
|
||||
}
|
||||
}
|
||||
|
||||
if codes[0] != "*" {
|
||||
if _, err := u.GetScopeGroup(ctx, codes[0]); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
if err := u.store.Session(ctx).Create(p).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return p, nil
|
||||
func (p *Privilege) Validate() error {
|
||||
return nil
|
||||
}
|
||||
|
@ -11,38 +11,3 @@ type Role struct {
|
||||
Parent string `json:"parent" gorm:"column:parent"`
|
||||
PrivilegeCodes sqlType.StrSlice `json:"privilege_codes" gorm:"column:privilege_codes"`
|
||||
}
|
||||
|
||||
func (u *Urbac) newRole(ctx context.Context, name, label, parent string, privileges ...*Privilege) (*Role, error) {
|
||||
ps := lo.FilterMap(
|
||||
privileges,
|
||||
func(p *Privilege, _ int) (string, bool) {
|
||||
if p == nil {
|
||||
return "", false
|
||||
}
|
||||
|
||||
return p.Code, p.Code != ""
|
||||
},
|
||||
)
|
||||
|
||||
r := &Role{
|
||||
Name: name,
|
||||
Label: label,
|
||||
Parent: parent,
|
||||
PrivilegeCodes: ps,
|
||||
}
|
||||
|
||||
if err := u.store.Session(ctx).Create(r).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return r, nil
|
||||
}
|
||||
|
||||
func (u *Urbac) GetRole(ctx context.Context, name string) (*Role, error) {
|
||||
var r Role
|
||||
if err := u.store.Session(ctx).Take(&r, "name = ?", name).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &r, nil
|
||||
}
|
||||
|
@ -9,19 +9,3 @@ type Scope struct {
|
||||
Label string `json:"label" gorm:"column:label;type:varchar(64)"`
|
||||
Parent string `json:"parent" gorm:"column:parent;type:varchar(8)"`
|
||||
}
|
||||
|
||||
func (u *Urbac) newScope(ctx context.Context, name, label, parent string) (*Scope, error) {
|
||||
s := &Scope{Name: name, Label: label, Parent: parent}
|
||||
if err := u.store.Session(ctx).Create(s).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (u *Urbac) GetScopeGroup(ctx context.Context, name string) (*Scope, error) {
|
||||
scope := new(Scope)
|
||||
err := u.store.Session(ctx).Where("name = ?", name).Take(scope).Error
|
||||
|
||||
return scope, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user