Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

app\commom\service\casbin.go的中加载策略重复 #50

Open
miwei230 opened this issue Jul 15, 2022 · 1 comment
Open

app\commom\service\casbin.go的中加载策略重复 #50

miwei230 opened this issue Jul 15, 2022 · 1 comment

Comments

@miwei230
Copy link

原函数:

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行)

它内部会 *adapterCasbin 的 LoadPolicy方法。
而下面的代码又手动再调用了一次:err = a.LoadPolicy(e.GetModel())
这样它就在数据库里查了两次

@tiger1103
Copy link
Owner

感谢反馈,确实是,下个版本一起修复

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants