drf-iam 0.0.3__py3-none-any.whl → 0.2.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
File without changes
@@ -0,0 +1,71 @@
1
+ import inspect
2
+
3
+ from django.template.base import kwarg_re
4
+ from django.urls.resolvers import URLPattern, URLResolver
5
+ from rest_framework.viewsets import ViewSetMixin
6
+ from rest_framework.decorators import action
7
+
8
+ from django.urls import get_resolver
9
+ from ..models import Policy
10
+
11
+
12
+ def is_viewset(cls):
13
+ return isinstance(cls, type) and issubclass(cls, ViewSetMixin)
14
+
15
+
16
+ def get_viewset_actions(viewset_cls):
17
+ actions = set()
18
+
19
+ # Default ViewSet methods
20
+ default_actions = {'list', 'retrieve', 'create', 'update', 'partial_update', 'destroy'}
21
+ for action_name in default_actions:
22
+ if hasattr(viewset_cls, action_name):
23
+ actions.add(action_name)
24
+
25
+ # Custom @action methods
26
+ for name, method in inspect.getmembers(viewset_cls, predicate=inspect.isfunction):
27
+ if hasattr(method, 'mapping'):
28
+ for http_method in method.mapping:
29
+ viewset_function = getattr(viewset_cls, name)
30
+ if viewset_function.get("kwargs"):
31
+ actions.add(f"{name}")
32
+
33
+ return actions
34
+
35
+
36
+ def extract_viewsets_from_urlpatterns(urlpatterns, prefix=''):
37
+ for pattern in urlpatterns:
38
+ if isinstance(pattern, URLResolver):
39
+ # Recurse into included URLConfs
40
+ yield from extract_viewsets_from_urlpatterns(pattern.url_patterns, prefix + str(pattern.pattern))
41
+ elif isinstance(pattern, URLPattern):
42
+ callback = pattern.callback
43
+ cls = getattr(callback, 'cls', None)
44
+ if is_viewset(cls):
45
+ yield {
46
+ 'prefix': prefix,
47
+ 'pattern': pattern.pattern,
48
+ 'viewset': cls,
49
+ 'callback': callback
50
+ }
51
+
52
+
53
+ def load_permissions_from_urls(**kwargs):
54
+ urlpatterns = get_resolver().url_patterns
55
+ for entry in extract_viewsets_from_urlpatterns(urlpatterns):
56
+ viewset_cls = entry['viewset']
57
+ basename = getattr(viewset_cls,"iam_policy_name") or viewset_cls.__name__.lower().replace('viewset', '')
58
+ exclude_from_permissions = getattr(viewset_cls, "drf_iam_exclude_from_permissions", False)
59
+ if exclude_from_permissions:
60
+ continue
61
+ actions = get_viewset_actions(viewset_cls)
62
+
63
+ for action in actions:
64
+ full_action = f"{basename}:{action}"
65
+ Policy.objects.get_or_create(
66
+ action=full_action,
67
+ resource_type=basename,
68
+ defaults={
69
+ 'description': f"Permission for {full_action} on {basename}"
70
+ }
71
+ )
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: drf-iam
3
- Version: 0.0.3
3
+ Version: 0.2.0
4
4
  Summary: IAM-style roles and permissions for Django Rest Framework
5
5
  Home-page: https://github.com/tushar1328/drf-iam.git
6
6
  Author: Tushar Patel
@@ -6,7 +6,9 @@ drf_iam/permissions.py,sha256=kUhB7V48sb4v3uZI2lWv40hvI85nE61rL-amh6OY8mg,958
6
6
  drf_iam/tests.py,sha256=mrbGGRNg5jwbTJtWWa7zSKdDyeB4vmgZCRc2nk6VY-g,60
7
7
  drf_iam/migrations/0001_initial.py,sha256=y_4jXnr7gjU4UXxVrgVrStTSFu3h1ZrmjEZDL4FtZa4,3086
8
8
  drf_iam/migrations/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- drf_iam-0.0.3.dist-info/METADATA,sha256=TX_Bn6q0VhHzdnDd-LgsjfbyhZe4T1cZ7zLkUWpeSyc,2763
10
- drf_iam-0.0.3.dist-info/WHEEL,sha256=CmyFI0kx5cdEMTLiONQRbGQwjIoR1aIYB7eCAQ4KPJ0,91
11
- drf_iam-0.0.3.dist-info/top_level.txt,sha256=daz6AaQ9e_cfCjLk2aRoLb_PCOoFofYUX4DU85VwHSM,8
12
- drf_iam-0.0.3.dist-info/RECORD,,
9
+ drf_iam/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
10
+ drf_iam/utils/load_viewset_permissions.py,sha256=xWV9-8qwuOYysofFcKHxhbvh4u-fOH-Wy37ysvx2XTw,2574
11
+ drf_iam-0.2.0.dist-info/METADATA,sha256=u1HeciXJ3Ndr1G3OQylQjH51ycaYniTFClHKC80xX1U,2763
12
+ drf_iam-0.2.0.dist-info/WHEEL,sha256=ck4Vq1_RXyvS4Jt6SI0Vz6fyVs4GWg7AINwpsaGEgPE,91
13
+ drf_iam-0.2.0.dist-info/top_level.txt,sha256=daz6AaQ9e_cfCjLk2aRoLb_PCOoFofYUX4DU85VwHSM,8
14
+ drf_iam-0.2.0.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (78.1.0)
2
+ Generator: setuptools (80.0.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5