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

[Bug] RoleManager's MatchingFunc Lost After Calling (*SyncedEnforcer).LoadPolicyFast #1339

Open
yougahu opened this issue Nov 13, 2023 · 2 comments

Comments

@yougahu
Copy link

yougahu commented Nov 13, 2023

Describe the bug

(*SyncedEnforcer).LoadPolicyFast do not inherit the previous RoleManager's MatchingFunc because it rebuilds all RoleManagers.

// LoadPolicyFast is not blocked when adapter calls LoadPolicy.
func (e *SyncedEnforcer) LoadPolicyFast() error {
        ...
	if e.autoBuildRoleLinks {
		for ptype := range newModel["g"] {
			newRmMap[ptype] = defaultrolemanager.NewRoleManager(10)
		}
		err = newModel.BuildRoleLinks(newRmMap)
		if err != nil {
			return err
		}
	}
        ...
}

Compare to (*Enforcer).LoadPolicy(which maintains RoleManager's MatchingFunc):

func (e *Enforcer) LoadPolicy() error {
        ...
	if e.autoBuildRoleLinks {
		needToRebuild = true
		if err := e.rebuildRoleLinks(newModel); err != nil {
			return err
		}

		if err := e.rebuildConditionalRoleLinks(newModel); err != nil {
			return err
		}
	}
        ...
}

func (e *Enforcer) rebuildRoleLinks(newModel model.Model) error {
	if len(e.rmMap) != 0 {
		for _, rm := range e.rmMap {
			err := rm.Clear()
			if err != nil {
				return err
			}
		}

		err := newModel.BuildRoleLinks(e.rmMap)
		if err != nil {
			return err
		}
	}

	return nil
}

Expected behavior

(*SyncedEnforcer).LoadPolicyFast maintains all RoleManager's MatchingFunc.

Actual behavior

RoleManager's MatchingFunc lost after calling (*SyncedEnforcer).LoadPolicyFast, which could cause enforce fail.

@casbin-bot
Copy link
Member

@tangyang9464 @JalinWang

@CaffreyZhang
Copy link

I'm having the same problem right now.
But I use the trick way to avoid it.

e, err := genEnforcer(db)
if err != nil {
log.WithField("err", err.Error()).Error("New casbin enforcer error.")
return
}

E.GetLock().Lock()
defer E.GetLock().Unlock()
E.Enforcer = e.Enforcer

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

No branches or pull requests

4 participants