|
1 | 1 | import binascii |
2 | 2 | from copy import deepcopy |
| 3 | +from functools import wraps |
3 | 4 |
|
| 5 | +from django.core.exceptions import PermissionDenied |
4 | 6 | from django.db import transaction |
5 | 7 | from graphene import Node |
6 | | -from graphql_jwt.decorators import user_passes_test |
7 | | -from graphql_jwt.exceptions import PermissionDenied |
| 8 | +from graphql.execution.base import ResolveInfo |
8 | 9 | from graphql_relay import from_global_id, to_global_id |
9 | 10 |
|
10 | 11 | from kukkuu import __version__ |
@@ -78,6 +79,34 @@ def get_obj_if_user_can_administer(info, global_id, expected_obj_type): |
78 | 79 | return obj |
79 | 80 |
|
80 | 81 |
|
| 82 | +def context(f): |
| 83 | + def decorator(func): |
| 84 | + def wrapper(*args, **kwargs): |
| 85 | + info = next(arg for arg in args if isinstance(arg, ResolveInfo)) |
| 86 | + return func(info.context, *args, **kwargs) |
| 87 | + |
| 88 | + return wrapper |
| 89 | + |
| 90 | + return decorator |
| 91 | + |
| 92 | + |
| 93 | +def user_passes_test(test_func): |
| 94 | + def decorator(f): |
| 95 | + @wraps(f) |
| 96 | + @context(f) |
| 97 | + def wrapper(context, *args, **kwargs): |
| 98 | + if test_func(context.user): |
| 99 | + return f(*args, **kwargs) |
| 100 | + raise PermissionDenied("You do not have permission to perform this action") |
| 101 | + |
| 102 | + return wrapper |
| 103 | + |
| 104 | + return decorator |
| 105 | + |
| 106 | + |
| 107 | +# copied from https://github.com/flavors/django-graphql-jwt/blob/704f24e7ebbea0b81015ef3c1f4a302e9d432ecf/graphql_jwt/decorators.py # noqa |
| 108 | +login_required = user_passes_test(lambda u: u.is_authenticated) |
| 109 | + |
81 | 110 | project_user_required = user_passes_test( |
82 | 111 | lambda u: u.is_authenticated and u.administered_projects |
83 | 112 | ) |
0 commit comments