Discussion service to enable permission and access provider (#37912)
* chore: discussion service to enable permission and access provider
This commit is contained in:
38
openedx/core/djangoapps/discussions/services.py
Normal file
38
openedx/core/djangoapps/discussions/services.py
Normal file
@@ -0,0 +1,38 @@
|
||||
"""
|
||||
Discussion Configuration Service for XBlock runtime.
|
||||
|
||||
This service provides discussion-related configuration and feature flags
|
||||
that are specific to the edx-platform implementation
|
||||
for the extracted discussion block in xblocks-contrib repository.
|
||||
"""
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib.auth.models import User # pylint: disable=imported-auth-user
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
from openedx.core.djangoapps.django_comment_common.models import has_permission
|
||||
from openedx.core.djangoapps.discussions.models import DiscussionsConfiguration, Provider
|
||||
|
||||
|
||||
class DiscussionConfigService:
|
||||
"""
|
||||
Service for providing discussion-related configuration and feature flags.
|
||||
"""
|
||||
|
||||
def has_permission(self, user: User, permission: str, course_id: CourseKey | None = None) -> bool:
|
||||
"""
|
||||
Return whether the user has the given discussion permission for a given course.
|
||||
"""
|
||||
return has_permission(user, permission, course_id)
|
||||
|
||||
def is_discussion_visible(self, course_key: CourseKey) -> bool:
|
||||
"""
|
||||
Discussion Xblock does not support new OPEN_EDX provider
|
||||
"""
|
||||
provider = DiscussionsConfiguration.get(course_key)
|
||||
return provider.provider_type == Provider.LEGACY
|
||||
|
||||
def is_discussion_enabled(self) -> bool:
|
||||
"""
|
||||
Return True if discussions are enabled; else False
|
||||
"""
|
||||
return settings.ENABLE_DISCUSSION_SERVICE
|
||||
@@ -14,6 +14,8 @@ from django.dispatch import receiver
|
||||
from django.utils.translation import gettext_noop
|
||||
from jsonfield.fields import JSONField
|
||||
from opaque_keys.edx.django.models import CourseKeyField
|
||||
from edx_django_utils.cache import DEFAULT_REQUEST_CACHE
|
||||
from opaque_keys.edx.keys import CourseKey
|
||||
|
||||
from openedx.core.djangoapps.xmodule_django.models import NoneToEmptyManager
|
||||
from openedx.core.lib.cache_utils import request_cached
|
||||
@@ -193,6 +195,37 @@ def all_permissions_for_user_in_course(user, course_id):
|
||||
return permission_names
|
||||
|
||||
|
||||
def has_permission(user, permission, course_id=None):
|
||||
"""
|
||||
This function resolves all discussion-related permissions for the given
|
||||
user and course, caches them for the duration of the request, and verifies
|
||||
whether the requested permission is present.
|
||||
|
||||
Args:
|
||||
user (User): Django user whose permissions are being checked.
|
||||
permission (str): Discussion permission identifier
|
||||
(e.g., "create_comment", "create_thread").
|
||||
course_id (CourseKey): Course context in which to evaluate
|
||||
the permission
|
||||
|
||||
Returns:
|
||||
bool: True if the user has the specified permission in the given
|
||||
course context; False otherwise.
|
||||
"""
|
||||
assert isinstance(course_id, (type(None), CourseKey))
|
||||
request_cache_dict = DEFAULT_REQUEST_CACHE.data
|
||||
cache_key = "django_comment_client.permissions.has_permission.all_permissions.{}.{}".format(
|
||||
user.id, course_id
|
||||
)
|
||||
if cache_key in request_cache_dict:
|
||||
all_permissions = request_cache_dict[cache_key]
|
||||
else:
|
||||
all_permissions = all_permissions_for_user_in_course(user, course_id)
|
||||
request_cache_dict[cache_key] = all_permissions
|
||||
|
||||
return permission in all_permissions
|
||||
|
||||
|
||||
class ForumsConfig(ConfigurationModel):
|
||||
"""
|
||||
Config for the connection to the cs_comments_service forums backend.
|
||||
|
||||
@@ -347,6 +347,9 @@ class XBlockRuntime(RuntimeShim, Runtime):
|
||||
# Import here to avoid circular dependency
|
||||
from openedx.core.djangoapps.video_config.services import VideoConfigService
|
||||
return VideoConfigService()
|
||||
elif service_name == 'discussion_config_service':
|
||||
from openedx.core.djangoapps.discussions.services import DiscussionConfigService
|
||||
return DiscussionConfigService()
|
||||
|
||||
# Otherwise, fall back to the base implementation which loads services
|
||||
# defined in the constructor:
|
||||
|
||||
Reference in New Issue
Block a user