Skip to content

Commit

Permalink
Require first & last name on project requests
Browse files Browse the repository at this point in the history
* 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
helbashandy committed Aug 19, 2024
1 parent 2c3b235 commit 743ad96
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
2 changes: 2 additions & 0 deletions coldfront/core/project/views_/join_views/request_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
from coldfront.core.project.views import ProjectListView
from coldfront.core.user.utils_.host_user_utils import is_lbl_employee
from coldfront.core.user.utils_.host_user_utils import needs_host
from coldfront.core.utils.permissions import permission_required, check_first_last_name


logger = logging.getLogger(__name__)
Expand All @@ -38,6 +39,7 @@ class ProjectJoinView(LoginRequiredMixin, UserPassesTestMixin, TemplateView):

logger = logging.getLogger(__name__)

@permission_required(check_first_last_name)
def test_func(self):
project_obj = get_object_or_404(Project, pk=self.kwargs.get('pk'))
user_obj = self.request.user
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,8 @@
from coldfront.core.user.utils import access_agreement_signed
from coldfront.core.utils.common import session_wizard_all_form_data
from coldfront.core.utils.common import utc_now_offset_aware
from coldfront.core.utils.permissions import permission_required, check_first_last_name


from django.conf import settings
from django.contrib import messages
Expand All @@ -58,6 +60,7 @@ class ProjectRequestView(LoginRequiredMixin, UserPassesTestMixin,
TemplateView):
template_name = 'project/project_request/project_request.html'

@permission_required(check_first_last_name)
def test_func(self):
if self.request.user.is_superuser:
return True
Expand Down
32 changes: 32 additions & 0 deletions coldfront/core/utils/permissions.py
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

0 comments on commit 743ad96

Please sign in to comment.