blob: e130cbf2677514d270dcd4a8cc948e10293d8a63 [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""Authorization permission classes for accessing the API."""
2
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02003from rest_framework.permissions import BasePermission
Mitja Nikolausbcaf5022018-08-30 16:40:38 +02004from crashreports.models import Device
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02005
6
Dirk Vogt7160b5e2016-10-12 17:04:40 +02007def user_owns_uuid(user, uuid):
Mitja Nikolaus6a679132018-08-30 14:35:29 +02008 """Determine whether a user is owning the device with the given UUID.
9
10 Args:
11 user: The user making the request.
12 uuid: The UUID of the device to be manipulated.
13
14 Returns: True if the user owns the device.
15
16 """
Dirk Vogt7160b5e2016-10-12 17:04:40 +020017 try:
18 device = Device.objects.get(user=user)
19 except:
20 return False
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020021 if uuid == device.uuid:
Dirk Vogt7160b5e2016-10-12 17:04:40 +020022 return True
23 return False
24
25
26def user_is_hiccup_staff(user):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020027 """Determine whether a user is part of the Hiccup staff.
28
29 Returns true if either the user is part of the group
30 "FairphoneSoftwareTeam", or he/she has all permissions for manipulating
31 crashreports, heartbeats and logfiles.
32
33 Args:
34 user: The user making the request.
35
36 Returns: True if user is part of the Hiccup staff.
37
38 """
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020039 if user.groups.filter(name="FairphoneSoftwareTeam").exists():
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020040 return True
41 else:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020042 return user.has_perms(
43 [
44 # Crashreports
45 "crashreports.add_crashreport",
46 "crashreports.change_crashreport",
47 "crashreports.del_crashreport",
48 # Heartbeats
49 "heartbeat.add_crashreport",
50 "heartbeat.change_crashreport",
51 "heartbeat.del_crashreport",
52 # Logfiles
53 "heartbeat.add_logfile",
54 "heartbeat.change_logfile",
55 "heartbeat.del_logfile",
56 ]
57 )
58
Dirk Vogt7160b5e2016-10-12 17:04:40 +020059
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020060class HasStatsAccess(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020061 """Authorization requires to be part of the Hiccup staff."""
62
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020063 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020064 """Check if user is part of the Hiccup staff."""
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020065 return user_is_hiccup_staff(request.user)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020066
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020067
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020068class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020069 """Authorization requires to be part of Hiccup staff or device owner."""
70
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020071 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020072 """Return true if user is part of Hiccp staff or device owner."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020073 if user_is_hiccup_staff(request.user):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020074 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020075
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020076 # special case:
77 # user is the owner of a device. in this case creations are allowed.
78 # we have to check if the device with the supplied uuid indeed
79 # belongs to the user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020080 if request.method == "POST":
81 if "uuid" not in request.data:
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020082 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020083 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020084 return False