Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

from rest_condition import Condition 

from rest_framework import viewsets 

from rest_framework.serializers import ModelSerializer 

 

from rest_delegated_permissions import RestPermissions, DelegatedPermission 

from rest_delegated_permissions.permissions import DjangoCombinedPermission 

from .permissions import OwnerPermission 

from .models import Container, ItemA, ItemB, ItemC, ItemD, ItemE 

 

# without parameters implicitly adds django model permissions (app_name.view_model, app_name.change_model) 

# and support for django-guardian 

perms = RestPermissions(add_django_permissions=True) 

 

# 

# can set the permission either here or on a perms.apply() call - see itemB, C, D. 

# In that case it takes the model class from the queryset field defined on the viewset 

# 

perms.update_permissions({ 

# User has permission to Container if he has guardian permissions or django model permissions 

Container: [], 

 

# User has permission to ItemA if he has guardian or django permissions to the instance or 

# if he has permission to Container pointed by "parent" field 

ItemA: DelegatedPermission(perms, 'parent'), 

}) 

 

 

class ContainerSerializer(ModelSerializer): 

class Meta: 

model = Container 

exclude = () 

 

 

@perms.apply() 

class ContainerViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = Container.objects.all() 

serializer_class = ContainerSerializer 

 

 

class ItemASerializer(ModelSerializer): 

class Meta: 

model = ItemA 

exclude = () 

 

 

@perms.apply() 

class ItemAViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemA.objects.all() 

serializer_class = ItemASerializer 

 

 

class ItemBSerializer(ModelSerializer): 

class Meta: 

model = ItemB 

exclude = () 

 

 

# User has permission to ItemB if he has guardian or django permissions to the instance or 

# if he has permission to any of Containers pointed by "parents" m2m field 

@perms.apply(permissions=DelegatedPermission(perms, 'parents')) 

class ItemBViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemB.objects.all() 

serializer_class = ItemBSerializer 

 

 

class ItemCSerializer(ModelSerializer): 

class Meta: 

model = ItemC 

exclude = () 

 

 

# User has permission to ItemC if he has guardian or django permissions to the instance or 

# if he has permission to any of Containers that point to it via its item_c ForeignKey. 

# Note: we are checking perms for ItemC and the Foreign Key is defined on Container, 

# so we need to put in the related_name 

@perms.apply(permissions=DelegatedPermission(perms, 'container')) 

class ItemCViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemC.objects.all() 

serializer_class = ItemCSerializer 

 

 

class ItemDSerializer(ModelSerializer): 

class Meta: 

model = ItemD 

exclude = () 

 

 

# User has permission to ItemD if he has guardian or django permissions to the instance or 

# if he has permission to any of Containers that point to it via its items_d m2m field 

# Note: we are checking perms for ItemD and the m2m field is defined on Container, 

# so we need to put in the related_name 

@perms.apply(permissions=DelegatedPermission(perms, 'containers')) 

class ItemDViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemD.objects.all() 

serializer_class = ItemDSerializer 

 

 

# different set of permissions 

perms1 = RestPermissions() 

 

 

# deny all viewset 

@perms1.apply(add_django_permissions=False, permissions=[]) 

class DenyAllViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemA.objects.all() 

serializer_class = ItemASerializer 

 

 

# different set of permissions 

perms2 = RestPermissions() 

 

 

class ItemESerializer(ModelSerializer): 

class Meta: 

model = ItemE 

exclude = () 

 

 

# allow only owner 

@perms2.apply(add_django_permissions=False, permissions=[OwnerPermission()]) 

class AllowOnlyOwnerViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemE.objects.all() 

serializer_class = ItemESerializer 

 

 

perms3 = RestPermissions(add_django_permissions=False, 

initial_permissions={ 

Container: Condition.Or(OwnerPermission(), DjangoCombinedPermission()) 

# this is the same as Container: [OwnerPermission(), DjangoCombinedPermission()] 

}) 

 

 

@perms3.apply(permissions=DelegatedPermission(perms3, 'parent')) 

class DelegatedOwnerViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemA.objects.all() 

serializer_class = ItemASerializer 

 

 

perms4 = RestPermissions(add_django_permissions=False, 

initial_permissions={ 

Container: Condition.And(OwnerPermission(), DjangoCombinedPermission()) 

# must be owner and have django permissions 

}) 

 

 

@perms4.apply(permissions=DelegatedPermission(perms4, 'parent')) 

class DelegatedOwnerWithAndViewSet(viewsets.ModelViewSet): 

""" 

This view set automatically provides `list` and `detail` actions. 

""" 

queryset = ItemA.objects.all() 

serializer_class = ItemASerializer