blob: ab00fe8f3dd6eb14268e9712fe3d629b45a1ed05 [file] [log] [blame]
Mitja Nikolausa9017982018-11-21 12:14:45 +01001"""Permissions for accessing the stats API."""
2from django.core.exceptions import PermissionDenied
3
4from crashreports.permissions import user_is_hiccup_staff
5from hiccup.allauth_adapters import FP_STAFF_GROUP_NAME
6
7
8def check_user_is_hiccup_staff(user):
9 """Check if the user is part of the Hiccup staff.
10
Mitja Nikolaus39f42a12018-12-10 15:40:22 +010011 Returns:
12 True if the user is part of the Hiccup staff group, False if the user
13 is not logged in.
Mitja Nikolausa9017982018-11-21 12:14:45 +010014
15 Raises:
16 PermissionDenied: If the user is not part of the Hiccup staff group.
17
18 """
Mitja Nikolaus39f42a12018-12-10 15:40:22 +010019 if not user.is_authenticated:
20 return False
Mitja Nikolausa9017982018-11-21 12:14:45 +010021 if not user_is_hiccup_staff(user):
22 raise PermissionDenied(
23 "User %s not part of the %s group" % (user, FP_STAFF_GROUP_NAME)
24 )
25 return True