forked from getsentry/sentry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpermissions.py
More file actions
246 lines (172 loc) · 5.57 KB
/
permissions.py
File metadata and controls
246 lines (172 loc) · 5.57 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
"""
sentry.permissions
~~~~~~~~~~~~~~~~~~
:copyright: (c) 2010-2014 by the Sentry Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""
from functools import wraps
from django.conf import settings
import six
from sentry.constants import MEMBER_OWNER
from sentry.plugins import plugins
from sentry.utils.cache import cached_for_request
class Permission(object):
def __init__(self, name, label):
self.name = name
self.label = label
def __unicode__(self):
return self.name
def __eq__(self, other):
return six.text_type(self) == six.text_type(other)
class Permissions(object):
ADD_PROJECT = Permission('add_project', 'create new projects')
ADD_TEAM = Permission('add_team', 'create new teams')
def requires_login(func):
@wraps(func)
def wrapped(user, *args, **kwargs):
if not (user and user.is_authenticated()):
return False
return func(user, *args, **kwargs)
return wrapped
@cached_for_request
@requires_login
def can_create_projects(user, team=None):
"""
Returns a boolean describing whether a user has the ability to
create new projects.
"""
if user.is_superuser:
return True
# must be an owner of team
if team and not team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'add_project', team)
if result is False:
return result
return True
@cached_for_request
@requires_login
def can_create_teams(user):
"""
Returns a boolean describing whether a user has the ability to
create new projects.
"""
if user.is_superuser:
return True
result = plugins.first('has_perm', user, 'add_team')
if result is False:
return result
return True
@requires_login
def can_set_public_projects(user):
"""
Returns a boolean describing whether a user has the ability to
change the ``public`` attribute of projects.
"""
if user.is_superuser:
return True
result = plugins.first('has_perm', user, 'set_project_public')
if result is None:
result = settings.SENTRY_ALLOW_PUBLIC_PROJECTS
if result is False:
return result
return True
@requires_login
def can_add_team_member(user, team):
# must be an owner of the team
if user.is_superuser:
return True
if not team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'add_team_member', team)
if result is False:
return False
return True
@requires_login
def can_manage_team_member(user, member, perm):
# permissions always take precedence
if user.is_superuser:
return True
# must be an owner of the team
if not member.team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, perm, member)
if result is False:
return False
return True
def can_edit_team_member(user, member):
return can_manage_team_member(user, member, 'edit_team_member')
def can_remove_team_member(user, member):
return can_manage_team_member(user, member, 'remove_team_member')
@requires_login
def can_remove_team(user, team):
if user.is_superuser:
return True
# must be an owner of the team
if team.owner != user:
return False
result = plugins.first('has_perm', user, 'remove_team', team)
if result is False:
return False
return True
@requires_login
def can_remove_project(user, project):
if project.is_default_project():
return False
if user.is_superuser:
return True
# must be an owner of the team
if not project.team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'remove_project', project)
if result is False:
return False
return True
@requires_login
def can_admin_group(user, group):
from sentry.models import Team
if user.is_superuser:
return True
# We make the assumption that we have a valid membership here
try:
Team.objects.get_for_user(user)[group.project.team.slug]
except KeyError:
return False
result = plugins.first('has_perm', user, 'admin_event', group)
if result is False:
return False
return True
@requires_login
def can_add_project_key(user, project):
if user.is_superuser:
return True
# must be an owner of the team
if project.team and not project.team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'add_project_key', project)
if result is False:
return False
return True
@requires_login
def can_edit_project_key(user, project):
if user.is_superuser:
return True
# must be an owner of the team
if project.team and not project.team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'edit_project_key', project)
if result is False:
return False
return True
@requires_login
def can_remove_project_key(user, key):
if user.is_superuser:
return True
project = key.project
# must be an owner of the team
if project.team and not project.team.member_set.filter(user=user, type=MEMBER_OWNER).exists():
return False
result = plugins.first('has_perm', user, 'remove_project_key', project, key)
if result is False:
return False
return True