Fix team permissions backend not pulling out manager_permissions

Something like
request.user.has_perm('reviews.can_manage_%s' % proposal.kind.section.slug)

Will aways return false as the backend does a lookup of team membership
(member or manager) but only grabs the 'permissions' and not the
'manager_permissions' field
This commit is contained in:
Scott Bragg 2016-06-13 21:20:46 +10:00
parent 13393ef826
commit a6405ccfc7

View file

@ -16,15 +16,27 @@ class TeamPermissionsBackend(object):
if user_obj.is_anonymous() or obj is not None: if user_obj.is_anonymous() or obj is not None:
return set() return set()
if not hasattr(user_obj, "_team_perm_cache"): if not hasattr(user_obj, "_team_perm_cache"):
# Member permissions
memberships = Team.objects.filter( memberships = Team.objects.filter(
Q(memberships__user=user_obj), Q(memberships__user=user_obj),
Q(memberships__state="manager") | Q(memberships__state="member"), Q(memberships__state="member"),
) )
perms = memberships.values_list( perms = memberships.values_list(
"permissions__content_type__app_label", "permissions__content_type__app_label",
"permissions__codename" "permissions__codename"
).order_by() ).order_by()
user_obj._team_perm_cache = set(["%s.%s" % (ct, name) for ct, name in perms]) permissions = ["%s.%s" % (ct, name) for ct, name in perms]
# 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()
permissions += ["%s.%s" % (ct, name) for ct, name in perms]
user_obj._team_perm_cache = set(permissions)
return user_obj._team_perm_cache return user_obj._team_perm_cache
def has_perm(self, user_obj, perm, obj=None): def has_perm(self, user_obj, perm, obj=None):