templatetags: allow UUIDs as |has_role filter argument (#79857)
gitea/combo/pipeline/head This commit looks good Details

This commit is contained in:
Paul Marillonnet 2023-07-20 15:27:21 +02:00
parent 903c856ff9
commit 875db57854
1 changed files with 16 additions and 1 deletions

View File

@ -47,6 +47,13 @@ from combo.utils import NothingInCacheException, flatten_context
from combo.utils.date import make_date, make_datetime
from combo.utils.requests_wrapper import WaitForCacheException
try:
from hobo.agent.common.models import Role
uses_hobo = True
except ImportError:
uses_hobo = False
register = template.Library()
@ -331,7 +338,15 @@ def has_role(user, groupname):
return False
if not hasattr(user, 'groups'):
return False
return user.groups.filter(name=groupname).exists()
by_uuid = False
if uses_hobo:
try:
role = Role.objects.get(uuid=groupname)
except Role.DoesNotExist:
pass
else:
by_uuid = role.user_set.filter(id=user.id).exists()
return bool(by_uuid or user.groups.filter(name=groupname).exists())
@register.filter