XCOM-416: Embargo restrictions are now enforced during logistration
This commit is contained in:
@@ -10,6 +10,9 @@ import pygeoip
|
||||
|
||||
from django.core.cache import cache
|
||||
from django.conf import settings
|
||||
from rest_framework.response import Response
|
||||
from rest_framework import status
|
||||
from ipware.ip import get_ip
|
||||
|
||||
from embargo.models import CountryAccessRule, RestrictedCourse
|
||||
|
||||
@@ -166,3 +169,30 @@ def _country_code_from_ip(ip_addr):
|
||||
return pygeoip.GeoIP(settings.GEOIPV6_PATH).country_code_by_addr(ip_addr)
|
||||
else:
|
||||
return pygeoip.GeoIP(settings.GEOIP_PATH).country_code_by_addr(ip_addr)
|
||||
|
||||
|
||||
def get_embargo_response(request, course_id, user):
|
||||
"""
|
||||
Check whether any country access rules block the user from enrollment.
|
||||
|
||||
Args:
|
||||
request (HttpRequest): The request object
|
||||
course_id (str): The requested course ID
|
||||
user (str): The current user object
|
||||
|
||||
Returns:
|
||||
HttpResponse: Response of the embargo page if embargoed, None if not
|
||||
|
||||
"""
|
||||
redirect_url = redirect_if_blocked(
|
||||
course_id, user=user, ip_address=get_ip(request), url=request.path)
|
||||
if redirect_url:
|
||||
return Response(
|
||||
status=status.HTTP_403_FORBIDDEN,
|
||||
data={
|
||||
"message": (
|
||||
u"Users from this location cannot access the course '{course_id}'."
|
||||
).format(course_id=course_id),
|
||||
"user_message_url": request.build_absolute_uri(redirect_url)
|
||||
}
|
||||
)
|
||||
|
||||
@@ -26,6 +26,7 @@ from util.models import RateLimitConfiguration
|
||||
from util.testing import UrlResetMixin
|
||||
from enrollment import api
|
||||
from enrollment.errors import CourseEnrollmentError
|
||||
from openedx.core.lib.django_test_client_utils import get_absolute_url
|
||||
from openedx.core.djangoapps.user_api.models import UserOrgTag
|
||||
from student.tests.factories import UserFactory, CourseModeFactory
|
||||
from student.models import CourseEnrollment
|
||||
@@ -725,10 +726,6 @@ class EnrollmentEmbargoTest(EnrollmentTestMixin, UrlResetMixin, ModuleStoreTestC
|
||||
'user': self.user.username
|
||||
})
|
||||
|
||||
def _get_absolute_url(self, path):
|
||||
""" Generate an absolute URL for a resource on the test server. """
|
||||
return u'http://testserver/{}'.format(path.lstrip('/'))
|
||||
|
||||
def assert_access_denied(self, user_message_path):
|
||||
"""
|
||||
Verify that the view returns HTTP status 403 and includes a URL in the response, and no enrollment is created.
|
||||
@@ -741,7 +738,7 @@ class EnrollmentEmbargoTest(EnrollmentTestMixin, UrlResetMixin, ModuleStoreTestC
|
||||
|
||||
# Expect that the redirect URL is included in the response
|
||||
resp_data = json.loads(response.content)
|
||||
user_message_url = self._get_absolute_url(user_message_path)
|
||||
user_message_url = get_absolute_url(user_message_path)
|
||||
self.assertEqual(resp_data['user_message_url'], user_message_url)
|
||||
|
||||
# Verify that we were not enrolled
|
||||
|
||||
@@ -32,7 +32,6 @@ from enrollment.errors import (
|
||||
)
|
||||
from student.models import User
|
||||
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@@ -406,21 +405,10 @@ class EnrollmentListView(APIView, ApiKeyPermissionMixIn):
|
||||
}
|
||||
)
|
||||
|
||||
# Check whether any country access rules block the user from enrollment
|
||||
# We do this at the view level (rather than the Python API level)
|
||||
# because this check requires information about the HTTP request.
|
||||
redirect_url = embargo_api.redirect_if_blocked(
|
||||
course_id, user=user, ip_address=get_ip(request), url=request.path)
|
||||
if redirect_url:
|
||||
return Response(
|
||||
status=status.HTTP_403_FORBIDDEN,
|
||||
data={
|
||||
"message": (
|
||||
u"Users from this location cannot access the course '{course_id}'."
|
||||
).format(course_id=course_id),
|
||||
"user_message_url": request.build_absolute_uri(redirect_url)
|
||||
}
|
||||
)
|
||||
embargo_response = embargo_api.get_embargo_response(request, course_id, user)
|
||||
|
||||
if embargo_response:
|
||||
return embargo_response
|
||||
|
||||
try:
|
||||
is_active = request.DATA.get('is_active')
|
||||
|
||||
Reference in New Issue
Block a user