Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
E
edx-platform
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
edx
edx-platform
Commits
eefbf77e
Commit
eefbf77e
authored
Jul 23, 2015
by
Will Daly
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow course staff to bypass embargo rules.
parent
5fe01036
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
50 additions
and
1 deletions
+50
-1
common/djangoapps/embargo/api.py
+5
-0
common/djangoapps/embargo/tests/test_api.py
+45
-1
No files found.
common/djangoapps/embargo/api.py
View file @
eefbf77e
...
@@ -14,6 +14,7 @@ from rest_framework.response import Response
...
@@ -14,6 +14,7 @@ from rest_framework.response import Response
from
rest_framework
import
status
from
rest_framework
import
status
from
ipware.ip
import
get_ip
from
ipware.ip
import
get_ip
from
student.auth
import
has_course_author_access
from
embargo.models
import
CountryAccessRule
,
RestrictedCourse
from
embargo.models
import
CountryAccessRule
,
RestrictedCourse
...
@@ -70,6 +71,10 @@ def check_course_access(course_key, user=None, ip_address=None, url=None):
...
@@ -70,6 +71,10 @@ def check_course_access(course_key, user=None, ip_address=None, url=None):
if
not
course_is_restricted
:
if
not
course_is_restricted
:
return
True
return
True
# Always give global and course staff access, regardless of embargo settings.
if
user
is
not
None
and
has_course_author_access
(
user
,
course_key
):
return
True
if
ip_address
is
not
None
:
if
ip_address
is
not
None
:
# Retrieve the country code from the IP address
# Retrieve the country code from the IP address
# and check it against the allowed countries list for a course
# and check it against the allowed countries list for a course
...
...
common/djangoapps/embargo/tests/test_api.py
View file @
eefbf77e
...
@@ -18,6 +18,11 @@ from xmodule.modulestore.tests.factories import CourseFactory
...
@@ -18,6 +18,11 @@ from xmodule.modulestore.tests.factories import CourseFactory
from
xmodule.modulestore.tests.django_utils
import
(
from
xmodule.modulestore.tests.django_utils
import
(
ModuleStoreTestCase
,
mixed_store_config
ModuleStoreTestCase
,
mixed_store_config
)
)
from
student.roles
import
(
GlobalStaff
,
CourseRole
,
OrgRole
,
CourseStaffRole
,
CourseInstructorRole
,
OrgStaffRole
,
OrgInstructorRole
)
from
embargo.models
import
(
from
embargo.models
import
(
RestrictedCourse
,
Country
,
CountryAccessRule
,
RestrictedCourse
,
Country
,
CountryAccessRule
,
...
@@ -166,7 +171,7 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
...
@@ -166,7 +171,7 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
# (restricted course, but pass all the checks)
# (restricted course, but pass all the checks)
# This is the worst case, so it will hit all of the
# This is the worst case, so it will hit all of the
# caching code.
# caching code.
with
self
.
assertNumQueries
(
3
):
with
self
.
assertNumQueries
(
4
):
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
with
self
.
assertNumQueries
(
0
):
with
self
.
assertNumQueries
(
0
):
...
@@ -182,6 +187,45 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
...
@@ -182,6 +187,45 @@ class EmbargoCheckAccessApiTests(ModuleStoreTestCase):
with
self
.
assertNumQueries
(
0
):
with
self
.
assertNumQueries
(
0
):
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
@ddt.data
(
GlobalStaff
,
CourseStaffRole
,
CourseInstructorRole
,
OrgStaffRole
,
OrgInstructorRole
,
)
def
test_staff_access_country_block
(
self
,
staff_role_cls
):
# Add a country to the blacklist
CountryAccessRule
.
objects
.
create
(
rule_type
=
CountryAccessRule
.
BLACKLIST_RULE
,
restricted_course
=
self
.
restricted_course
,
country
=
Country
.
objects
.
get
(
country
=
'US'
)
)
# Appear to make a request from an IP in the blocked country
with
self
.
_mock_geoip
(
'US'
):
result
=
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
# Expect that the user is blocked, because the user isn't staff
self
.
assertFalse
(
result
,
msg
=
"User should not have access because the user isn't staff."
)
# Instantiate the role, configuring it for this course or org
if
issubclass
(
staff_role_cls
,
CourseRole
):
staff_role
=
staff_role_cls
(
self
.
course
.
id
)
elif
issubclass
(
staff_role_cls
,
OrgRole
):
staff_role
=
staff_role_cls
(
self
.
course
.
id
.
org
)
else
:
staff_role
=
staff_role_cls
()
# Add the user to the role
staff_role
.
add_users
(
self
.
user
)
# Now the user should have access
with
self
.
_mock_geoip
(
'US'
):
result
=
embargo_api
.
check_course_access
(
self
.
course
.
id
,
user
=
self
.
user
,
ip_address
=
'0.0.0.0'
)
self
.
assertTrue
(
result
,
msg
=
"User should have access because the user is staff."
)
@contextmanager
@contextmanager
def
_mock_geoip
(
self
,
country_code
):
def
_mock_geoip
(
self
,
country_code
):
with
mock
.
patch
.
object
(
pygeoip
.
GeoIP
,
'country_code_by_addr'
)
as
mock_ip
:
with
mock
.
patch
.
object
(
pygeoip
.
GeoIP
,
'country_code_by_addr'
)
as
mock_ip
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment