forked from ubccr/coldfront
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Require first & last name on project requests
* Adds a permissions.py file as an initial step to centeralize permissions across the app. * Adds first and last name check to the test_func on ProjectRequestView. * Creates a generic wrapper decorator to be used around test_func to allow gradual progressive refactor of test_func towards a more centeralized and modular permissions management solution. closes #605
- Loading branch information
1 parent
2c3b235
commit 743ad96
Showing
3 changed files
with
37 additions
and
0 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
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,32 @@ | ||
from functools import wraps | ||
from django.contrib import messages | ||
|
||
def permission_required(permission_check): | ||
""" | ||
Decorator to check if a user has a certain permission before allowing them to access a view. | ||
The decorater is used to wrap a test_func from UserPassesTestMixin, which allows gradular refactoring of | ||
the permission check logic on test_func without having to change the permission logic. | ||
:param permission_check: function that returns True if the user has the permission, False otherwise | ||
:return: | ||
""" | ||
def decorator(test_func): | ||
@wraps(test_func) | ||
def wrapper(view_instance, *args, **kwargs): | ||
if not permission_check(view_instance.request): | ||
return False | ||
return test_func(view_instance, *args, **kwargs) | ||
return wrapper | ||
return decorator | ||
|
||
|
||
def check_first_last_name(request): | ||
""" | ||
Check if the user has set their first and last name on their account before allowing them to make requests. | ||
:param request: | ||
:return: | ||
""" | ||
if request.user.first_name == '' or request.user.last_name == '': | ||
messages.error(request, 'You must set your first and last name on your account before you can make requests.') | ||
return False | ||
return True |