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

UniqueConstrain isn't respected on ActionRoles #188

Open
zannkukai opened this issue Jan 31, 2022 · 0 comments
Open

UniqueConstrain isn't respected on ActionRoles #188

zannkukai opened this issue Jan 31, 2022 · 0 comments

Comments

@zannkukai
Copy link

Package version (if known): 1.4.2

Describe the bug

It is possible to register multiple ActionRoles with same values despite the SQLAlchemy UniqueConstraint (https://github.com/inveniosoftware/invenio-access/blob/master/invenio_access/models.py#L115-L118) if argument is None.

Steps to Reproduce

access_actionsroles table initial state

image

from invenio_access import action_factory, ActionRoles
from invenio_accounts.models import Role
from invenio_db import db
import random

random_role = random.choice(Role.query.all())

action = action_factory('my-action')
action_arg = action_factory('my-action-arg', parameter=True)

for i in range(3):
    db.session.add(ActionRoles.allow(action, role=random_role))
    db.session.commit()

for i in range(3):
    try:     
        db.session.add(ActionRoles.allow(action_arg('argValue'), role=random_role))
        db.session.commit()
    except Exception as e:
        print('Error', str(e))  # You should see 2 SQLAlchemy errors

access_actionroles table after the script
image

Expected behavior

The UniqueConstraint key should be respected even if action doesn't have any argument.
Regarding this StackOverflow answer, it should be possible using 2 indexes (one if argument is null, second if argument is not null) : https://stackoverflow.com/a/57647018

Additional context

It should be the same for ActionUsers and ActionSystemRoles

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

1 participant