Exemple #1
0
def login(email=None, password=None):
    user = UserManager.login_user(email, password)
    if not user:
        raise errors.InvalidLoginOrPassword()

    google_client_id = request.cookies.get('_ga_cid')
    old_user_id = None
    if current_user and not current_user.is_anonymous:
        if not current_user.temporal:
            my_resp = MyResp()
            current_app.session_interface.save_session(current_app, flask.session, my_resp)
            if google_client_id:
                metrics.update_user_info(current_user, google_client_id=google_client_id)
            # noinspection PyUnresolvedReferences
            my_resp = MyResp(json.dumps({"result": my_resp._MyResp__val}), status=200, content_type="application/json")
            return my_resp

        old_user_id = current_user.id

    login_user(user)
    if google_client_id:
        metrics.update_user_info(user, google_client_id=google_client_id)

    if old_user_id:
        new_user_id = user.id
        change_account_data_owner(old_user_id, new_user_id)

    my_resp = MyResp()
    current_app.session_interface.save_session(current_app, flask.session, my_resp)
    # noinspection PyUnresolvedReferences
    my_resp = MyResp(json.dumps({"result": my_resp._MyResp__val}), status=200, content_type="application/json")
    user.last_login_date = datetime.utcnow()
    return my_resp
Exemple #2
0
def signup(email=None, access_token=None, password=None, social_network=None):
    if "X-Forwarded-For" in request.headers and request.headers.getlist("X-Forwarded-For"):
        ip = request.headers.getlist("X-Forwarded-For")[0]
    elif "X-Real-Ip" in request.headers and request.headers.getlist("X-Real-Ip"):
        ip = request.headers.getlist("X-Real-Ip")[0]
    else:
        ip = request.remote_addr

    if ip in current_app.config['OFFICE_IP']:
        is_test_user = True
    else:
        is_test_user = False

    if not email and not access_token:
        raise errors.MissingRequiredParameter('email')

    if not EmailAddressValidator().validate(email) and not access_token:
        raise errors.InvalidParameterValue('email')

    if access_token:
        password = ''

    if social_network and social_network not in SocialServiceEnum.TAG_ALL:
        raise errors.InvalidParameterValue('social_network')

    if not PasswordValidator().validate(password) and not access_token:
        raise errors.MissingRequiredParameter('password')

    if current_user and not current_user.is_anonymous:
        if not current_user.temporal:
            raise errors.InvalidParameterValue('email')

        new_user = UserManager.promote_temp_user(current_user, access_token, None, email, u"", u"", u"", password, social_network)
        new_user.is_tester = is_test_user
    else:
        new_user = UserManager.register_user(access_token, None, email, u"", u"", u"", password, social_network)
        new_user.is_tester = is_test_user

    google_client_id = request.cookies.get('_ga_cid')
    if google_client_id and not new_user.temporal:
        metrics.update_user_info(new_user, google_client_id=google_client_id)

    new_user.email = new_user.email.lower() if new_user.email else u""

    data = get_user_api_structure(new_user)
    result = {"result": data}

    if not email and access_token:
        login_user(new_user)
        my_resp = MyResp()
        current_app.session_interface.save_session(current_app, flask.session, my_resp)
        return result

    user = UserManager.login_user(email, password)
    if user:
        login_user(user)
        my_resp = MyResp()
        current_app.session_interface.save_session(current_app, flask.session, my_resp)
        user.last_login_date = datetime.utcnow()

    return result