Self hosted FLOSS fitness/workout, nutrition and weight tracker
Require add_exercise permission to create exercises
Roland Geider0c125dffaa41
diff --git a/CHANGELOG.md b/CHANGELOG.mdindex bf0df247b..ff9145f13 100644--- a/CHANGELOG.md+++ b/CHANGELOG.md@@ -21,6 +21,10 @@## Breaking API changes*(only relevant if you have your own scripts or interact with the REST API)*+* Creating exercises via `POST /api/v2/exercise/` now requires the `add_exercise`+ permission. Regular users should use `/api/v2/exercise-submission/`, which+ creates the exercise together with at least one translation.+## Upgrade steps```bashdiff --git a/wger/exercises/api/permissions.py b/wger/exercises/api/permissions.pyindex 2523e195f..d8cefa01a 100644--- a/wger/exercises/api/permissions.py+++ b/wger/exercises/api/permissions.py@@ -52,3 +52,19 @@ class CanContributeExercises(BasePermission):# Only admins are allowed to delete entriesif request.method in self.DELETE_METHODS:return request.user.has_perm('exercises.delete_exercise')+++class CanCreateExercises(CanContributeExercises):+ """+ Like CanContributeExercises, but creating requires the add_exercise permission.++ Regular users submit new exercises (base data plus translations) through+ the submission endpoint, so this endpoint doesn't produce untranslated+ exercises.+ """++ def has_permission(self, request, view):+ if request.method == 'POST':+ return request.user.is_authenticated and request.user.has_perm('exercises.add_exercise')++ return super().has_permission(request, view)diff --git a/wger/exercises/api/views.py b/wger/exercises/api/views.pyindex 6cc7ae8e6..73092bd72 100644--- a/wger/exercises/api/views.py+++ b/wger/exercises/api/views.py@@ -41,7 +41,10 @@ from wger.exercises.api.filtersets import (ExerciseFilterSet,RelevanceOrderingFilter,)-from wger.exercises.api.permissions import CanContributeExercises+from wger.exercises.api.permissions import (+ CanContributeExercises,+ CanCreateExercises,+)from wger.exercises.api.serializers import (DeletionLogSerializer,EquipmentSerializer,@@ -79,12 +82,15 @@ class ExerciseViewSet(ModelViewSet):"""API endpoint for exercise objects.+ Creating an exercise here requires the add_exercise permission, regular+ users submit new exercises via /api/v2/exercise-submission/.+For a read-only endpoint with all the information of an exercise, see /api/v2/exerciseinfo/"""queryset = Exercise.with_translations.all()serializer_class = ExerciseSerializer- permission_classes = (CanContributeExercises,)+ permission_classes = (CanCreateExercises,)throttle_classes = (CreateScopedRateThrottle,)throttle_scope = 'exercise_create'ordering_fields = '__all__'diff --git a/wger/exercises/tests/test_exercise.py b/wger/exercises/tests/test_exercise.pyindex b8b80e767..e46ddc413 100644--- a/wger/exercises/tests/test_exercise.py+++ b/wger/exercises/tests/test_exercise.py@@ -113,6 +113,20 @@ class ExerciseCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestCase):def get_resource_name(self):return 'exercise'+ def test_post_user_verified_email(self):+ """Trustworthy users without the add_exercise permission can't create here"""+ self.authenticate('trainer1')+ count_before = Exercise.objects.count()+ response = self.client.post(self.url, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_403_FORBIDDEN)+ self.assertEqual(Exercise.objects.count(), count_before)++ def test_post_user_with_permissions(self):+ """Users with the add_exercise permission can create exercises directly"""+ self.authenticate('admin')+ response = self.client.post(self.url, data=self.data)+ self.assertEqual(response.status_code, status.HTTP_201_CREATED)+def test_delete_replace_by(self):"""Test that setting the replaced_by attribute works"""@@ -163,7 +177,7 @@ class ExerciseCustomApiTestCase(ActstreamApiMixin, ExerciseCrudApiTestCase):Test that it is not possible to set the license for a newly createdexercise base (the license is always set to the default)"""- self.authenticate('trainer1')+ self.authenticate('admin')response = self.client.post(self.url, data={**self.data, 'license': 3})self.assertEqual(response.status_code, status.HTTP_201_CREATED)