-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow users to have specific write access to certain endpoints
- Loading branch information
1 parent
fb1a469
commit 9f0c215
Showing
7 changed files
with
406 additions
and
37 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
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,6 @@ | ||
-- drop and create database for use in testing | ||
DROP DATABASE IF EXISTS `atlas_test`; | ||
CREATE DATABASE `atlas_test`; | ||
|
||
-- -- create user and grant rights | ||
-- GRANT ALL ON atlas_test.* TO 'atlas'@'%'; |
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
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 |
---|---|---|
@@ -1,12 +1,45 @@ | ||
import logging | ||
|
||
from rest_framework.permissions import BasePermission, SAFE_METHODS | ||
|
||
class IsApprovedUser(BasePermission): | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
class HasReadAccess(BasePermission): | ||
def has_permission(self, request, view): | ||
# Allow all safe methods (GET, OPTIONS, HEAD) | ||
if request.method in SAFE_METHODS: | ||
return True | ||
|
||
# Allow POST if the user is authenticated | ||
return (request.user | ||
and request.user.is_authenticated) | ||
|
||
|
||
class HasWriteAccess(BasePermission): | ||
def has_permission(self, request, view): | ||
# Allow all safe methods (GET, OPTIONS, HEAD) | ||
if request.method in SAFE_METHODS: | ||
return True | ||
|
||
# Only allow POST if the user is authenticated, active, and staff | ||
write_fl = False | ||
user = request.user | ||
# Retrieve the user's group and get the api write access flag from the | ||
# group profile | ||
if user.groups.exists(): | ||
try: | ||
group_profile = user.groups.first().profile | ||
write_fl = group_profile.api_write_access | ||
except AttributeError: | ||
# If the group has no profile, then there's something wrong with | ||
# the database. This should be fixed by an administrator, but | ||
# we don't need to block the user from accessing the API. | ||
msg = 'Could not authorise based on group: Group has no profile.' | ||
logger.error(msg) | ||
write_fl = False | ||
|
||
# Only allow POST to write endpoints if the user is authenticated and is | ||
# either in a writeable group or is a staff member | ||
return (request.user | ||
and request.user.is_authenticated | ||
and request.user.is_staff) | ||
and (write_fl or request.user.is_staff)) |
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
Oops, something went wrong.