Skip to content

Commit b7cd161

Browse files
committed
fix: bypass access checks when populating course blocks cache
1 parent e28a01e commit b7cd161

File tree

2 files changed

+20
-4
lines changed
  • lms/djangoapps/course_blocks
  • openedx/core/djangoapps/content/block_structure

2 files changed

+20
-4
lines changed

lms/djangoapps/course_blocks/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,4 +108,5 @@ def get_course_blocks(
108108
transformers,
109109
starting_block_usage_key,
110110
collected_block_structure,
111+
user,
111112
)

openedx/core/djangoapps/content/block_structure/manager.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ def __init__(self, root_block_usage_key, modulestore, cache):
3535
self.modulestore = modulestore
3636
self.store = BlockStructureStore(cache)
3737

38-
def get_transformed(self, transformers, starting_block_usage_key=None, collected_block_structure=None):
38+
def get_transformed(self, transformers, starting_block_usage_key=None, collected_block_structure=None, user=None):
3939
"""
4040
Returns the transformed Block Structure for the root_block_usage_key,
4141
starting at starting_block_usage_key, getting block data from the cache
@@ -57,11 +57,14 @@ def get_transformed(self, transformers, starting_block_usage_key=None, collected
5757
get_collected. Can be optionally provided if already available,
5858
for optimization.
5959
60+
user (django.contrib.auth.models.User) - User object for
61+
which the block structure is to be transformed.
62+
6063
Returns:
6164
BlockStructureBlockData - A transformed block structure,
6265
starting at starting_block_usage_key.
6366
"""
64-
block_structure = collected_block_structure.copy() if collected_block_structure else self.get_collected()
67+
block_structure = collected_block_structure.copy() if collected_block_structure else self.get_collected(user)
6568

6669
if starting_block_usage_key:
6770
# Override the root_block_usage_key so traversals start at the
@@ -77,7 +80,7 @@ def get_transformed(self, transformers, starting_block_usage_key=None, collected
7780
transformers.transform(block_structure)
7881
return block_structure
7982

80-
def get_collected(self):
83+
def get_collected(self, user=None):
8184
"""
8285
Returns the collected Block Structure for the root_block_usage_key,
8386
getting block data from the cache and modulestore, as needed.
@@ -86,6 +89,10 @@ def get_collected(self):
8689
the modulestore is accessed if needed (at cache miss), and the
8790
transformers data is collected if needed.
8891
92+
In the case of a cache miss, the function bypasses runtime access checks for the current
93+
user. This is done to prevent inconsistencies in the data, which can occur when
94+
certain blocks are inaccessible due to access restrictions.
95+
8996
Returns:
9097
BlockStructureBlockData - A collected block structure,
9198
starting at root_block_usage_key, with collected data
@@ -99,7 +106,15 @@ def get_collected(self):
99106
BlockStructureTransformers.verify_versions(block_structure)
100107

101108
except (BlockStructureNotFound, TransformerDataIncompatible):
102-
block_structure = self._update_collected()
109+
if user:
110+
# This bypasses the runtime access checks. When we are populating the course blocks cache,
111+
# we do not want to perform access checks. Access checks result in inconsistent blocks where
112+
# inaccessible blocks are missing from the cache. Cached course blocks are then used for all the users.
113+
user.known = False
114+
block_structure = self._update_collected()
115+
user.known = True
116+
else:
117+
block_structure = self._update_collected()
103118

104119
return block_structure
105120

0 commit comments

Comments
 (0)