-
-
Notifications
You must be signed in to change notification settings - Fork 195
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
remove_grouping_policy: If there are duplicate group rules, remove_grouping_policy will be invalid #258
Comments
Here is my code:
model.conf
|
@ffyuanda @Zxilly @techoner @elfisworking |
@cs1137195420 I tried to remove duplicate rules with from casbin_peewee_adapter import CasbinRule, Adapter
import casbin
import peewee
import os
db_name = 'db.sqlite3'
if os.path.exists(db_name):
os.remove(db_name)
db = peewee.SqliteDatabase(db_name)
adapter = Adapter(database=db)
db.create_tables([CasbinRule])
e = casbin.Enforcer('model.conf', adapter)
#add twice
e.add_grouping_policy('bob', 'admin')
e.add_grouping_policy('bob', 'admin')
assert CasbinRule.select().count() == 1
#add manually
rule = CasbinRule.create(ptype='g', v0='bob', v1='admin')
rule.save()
assert CasbinRule.select().count() == 2
e.remove_grouping_policy('bob', 'admin')
assert CasbinRule.select().count() == 0
print("OK") output:
Can you try this example with peewee.MySQLDatabase? |
I tried it with peewee.MySQLDatabase, and my program also printed OK. However, when I called the add_grouping_policy, CasbinRule.create and remove_grouping_policy functions respectively, the error reappeared.
output:
output:
Can you try again in this way? |
This time I was able to reproduce your error. Example: from casbin_peewee_adapter import CasbinRule, Adapter
import casbin
import peewee
import os
db_name = 'db.sqlite3'
if os.path.exists(db_name):
os.remove(db_name)
db = peewee.SqliteDatabase(db_name)
adapter = Adapter(database=db)
db.create_tables([CasbinRule])
e = casbin.Enforcer('model.conf', adapter)
#add twice
e.add_grouping_policy('bob', 'admin')
e.add_grouping_policy('bob', 'admin')
assert CasbinRule.select().count() == 1
#add manually
rule = CasbinRule.create(ptype='g', v0='bob', v1='admin')
rule.save()
assert CasbinRule.select().count() == 2
e2 = casbin.Enforcer('model.conf', adapter)
e2.remove_grouping_policy('bob', 'admin')
assert CasbinRule.select().count() == 0, f'{CasbinRule.select().count()} == 0'
print("OK") Output:
The problem is, that the peewee adapter loads the rule twice into the enforcer instance
When In my opinion, the best solution would be to prevent duplicate entries in the adapter, using a unique constraint. |
@abichinger In case there are duplicate entries in the adapter, should |
@cs1137195420 @abichinger plz see how Golang Casbin works |
I would say yes, but that's something the adapter has to do. @hsluoyz The golang version has a similar issue. (link is above) I think we could fix this issue, if we use @cs1137195420 Would you like to create a PR for this? |
Sure, I would like to. |
🎉 This issue has been resolved in version 1.16.5 🎉 The release is available on:
Your semantic-release bot 📦🚀 |
I accidentally created two identical group rules in mysql, and then when I called the remove_grouping_policy function, I found that it failed to delete. There were still two identical group rules in mysql. So, should the remove_grouping_policy function remove all the same grouping rules?
The text was updated successfully, but these errors were encountered: