1
- from functools import partial
2
-
3
1
from django .db .models import Q
4
2
from django .http import Http404
5
3
from django .shortcuts import get_object_or_404
10
8
from rest_framework .mixins import (CreateModelMixin , DestroyModelMixin ,
11
9
ListModelMixin , RetrieveModelMixin )
12
10
from rest_framework .permissions import BasePermission
13
- from rest_framework .relations import HyperlinkedRelatedField , RelatedField
11
+ from rest_framework .relations import RelatedField
14
12
from rest_framework .serializers import ModelSerializer , SerializerMethodField
15
13
16
- from access import acl
17
14
from addons .models import Addon
18
15
from users .models import UserProfile
19
- from comm .models import ( CommunicationNote , CommunicationThread ,
20
- CommunicationThreadCC , CommunicationThreadToken )
16
+ from comm .models import CommunicationNote , CommunicationThread
17
+ from comm . utils import ThreadObjectPermission
21
18
from mkt .api .authentication import (RestOAuthAuthentication ,
22
19
RestSharedSecretAuthentication )
23
20
from mkt .api .base import CORSViewSet
@@ -70,24 +67,12 @@ def get_notes_count(self, obj):
70
67
return obj .notes .count ()
71
68
72
69
73
- class ThreadPermission (BasePermission ):
70
+ class ThreadPermission (BasePermission , ThreadObjectPermission ):
74
71
"""
75
72
Permission wrapper for checking if the authenticated user has the
76
73
permission to view the thread.
77
74
"""
78
75
79
- def check_acls (self , request , obj , acl_type ):
80
- if acl_type == 'moz_contact' :
81
- return request .user .email == obj .addon .mozilla_contact
82
- elif acl_type == 'admin' :
83
- return acl .action_allowed (request , 'Admin' , '%' )
84
- elif acl_type == 'reviewer' :
85
- return acl .action_allowed (request , 'Apps' , 'Review' )
86
- elif acl_type == 'senior_reviewer' :
87
- return acl .action_allowed (request , 'Apps' , 'ReviewEscalated' )
88
- else :
89
- raise 'Invalid ACL lookup.'
90
-
91
76
def has_permission (self , request , view ):
92
77
# Let `has_object_permission` handle the permissions when we retrieve
93
78
# an object.
@@ -101,47 +86,12 @@ def has_permission(self, request, view):
101
86
def has_object_permission (self , request , view , obj ):
102
87
"""
103
88
Make sure we give correct permissions to read/write the thread.
104
-
105
- Developers of the add-on used in the thread, users in the CC list,
106
- and users who post to the thread are allowed to access the object.
107
-
108
- Moreover, other object permissions are also checked agaisnt the ACLs
109
- of the user.
110
89
"""
111
90
if not request .user .is_authenticated () or obj .read_permission_public :
112
91
return obj .read_permission_public
113
92
114
93
profile = request .amo_user
115
- user_post = CommunicationNote .objects .filter (author = profile ,
116
- thread = obj )
117
- user_cc = CommunicationThreadCC .objects .filter (user = profile ,
118
- thread = obj )
119
-
120
- if user_post .exists () or user_cc .exists ():
121
- return True
122
-
123
- check_acls = partial (self .check_acls , request , obj )
124
-
125
- # User is a developer of the add-on and has the permission to read.
126
- user_is_author = profile .addons .filter (pk = obj .addon_id )
127
- if obj .read_permission_developer and user_is_author .exists ():
128
- return True
129
-
130
- if obj .read_permission_reviewer and check_acls ('reviewer' ):
131
- return True
132
-
133
- if (obj .read_permission_senior_reviewer and check_acls (
134
- 'senior_reviewer' )):
135
- return True
136
-
137
- if (obj .read_permission_mozilla_contact and check_acls (
138
- 'moz_contact' )):
139
- return True
140
-
141
- if obj .read_permission_staff and check_acls ('admin' ):
142
- return True
143
-
144
- return False
94
+ return self .user_has_permission (obj , profile )
145
95
146
96
147
97
class NotePermission (ThreadPermission ):
0 commit comments