Created
April 5, 2010 03:34
-
-
Save vishnevskiy/355979 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def permission_required(*perms, **options): | |
def decorator(method): | |
def wrapper(self, *args, **kwargs): | |
for perm in perms: | |
perm = perm.split('.') | |
assert self.perms[perm[0]][perm[1]], \ | |
options.get('message', 'You have attempted to perform an action that you do not have permission to perform.') | |
method(self, *args, **kwargs) | |
return wrapper | |
return decorator | |
modules = {} | |
class ModuleBase(type): | |
def __new__(cls, name, bases, attrs): | |
if not [base for base in bases if isinstance(base, ModuleBase)]: | |
return type.__new__(cls, name, bases, attrs) | |
new_cls = type.__new__(cls, name, bases, {'__module__': attrs.pop('__module__')}) | |
new_cls.perms = [] | |
for key, value in attrs.iteritems(): | |
if isinstance(value, Permission): | |
new_cls.perms.append(key) | |
setattr(new_cls, key, value) | |
modules[name.lower()] = new_cls | |
return new_cls | |
class Module(object): | |
__metaclass__ = ModuleBase | |
def __init__(self, user, guild, group=None): | |
self.user = user | |
self.guild = guild | |
for perm in self.perms: | |
setattr(self, perm, user.has_admin or group in self[perm].groups) | |
def __getitem__(self, key): | |
return getattr(self, key) | |
class Permission(object): | |
def __init__(self, name=None, groups=[]): | |
self.name = name | |
self.groups = groups | |
class PermsWrapper(object): | |
def __init__(self, user, guild, group=None, exclude={}): | |
for module, cls in modules.iteritems(): | |
setattr(self, module, cls(user=user, guild=guild, group=group)) | |
for k, v in exclude.iteritems(): | |
module = getattr(self, k) | |
for perm in v: | |
module[perm] = False |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment