Mitja Nikolaus | a901798 | 2018-11-21 12:14:45 +0100 | [diff] [blame] | 1 | """Permissions for accessing the stats API.""" |
| 2 | from django.core.exceptions import PermissionDenied |
| 3 | |
| 4 | from crashreports.permissions import user_is_hiccup_staff |
| 5 | from hiccup.allauth_adapters import FP_STAFF_GROUP_NAME |
| 6 | |
| 7 | |
| 8 | def check_user_is_hiccup_staff(user): |
| 9 | """Check if the user is part of the Hiccup staff. |
| 10 | |
Mitja Nikolaus | 39f42a1 | 2018-12-10 15:40:22 +0100 | [diff] [blame^] | 11 | Returns: |
| 12 | True if the user is part of the Hiccup staff group, False if the user |
| 13 | is not logged in. |
Mitja Nikolaus | a901798 | 2018-11-21 12:14:45 +0100 | [diff] [blame] | 14 | |
| 15 | Raises: |
| 16 | PermissionDenied: If the user is not part of the Hiccup staff group. |
| 17 | |
| 18 | """ |
Mitja Nikolaus | 39f42a1 | 2018-12-10 15:40:22 +0100 | [diff] [blame^] | 19 | if not user.is_authenticated: |
| 20 | return False |
Mitja Nikolaus | a901798 | 2018-11-21 12:14:45 +0100 | [diff] [blame] | 21 | 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 |