Coverage for apps/users/permissions.py: 68%

16 statements  

« prev     ^ index     » next       coverage.py v6.4.4, created at 2023-09-08 12:47 -0600

1from rest_framework.permissions import BasePermission 

2 

3from apps.organizations.models import Organization 

4 

5from .models import Employee, Fan, SuperUser 

6 

7 

8class SuperUserPanel(BasePermission): 

9 """ 

10 Allows access only to authentcated super users 

11 """ 

12 

13 def has_permission(self, request, view): 

14 return ( 

15 bool(request.user and request.user.is_authenticated) 

16 and isinstance(request.user, SuperUser) 

17 and getattr(request.user, "is_active", False) 

18 ) 

19 

20 

21class EmployeePanel(BasePermission): 

22 """ 

23 Allows access only to authenticated users that are employees. 

24 """ 

25 

26 def has_permission(self, request, view): 

27 return ( 

28 bool(request.user and request.user.is_authenticated) 

29 and isinstance(request.user, Employee) 

30 and getattr(request.user, "is_active", False) 

31 ) 

32 

33 

34class FanPanel(BasePermission): 

35 """ 

36 Allows access only to authenticated users that belong to the organization from the request 

37 """ 

38 

39 def has_permission(self, request, view): 

40 try: 

41 Organization.objects.get(domain=request.headers.get("Origin", None)) 

42 except Organization.DoesNotExist: 

43 return False 

44 return ( 

45 bool(request.user and request.user.is_authenticated) 

46 and isinstance(request.user, Fan) 

47 and getattr(request.user, "is_active", False) 

48 )