Browse Source

DBPolicyBuilder

xuxin 1 year ago
parent
commit
09d1aac333
2 changed files with 34 additions and 14 deletions
  1. 10 14
      perm/builder.go
  2. 24 0
      perm/db_policy.go

+ 10 - 14
perm/builder.go

@@ -57,12 +57,12 @@ func ToPermissionRN(v interface{}) []string {
 }
 
 type Builder struct {
-	m             sync.Mutex
-	policies      []*PolicyBuilder
-	ladon         *ladon.Ladon
-	subjectsFunc  SubjectsFunc
-	contextFunc   ContextFunc
-	dbPolicyModel DBPolicy
+	m            sync.Mutex
+	policies     []*PolicyBuilder
+	ladon        *ladon.Ladon
+	subjectsFunc SubjectsFunc
+	contextFunc  ContextFunc
+	dbPolicy     *DBPolicyBuilder
 }
 
 func New() *Builder {
@@ -144,14 +144,10 @@ func (b *Builder) ContextFunc(v ContextFunc) (r *Builder) {
 	return b
 }
 
-func (b *Builder) EnableDBPolicy(db *gorm.DB, dbPolicyModel DBPolicy, loadDuration time.Duration) (r *Builder) {
-	if dbPolicyModel == nil {
-		b.dbPolicyModel = DefaultDBPolicy{}
-	} else {
-		b.dbPolicyModel = dbPolicyModel
-	}
+func (b *Builder) DBPolicy(dpb *DBPolicyBuilder) (r *Builder) {
+	b.dbPolicy = dpb
 
-	go b.loopLoadDBPolicies(db, loadDuration)
+	go b.loopLoadDBPolicies(dpb.db, dpb.loadFrequency)
 	return b
 }
 
@@ -188,7 +184,7 @@ func (b *Builder) DeletePolicies(toDelete ...*PolicyBuilder) {
 }
 
 func (b *Builder) LoadDBPoliciesToMemory(db *gorm.DB, startFrom *time.Time) {
-	toUpdateOrCreate, toDelete := b.dbPolicyModel.LoadDBPolicies(db, startFrom)
+	toUpdateOrCreate, toDelete := b.dbPolicy.model.LoadDBPolicies(db, startFrom)
 	b.DeletePolicies(toDelete...)
 	b.UpdateOrCreatePolicies(toUpdateOrCreate...)
 	if Verbose {

+ 24 - 0
perm/db_policy.go

@@ -9,6 +9,30 @@ import (
 	"gorm.io/gorm"
 )
 
+type DBPolicyBuilder struct {
+	db            *gorm.DB
+	model         DBPolicy
+	loadFrequency time.Duration
+}
+
+func NewDBPolicy(db *gorm.DB) *DBPolicyBuilder {
+	return &DBPolicyBuilder{
+		db:            db,
+		model:         DefaultDBPolicy{},
+		loadFrequency: time.Minute,
+	}
+}
+
+func (dpb *DBPolicyBuilder) Model(m DBPolicy) *DBPolicyBuilder {
+	dpb.model = m
+	return dpb
+}
+
+func (dpb *DBPolicyBuilder) LoadFrequency(d time.Duration) *DBPolicyBuilder {
+	dpb.loadFrequency = d
+	return dpb
+}
+
 type DefaultDBPolicy struct {
 	gorm.Model