1
0
mirror of https://gitlab.crans.org/bde/nk20 synced 2024-12-18 21:42:21 +00:00
nk20/apps/member/backends.py
2019-09-18 14:26:42 +02:00

34 lines
1.2 KiB
Python

from django.contribs.contenttype.models import ContentType
from member.models import Club, Membership, RolePermissions
class PermissionBackend(object):
supports_object_permissions = True
supports_anonymous_user = False
supports_inactive_user = False
def authenticate(self, username, password):
return None
def permissions(self, user, obj):
for membership in user.memberships.all():
if not membership.valid() or membership.role is None:
continue
for permission in RolePermissions.objects.get(role=membership.role).permissions.objects.all():
permission = permission.about(user=user, club=membership.club)
yield permission
def has_perm(self, user_obj, perm, obj=None):
if obj is None:
return False
perm = perm.split('_')
perm_type = perm[1]
perm_field = perm[2] if len(perm) == 3 else None
return any(permission.applies(obj, perm_type, perm_field) for obj in self.permissions(user_obj, obj))
def get_all_permissions(self, user_obj, obj=None):
if obj is None:
return []
else:
return list(self.permissions(user_obj, obj))