Skip to content

armgit / wger

Public Imported from GitHub
0

Self hosted FLOSS fitness/workout, nutrition and weight tracker

Add helper function to check for foreign access

Roland Geiderbcb0ac1634a7
diff --git a/wger/manager/fixtures/test-user-data.json b/wger/manager/fixtures/test-user-data.json
index 5d62567e2..c6420ba25 100644
--- a/wger/manager/fixtures/test-user-data.json
+++ b/wger/manager/fixtures/test-user-data.json
@@ -501,7 +501,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": true
}
},
{
@@ -530,7 +531,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -557,7 +559,8 @@
"workout_duration": 15,
"notification_language": 2,
"gym": 2,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": true
}
},
{
@@ -586,7 +589,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -615,7 +619,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -644,7 +649,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -673,7 +679,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 2,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": false
}
},
{
@@ -702,7 +709,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 2,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": true
}
},
{
@@ -731,7 +739,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -760,7 +769,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -789,7 +799,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 2,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": false
}
},
{
@@ -818,7 +829,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 1,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -847,7 +859,8 @@
"timer_active": true,
"timer_pause": 90,
"gym": 2,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": true
}
},
{
@@ -876,7 +889,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -905,7 +919,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -934,7 +949,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -963,7 +979,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -992,7 +1009,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -1021,7 +1039,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": false
}
},
{
@@ -1050,7 +1069,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "lb"
+ "weight_unit": "lb",
+ "ro_access": false
}
},
{
@@ -1079,7 +1099,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -1108,7 +1129,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -1137,7 +1159,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
},
{
@@ -1166,7 +1189,8 @@
"calories": 3500,
"sport_hours": 6,
"is_temporary": false,
- "weight_unit": "kg"
+ "weight_unit": "kg",
+ "ro_access": false
}
}
]
diff --git a/wger/utils/helpers.py b/wger/utils/helpers.py
index 6862b946c..14e41e0a5 100644
--- a/wger/utils/helpers.py
+++ b/wger/utils/helpers.py
@@ -25,6 +25,7 @@ import datetime
from functools import wraps
from django.contrib.auth.models import User
from django.contrib.auth.tokens import default_token_generator
+from django.shortcuts import get_object_or_404
from django.utils.encoding import force_bytes
from django.utils.http import urlsafe_base64_decode, urlsafe_base64_encode
@@ -125,3 +126,29 @@ def password_generator(length=15):
chars = chars.replace(char, '')
return ''.join(random.choice(chars) for i in range(length))
+
+
+def check_access(request_user, user_pk=None):
+ '''
+ Small helper function to check that the current (possibly unauthenticated)
+ user can access a URL that the owner user shared the link.
+
+ :param request_user: the user in the current request
+ :param user_pk: the user_pk parameter given to the view (if any)
+ :return: returns False if unauthorized or a tuple otherwise (is_owner, user)
+ '''
+ if user_pk:
+ user = get_object_or_404(User, pk=user_pk)
+ if request_user.pk == int(user_pk):
+ user = request_user
+ elif not user.userprofile.ro_access:
+ return False
+
+ # If there is no user_pk, just show the user his own data
+ else:
+ if not request_user.is_authenticated():
+ return False
+ user = request_user
+
+ is_owner = request_user == user
+ return is_owner, user
diff --git a/wger/utils/tests/test_check_access.py b/wger/utils/tests/test_check_access.py
new file mode 100644
index 000000000..d0e4f7779
--- /dev/null
+++ b/wger/utils/tests/test_check_access.py
@@ -0,0 +1,55 @@
+# 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
+
+from django.contrib.auth.models import User, AnonymousUser
+from django.http import Http404
+
+from wger.manager.tests.testcase import WorkoutManagerTestCase
+from wger.utils.helpers import check_access
+
+
+class CheckAccessTestCase(WorkoutManagerTestCase):
+ '''
+ Test the "check_access" helper function
+ '''
+
+ def test_helper(self):
+ '''
+ Test the helper function
+ '''
+
+ user_share = User.objects.get(pk=1)
+ self.assertTrue(user_share.userprofile.ro_access)
+
+ user_no_share = User.objects.get(pk=2)
+ self.assertFalse(user_no_share.userprofile.ro_access)
+
+ anon = AnonymousUser()
+
+ # Logged out user
+ self.assertEqual(check_access(anon, 1), (False, user_share))
+ self.assertFalse(check_access(anon, 2))
+ self.assertRaises(Http404, check_access, anon, 100)
+ self.assertFalse(check_access(anon))
+
+ # Logged in user
+ self.assertEqual(check_access(user_share, 1), (True, user_share))
+ self.assertFalse(check_access(user_share, 2))
+ self.assertEqual(check_access(user_share), (True, user_share))
+ self.assertRaises(Http404, check_access, user_share, 100)
+
+ self.assertEqual(check_access(user_no_share, 1), (False, user_share))
+ self.assertEqual(check_access(user_no_share, 2), (True, user_no_share))
+ self.assertEqual(check_access(user_no_share), (True, user_no_share))
+ self.assertRaises(Http404, check_access, user_no_share, 100)