Skip to content

Commit

Permalink
Merge pull request casbin#366 from nodece/master
Browse files Browse the repository at this point in the history
feat: controls whether to automatically notify Watcher
  • Loading branch information
hsluoyz authored Feb 24, 2020
2 parents 3f90d89 + fd75b44 commit 5baaaac
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 15 deletions.
7 changes: 7 additions & 0 deletions enforcer.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ type Enforcer struct {
enabled bool
autoSave bool
autoBuildRoleLinks bool
autoNotifyWatcher bool
}

// NewEnforcer creates an enforcer via file or DB.
Expand Down Expand Up @@ -171,6 +172,7 @@ func (e *Enforcer) initialize() {
e.enabled = true
e.autoSave = true
e.autoBuildRoleLinks = true
e.autoNotifyWatcher = true
}

// LoadModel reloads the model from the model CONF file.
Expand Down Expand Up @@ -316,6 +318,11 @@ func (e *Enforcer) EnableLog(enable bool) {
log.GetLogger().EnableLog(enable)
}

// EnableAutoNotifyWatcher controls whether to save a policy rule automatically notify the Watcher when it is added or removed.
func (e *Enforcer) EnableAutoNotifyWatcher(enable bool) {
e.autoNotifyWatcher = enable
}

// EnableAutoSave controls whether to save a policy rule automatically to the adapter when it is added or removed.
func (e *Enforcer) EnableAutoSave(autoSave bool) {
e.autoSave = autoSave
Expand Down
32 changes: 17 additions & 15 deletions internal_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ func (e *Enforcer) addPolicy(sec string, ptype string, rule []string) (bool, err
return ruleAdded, err
}
}
}

if e.watcher != nil {
err := e.watcher.Update()
if err != nil {
return ruleAdded, err
}
if e.watcher !=nil && e.autoNotifyWatcher {
err := e.watcher.Update()
if err != nil {
return ruleAdded, err
}
}

Expand All @@ -56,11 +56,12 @@ func (e *Enforcer) removePolicy(sec string, ptype string, rule []string) (bool,
return ruleRemoved, err
}
}
if e.watcher != nil {
err := e.watcher.Update()
if err != nil {
return ruleRemoved, err
}
}

if e.watcher !=nil && e.autoNotifyWatcher {
err := e.watcher.Update()
if err != nil {
return ruleRemoved, err
}
}

Expand All @@ -80,11 +81,12 @@ func (e *Enforcer) removeFilteredPolicy(sec string, ptype string, fieldIndex int
return ruleRemoved, err
}
}
if e.watcher != nil {
err := e.watcher.Update()
if err != nil {
return ruleRemoved, err
}
}

if e.watcher !=nil && e.autoNotifyWatcher {
err := e.watcher.Update()
if err != nil {
return ruleRemoved, err
}
}

Expand Down

0 comments on commit 5baaaac

Please sign in to comment.