2012-07-31 19:24:26 +00:00
|
|
|
from django.db.models import Q
|
|
|
|
|
|
|
|
from .models import Team
|
|
|
|
|
|
|
|
|
|
|
|
class TeamPermissionsBackend(object):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-31 19:24:26 +00:00
|
|
|
def authenticate(self, username=None, password=None):
|
|
|
|
return None
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-31 19:24:26 +00:00
|
|
|
def get_team_permissions(self, user_obj, obj=None):
|
|
|
|
"""
|
|
|
|
Returns a set of permission strings that this user has through his/her
|
|
|
|
team memberships.
|
|
|
|
"""
|
|
|
|
if user_obj.is_anonymous() or obj is not None:
|
|
|
|
return set()
|
|
|
|
if not hasattr(user_obj, "_team_perm_cache"):
|
2017-04-17 12:51:48 +00:00
|
|
|
# Member permissions
|
2012-07-31 19:24:26 +00:00
|
|
|
memberships = Team.objects.filter(
|
2017-04-17 12:51:48 +00:00
|
|
|
Q(memberships__user=user_obj),
|
2016-06-13 11:20:46 +00:00
|
|
|
Q(memberships__state="member"),
|
2012-07-31 19:24:26 +00:00
|
|
|
)
|
|
|
|
perms = memberships.values_list(
|
|
|
|
"permissions__content_type__app_label",
|
|
|
|
"permissions__codename"
|
|
|
|
).order_by()
|
2017-04-17 12:51:48 +00:00
|
|
|
permissions = ["%s.%s" % (ct, name) for ct, name in perms]
|
2016-06-13 11:20:46 +00:00
|
|
|
# Manager permissions
|
|
|
|
memberships = Team.objects.filter(
|
|
|
|
Q(memberships__user=user_obj),
|
|
|
|
Q(memberships__state="manager"),
|
|
|
|
)
|
|
|
|
perms = memberships.values_list(
|
|
|
|
"manager_permissions__content_type__app_label",
|
|
|
|
"manager_permissions__codename"
|
|
|
|
).order_by()
|
2017-04-17 12:51:48 +00:00
|
|
|
permissions += ["%s.%s" % (ct, name) for ct, name in perms]
|
2016-06-13 11:20:46 +00:00
|
|
|
user_obj._team_perm_cache = set(permissions)
|
2012-07-31 19:24:26 +00:00
|
|
|
return user_obj._team_perm_cache
|
|
|
|
|
|
|
|
def has_perm(self, user_obj, perm, obj=None):
|
|
|
|
if not user_obj.is_active:
|
|
|
|
return False
|
|
|
|
return perm in self.get_team_permissions(user_obj, obj)
|