You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
func (a *adapterCasbin) initPolicy(ctx context.Context) {
// Because the DB is empty at first,
// so we need to load the policy from the file adapter (.CSV) first.
e, err := casbin.NewSyncedEnforcer(g.Cfg().MustGet(ctx, "casbin.modelFile").String(), a)
if err != nil {
a.EnforcerErr = err
return
}
// This is a trick to save the current policy to the DB.
// We can't call e.SavePolicy() because the adapter in the enforcer is still the file adapter.
// The current policy means the policy in the Casbin enforcer (aka in memory).
//err = a.SavePolicy(e.GetModel())
//if err != nil {
// return err
//}
//set adapter
//e.SetAdapter(a)
// Clear the current policy.
e.ClearPolicy()
a.Enforcer = e
// Load the policy from DB.
err = a.LoadPolicy(e.GetModel())
if err != nil {
a.EnforcerErr = err
return
}
}
这行代码:e, err := casbin.NewSyncedEnforcer(g.Cfg().MustGet(ctx, "casbin.modelFile").String(), a) (源码调用见casbin\casbin\[email protected]\enforcer.go第178行)
原函数:
这行代码:
e, err := casbin.NewSyncedEnforcer(g.Cfg().MustGet(ctx, "casbin.modelFile").String(), a)
(源码调用见casbin\casbin\[email protected]\enforcer.go第178行)它内部会 *adapterCasbin 的 LoadPolicy方法。
而下面的代码又手动再调用了一次:
err = a.LoadPolicy(e.GetModel())
这样它就在数据库里查了两次
The text was updated successfully, but these errors were encountered: