blob: 0ff8ae8f1419c05e2910e982d97030092873e9fd [file] [log] [blame]
Mitja Nikolaus6a679132018-08-30 14:35:29 +02001"""Authorization permission classes for accessing the API."""
Mitja Nikolause1389bd2018-08-30 17:09:04 +02002import logging
Mitja Nikolaus6a679132018-08-30 14:35:29 +02003
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02004from rest_framework.permissions import BasePermission
Mitja Nikolausbcaf5022018-08-30 16:40:38 +02005from crashreports.models import Device
Dirk Vogtc9e10ab2016-10-12 13:58:15 +02006
7
Dirk Vogt7160b5e2016-10-12 17:04:40 +02008def user_owns_uuid(user, uuid):
Mitja Nikolaus6a679132018-08-30 14:35:29 +02009 """Determine whether a user is owning the device with the given UUID.
10
11 Args:
12 user: The user making the request.
13 uuid: The UUID of the device to be manipulated.
14
15 Returns: True if the user owns the device.
16
17 """
Dirk Vogt7160b5e2016-10-12 17:04:40 +020018 try:
19 device = Device.objects.get(user=user)
Mitja Nikolause1389bd2018-08-30 17:09:04 +020020 except Exception as exception: # pylint: disable=broad-except
21 logging.exception(exception)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020022 return False
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020023 if uuid == device.uuid:
Dirk Vogt7160b5e2016-10-12 17:04:40 +020024 return True
25 return False
26
27
28def user_is_hiccup_staff(user):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020029 """Determine whether a user is part of the Hiccup staff.
30
31 Returns true if either the user is part of the group
32 "FairphoneSoftwareTeam", or he/she has all permissions for manipulating
33 crashreports, heartbeats and logfiles.
34
35 Args:
36 user: The user making the request.
37
38 Returns: True if user is part of the Hiccup staff.
39
40 """
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020041 if user.groups.filter(name="FairphoneSoftwareTeam").exists():
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020042 return True
43 else:
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020044 return user.has_perms(
45 [
46 # Crashreports
47 "crashreports.add_crashreport",
48 "crashreports.change_crashreport",
49 "crashreports.del_crashreport",
50 # Heartbeats
51 "heartbeat.add_crashreport",
52 "heartbeat.change_crashreport",
53 "heartbeat.del_crashreport",
54 # Logfiles
55 "heartbeat.add_logfile",
56 "heartbeat.change_logfile",
57 "heartbeat.del_logfile",
58 ]
59 )
60
Dirk Vogt7160b5e2016-10-12 17:04:40 +020061
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020062class HasStatsAccess(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020063 """Authorization requires to be part of the Hiccup staff."""
64
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020065 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020066 """Check if user is part of the Hiccup staff."""
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020067 return user_is_hiccup_staff(request.user)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020068
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020069
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020070class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020071 """Authorization requires to be part of Hiccup staff or device owner."""
72
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020073 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020074 """Return true if user is part of Hiccp staff or device owner."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020075 if user_is_hiccup_staff(request.user):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020076 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020077
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020078 # special case:
79 # user is the owner of a device. in this case creations are allowed.
80 # we have to check if the device with the supplied uuid indeed
81 # belongs to the user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020082 if request.method == "POST":
83 if "uuid" not in request.data:
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020084 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020085 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020086 return False