template processor can_user to check permissions
This commit is contained in:
parent
d0f1908a6a
commit
5465d537c3
2 changed files with 30 additions and 0 deletions
|
@ -7,3 +7,32 @@ def projects(request):
|
||||||
return {'projects': request.projects}
|
return {'projects': request.projects}
|
||||||
else:
|
else:
|
||||||
return {}
|
return {}
|
||||||
|
|
||||||
|
|
||||||
|
class PermissionChecker:
|
||||||
|
|
||||||
|
def getattr(self, request, perm, obj=None):
|
||||||
|
if request.user.is_authenticated():
|
||||||
|
return request.user.has_perm(perm, obj)
|
||||||
|
|
||||||
|
class PermWrapper:
|
||||||
|
|
||||||
|
def __init__(self, user):
|
||||||
|
self.user = user
|
||||||
|
|
||||||
|
def __getitem__(self, perm):
|
||||||
|
return self.user.has_perm(perm)
|
||||||
|
|
||||||
|
def __iter__(self):
|
||||||
|
raise TypeError("PermWrapper is not iterable.")
|
||||||
|
|
||||||
|
def __contains__(self, perm):
|
||||||
|
return self[perm]
|
||||||
|
|
||||||
|
def can_user(request):
|
||||||
|
|
||||||
|
wrapper = PermWrapper(request.user)
|
||||||
|
|
||||||
|
return {
|
||||||
|
'can_user': wrapper,
|
||||||
|
}
|
||||||
|
|
|
@ -109,6 +109,7 @@ TEMPLATE_CONTEXT_PROCESSORS = (
|
||||||
'django.contrib.auth.context_processors.auth',
|
'django.contrib.auth.context_processors.auth',
|
||||||
'django.core.context_processors.request',
|
'django.core.context_processors.request',
|
||||||
'issue.context_processors.projects',
|
'issue.context_processors.projects',
|
||||||
|
'issue.context_processors.can_user',
|
||||||
)
|
)
|
||||||
|
|
||||||
AUTHENTICATION_BACKENDS = (
|
AUTHENTICATION_BACKENDS = (
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue