wip: 继续

This commit is contained in:
loveuer
2024-11-01 17:53:47 +08:00
parent 58fae2e090
commit cef1775811
4 changed files with 156 additions and 0 deletions

View File

@ -1,5 +1,10 @@
package model
import (
"fmt"
"strings"
)
type Privilege struct {
CreatedAt int64 `json:"created_at" gorm:"column:created_at;autoCreateTime:milli"`
UpdatedAt int64 `json:"updated_at" gorm:"column:updated_at;autoUpdateTime:milli"`
@ -9,3 +14,39 @@ type Privilege struct {
Parent string `json:"parent" gorm:"column:parent"`
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
}

View File

@ -11,3 +11,38 @@ 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
}

View File

@ -9,3 +9,19 @@ 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
}