blob: 3d953ebdc2d2c55f1d20258d7816762fbb4632f6 [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
Mitja Nikolausb4e3bec2018-08-30 17:16:21 +020043 return user.has_perms(
44 [
45 # Crashreports
46 "crashreports.add_crashreport",
47 "crashreports.change_crashreport",
48 "crashreports.del_crashreport",
49 # Heartbeats
50 "heartbeat.add_crashreport",
51 "heartbeat.change_crashreport",
52 "heartbeat.del_crashreport",
53 # Logfiles
54 "heartbeat.add_logfile",
55 "heartbeat.change_logfile",
56 "heartbeat.del_logfile",
57 ]
58 )
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020059
Dirk Vogt7160b5e2016-10-12 17:04:40 +020060
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020061class HasStatsAccess(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020062 """Authorization requires to be part of the Hiccup staff."""
63
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020064 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020065 """Check if user is part of the Hiccup staff."""
Borjan Tchakalofffa134bd2018-04-09 16:16:11 +020066 return user_is_hiccup_staff(request.user)
Dirk Vogt7160b5e2016-10-12 17:04:40 +020067
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020068
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020069class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020070 """Authorization requires to be part of Hiccup staff or device owner."""
71
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020072 def has_permission(self, request, view):
Mitja Nikolaus6a679132018-08-30 14:35:29 +020073 """Return true if user is part of Hiccp staff or device owner."""
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020074 if user_is_hiccup_staff(request.user):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020075 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020076
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020077 # special case:
78 # user is the owner of a device. in this case creations are allowed.
79 # we have to check if the device with the supplied uuid indeed
80 # belongs to the user
Mitja Nikolauscb50f2c2018-08-24 13:54:48 +020081 if request.method == "POST":
82 if "uuid" not in request.data:
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020083 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020084 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020085 return False