from rest_framework.permissions import BasePermission from labs.utils.network import is_from_company_vpn class NoPermission(BasePermission): def has_permission(self, request, view): return False class GroupPermission(BasePermission): """ This permission class is used to check if the user is in the group. The view should have a `perm_groups` attribute, which is a list of group names. Example: class MyView(APIView): permission_classes = [GroupPermission] perm_groups = ["group1", "group2"] """ def has_permission(self, request, view): perm_groups = getattr(view, "perm_groups", None) if perm_groups is None: return False return request.user.groups.filter(name__in=perm_groups).exists() class CompanyVPNPermission(BasePermission): message = "Please access this endpoint via company VPN or office network." def has_permission(self, request, view): return is_from_company_vpn(request)