def authentication_hook(self, request, user_id=None, username=None, email=None, extra_params=None):
extra = extra_params if extra_params else {}
# automatically generate password from user_id
password = self._generate_password(user_id, settings.PASSWORD_GENERATOR_NONCE)
# username and email might be empty, depending on how edX LTI module is configured:
# there are individual settings for that + if it's embedded into an iframe it never sends
# email and username in any case
# so, since we want to track user for both iframe and non-iframe LTI blocks, username is completely ignored
uname = self._compress_user_name(user_id)
email = email if email else user_id+'@localhost'
try:
User.objects.get(username=uname)
except User.DoesNotExist:
try:
User.objects.create_user(username=uname, email=email, password=password)
except IntegrityError as e:
# A result of race condition of multiple simultaneous LTI requests - should be safe to ignore,
# as password and uname are stable (i.e. not change for the same user)
logger.info("IntegrityError creating user - assuming result of race condition: %s", e.message)
authenticated = authenticate(username=uname, password=password)
login(request, authenticated)
评论列表
文章目录