SavePolicy saves the current policy (usually after changed with Casbin API) back to file/database.
()
| 525 | |
| 526 | // SavePolicy saves the current policy (usually after changed with Casbin API) back to file/database. |
| 527 | func (e *Enforcer) SavePolicy() error { |
| 528 | logEntry := e.onLogBeforeEventInSavePolicy() |
| 529 | |
| 530 | if e.IsFiltered() { |
| 531 | err := errors.New("cannot save a filtered policy") |
| 532 | e.onLogAfterEventWithError(logEntry, err) |
| 533 | return err |
| 534 | } |
| 535 | if err := e.adapter.SavePolicy(e.model); err != nil { |
| 536 | e.onLogAfterEventWithError(logEntry, err) |
| 537 | return err |
| 538 | } |
| 539 | |
| 540 | e.onLogAfterEventInSavePolicy(logEntry) |
| 541 | |
| 542 | if e.watcher != nil { |
| 543 | var err error |
| 544 | if watcher, ok := e.watcher.(persist.WatcherEx); ok { |
| 545 | err = watcher.UpdateForSavePolicy(e.model) |
| 546 | } else { |
| 547 | err = e.watcher.Update() |
| 548 | } |
| 549 | return err |
| 550 | } |
| 551 | return nil |
| 552 | } |
| 553 | |
| 554 | // getDomainTokens extracts domain token names from request and policy definitions. |
| 555 | // Returns empty strings if tokens cannot be found. |