-
-
Notifications
You must be signed in to change notification settings - Fork 91
[feature] Implemented view permissions by extending DjangoModelPermissions class #249
#251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
9665dbc
3e9ad53
2fe5487
c446b0d
b78c4a1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,7 @@ | ||
| import copy | ||
|
|
||
| from django.utils.translation import gettext_lazy as _ | ||
| from rest_framework.permissions import BasePermission | ||
| from rest_framework.permissions import BasePermission, DjangoModelPermissions | ||
| from swapper import load_model | ||
|
|
||
| Organization = load_model('openwisp_users', 'Organization') | ||
|
|
@@ -66,3 +68,32 @@ class IsOrganizationOwner(BaseOrganizationPermission): | |
|
|
||
| def validate_membership(self, user, org): | ||
| return org and (user.is_superuser or user.is_owner(org)) | ||
|
|
||
|
|
||
| class CustomDjangoModelPermissions(DjangoModelPermissions): | ||
|
atb00ker marked this conversation as resolved.
Outdated
|
||
| def __init__(self): | ||
| self.perms_map = copy.deepcopy(self.perms_map) | ||
| self.perms_map['GET'] = ['%(app_label)s.view_%(model_name)s'] | ||
|
atb00ker marked this conversation as resolved.
Outdated
|
||
|
|
||
| def has_permission(self, request, view): | ||
| # Workaround to ensure DjangoModelPermissions are not applied | ||
| # to the root view when using DefaultRouter. | ||
| if getattr(view, '_ignore_model_permissions', False): | ||
| return True | ||
|
|
||
| if not request.user or ( | ||
| not request.user.is_authenticated and self.authenticated_users_only | ||
| ): | ||
| return False | ||
|
|
||
| queryset = self._queryset(view) | ||
| perms = self.get_required_permissions(request.method, queryset.model) | ||
| change_perm = self.get_required_permissions('PUT', queryset.model) | ||
|
|
||
| if request.method == 'GET': | ||
| if request.user.has_perms(perms) or request.user.has_perms(change_perm): | ||
| return True | ||
| else: | ||
| return False | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Did not handled the case of super user becasue superuser by default has all the permissions.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you can add a check at the top of the function stating: They are allowed to do anything regardless of any permission (this check is there in other has_permission functions too)! P.S: Not sure if this would be required, please test! 😄
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @atb00ker I am trying to implement this in more generic way, cause am thinking of opening a similar PR in DRF repo once this gets merged here. By default What do you think?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To the best of my knowledge, removing permissions from
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what about: # user must have either view permission or change permission
return user.has_perms(perms) or user.has_perms(change_perm) |
||
|
|
||
| return request.user.has_perms(perms) | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
here you'd import it as
BaseDjangoModelPermissions