Self hosted FLOSS fitness/workout, nutrition and weight tracker
Use UIDs instead of plain user IDs
Roland Geider3417c6f42bc7
diff --git a/wger/manager/templates/mobile/workout/calendar.html b/wger/manager/templates/mobile/workout/calendar.htmlindex 08f2415f4..6bed61443 100644--- a/wger/manager/templates/mobile/workout/calendar.html+++ b/wger/manager/templates/mobile/workout/calendar.html@@ -204,7 +204,7 @@ you can click on them to see the details.{% endblocktrans %}</p></p><h3>{% trans 'Log' %}</h3>- <a href="{% url 'manager:workout:calendar' %}" class="btn btn-default btn-block">+ <a href="{% url 'manager:workout:calendar' uid %}" class="btn btn-default btn-block"><img src="{% static 'images/icons/calendar.svg' %}"width="32"height="32"@@ -212,12 +212,14 @@ you can click on them to see the details.{% endblocktrans %}</p>>{% trans "Go to current month" %}</a>+ {% if is_owner %}<a href="#calendar-day-select-popup" class="btn btn-default btn-block" data-toggle="modal"><img src="{% static 'images/icons/add.svg' %}"width="32"height="32"alt="{% trans 'Add new log' %}">{% trans "Add new log" %}</a>+ {% endif %}<div class="timeline-log" style="margin-top: 1em;"><div class="list-group">@@ -228,11 +230,11 @@ you can click on them to see the details.{% endblocktrans %}</p></a>{% endifchanged %}{% if current_month = date.month and current_year = date.year %}- <a href="{% url 'manager:workout:calendar' date.year date.month %}" class="list-group-item wger-list-group-item">+ <a href="{% url 'manager:workout:calendar' uid date.year date.month %}" class="list-group-item wger-list-group-item"><em>{{date|date:"F"}}</em></a>{% else %}- <a href="{% url 'manager:workout:calendar' date.year date.month %}" class="list-group-item wger-list-group-item">+ <a href="{% url 'manager:workout:calendar' uid date.year date.month %}" class="list-group-item wger-list-group-item"><span class="glyphicon glyphicon-chevron-right pull-right"></span>{{date|date:"F"}}</a>diff --git a/wger/manager/templates/workout/calendar.html b/wger/manager/templates/workout/calendar.htmlindex 7e6203c31..03e0346ec 100644--- a/wger/manager/templates/workout/calendar.html+++ b/wger/manager/templates/workout/calendar.html@@ -168,7 +168,7 @@ you can click on them to see the details.{% endblocktrans %}</p><h3>{% trans 'Log' %}</h3><p>- <a href="{% url 'manager:workout:calendar' owner_user.pk %}">+ <a href="{% url 'manager:workout:calendar' uid %}"><img src="{% static 'images/icons/calendar.svg' %}"width="32"height="32"@@ -195,7 +195,7 @@ you can click on them to see the details.{% endblocktrans %}</p>{% if current_month = date.month and current_year = date.year %}<span>{{date|date:"F"}}</span><br>{% else %}- <span><a href="{% url 'manager:workout:calendar' owner_user.pk date.year date.month %}">{{date|date:"F"}}</a></span><br>+ <span><a href="{% url 'manager:workout:calendar' uid date.year date.month %}">{{date|date:"F"}}</a></span><br>{% endif %}{% endfor %}</div>diff --git a/wger/manager/tests/test_weight_log.py b/wger/manager/tests/test_weight_log.pyindex 2a187e106..2fd4489dd 100644--- a/wger/manager/tests/test_weight_log.py+++ b/wger/manager/tests/test_weight_log.py@@ -28,6 +28,7 @@ from wger.manager.tests.testcase import WorkoutManagerTestCasefrom wger.manager.tests.testcase import WorkoutManagerAddTestCasefrom wger.manager.tests.testcase import WorkoutManagerDeleteTestCasefrom wger.utils.cache import get_template_cache_name, cache_mapper+from wger.utils.helpers import make_uidlogger = logging.getLogger('wger.custom')@@ -40,18 +41,21 @@ class CalendarAccessTestCase(WorkoutManagerTestCase):def test_access_shared(self):'''- Test accessing the URL of a shared calendar+ Test accessing the URL of a shared calendar page'''self.user_login('admin')- response = self.client.get(reverse('manager:workout:calendar', kwargs={'user_pk': 1}))+ response = self.client.get(reverse('manager:workout:calendar',+ kwargs={'uidb64': make_uid(1)}))self.assertEqual(response.status_code, 200)self.user_login('test')- response = self.client.get(reverse('manager:workout:calendar', kwargs={'user_pk': 1}))+ response = self.client.get(reverse('manager:workout:calendar',+ kwargs={'uidb64': make_uid(1)}))self.assertEqual(response.status_code, 200)self.user_logout()- response = self.client.get(reverse('manager:workout:calendar', kwargs={'user_pk': 1}))+ response = self.client.get(reverse('manager:workout:calendar',+ kwargs={'uidb64': make_uid(1)}))self.assertEqual(response.status_code, 200)diff --git a/wger/manager/urls.py b/wger/manager/urls.pyindex 57af4dc9c..084e3a26a 100644--- a/wger/manager/urls.py+++ b/wger/manager/urls.py@@ -65,13 +65,13 @@ patterns_workout = patterns('',url(r'^(?P<id>\d+)/view/$',workout.view,name='view'),- url(r'^calendar/u-(?P<user_pk>\d+)$',+ url(r'^calendar/(?P<uidb64>[0-9A-Za-z_\-]+)$',log.calendar,name='calendar'),url(r'^calendar$',log.calendar,name='calendar'),- url(r'^calendar/(?P<user_pk>\d+)-(?P<year>\d{4})-(?P<month>\d{1,2})$',+ url(r'^calendar/(?P<uidb64>[0-9A-Za-z_\-]+)/(?P<year>\d{4})-(?P<month>\d{1,2})$',log.calendar,name='calendar'),url(r'^calendar/(?P<year>\d{4})-(?P<month>\d{1,2})$',diff --git a/wger/manager/views/log.py b/wger/manager/views/log.pyindex cd6df8d09..b0426dab5 100644--- a/wger/manager/views/log.py+++ b/wger/manager/views/log.py@@ -48,7 +48,7 @@ from wger.utils.cache import cache_mapperfrom wger.utils.generic_views import WgerFormMixinfrom wger.utils.generic_views import WgerDeleteMixinfrom wger.utils.generic_views import WgerPermissionMixin-from wger.utils.helpers import check_access+from wger.utils.helpers import check_access, make_tokenfrom wger.weight.helpers import process_log_entries@@ -372,15 +372,16 @@ class WorkoutCalendar(HTMLCalendar):return '<td class="{0}" style="vertical-align: middle;">{1}</td>'.format(cssclass, body)-def calendar(request, user_pk=None, year=None, month=None):+def calendar(request, uidb64=None, year=None, month=None):'''Show a calendar with all the workout logs'''try:- is_owner, user = check_access(request.user, user_pk)+ is_owner, user = check_access(request.user, uidb64)except ValueError:return HttpResponseForbidden()+ uid, token = make_token(user)year = int(year) if year else datetime.date.today().yearmonth = int(month) if month else datetime.date.today().month@@ -416,5 +417,6 @@ def calendar(request, user_pk=None, year=None, month=None):context['owner_user'] = usercontext['is_owner'] = is_ownercontext['impressions'] = WorkoutSession.IMPRESSION+ context['uid'] = uidcontext['month_list'] = WorkoutLog.objects.filter(user=user).dates('date', 'month')return render(request, 'workout/calendar.html', context)diff --git a/wger/utils/helpers.py b/wger/utils/helpers.pyindex 14e41e0a5..685d7cf0a 100644--- a/wger/utils/helpers.py+++ b/wger/utils/helpers.py@@ -81,6 +81,14 @@ def next_weekday(date, weekday):return date + datetime.timedelta(days_ahead)+def make_uid(input):+ '''+ Small wrapper to generate a UID, usually used in URLs to allow for+ anonymous access+ '''+ return urlsafe_base64_encode(force_bytes(input))++def make_token(user):'''Convenience function that generates the UID and token for a user@@ -88,7 +96,7 @@ def make_token(user)::param user: a user object:return: the uid and the token'''- uid = urlsafe_base64_encode(force_bytes(user.pk))+ uid = make_uid(user.pk)token = default_token_generator.make_token(user)return uid, token@@ -128,7 +136,7 @@ def password_generator(length=15):return ''.join(random.choice(chars) for i in range(length))-def check_access(request_user, user_pk=None):+def check_access(request_user, uidb64=None):'''Small helper function to check that the current (possibly unauthenticated)user can access a URL that the owner user shared the link.@@ -137,9 +145,16 @@ def check_access(request_user, user_pk=None)::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:++ if uidb64:+ try:+ user_pk = int(urlsafe_base64_decode(uidb64))+ except ValueError as e:+ logger.debug("Error while calculating UID: {0}".format(e))+ return False+user = get_object_or_404(User, pk=user_pk)- if request_user.pk == int(user_pk):+ if request_user.pk == user_pk:user = request_userelif not user.userprofile.ro_access:return Falsediff --git a/wger/utils/tests/test_check_access.py b/wger/utils/tests/test_check_access.pyindex d0e4f7779..0c4abad81 100644--- a/wger/utils/tests/test_check_access.py+++ b/wger/utils/tests/test_check_access.py@@ -16,7 +16,7 @@ from django.contrib.auth.models import User, AnonymousUserfrom django.http import Http404from wger.manager.tests.testcase import WorkoutManagerTestCase-from wger.utils.helpers import check_access+from wger.utils.helpers import check_access, make_uidclass CheckAccessTestCase(WorkoutManagerTestCase):@@ -30,26 +30,32 @@ class CheckAccessTestCase(WorkoutManagerTestCase):'''user_share = User.objects.get(pk=1)+ uid_share = make_uid(1)self.assertTrue(user_share.userprofile.ro_access)user_no_share = User.objects.get(pk=2)+ uid_no_share = make_uid(2)self.assertFalse(user_no_share.userprofile.ro_access)anon = AnonymousUser()+ uid3 = make_uid(100)+# 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.assertEqual(check_access(anon, uid_share), (False, user_share))+ self.assertFalse(check_access(anon, uid_no_share))+ self.assertRaises(Http404, check_access, anon, uid3)+ self.assertFalse(check_access(anon, 'not a UID'))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, uid_share), (True, user_share))+ self.assertFalse(check_access(user_share, uid_no_share))self.assertEqual(check_access(user_share), (True, user_share))- self.assertRaises(Http404, check_access, user_share, 100)+ self.assertRaises(Http404, check_access, user_share, uid3)+ self.assertFalse(check_access(user_share, 'not a UID'))- 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, uid_share), (False, user_share))+ self.assertEqual(check_access(user_no_share, uid_no_share), (True, user_no_share))self.assertEqual(check_access(user_no_share), (True, user_no_share))- self.assertRaises(Http404, check_access, user_no_share, 100)+ self.assertRaises(Http404, check_access, user_no_share, uid3)