Hello everyone!
I am trying to build system with:
permissions (I use p_type=p for defining permissions), example:
p,root:root:root,, - root permission
p,core:users:create,/users,POST - permission for user creating
roles (I use p_type=g for defining roles), example:
g,Root,root:root:root - Root role
g,UserCreator,core:users:create, - UserCreator role
users (I use p_type=g for defining users), example:
g,1,Root - user (id=1) with Root role
g,2,UserCreator - user (id=2) with UserCreator role
One role can has many permissions
One permission can be addeded to many Roles
One role can be addeded to many Users
One User can has one role (even technicly one user can has many roles)
But I faced with an issue. During implementing "assign role to user" feature I noticed some weird behavior - it can be desynchronization between Database and model. Even desynchronization between Assertion.Policy and Assertion.PolicyMap
The first time I noticed the iisue when I used two methods one by one (in this example i skip error checking but I my code these checks are)
s.enforcer.RemoveFilteredGroupingPolicy(0, userStringId) - remove old role
s.enforcer.AddGroupingPolicy(userStringId, role) - assign new role
And somethimes when I try assing the same role for different users the model save wrong values, but in Database all are correct, and after reloading application everutyng is OK in model
For example, I try assing users with ids 6,7,8 to Root role:
In DB:
id,p_type,v0,v1,v2,v3,v4,v5
513,g,6,Root,"","","",""
514,g,8,Root,"","","",""
515,g,7,Root,"","","",""
In PolicyMap:
"6,Root": 22
"8,Root": 23
"7,Root": 24
In Policy:
22: [7 Root]
23: [8 Root]
24: [7 Root]
After restarting application, all are OK
In Policy:
22: [6 Root]
23: [8 Root]
24: [7 Root]
I noticed if I added some delay between methods calling the issue is not reproduced:
s.enforcer.RemoveFilteredGroupingPolicy(0, userStringId) - remove old role
time.Sleep(time.Second)
s.enforcer.AddGroupingPolicy(userStringId, role) - assign new role
And I tried to change these method to one
s.enforcer.UpdateGroupingPolicy([]string{userStringId, oldRole}, []string{userStringId, newRole})
But issue is still reproduced
Even I use direct method:
s.enforcer.GetModel().UpdatePolicy("g", "g", []string{userStringId, oldRole}, []string{userStringId, newRole})
I use
go1.19.8 linux/amd64
github.com/casbin/casbin/v2 v2.73.0
github.com/Blank-Xu/sql-adapter v1.0.0
Plese, help)
