blob: ff79149f111fb1bae18a4c229bfa9ef808ff6b73 [file] [log] [blame]
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02001from crashreports.models import Device
2from rest_framework.permissions import BasePermission
3
4
Dirk Vogt7160b5e2016-10-12 17:04:40 +02005def user_owns_uuid(user, uuid):
6 try:
7 device = Device.objects.get(user=user)
8 except:
9 return False
10 if (uuid == device.uuid):
11 return True
12 return False
13
14
15def user_is_hiccup_staff(user):
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020016 if (user.groups.filter(name='FairphoneSoftwareTeam').exists()):
17 return True
18 else:
19 return user.has_perms([
20 # Crashreports
21 'crashreports.add_crashreport', 'crashreports.change_crashreport',
22 'crashreports.del_crashreport',
23 # Heartbeats
24 'heartbeat.add_crashreport', 'heartbeat.change_crashreport',
25 'heartbeat.del_crashreport',
26 # Logfiles
27 'heartbeat.add_logfile', 'heartbeat.change_logfile',
28 'heartbeat.del_logfile',
29 ])
Dirk Vogt7160b5e2016-10-12 17:04:40 +020030
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020031class HasStatsAccess(BasePermission):
32 def has_permission(self, request, view):
33 return user_is_hiccup_staff(request.user)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020034
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020035class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020036 def has_permission(self, request, view):
Dirk Vogt7160b5e2016-10-12 17:04:40 +020037 if (user_is_hiccup_staff(request.user)):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020038 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020039
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020040 # special case:
41 # user is the owner of a device. in this case creations are allowed.
42 # we have to check if the device with the supplied uuid indeed
43 # belongs to the user
44 if request.method == 'POST':
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020045 if ('uuid' not in request.data):
46 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020047 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020048 return False