drf-iam 0.0.3__py3-none-any.whl → 0.1.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.
- drf_iam/utils/__init__.py +0 -0
- drf_iam/utils/load_viewset_permissions.py +64 -0
- {drf_iam-0.0.3.dist-info → drf_iam-0.1.0.dist-info}/METADATA +2 -2
- {drf_iam-0.0.3.dist-info → drf_iam-0.1.0.dist-info}/RECORD +6 -4
- {drf_iam-0.0.3.dist-info → drf_iam-0.1.0.dist-info}/WHEEL +1 -1
- {drf_iam-0.0.3.dist-info → drf_iam-0.1.0.dist-info}/top_level.txt +0 -0
File without changes
|
@@ -0,0 +1,64 @@
|
|
1
|
+
import inspect
|
2
|
+
from django.urls.resolvers import URLPattern, URLResolver
|
3
|
+
from rest_framework.viewsets import ViewSetMixin
|
4
|
+
from rest_framework.decorators import action
|
5
|
+
|
6
|
+
from django.urls import get_resolver
|
7
|
+
from ..models import Policy
|
8
|
+
|
9
|
+
|
10
|
+
def is_viewset(cls):
|
11
|
+
return isinstance(cls, type) and issubclass(cls, ViewSetMixin)
|
12
|
+
|
13
|
+
|
14
|
+
def get_viewset_actions(viewset_cls):
|
15
|
+
actions = set()
|
16
|
+
|
17
|
+
# Default ViewSet methods
|
18
|
+
default_actions = {'list', 'retrieve', 'create', 'update', 'partial_update', 'destroy'}
|
19
|
+
for action_name in default_actions:
|
20
|
+
if hasattr(viewset_cls, action_name):
|
21
|
+
actions.add(action_name)
|
22
|
+
|
23
|
+
# Custom @action methods
|
24
|
+
for name, method in inspect.getmembers(viewset_cls, predicate=inspect.isfunction):
|
25
|
+
if hasattr(method, 'mapping'):
|
26
|
+
for http_method in method.mapping:
|
27
|
+
actions.add(f"{name}")
|
28
|
+
|
29
|
+
return actions
|
30
|
+
|
31
|
+
|
32
|
+
def extract_viewsets_from_urlpatterns(urlpatterns, prefix=''):
|
33
|
+
for pattern in urlpatterns:
|
34
|
+
if isinstance(pattern, URLResolver):
|
35
|
+
# Recurse into included URLConfs
|
36
|
+
yield from extract_viewsets_from_urlpatterns(pattern.url_patterns, prefix + str(pattern.pattern))
|
37
|
+
elif isinstance(pattern, URLPattern):
|
38
|
+
callback = pattern.callback
|
39
|
+
cls = getattr(callback, 'cls', None)
|
40
|
+
if is_viewset(cls):
|
41
|
+
yield {
|
42
|
+
'prefix': prefix,
|
43
|
+
'pattern': pattern.pattern,
|
44
|
+
'viewset': cls,
|
45
|
+
'callback': callback
|
46
|
+
}
|
47
|
+
|
48
|
+
|
49
|
+
def load_permissions_from_urls(**kwargs):
|
50
|
+
urlpatterns = get_resolver().url_patterns
|
51
|
+
for entry in extract_viewsets_from_urlpatterns(urlpatterns):
|
52
|
+
viewset_cls = entry['viewset']
|
53
|
+
basename = getattr(viewset_cls,"iam_policy_name") or viewset_cls.__name__.lower().replace('viewset', '')
|
54
|
+
actions = get_viewset_actions(viewset_cls)
|
55
|
+
|
56
|
+
for action in actions:
|
57
|
+
full_action = f"{basename}:{action}"
|
58
|
+
Policy.objects.get_or_create(
|
59
|
+
action=full_action,
|
60
|
+
resource_type=basename,
|
61
|
+
defaults={
|
62
|
+
'description': f"Permission for {full_action} on {basename}"
|
63
|
+
}
|
64
|
+
)
|
@@ -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
|
10
|
-
drf_iam
|
11
|
-
drf_iam-0.0.
|
12
|
-
drf_iam-0.0.
|
9
|
+
drf_iam/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
10
|
+
drf_iam/utils/load_viewset_permissions.py,sha256=EVNrldOOkBMjEIoTBlHwSotBOtvW0kHjNONQz_blnxA,2257
|
11
|
+
drf_iam-0.1.0.dist-info/METADATA,sha256=SqDZ1fsPg5RyzvNVIjs_nCjpxUjT4Wi_0-aAX11Mmjc,2763
|
12
|
+
drf_iam-0.1.0.dist-info/WHEEL,sha256=In9FTNxeP60KnTkGw7wk6mJPYd_dQSjEZmXdBdMCI-8,91
|
13
|
+
drf_iam-0.1.0.dist-info/top_level.txt,sha256=daz6AaQ9e_cfCjLk2aRoLb_PCOoFofYUX4DU85VwHSM,8
|
14
|
+
drf_iam-0.1.0.dist-info/RECORD,,
|
File without changes
|