Skip to content

Commit

Permalink
fix(model): emit events
Browse files Browse the repository at this point in the history
  • Loading branch information
abichinger committed May 8, 2022
1 parent 8677eeb commit 858c8ff
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
26 changes: 20 additions & 6 deletions model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -248,25 +248,39 @@ func (m *Model) BuildMatcherFromDef(mDef *defs.MatcherDef) (matcher.IMatcher, er
func (m *Model) AddRule(rule []string) (bool, error) {
key := rule[0]
sec := key[0]
var added bool
var err error
switch sec {
case 'p':
return m.addPolicyRule(key, rule[1:])
added, err = m.addPolicyRule(key, rule[1:])
case 'g':
return m.addRoleRule(key, rule[1:])
added, err = m.addRoleRule(key, rule[1:])
default:
return false, fmt.Errorf(str.ERR_POLICY_NOT_FOUND, key)
}
if added {
m.Emitter.EmitEvent(RULE_ADDED, rule)
}
return false, fmt.Errorf(str.ERR_POLICY_NOT_FOUND, key)
return added, err
}

func (m *Model) RemoveRule(rule []string) (bool, error) {
key := rule[0]
sec := key[0]
var removed bool
var err error
switch sec {
case 'p':
return m.removePolicyRule(key, rule[1:])
removed, err = m.removePolicyRule(key, rule[1:])
case 'g':
return m.removeRoleRule(key, rule[1:])
removed, err = m.removeRoleRule(key, rule[1:])
default:
return false, fmt.Errorf(str.ERR_POLICY_NOT_FOUND, key)
}
if removed {
m.Emitter.EmitEvent(RULE_REMOVED, rule)
}
return false, fmt.Errorf(str.ERR_POLICY_NOT_FOUND, key)
return removed, err
}

func (m *Model) addPolicyRule(key string, rule []string) (bool, error) {
Expand Down
5 changes: 4 additions & 1 deletion storage/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ func (sc *StorageController) addOp(opc opcode, rule []string) {
if sc.autosave {
sc.wait--
if sc.wait <= 0 {
sc.Flush()
err := sc.Flush()
if err != nil {
panic(err)
}
}
}
}
Expand Down

0 comments on commit 858c8ff

Please sign in to comment.