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

RoleMixin __init__ hasattr fails #24

Open
roelstorms opened this issue Jan 10, 2019 · 0 comments
Open

RoleMixin __init__ hasattr fails #24

roelstorms opened this issue Jan 10, 2019 · 0 comments

Comments

@roelstorms
Copy link

I have a class Role as follows (following your example in the docs):

class Role(Base, RoleMixin):

    id = Column(Integer, primary_key=True)
    name = Column(String(20))
    parents = relationship(
        'Role',
        secondary=roles_parents,
        primaryjoin=(id == roles_parents.c.role_id),
        secondaryjoin=(id == roles_parents.c.parent_id),
        backref=backref('children', lazy='joined'),
        lazy='joined'
    )

    def __init__(self, name):
        RoleMixin.__init__(self)
        self.name = name
...

However in Python 3.6.3 with flask-rbac 0.2.1 I have some problems.

$ pip show flask-rbac
Name: Flask-RBAC
Version: 0.2.1
Summary: RBAC support for Flask
Home-page: https://github.com/shonenada/flask-rbac
Author: Yaoda Liu
Author-email: [email protected]
License: UNKNOWN
Location: c:\program files (x86)\python\lib\site-packages
Requires: Flask
Required-by:

I can narrow it down to the RoleMixin.init:

    def __init__(self, name=None):
        self.name = name
        if not hasattr(self.__class__, 'parents'):
            self.parents = set()
        if not hasattr(self.__class__, 'children'):
            self.children = set()
        RoleMixin.roles[name] = self

hasattr(self.class, 'parents') returns False
hasattr(self.class, 'children') returns False

I would expect the Role class to have a parent attribute and therefor this method to return False.
In my case, self.parents and self.children get set to set() which messes up the rest of the method calls when checking for an access control decision.

I believe children and parents should be set by my class. Parents are already set to the relationship and children are set via SQLAlchemy via a backref. This is probably done dynamically and might explain why hasattr(self.class, 'children') returns False.

It is unclear to me why hasattr(self.class, 'parents') returns False in my case.

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