Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add filtering for user owned objects

Roland Geider4f6b3e284d6f
diff --git a/wger/manager/api/resources.py b/wger/manager/api/resources.py
index 04e9576ba..7c018562f 100644
--- a/wger/manager/api/resources.py
+++ b/wger/manager/api/resources.py
@@ -21,6 +21,7 @@ from tastypie.resources import ModelResource
from django.contrib.auth.models import User
from wger.exercises.api.resources import ExerciseResource
+from wger.utils.resources import UserObjectsOnlyAuthorization
from wger.manager.models import UserProfile
from wger.manager.models import Workout
@@ -40,7 +41,6 @@ class UserProfileResource(ModelResource):
'''
Resource for user profiles
'''
- user = fields.ToOneField('wger.manager.api.resources.UserResource', 'user')
def authorized_read_list(self, object_list, bundle):
'''
@@ -51,23 +51,7 @@ class UserProfileResource(ModelResource):
class Meta:
queryset = UserProfile.objects.all()
authentication = ApiKeyAuthentication()
-
-
-class UserResource(ModelResource):
- '''
- Resource for users
- '''
-
- def authorized_read_list(self, object_list, bundle):
- '''
- Filter to own objects
- '''
- return object_list.filter(pk=bundle.request.user.pk)
-
- class Meta:
- queryset = User.objects.all()
- excludes = ['is_active', 'is_staff', 'is_superuser', 'password', ]
- authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
#
@@ -78,7 +62,7 @@ class WorkoutResource(ModelResource):
Resource for workouts
'''
- days = fields.ToManyField('wger.manager.api.resources.DaysOfWeekResource', 'day_set')
+ days = fields.ToManyField('wger.manager.api.resources.DayResource', 'day_set')
def authorized_read_list(self, object_list, bundle):
'''
@@ -89,6 +73,7 @@ class WorkoutResource(ModelResource):
class Meta:
queryset = Workout.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class ScheduleStepResource(ModelResource):
@@ -107,6 +92,7 @@ class ScheduleStepResource(ModelResource):
class Meta:
queryset = ScheduleStep.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class ScheduleResource(ModelResource):
@@ -125,6 +111,7 @@ class ScheduleResource(ModelResource):
class Meta:
queryset = Schedule.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class DaysOfWeekResource(ModelResource):
@@ -142,7 +129,7 @@ class DayResource(ModelResource):
'''
workout = fields.ToOneField(WorkoutResource, 'training')
- days = fields.ToManyField(DaysOfWeekResource, 'day')
+ days_of_week = fields.ToManyField(DaysOfWeekResource, 'day')
def authorized_read_list(self, object_list, bundle):
'''
@@ -153,6 +140,7 @@ class DayResource(ModelResource):
class Meta:
queryset = Day.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class SetResource(ModelResource):
@@ -172,6 +160,7 @@ class SetResource(ModelResource):
class Meta:
queryset = Set.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class SettingResource(ModelResource):
@@ -191,6 +180,7 @@ class SettingResource(ModelResource):
class Meta:
queryset = Setting.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
class WorkoutLogResource(ModelResource):
@@ -198,7 +188,6 @@ class WorkoutLogResource(ModelResource):
Resource for a workout log
'''
- user = fields.ToOneField(UserResource, 'user')
exercise = fields.ToOneField(ExerciseResource, 'exercise')
workout = fields.ToOneField(WorkoutResource, 'workout')
@@ -211,3 +200,4 @@ class WorkoutLogResource(ModelResource):
class Meta:
queryset = WorkoutLog.objects.all()
authentication = ApiKeyAuthentication()
+ authorization = UserObjectsOnlyAuthorization()
diff --git a/wger/urls.py b/wger/urls.py
index ccea2ea8c..d11a4d48e 100644
--- a/wger/urls.py
+++ b/wger/urls.py
@@ -30,7 +30,6 @@ v1_api.register(nutrition_api.WeightUnitResource())
# Manager app
v1_api.register(manager_api.UserProfileResource())
-v1_api.register(manager_api.UserResource())
v1_api.register(manager_api.WorkoutResource())
v1_api.register(manager_api.ScheduleResource())
v1_api.register(manager_api.ScheduleStepResource())
diff --git a/wger/utils/resources.py b/wger/utils/resources.py
new file mode 100644
index 000000000..fd0748650
--- /dev/null
+++ b/wger/utils/resources.py
@@ -0,0 +1,32 @@
+# This file is part of wger Workout Manager.
+#
+# wger Workout Manager is free software: you can redistribute it and/or modify
+# it under the terms of the GNU Affero General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# wger Workout Manager is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+
+import logging
+
+from tastypie.authorization import ReadOnlyAuthorization
+
+logger = logging.getLogger('wger.custom')
+
+
+class UserObjectsOnlyAuthorization(ReadOnlyAuthorization):
+ '''
+ Custom authorization class to limit the user's access to his own objects
+ '''
+
+ def read_detail(self, object_list, bundle):
+ try:
+ return bundle.obj.get_owner_object().user == bundle.request.user
+ # Objects without owner information can be accessed
+ except AttributeError:
+ return True
\ No newline at end of file