Skip to content

Commit b190fce

Browse files
committed
feat: add logger integration to Casbin core enforcement and policy APIs (#1651)
1 parent 61c876f commit b190fce

File tree

4 files changed

+462
-13
lines changed

4 files changed

+462
-13
lines changed

enforcer.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"sync"
2323

2424
"github.com/casbin/casbin/v3/effector"
25+
"github.com/casbin/casbin/v3/log"
2526
"github.com/casbin/casbin/v3/model"
2627
"github.com/casbin/casbin/v3/persist"
2728
fileadapter "github.com/casbin/casbin/v3/persist/file-adapter"
@@ -45,6 +46,7 @@ type Enforcer struct {
4546
rmMap map[string]rbac.RoleManager
4647
condRmMap map[string]rbac.ConditionalRoleManager
4748
matcherMap sync.Map
49+
logger log.Logger
4850

4951
enabled bool
5052
autoSave bool
@@ -281,6 +283,11 @@ func (e *Enforcer) SetEffector(eft effector.Effector) {
281283
e.eft = eft
282284
}
283285

286+
// SetLogger sets the logger for the enforcer.
287+
func (e *Enforcer) SetLogger(logger log.Logger) {
288+
e.logger = logger
289+
}
290+
284291
// ClearPolicy clears all policy.
285292
func (e *Enforcer) ClearPolicy() {
286293
e.invalidateMatcherMap()
@@ -294,14 +301,21 @@ func (e *Enforcer) ClearPolicy() {
294301

295302
// LoadPolicy reloads the policy from file/database.
296303
func (e *Enforcer) LoadPolicy() error {
304+
logEntry := e.onLogBeforeEventInLoadPolicy()
305+
297306
newModel, err := e.loadPolicyFromAdapter(e.model)
298307
if err != nil {
308+
e.onLogAfterEventWithError(logEntry, err)
299309
return err
300310
}
301311
err = e.applyModifiedModel(newModel)
302312
if err != nil {
313+
e.onLogAfterEventWithError(logEntry, err)
303314
return err
304315
}
316+
317+
e.onLogAfterEventInLoadPolicy(logEntry, newModel)
318+
305319
return nil
306320
}
307321

@@ -445,12 +459,20 @@ func (e *Enforcer) IsFiltered() bool {
445459

446460
// SavePolicy saves the current policy (usually after changed with Casbin API) back to file/database.
447461
func (e *Enforcer) SavePolicy() error {
462+
logEntry := e.onLogBeforeEventInSavePolicy()
463+
448464
if e.IsFiltered() {
449-
return errors.New("cannot save a filtered policy")
465+
err := errors.New("cannot save a filtered policy")
466+
e.onLogAfterEventWithError(logEntry, err)
467+
return err
450468
}
451469
if err := e.adapter.SavePolicy(e.model); err != nil {
470+
e.onLogAfterEventWithError(logEntry, err)
452471
return err
453472
}
473+
474+
e.onLogAfterEventInSavePolicy(logEntry)
475+
454476
if e.watcher != nil {
455477
var err error
456478
if watcher, ok := e.watcher.(persist.WatcherEx); ok {
@@ -593,10 +615,16 @@ func (e *Enforcer) invalidateMatcherMap() {
593615

594616
// enforce use a custom matcher to decides whether a "subject" can access a "object" with the operation "action", input parameters are usually: (matcher, sub, obj, act), use model matcher by default when matcher is "".
595617
func (e *Enforcer) enforce(matcher string, explains *[]string, rvals ...interface{}) (ok bool, err error) { //nolint:funlen,cyclop,gocyclo // TODO: reduce function complexity
618+
logEntry := e.onLogBeforeEventInEnforce(rvals)
619+
596620
defer func() {
597621
if r := recover(); r != nil {
598622
err = fmt.Errorf("panic: %v\n%s", r, debug.Stack())
623+
if e.logger != nil && logEntry != nil {
624+
logEntry.Error = err
625+
}
599626
}
627+
e.onLogAfterEventInEnforce(logEntry, ok)
600628
}()
601629

602630
if !e.enabled {

internal_api.go

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919

2020
Err "github.com/casbin/casbin/v3/errors"
21+
"github.com/casbin/casbin/v3/log"
2122
"github.com/casbin/casbin/v3/model"
2223
"github.com/casbin/casbin/v3/persist"
2324
)
@@ -375,19 +376,22 @@ func (e *Enforcer) updateFilteredPoliciesWithoutNotify(sec string, ptype string,
375376

376377
// addPolicy adds a rule to the current policy.
377378
func (e *Enforcer) addPolicy(sec string, ptype string, rule []string) (bool, error) {
378-
ok, err := e.addPolicyWithoutNotify(sec, ptype, rule)
379+
ok, err := e.logPolicyOperation(log.EventAddPolicy, sec, rule, func() (bool, error) {
380+
return e.addPolicyWithoutNotify(sec, ptype, rule)
381+
})
382+
379383
if !ok || err != nil {
380384
return ok, err
381385
}
382386

383387
if e.shouldNotify() {
384-
var err error
385-
if watcher, ok := e.watcher.(persist.WatcherEx); ok {
386-
err = watcher.UpdateForAddPolicy(sec, ptype, rule...)
388+
var notifyErr error
389+
if watcher, isWatcherEx := e.watcher.(persist.WatcherEx); isWatcherEx {
390+
notifyErr = watcher.UpdateForAddPolicy(sec, ptype, rule...)
387391
} else {
388-
err = e.watcher.Update()
392+
notifyErr = e.watcher.Update()
389393
}
390-
return true, err
394+
return true, notifyErr
391395
}
392396

393397
return true, nil
@@ -417,19 +421,22 @@ func (e *Enforcer) addPolicies(sec string, ptype string, rules [][]string, autoR
417421

418422
// removePolicy removes a rule from the current policy.
419423
func (e *Enforcer) removePolicy(sec string, ptype string, rule []string) (bool, error) {
420-
ok, err := e.removePolicyWithoutNotify(sec, ptype, rule)
424+
ok, err := e.logPolicyOperation(log.EventRemovePolicy, sec, rule, func() (bool, error) {
425+
return e.removePolicyWithoutNotify(sec, ptype, rule)
426+
})
427+
421428
if !ok || err != nil {
422429
return ok, err
423430
}
424431

425432
if e.shouldNotify() {
426-
var err error
427-
if watcher, ok := e.watcher.(persist.WatcherEx); ok {
428-
err = watcher.UpdateForRemovePolicy(sec, ptype, rule...)
433+
var notifyErr error
434+
if watcher, isWatcherEx := e.watcher.(persist.WatcherEx); isWatcherEx {
435+
notifyErr = watcher.UpdateForRemovePolicy(sec, ptype, rule...)
429436
} else {
430-
err = e.watcher.Update()
437+
notifyErr = e.watcher.Update()
431438
}
432-
return true, err
439+
return true, notifyErr
433440
}
434441

435442
return true, nil

0 commit comments

Comments
 (0)