blob: 8c1fda709a9199545d853cfff4287f62d8ae6e58 [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):
16 return (user.has_perm('crashreports.add_crashreport')
17 and user.has_perm('crashreports.change_crashreport')
18 and user.has_perm('crashreports.del_crashreport')
19 and user.has_perm('heartbeat.add_crashreport')
20 and user.has_perm('heartbeat.change_crashreport')
21 and user.has_perm('heartbeat.del_crashreport')
22 and user.has_perm('heartbeat.add_logfile')
23 and user.has_perm('heartbeat.change_logfile')
24 and user.has_perm('heartbeat.del_logfile'))
25
26
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020027class HasRightsOrIsDeviceOwnerDeviceCreation(BasePermission):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020028 def has_permission(self, request, view):
29 # if user has all permissions for crashreport return true
Dirk Vogt7160b5e2016-10-12 17:04:40 +020030 if (user_is_hiccup_staff(request.user)):
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020031 return True
Dirk Vogt57a615d2017-05-04 22:29:54 +020032
33 if (request.user.groups.filter(name='FairphoneSoftwareTeam').exists()):
34 return True
35
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020036 # special case:
37 # user is the owner of a device. in this case creations are allowed.
38 # we have to check if the device with the supplied uuid indeed
39 # belongs to the user
40 if request.method == 'POST':
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020041 if ('uuid' not in request.data):
42 return False
Dirk Vogt7160b5e2016-10-12 17:04:40 +020043 return user_owns_uuid(request.user, request.data["uuid"])
Dirk Vogtc9e10ab2016-10-12 13:58:15 +020044 return False