Documentation ¶
Index ¶
- Variables
- func DefaultCallback(e casbin.IEnforcer) func(string)
- type MSG
- type Option
- type UpdateType
- type Watcher
- func (w *Watcher) Close()
- func (w *Watcher) GetLocalID() string
- func (w *Watcher) GetVerbose() bool
- func (w *Watcher) SetUpdateCallback(callbackFunc func(string)) error
- func (w *Watcher) Update() error
- func (w *Watcher) UpdateForAddPolicies(sec string, ptype string, rules ...[]string) error
- func (w *Watcher) UpdateForAddPolicy(sec string, ptype string, params ...string) error
- func (w *Watcher) UpdateForRemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
- func (w *Watcher) UpdateForRemovePolicies(sec string, ptype string, rules ...[]string) error
- func (w *Watcher) UpdateForRemovePolicy(sec string, ptype string, params ...string) error
- func (w *Watcher) UpdateForSavePolicy(model model.Model) error
- func (w *Watcher) UpdateForUpdatePolicies(sec string, ptype string, oldRules, newRules [][]string) error
- func (w *Watcher) UpdateForUpdatePolicy(sec string, ptype string, oldRule, newRule []string) error
Constants ¶
This section is empty.
Variables ¶
var (
ErrNotConnected = errors.New("pubsub not connected, cannot dispatch update message")
)
Errors
Functions ¶
func DefaultCallback ¶
func DefaultCallback(e casbin.IEnforcer) func(string)
DefaultCallback is the default callback function that the watcher will call when the policy in DB has been changed by other instances.
Types ¶
type MSG ¶
type MSG struct { Method UpdateType `json:"method"` // the update method ID string `json:"id"` // the unique ID of the watcher instance Sec string `json:"sec,omitempty"` // the section of the policy Ptype string `json:"ptype,omitempty"` // the policy type OldRules [][]string `json:"old_rules,omitempty"` // the old rules NewRules [][]string `json:"new_rules,omitempty"` // the new rules FieldIndex int `json:"field_index,omitempty"` // the field index FieldValues []string `json:"field_values,omitempty"` // the field values }
MSG is the payload for the pubsub message
func (MSG) MarshalBinary ¶
MarshalBinary implements the encoding.BinaryMarshaler interface.
func (*MSG) UnmarshalBinary ¶
UnmarshalBinary implements the encoding.BinaryUnmarshaler interface.
type Option ¶
type Option struct { Verbose bool // Verbose indicates the watcher should print verbose log. LocalID string // LocalID indicates the watcher's local ID, used to ignore self update event. Generates a random id if not specified. }
Option is the option for the watcher.
type UpdateType ¶
type UpdateType string
UpdateType is the type of update
const ( Update UpdateType = "Update" // Update is the default update type UpdateForAddPolicy UpdateType = "UpdateForAddPolicy" // UpdateForAddPolicy is the update type for AddPolicy UpdateForRemovePolicy UpdateType = "UpdateForRemovePolicy" // UpdateForRemovePolicy is the update type for RemovePolicy UpdateForRemoveFilteredPolicy UpdateType = "UpdateForRemoveFilteredPolicy" // UpdateForRemoveFilteredPolicy is the update type for RemoveFilteredPolicy UpdateForSavePolicy UpdateType = "UpdateForSavePolicy" // UpdateForSavePolicy is the update type for SavePolicy UpdateForAddPolicies UpdateType = "UpdateForAddPolicies" // UpdateForAddPolicies is the update type for AddPolicies UpdateForRemovePolicies UpdateType = "UpdateForRemovePolicies" // UpdateForRemovePolicies is the update type for RemovePolicies UpdateForUpdatePolicy UpdateType = "UpdateForUpdatePolicy" // UpdateForUpdatePolicy is the update type for UpdatePolicy UpdateForUpdatePolicies UpdateType = "UpdateForUpdatePolicies" // UpdateForUpdatePolicies is the update type for UpdatePolicies )
type Watcher ¶
type Watcher struct {
// contains filtered or unexported fields
}
Watcher implements Casbin updates watcher to synchronize policy changes between the nodes
func New ¶
New creates a new watcher
Parameters:
- ctx: the context for pubsub connections
- url: the pubsub url (e.g. "kafka://my-topic")
Returns:
- Watcher: the new watcher instance
- error: the error if the watcher cannot be created
func NewWithOption ¶
NewWithOption creates a new watcher with the option
Parameters:
- ctx: the context for pubsub connections
- url: the pubsub url (e.g. "kafka://my-topic")
- opt: the watcher option
Returns:
- Watcher: the new watcher instance
- error: the error if the watcher cannot be created
func (*Watcher) Close ¶
func (w *Watcher) Close()
Close stops and releases the watcher, the callback function will not be called any more.
func (*Watcher) GetLocalID ¶
GetLocalID gets the local id for the option.
func (*Watcher) GetVerbose ¶
GetVerbose gets the verbose for the option.
func (*Watcher) SetUpdateCallback ¶
SetUpdateCallback sets the callback function that the watcher will call when the policy in DB has been changed by other instances. A classic callback is Enforcer.LoadPolicy().
func (*Watcher) Update ¶
Update calls the update callback of other instances to synchronize their policy. It is usually called after changing the policy in DB, like Enforcer.SavePolicy(), Enforcer.AddPolicy(), Enforcer.RemovePolicy(), etc.
func (*Watcher) UpdateForAddPolicies ¶
UpdateForAddPolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.AddPolicies(), Enforcer.AddNamedPolicies(), Enforcer.AddGroupingPolicies() and Enforcer.AddNamedGroupingPolicies().
func (*Watcher) UpdateForAddPolicy ¶
UpdateForAddPolicy calls the update callback of other instances to synchronize their policy. It is called after a policy is added via Enforcer.AddPolicy(), Enforcer.AddNamedPolicy(), Enforcer.AddGroupingPolicy() and Enforcer.AddNamedGroupingPolicy().
func (*Watcher) UpdateForRemoveFilteredPolicy ¶
func (w *Watcher) UpdateForRemoveFilteredPolicy(sec string, ptype string, fieldIndex int, fieldValues ...string) error
UpdateForRemoveFilteredPolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemoveFilteredPolicy(), Enforcer.RemoveFilteredNamedPolicy(), Enforcer.RemoveFilteredGroupingPolicy() and Enforcer.RemoveFilteredNamedGroupingPolicy().
func (*Watcher) UpdateForRemovePolicies ¶
UpdateForRemovePolicies calls the update callback of other instances to synchronize their policy. It is called after Enforcer.RemovePolicies(), Enforcer.RemoveNamedPolicies(), Enforcer.RemoveGroupingPolicies() and Enforcer.RemoveNamedGroupingPolicies().
func (*Watcher) UpdateForRemovePolicy ¶
UPdateForRemovePolicy calls the update callback of other instances to synchronize their policy. It is called after a policy is removed by Enforcer.RemovePolicy(), Enforcer.RemoveNamedPolicy(), Enforcer.RemoveGroupingPolicy() and Enforcer.RemoveNamedGroupingPolicy().
func (*Watcher) UpdateForSavePolicy ¶
UpdateForSavePolicy calls the update callback of other instances to synchronize their policy. It is called after Enforcer.SavePolicy()