I have to manage permissions and I have different user types, as for example a couple here.
def not_allowed(*args, **kwargs): return False
class User(object):
def __init__(self, userid):
self.userid = userid
def __getattr__(self, attr):
return not_allowed
def view_profile(self)
return True
class Admin(User):
def edit_comment(self):
return True
class Expert(User):
def delete_post(self):
return True
user = {'userid': 'user', 'roles': ['admin', 'expert']}
Now I want to be able to have MultiRole type, which in theory should simply be able to do everything that its roles are able to do.
I tried with something like this:
class MultiRoleUser(User):
"""A multirole user has all the power of all the roles together
"""
def __init__(self, userid, roles):
super(MultiRoleUser, self).__init__(userid)
self.roles = roles
def __getattr__(self, attr):
all_funcs = [getattr(x, attr) for x in self.roles]
return any(x() for x in all_funcs)
Which should be used as
u = MultiRoleUser('userid', [Admin, Expert])
But it doesn't work, ideally I would like to call the methods for all the classes passed in and do a or (with any).
The problem is that to call the methods I need to have an object of that type..
In theory I might even just use a dictionary for each role instead, but I liked the default to false trick which makes, and sometimes I also need a function to compute the permission.
Any suggestions?