-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from avantifellows/feature/api-permissions
Api permissions scaffolding
- Loading branch information
Showing
6 changed files
with
100 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class OrganizationPermission(permissions.BasePermission): | ||
""" | ||
Permission check for organizations. | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
"""View-level permissions for organization. This determines whether the request can access organization instances or not.""" | ||
return request.user.is_superuser |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
from rest_framework import permissions | ||
from organizations.middleware import OrganizationTenantMiddleware | ||
from plio.settings import DEFAULT_TENANT_SHORTCODE | ||
from users.models import OrganizationUser | ||
|
||
|
||
class PlioPermission(permissions.BasePermission): | ||
""" | ||
Permission check for plios. | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
"""View-level permissions for plio. This determines whether the request can access plio instances or not.""" | ||
return True | ||
|
||
def has_object_permission(self, request, view, obj): | ||
"""Object-level permissions for plio. This determines whether the request can access a plio instance or not.""" | ||
|
||
if request.user.is_superuser: | ||
return True | ||
|
||
organization_shortcode = ( | ||
OrganizationTenantMiddleware.get_organization_shortcode(request) | ||
) | ||
if organization_shortcode == DEFAULT_TENANT_SHORTCODE: | ||
# this is the user's personal workspace in the public DB schema | ||
# only plios created by the user can be accessed | ||
return request.user == obj.created_by | ||
|
||
# checking if user is a member of the organization | ||
user_belongs_to_organization = OrganizationUser.objects.filter( | ||
organization__shortcode=organization_shortcode, | ||
user=request.user.id, | ||
).exists() | ||
|
||
if user_belongs_to_organization and obj.is_public: | ||
return True | ||
|
||
# user doesn't belong to organization or plio isn't public | ||
return request.user == obj.created_by |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
from rest_framework import permissions | ||
|
||
|
||
class UserPermission(permissions.BasePermission): | ||
""" | ||
Permission check for users. | ||
""" | ||
|
||
def has_permission(self, request, view): | ||
"""View-level permissions for user. This determines whether the request can access user instances or not.""" | ||
if view.action in ["list", "create"]: | ||
# only superuser can list or create users | ||
return request.user.is_superuser | ||
|
||
return True | ||
|
||
def has_object_permission(self, request, view, obj): | ||
"""Object-level permissions for user. This determines whether the request can access a user instance or not.""" | ||
return request.user.is_superuser or request.user == obj |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters