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 requests to create or join a project. * Creates a generic wrapper decorator to be used around test_func to allow gradual progressive refactor of test_func towards a more centralized and modular permissions management solution. * Adds test for request creation Fixes #605
- Loading branch information
1 parent
1f76b06
commit ea63ec3
Showing
10 changed files
with
110 additions
and
20 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
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
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,38 @@ | ||
from functools import wraps | ||
from django.contrib import messages | ||
from django.urls import reverse | ||
from django.utils.html import format_html | ||
|
||
|
||
|
||
def permissions_required(*permissions): | ||
""" | ||
Decorator to check if a user has all specified permissions before allowing them to access a view. | ||
The decorator is used to wrap a test_func from UserPassesTestMixin, which allows granular refactoring of | ||
the permission check logic on test_func without having to change the permission logic. | ||
:param permissions: variable number of functions where each returns True if the user has the permission, False otherwise | ||
:return: | ||
""" | ||
def decorator(test_func): | ||
@wraps(test_func) | ||
def wrapper(view_instance, *args, **kwargs): | ||
# Check if all provided permissions return True | ||
if not all(permission(view_instance.request) for permission in permissions): | ||
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 == '': | ||
profile_url = request.build_absolute_uri(reverse('user-profile')) | ||
messages.error(request, format_html(f'You must set your first and last name on your account before you can make requests. Update your profile <a href="{profile_url}">here</a>.')) | ||
return False | ||
return True |
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 |
---|---|---|
|
@@ -56,3 +56,4 @@ tqdm==4.62.3 | |
urllib3==1.24.2 | ||
user-agents==2.2.0 | ||
wcwidth==0.1.7 | ||
pytest-django |