Пример #1
0
def create_user_if_allowed(strategy,
                           details,
                           backend,
                           user=None,
                           *args,
                           **kwargs):
    """Create new Users if the domain of the email address is whitelisted."""
    if user is None:
        if not email_address_is_whitelisted(details.get('email')):
            # TODO: Create a middleware to gracefully handle this.
            raise AuthForbidden(backend)
        return create_user(strategy, details, backend, *args, **kwargs)
Пример #2
0
def create_google_user(strategy, details, backend, user=None, *args, **kwargs):
    """
    Override social_auth's create_user method so we can prevent Monash users
    from creating Store.Monash accounts via Google
    """
    from social_core.pipeline.user import create_user

    if 'monash.edu' not in details['email']:
        return create_user(strategy, details, backend, user, *args, **kwargs)
    raise AuthFailed(
        backend,
        "Monash users should log in using the Australian Access Federation instead of Google."
    )
Пример #3
0
def create_user_disabled(strategy, details, backend, user=None, *args, **kwargs):
    """
    Create a user account for the user being authenticated - but mark it as disabled.

    A new user must have their account enabled by an admin before they are able to log in.
    """
    # Returns dict containing 'is_new' and 'user'
    result = create_user(strategy, details, backend, user, *args, **kwargs)

    if result['is_new']:
        django_user = result['user']
        django_user.is_active = False
        django_user.save()

    return result
Пример #4
0
def create_user_account(strategy,
                        details,
                        backend,
                        user=None,
                        *args,
                        **kwargs):

    if not user:
        res = create_user(strategy=strategy,
                          details=details,
                          backend=backend,
                          user=user,
                          *args,
                          **kwargs)
        res['is_new_forus_user'] = res['is_new']
        return res
Пример #5
0
def process_join_answer(message):
    invite_token = message.text
    try:
        company = Company.objects.get(invite_token=invite_token)
    except Company.DoesNotExist:
        bot.reply_to(
            message,
            _("Sorry, couldn't find lunch group with the given token. :("))
        state_registry.del_state(message.from_user.id)
        return

    # Authenticate user, create if need to
    from_user = message.from_user
    try:
        social_auth = UserSocialAuth.objects.get(provider='telegram',
                                                 uid=from_user.id)
    except UserSocialAuth.DoesNotExist:
        storage = DjangoStorage()
        strategy = DjangoStrategy(storage)
        backend = TelegramAuth(strategy)

        username = get_username(strategy, from_user.__dict__,
                                backend)['username']
        with transaction.atomic():
            user = create_user(strategy,
                               from_user.__dict__,
                               backend,
                               username=username)['user']
            user.first_name = from_user.first_name or ''
            user.last_name = from_user.last_name or ''
            user.has_telegram = True
            user.telegram_chat_id = message.chat.id
            user.save()
            UserSocialAuth.objects.get_or_create(provider='telegram',
                                                 uid=from_user.id,
                                                 defaults={
                                                     'extra_data':
                                                     from_user.__dict__,
                                                     'user': user,
                                                 })
    else:
        user = social_auth.user

    if not user.is_active:
        bot.send_message(message.chat.id,
                         _('Sorry, your account has been deactivated.'))
        return

    # Create employee and add him to the group
    employee, created = Employee.objects.get_or_create(company=company,
                                                       user=user)
    if created:
        msg = _(
            "You've successfully joined lunch group «{}». "
            "You may manage your participation with /offline and /online commands."
        ).format(company.name)
    else:
        msg = _(
            "You've joined lunch group «{}» already. "
            "You may manage your participation with /offline and /online commands."
        ).format(company.name)
    bot.send_message(message.chat.id, msg)