Fixing django 2.1 probs in student management commands (#23587)
* Requiring at least one args in a mutually exclusive group to be set bulk_change_enrollment requires one of agrs: org or course to be set for it to function correctly. * Creating new function to verify initial password hash Starting with django 2.1, the function is_password_usable no longer checks whether encode is a valid password created by a django hasher(hasher in PASSWORD_HASHERS setting) Adding this function to create constant behavior as we upgrade django versions
This commit is contained in:
@@ -36,7 +36,7 @@ class Command(BaseCommand):
|
||||
"""
|
||||
|
||||
def add_arguments(self, parser):
|
||||
group = parser.add_mutually_exclusive_group()
|
||||
group = parser.add_mutually_exclusive_group(required=True)
|
||||
group.add_argument(
|
||||
'-c', '--course',
|
||||
help='The course to change enrollments in')
|
||||
@@ -64,7 +64,6 @@ class Command(BaseCommand):
|
||||
to_mode = options['to_mode']
|
||||
commit = options['commit']
|
||||
course_keys = []
|
||||
|
||||
if course_id:
|
||||
try:
|
||||
course_key = CourseKey.from_string(course_id)
|
||||
|
||||
@@ -5,7 +5,7 @@ Django users, set/unset permission bits, and associate groups by name.
|
||||
|
||||
|
||||
from django.contrib.auth import get_user_model
|
||||
from django.contrib.auth.hashers import is_password_usable
|
||||
from django.contrib.auth.hashers import is_password_usable, identify_hasher
|
||||
from django.contrib.auth.models import Group
|
||||
from django.core.management.base import BaseCommand, CommandError
|
||||
from django.db import transaction
|
||||
@@ -15,6 +15,20 @@ from openedx.core.djangoapps.user_authn.utils import generate_password
|
||||
from student.models import UserProfile
|
||||
|
||||
|
||||
def is_valid_django_hash(encoded):
|
||||
"""
|
||||
Starting with django 2.1, the function is_password_usable no longer checks whether encode
|
||||
is a valid password created by a django hasher (hasher in PASSWORD_HASHERS setting)
|
||||
|
||||
Adding this function to create constant behavior as we upgrade django versions
|
||||
"""
|
||||
try:
|
||||
identify_hasher(encoded)
|
||||
except ValueError:
|
||||
return False
|
||||
return True
|
||||
|
||||
|
||||
class Command(BaseCommand):
|
||||
# pylint: disable=missing-docstring
|
||||
|
||||
@@ -87,7 +101,7 @@ class Command(BaseCommand):
|
||||
|
||||
if created:
|
||||
if initial_password_hash:
|
||||
if not is_password_usable(initial_password_hash):
|
||||
if not (is_password_usable(initial_password_hash) and is_valid_django_hash(initial_password_hash)):
|
||||
raise CommandError('The password hash provided for user {} is invalid.'.format(username))
|
||||
user.password = initial_password_hash
|
||||
else:
|
||||
|
||||
Reference in New Issue
Block a user