INNER CODE UNIT · Go

initPolicy

apache/casbin-mongodb-adapter · adapter_test.go:108

func initPolicy(t *testing.T, dbURL string) {
	// Because the DB is empty at first,
	// so we need to load the policy from the file adapter (.CSV) first.
	e, err := casbin.NewEnforcer("examples/rbac_model.conf", "examples/rbac_policy.csv")
	if err != nil {
		panic(err)
	}

	a, err := NewAdapter(dbURL)
	if err != nil {
		panic(err)
	}
	// 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 {
		panic(err)

View source record →

📰 Research Paper
Loading…
⏳ Fetching content…