blob: c1efe79181fb38dc232f9b2f793da9c17790b475 [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
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020010 if uuid == device.uuid:
Dirk Vogt7160b5e2016-10-12 17:04:40 +020011 return True
12 return False
13
14
15def user_is_hiccup_staff(user):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020016 if user.groups.filter(name="FairphoneSoftwareTeam").exists():
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020017 return True
18 else:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020019 return user.has_perms(
20 [
21 # Crashreports
22 "crashreports.add_crashreport",
23 "crashreports.change_crashreport",
24 "crashreports.del_crashreport",
25 # Heartbeats
26 "heartbeat.add_crashreport",
27 "heartbeat.change_crashreport",
28 "heartbeat.del_crashreport",
29 # Logfiles
30 "heartbeat.add_logfile",
31 "heartbeat.change_logfile",
32 "heartbeat.del_logfile",
33 ]
34 )
35
Dirk Vogt7160b5e2016-10-12 17:04:40 +020036
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020037class HasStatsAccess(BasePermission):
38 def has_permission(self, request, view):
39 return user_is_hiccup_staff(request.user)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020040
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020041
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020042class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020043 def has_permission(self, request, view):
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020044 if user_is_hiccup_staff(request.user):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020045 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020046
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020047 # special case:
48 # user is the owner of a device. in this case creations are allowed.
49 # we have to check if the device with the supplied uuid indeed
50 # belongs to the user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020051 if request.method == "POST":
52 if "uuid" not in request.data:
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020053 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020054 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020055 return False