realize CourseAccessRoles for a user when an LMS user is linked to their external_user_key

This commit is contained in:
Michael Roytman
2020-04-14 18:45:42 -04:00
parent 855c8c4a71
commit 316e76f7e9
4 changed files with 185 additions and 4 deletions

View File

@@ -1,7 +1,14 @@
# pylint: disable=unused-import
"""
Python APIs exposed by the student app to other in-process apps.
"""
from django.contrib.auth import get_user_model
from django.conf import settings
from student.models_api import create_manual_enrollment_audit as _create_manual_enrollment_audit
from student.models_api import get_course_access_role
from student.models_api import get_course_enrollment as _get_course_enrollment
from student.models_api import (
ENROLLED_TO_ENROLLED as _ENROLLED_TO_ENROLLED,
@@ -13,6 +20,7 @@ from student.models_api import (
ALLOWEDTOENROLL_TO_UNENROLLED as _ALLOWEDTOENROLL_TO_UNENROLLED,
DEFAULT_TRANSITION_STATE as _DEFAULT_TRANSITION_STATE,
)
from student.roles import REGISTERED_ACCESS_ROLES as _REGISTERED_ACCESS_ROLES
from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers
@@ -90,3 +98,16 @@ def create_manual_enrollment_audit(
enrollment,
role
)
def get_access_role_by_role_name(role_name):
"""
Get the concrete child class of the AccessRole abstract class associated with the string role_name
by looking in REGISTERED_ACCESS_ROLES. If there is no class associated with this name, return None.
Note that this will only return classes that are registered in _REGISTERED_ACCESS_ROLES.
Arguments:
role_name: the name of the role
"""
return _REGISTERED_ACCESS_ROLES.get(role_name, None)

View File

@@ -3,6 +3,7 @@ Provides Python APIs exposed from Student models.
"""
import logging
from student.models import CourseAccessRole as _CourseAccessRole
from student.models import CourseEnrollment as _CourseEnrollment
from student.models import ManualEnrollmentAudit as _ManualEnrollmentAudit
from student.models import (
@@ -62,3 +63,33 @@ def get_phone_number(user_id):
log.exception(exception)
return None
return student.phone_number or None
def get_course_access_role(user, org, course_id, role):
"""
Get a specific CourseAccessRole object. Return None if
it does not exist.
Arguments:
user: User object for the user who has access in a course
org: the org the course is in
course_id: the course_id of the CourseAccessRole
role: the role type of the role
"""
try:
course_access_role = _CourseAccessRole.objects.get(
user=user,
org=org,
course_id=course_id,
role=role,
)
except _CourseAccessRole.DoesNotExist:
log.exception('No CourseAccessRole found for user_id=%(user_id)s, org=%(org)s, '
'course_id=%(course_id)s, and role=%(role)s.', {
'user': user.id,
'org': org,
'course_id': course_id,
'role': role,
})
return None
return course_access_role