Commit 985417fc by Don Mitchell Committed by Adam Palay

Auth converting to new course_id syntax

parent 0d593010
...@@ -8,6 +8,9 @@ from abc import ABCMeta, abstractmethod ...@@ -8,6 +8,9 @@ from abc import ABCMeta, abstractmethod
from django.contrib.auth.models import User, Group from django.contrib.auth.models import User, Group
from xmodule.modulestore import Location from xmodule.modulestore import Location
from xmodule.modulestore.exceptions import InvalidLocationError, ItemNotFoundError
from xmodule.modulestore.django import loc_mapper
from xmodule.modulestore.locator import CourseLocator
class CourseContextRequired(Exception): class CourseContextRequired(Exception):
...@@ -134,20 +137,40 @@ class CourseRole(GroupBasedRole): ...@@ -134,20 +137,40 @@ class CourseRole(GroupBasedRole):
A named role in a particular course A named role in a particular course
""" """
def __init__(self, role, location, course_context=None): def __init__(self, role, location, course_context=None):
# pylint: disable=no-member # TODO: figure out how to make the group name generation lazy so it doesn't force the
loc = Location(location) # loc mapping?
legacy_group_name = '{0}_{1}'.format(role, loc.course) if not hasattr(location, 'course_id'):
location = Location(location)
if loc.category.lower() == 'course': # direct copy from auth.authz.get_all_course_role_groupnames will refactor to one impl asap
course_id = loc.course_id groupnames = []
else: try:
groupnames.append('{0}_{1}'.format(role, location.course_id))
except InvalidLocationError: # will occur on old locations where location is not of category course
if course_context is None: if course_context is None:
raise CourseContextRequired() raise CourseContextRequired()
course_id = course_context else:
groupnames.append('{0}_{1}'.format(role, course_context))
group_name = '{0}_{1}'.format(role, course_id) # pylint: disable=no-member
if isinstance(location, Location):
super(CourseRole, self).__init__([group_name, legacy_group_name]) # least preferred legacy role_course format
groupnames.append('{0}_{1}'.format(role, location.course))
try:
locator = loc_mapper().translate_location(location.course_id, location, False, False)
groupnames.append('{0}_{1}'.format(role, locator.course_id))
except (InvalidLocationError, ItemNotFoundError):
# if it's never been mapped, the auth won't be via the Locator syntax
pass
elif isinstance(location, CourseLocator):
# handle old Location syntax
old_location = loc_mapper().translate_locator_to_location(location, get_course=True)
if old_location:
# the slashified version of the course_id (myu/mycourse/myrun)
groupnames.append('{0}_{1}'.format(role, old_location.course_id))
# add the least desirable but sometimes occurring format.
groupnames.append('{0}_{1}'.format(role, old_location.course))
super(CourseRole, self).__init__(groupnames)
class OrgRole(GroupBasedRole): class OrgRole(GroupBasedRole):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment