def _get_account_info(self, username_or_email, idp_provider=None):
        """
        Provided the edx account username or email, and the SAML provider selected,
        return edx account info and program_enrollments_info.
        If we cannot identify the user, return empty object and error.
        """
        try:
            user = User.objects.get(
                Q(username=username_or_email) | Q(email=username_or_email))
            user_social_auths = None
            user_social_auths = UserSocialAuth.objects.filter(user=user)
            if idp_provider:
                user_social_auths = user_social_auths.filter(
                    provider=idp_provider.backend_name)
            user_info = serialize_user_info(user, user_social_auths)
            enrollments = self._get_enrollments(user=user)
            result = {'user': user_info}
            if enrollments:
                result['enrollments'] = enrollments

            result['id_verification'] = IDVerificationService.user_status(user)
            return result, ''
        except User.DoesNotExist:
            return {}, 'Could not find edx account with {}'.format(
                username_or_email)
Ejemplo n.º 2
0
    def _get_external_user_info(self, external_user_key, org_key):
        """
        Provided the external_user_key and org_key, return edx account info
        and program_enrollments_info if any. If we cannot identify the data,
        return empty object.
        """
        found_user = None
        result = {}
        try:
            users_by_key = get_users_by_external_keys_and_org_key(
                [external_user_key], org_key)
            found_user = users_by_key.get(external_user_key)
        except (BadOrganizationShortNameException,
                ProviderConfigurationException, ProviderDoesNotExistException):
            # We cannot identify edX user from external_user_key and org_key pair
            pass

        if found_user:
            try:
                user_social_auth = UserSocialAuth.objects.get(user=found_user)
            except UserSocialAuth.DoesNotExist:
                user_social_auth = None
            user_info = serialize_user_info(found_user, user_social_auth)
            result['user'] = user_info
            result['id_verification'] = IDVerificationService.user_status(
                found_user)
        enrollments = self._get_enrollments(
            external_user_key=external_user_key)
        if enrollments:
            result['enrollments'] = enrollments
        return result
    def _get_external_user_info(self,
                                external_user_key,
                                org_key,
                                idp_provider=None):
        """
        Provided the external_user_key and org_key, return edx account info
        and program_enrollments_info if any. If we cannot identify the data,
        return empty object.
        """
        found_user = None
        result = {}
        try:
            users_by_key = get_users_by_external_keys_and_org_key(
                [external_user_key], org_key)
            # Remove entries with no corresponding user and convert keys to lowercase
            users_by_key_lower = {
                key.lower(): value
                for key, value in users_by_key.items() if value
            }
            found_user = users_by_key_lower.get(external_user_key.lower())
        except (BadOrganizationShortNameException,
                ProviderDoesNotExistException):
            # We cannot identify edX user from external_user_key and org_key pair
            pass

        enrollments = self._get_enrollments(
            external_user_key=external_user_key)
        if enrollments:
            result['enrollments'] = enrollments
        if found_user:
            user_social_auths = UserSocialAuth.objects.filter(user=found_user)
            if idp_provider:
                user_social_auths = user_social_auths.filter(
                    provider=idp_provider.backend_name)
            user_info = serialize_user_info(found_user, user_social_auths)
            result['user'] = user_info
            result['id_verification'] = IDVerificationService.user_status(
                found_user)
        elif 'enrollments' in result:
            result['user'] = {'external_user_key': external_user_key}

        return result