drf-iam 0.0.3__tar.gz → 0.2.0__tar.gz

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.
@@ -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
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
@@ -13,4 +13,6 @@ drf_iam.egg-info/dependency_links.txt
13
13
  drf_iam.egg-info/requires.txt
14
14
  drf_iam.egg-info/top_level.txt
15
15
  drf_iam/migrations/0001_initial.py
16
- drf_iam/migrations/__init__.py
16
+ drf_iam/migrations/__init__.py
17
+ drf_iam/utils/__init__.py
18
+ drf_iam/utils/load_viewset_permissions.py
@@ -2,7 +2,7 @@ from setuptools import setup, find_packages
2
2
 
3
3
  setup(
4
4
  name='drf-iam',
5
- version='0.0.3',
5
+ version='0.2.0',
6
6
  packages=find_packages(), # Automatically finds all sub-packages
7
7
  include_package_data=True, # Include non-Python files from MANIFEST.in
8
8
  install_requires=[
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes
File without changes