|
| 1 | +#!/usr/bin/env python |
| 2 | +# -*- coding: utf-8 -*- |
| 3 | +""" |
| 4 | +Backend for the create_edxapp_user that works under the open-release/juniper.master tag |
| 5 | +""" |
| 6 | +from __future__ import absolute_import, unicode_literals |
| 7 | + |
| 8 | +import logging |
| 9 | + |
| 10 | +from django.conf import settings |
| 11 | +from django.contrib.auth import get_user_model |
| 12 | +from django.db import transaction |
| 13 | +from openedx.core.djangoapps.lang_pref import LANGUAGE_KEY # pylint: disable=import-error |
| 14 | +from openedx.core.djangoapps.site_configuration import helpers as configuration_helpers # pylint: disable=import-error |
| 15 | +from openedx.core.djangoapps.user_api.accounts import USERNAME_MAX_LENGTH # pylint: disable=import-error,unused-import |
| 16 | +from openedx.core.djangoapps.user_api.accounts.serializers import UserReadOnlySerializer # pylint: disable=import-error |
| 17 | +from openedx.core.djangoapps.user_api.preferences import api as preferences_api # pylint: disable=import-error |
| 18 | +from openedx.core.djangoapps.user_authn.views.registration_form import ( # pylint: disable=import-error |
| 19 | + AccountCreationForm, |
| 20 | +) |
| 21 | +from rest_framework.exceptions import NotFound |
| 22 | +from student.helpers import create_or_set_user_attribute_created_on_site # pylint: disable=import-error |
| 23 | +from student.models import ( # pylint: disable=import-error |
| 24 | + LoginFailures, |
| 25 | + UserAttribute, |
| 26 | + UserProfile, |
| 27 | + UserSignupSource, |
| 28 | + create_comments_service_user, |
| 29 | + email_exists_or_retired, |
| 30 | + username_exists_or_retired, |
| 31 | +) |
| 32 | + |
| 33 | +from student.helpers import do_create_account # pylint: disable=import-error; pylint: disable=import-error |
| 34 | +from student.models import CourseEnrollment # pylint: disable=import-error; pylint: disable=import-error |
| 35 | + |
| 36 | +LOG = logging.getLogger(__name__) |
| 37 | +User = get_user_model() # pylint: disable=invalid-name |
| 38 | + |
| 39 | + |
| 40 | +def get_user_read_only_serializer(): |
| 41 | + """ |
| 42 | + Great serializer that fits our needs |
| 43 | + """ |
| 44 | + return UserReadOnlySerializer |
| 45 | + |
| 46 | + |
| 47 | +def check_edxapp_account_conflicts(email, username): |
| 48 | + """ |
| 49 | + Exposed function to check conflicts |
| 50 | + """ |
| 51 | + conflicts = [] |
| 52 | + |
| 53 | + if username and username_exists_or_retired(username): |
| 54 | + conflicts.append("username") |
| 55 | + |
| 56 | + if email and email_exists_or_retired(email): |
| 57 | + conflicts.append("email") |
| 58 | + |
| 59 | + return conflicts |
| 60 | + |
| 61 | + |
| 62 | +def create_edxapp_user(*args, **kwargs): |
| 63 | + """ |
| 64 | + Creates a user on the open edx django site using calls to |
| 65 | + functions defined in the edx-platform codebase |
| 66 | +
|
| 67 | + Example call: |
| 68 | +
|
| 69 | + data = { |
| 70 | + 'email': "address@example.org", |
| 71 | + 'username': "Username", |
| 72 | + 'password': "P4ssW0rd", |
| 73 | + 'fullname': "Full Name", |
| 74 | + 'activate': True, |
| 75 | + 'site': request.site, |
| 76 | + 'language_preference': 'es-419', |
| 77 | + } |
| 78 | + user = create_edxapp_user(**data) |
| 79 | +
|
| 80 | + """ |
| 81 | + errors = [] |
| 82 | + |
| 83 | + email = kwargs.pop("email") |
| 84 | + username = kwargs.pop("username") |
| 85 | + conflicts = check_edxapp_account_conflicts(email=email, username=username) |
| 86 | + if conflicts: |
| 87 | + return None, ["Fatal: account collition with the provided: {}".format(", ".join(conflicts))] |
| 88 | + |
| 89 | + data = { |
| 90 | + 'username': username, |
| 91 | + 'email': email, |
| 92 | + 'password': kwargs.pop("password"), |
| 93 | + 'name': kwargs.pop("fullname"), |
| 94 | + } |
| 95 | + # Go ahead and create the new user |
| 96 | + with transaction.atomic(): |
| 97 | + # In theory is possible to extend the registration form with a custom app |
| 98 | + # An example form app for this can be found at http://github.com/open-craft/custom-form-app |
| 99 | + # form = get_registration_extension_form(data=params) |
| 100 | + # if not form: |
| 101 | + form = AccountCreationForm( |
| 102 | + data=data, |
| 103 | + tos_required=False, |
| 104 | + # TODO: we need to support the extra profile fields as defined in the django.settings |
| 105 | + # extra_fields=extra_fields, |
| 106 | + # extended_profile_fields=extended_profile_fields, |
| 107 | + # enforce_password_policy=enforce_password_policy, |
| 108 | + ) |
| 109 | + (user, profile, registration) = do_create_account(form) # pylint: disable=unused-variable |
| 110 | + |
| 111 | + site = kwargs.pop("site", False) |
| 112 | + if site: |
| 113 | + create_or_set_user_attribute_created_on_site(user, site) |
| 114 | + else: |
| 115 | + errors.append("The user was not assigned to any site") |
| 116 | + |
| 117 | + try: |
| 118 | + create_comments_service_user(user) |
| 119 | + except Exception: # pylint: disable=broad-except |
| 120 | + errors.append("No comments_service_user was created") |
| 121 | + |
| 122 | + # TODO: link account with third party auth |
| 123 | + |
| 124 | + lang_pref = kwargs.pop("language_preference", False) |
| 125 | + if lang_pref: |
| 126 | + try: |
| 127 | + preferences_api.set_user_preference(user, LANGUAGE_KEY, lang_pref) |
| 128 | + except Exception: # pylint: disable=broad-except |
| 129 | + errors.append("Could not set lang preference '{} for user '{}'".format( |
| 130 | + lang_pref, |
| 131 | + user.username, |
| 132 | + )) |
| 133 | + |
| 134 | + if kwargs.pop("activate_user", False): |
| 135 | + user.is_active = True |
| 136 | + user.save() |
| 137 | + |
| 138 | + # TODO: run conditional email sequence |
| 139 | + |
| 140 | + return user, errors |
| 141 | + |
| 142 | + |
| 143 | +def get_edxapp_user(**kwargs): |
| 144 | + """ |
| 145 | + Retrieve an user by username and/or email |
| 146 | +
|
| 147 | + The user will be returned only if it belongs to the calling site |
| 148 | +
|
| 149 | + Examples: |
| 150 | + >>> get_edxapp_user( |
| 151 | + { |
| 152 | + "username": "Bob", |
| 153 | + "site": request.site |
| 154 | + } |
| 155 | + ) |
| 156 | + >>> get_edxapp_user( |
| 157 | + { |
| 158 | + "email": "Bob@mailserver.com", |
| 159 | + "site": request.site |
| 160 | + } |
| 161 | + ) |
| 162 | + """ |
| 163 | + params = {key: kwargs.get(key) for key in ['username', 'email'] if key in kwargs} |
| 164 | + site = kwargs.get('site') |
| 165 | + try: |
| 166 | + domain = site.domain |
| 167 | + except AttributeError: |
| 168 | + domain = None |
| 169 | + |
| 170 | + try: |
| 171 | + user = User.objects.get(**params) |
| 172 | + for source_method in FetchUserSiteSources.get_enabled_source_methods(): |
| 173 | + if source_method(user, domain): |
| 174 | + break |
| 175 | + else: |
| 176 | + raise User.DoesNotExist |
| 177 | + except User.DoesNotExist: |
| 178 | + raise NotFound('No user found by {query} on site {site}.'.format(query=str(params), site=domain)) |
| 179 | + return user |
| 180 | + |
| 181 | + |
| 182 | +def get_course_team_user(*args, **kwargs): |
| 183 | + """ |
| 184 | + Get _course_team_user function. |
| 185 | + We need to check if the SERVICE_VARIANT is equal to cms, since |
| 186 | + contentstore is a module registered in the INSTALLED_APPS |
| 187 | + of the cms only. |
| 188 | + """ |
| 189 | + if settings.SERVICE_VARIANT == 'cms': |
| 190 | + from contentstore.views.user import _course_team_user # pylint: disable=import-error |
| 191 | + return _course_team_user(*args, **kwargs) |
| 192 | + return None |
| 193 | + |
| 194 | + |
| 195 | +class FetchUserSiteSources(object): |
| 196 | + """ |
| 197 | + Methods to make the comparison to check if an user belongs to a site plus the |
| 198 | + get_enabled_source_methods that just brings an array of functions enabled to do so |
| 199 | + """ |
| 200 | + |
| 201 | + @classmethod |
| 202 | + def get_enabled_source_methods(cls): |
| 203 | + """ Brings the array of methods to check if an user belongs to a site. """ |
| 204 | + sources = configuration_helpers.get_value( |
| 205 | + 'EOX_CORE_USER_ORIGIN_SITE_SOURCES', |
| 206 | + getattr(settings, 'EOX_CORE_USER_ORIGIN_SITE_SOURCES') |
| 207 | + ) |
| 208 | + return [getattr(cls, source) for source in sources] |
| 209 | + |
| 210 | + @staticmethod |
| 211 | + def fetch_from_created_on_site_prop(user, domain): |
| 212 | + """ Fetch option. """ |
| 213 | + if not domain: |
| 214 | + return False |
| 215 | + return UserAttribute.get_user_attribute(user, 'created_on_site') == domain |
| 216 | + |
| 217 | + @staticmethod |
| 218 | + def fetch_from_user_signup_source(user, domain): |
| 219 | + """ Read the signup source. """ |
| 220 | + return len(UserSignupSource.objects.filter(user=user, site=domain)) > 0 |
| 221 | + |
| 222 | + @staticmethod |
| 223 | + def fetch_from_unfiltered_table(user, site): |
| 224 | + """ Fetch option that does not take into account the multi-tentancy model of the installation. """ |
| 225 | + return bool(user) |
| 226 | + |
| 227 | + |
| 228 | +def get_course_enrollment(): |
| 229 | + """ get CourseEnrollment model """ |
| 230 | + return CourseEnrollment |
| 231 | + |
| 232 | + |
| 233 | +def get_user_signup_source(): |
| 234 | + """ get UserSignupSource model """ |
| 235 | + return UserSignupSource |
| 236 | + |
| 237 | + |
| 238 | +def get_login_failures(): |
| 239 | + """ get LoginFailures model """ |
| 240 | + return LoginFailures |
| 241 | + |
| 242 | + |
| 243 | +def get_user_profile(): |
| 244 | + """ Gets the UserProfile model """ |
| 245 | + |
| 246 | + return UserProfile |
0 commit comments