drf-iam 0.0.2__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.1.0.dist-info/METADATA +100 -0
- {drf_iam-0.0.2.dist-info → drf_iam-0.1.0.dist-info}/RECORD +6 -4
- {drf_iam-0.0.2.dist-info → drf_iam-0.1.0.dist-info}/WHEEL +1 -1
- drf_iam-0.0.2.dist-info/METADATA +0 -38
- {drf_iam-0.0.2.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
|
+
)
|
@@ -0,0 +1,100 @@
|
|
1
|
+
Metadata-Version: 2.2
|
2
|
+
Name: drf-iam
|
3
|
+
Version: 0.1.0
|
4
|
+
Summary: IAM-style roles and permissions for Django Rest Framework
|
5
|
+
Home-page: https://github.com/tushar1328/drf-iam.git
|
6
|
+
Author: Tushar Patel
|
7
|
+
Author-email: tushar.patel@gmail.com
|
8
|
+
Project-URL: Documentation, https://drf-iam.readthedocs.io/en/latest/installation.html
|
9
|
+
Project-URL: Source, https://github.com/tushar1328/drf-iam.git
|
10
|
+
Classifier: Framework :: Django
|
11
|
+
Classifier: Framework :: Django :: 3.2
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
13
|
+
Classifier: License :: OSI Approved :: MIT License
|
14
|
+
Classifier: Operating System :: OS Independent
|
15
|
+
Requires-Python: >=3.6
|
16
|
+
Description-Content-Type: text/markdown
|
17
|
+
Requires-Dist: Django>=3.2
|
18
|
+
Requires-Dist: djangorestframework>=3.12
|
19
|
+
Dynamic: author
|
20
|
+
Dynamic: author-email
|
21
|
+
Dynamic: classifier
|
22
|
+
Dynamic: description
|
23
|
+
Dynamic: description-content-type
|
24
|
+
Dynamic: home-page
|
25
|
+
Dynamic: project-url
|
26
|
+
Dynamic: requires-dist
|
27
|
+
Dynamic: requires-python
|
28
|
+
Dynamic: summary
|
29
|
+
|
30
|
+
# DRF-IAM (Django REST Framework - Identity and Access Management)
|
31
|
+
|
32
|
+
[](https://badge.fury.io/py/drf-iam)
|
33
|
+
[](https://drf-iam.readthedocs.io/en/latest/?badge=latest)
|
34
|
+
|
35
|
+
A powerful Django application that provides Role-Based Access Control (RBAC) for Django REST Framework applications. Easily manage permissions and roles across your API endpoints.
|
36
|
+
|
37
|
+
## Features
|
38
|
+
|
39
|
+
- 🔒 Role-Based Access Control (RBAC)
|
40
|
+
- 🎯 Custom policy names for viewsets
|
41
|
+
- 🔄 Seamless integration with Django's User model
|
42
|
+
- ⚡ Easy to set up and configure
|
43
|
+
- 📚 Comprehensive documentation
|
44
|
+
|
45
|
+
## Installation
|
46
|
+
|
47
|
+
```bash
|
48
|
+
pip install drf-iam
|
49
|
+
```
|
50
|
+
|
51
|
+
## Quick Start
|
52
|
+
|
53
|
+
1. Add `drf_iam` to your `INSTALLED_APPS` in `settings.py`:
|
54
|
+
|
55
|
+
```python
|
56
|
+
INSTALLED_APPS = [
|
57
|
+
...
|
58
|
+
'drf_iam',
|
59
|
+
]
|
60
|
+
```
|
61
|
+
|
62
|
+
2. Add the Role relationship to your User model:
|
63
|
+
|
64
|
+
```python
|
65
|
+
from django.contrib.auth.models import AbstractUser
|
66
|
+
from drf_iam.models import Role
|
67
|
+
|
68
|
+
class User(AbstractUser):
|
69
|
+
role = models.ForeignKey(Role, on_delete=models.SET_NULL, null=True)
|
70
|
+
```
|
71
|
+
|
72
|
+
3. Run migrations:
|
73
|
+
|
74
|
+
```bash
|
75
|
+
python manage.py makemigrations
|
76
|
+
python manage.py migrate
|
77
|
+
```
|
78
|
+
|
79
|
+
4. Use in your viewsets:
|
80
|
+
|
81
|
+
```python
|
82
|
+
from rest_framework import viewsets
|
83
|
+
from drf_iam.permissions import IAMPermission
|
84
|
+
|
85
|
+
class UserViewSet(viewsets.ModelViewSet):
|
86
|
+
permission_classes = [IAMPermission]
|
87
|
+
iam_policy_name = "users" # Optional: Custom policy name for this viewset
|
88
|
+
```
|
89
|
+
|
90
|
+
## Documentation
|
91
|
+
|
92
|
+
For detailed documentation, visit [https://drf-iam.readthedocs.io/](https://drf-iam.readthedocs.io/)
|
93
|
+
|
94
|
+
## Contributing
|
95
|
+
|
96
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
97
|
+
|
98
|
+
## License
|
99
|
+
|
100
|
+
This project is licensed under the MIT License - see the LICENSE file for details.
|
@@ -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,,
|
drf_iam-0.0.2.dist-info/METADATA
DELETED
@@ -1,38 +0,0 @@
|
|
1
|
-
Metadata-Version: 2.4
|
2
|
-
Name: drf-iam
|
3
|
-
Version: 0.0.2
|
4
|
-
Summary: IAM-style roles and permissions for Django Rest Framework
|
5
|
-
Home-page: https://github.com/tushar1328/drf-iam.git
|
6
|
-
Author: Tushar Patel
|
7
|
-
Author-email: tushar.patel@gmail.com
|
8
|
-
Project-URL: Documentation, https://drf-iam.readthedocs.io/en/latest/installation.html
|
9
|
-
Project-URL: Source, https://github.com/tushar1328/drf-iam.git
|
10
|
-
Classifier: Framework :: Django
|
11
|
-
Classifier: Framework :: Django :: 3.2
|
12
|
-
Classifier: Programming Language :: Python :: 3
|
13
|
-
Classifier: License :: OSI Approved :: MIT License
|
14
|
-
Classifier: Operating System :: OS Independent
|
15
|
-
Requires-Python: >=3.6
|
16
|
-
Description-Content-Type: text/markdown
|
17
|
-
Requires-Dist: Django>=3.2
|
18
|
-
Requires-Dist: djangorestframework>=3.12
|
19
|
-
Dynamic: author
|
20
|
-
Dynamic: author-email
|
21
|
-
Dynamic: classifier
|
22
|
-
Dynamic: description
|
23
|
-
Dynamic: description-content-type
|
24
|
-
Dynamic: home-page
|
25
|
-
Dynamic: project-url
|
26
|
-
Dynamic: requires-dist
|
27
|
-
Dynamic: requires-python
|
28
|
-
Dynamic: summary
|
29
|
-
|
30
|
-
## Rest Framework IAM
|
31
|
-
|
32
|
-
A simple Django application for Role Based Access Control (RBAC) using Django Rest Framework
|
33
|
-
|
34
|
-
## Installation
|
35
|
-
|
36
|
-
```bash
|
37
|
-
pip install drf-iam
|
38
|
-
```
|
File without changes
|