-
Notifications
You must be signed in to change notification settings - Fork 220
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
restrict non-approved users from accessing dashboard
- Loading branch information
1 parent
8ffc8fb
commit 0666bc8
Showing
1 changed file
with
33 additions
and
12 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 |
---|---|---|
@@ -1,33 +1,54 @@ | ||
""" | ||
Handling permissions for users who are assigned | ||
Handling permissions for users who are assigned | ||
for basic level actions in the project. (view few data, modify some of their data etc). | ||
UserTypes: Student, Teacher | ||
""" | ||
|
||
from django.http import HttpResponse | ||
from django.contrib.auth.decorators import login_required | ||
|
||
from django_school_management.accounts.constants import AccountTypesEnum | ||
from django_school_management.accounts.constants import ( | ||
AccountTypesEnum, | ||
ProfileApprovalStatusEnum, | ||
) | ||
from django_school_management.accounts.models import User | ||
|
||
|
||
@login_required | ||
def permission_error(request): | ||
return HttpResponse('You don\'t have right permissio to access this page.') | ||
return HttpResponse("You don't have right permission to access this page.") | ||
|
||
|
||
def user_is_verified(user): | ||
return user.approval_status == 'a' if user.is_authenticated else False | ||
return ( | ||
user.approval_status == ProfileApprovalStatusEnum.approved.value | ||
if user.is_authenticated | ||
else False | ||
) | ||
|
||
|
||
def user_is_student(user): | ||
return user_is_verified(user) and user.requested_role == 'student' \ | ||
if user.is_authenticated else False | ||
return ( | ||
user_is_verified(user) | ||
and user.requested_role == AccountTypesEnum.student.value | ||
if user.is_authenticated | ||
else False | ||
) | ||
|
||
|
||
def user_is_teacher(user): | ||
return user_is_verified(user) and user.requested_role == 'teacher' \ | ||
if user.is_authenticated else False | ||
return ( | ||
user_is_verified(user) | ||
and user.requested_role == AccountTypesEnum.teacher.value | ||
if user.is_authenticated | ||
else False | ||
) | ||
|
||
|
||
def can_access_dashboard(user): | ||
restricted_roles = [ | ||
AccountTypesEnum.subscriber.value | ||
] | ||
def can_access_dashboard(user: User): | ||
restricted_roles = [AccountTypesEnum.subscriber.value] | ||
if user.requested_role in restricted_roles: | ||
return False | ||
if user.approval_status != ProfileApprovalStatusEnum.approved.value: | ||
return False | ||
return True |