Beispiel #1
0
def register():
    """ Perform registration of a new user """
    disable_cache()
    require.account.create()
    data = AccountRegister().deserialize(request_data())

    # Check if the username already exists, return an error if so
    if Account.by_name(data['name']):
        raise colander.Invalid(
            AccountRegister.name,
            _("Login name already exists, please choose a "
              "different one"))

    # Check if passwords match, return error if not
    if not data['password1'] == data['password2']:
        raise colander.Invalid(AccountRegister.password1,
                               _("Passwords don't match!"))

    # Create the account
    account = Account()
    account.name = data['name']
    account.fullname = data['fullname']
    account.email = data['email']
    account.public_email = data['public_email']
    account.password = generate_password_hash(data['password1'])

    db.session.add(account)
    db.session.commit()

    # Perform a login for the user
    login_user(account, remember=True)

    # Registration successful - Redirect to the front page
    return jsonify(account)
Beispiel #2
0
def load_user_from_request(request):
    api_key = request.args.get("api_key")
    if api_key and len(api_key):
        account = Account.by_api_key(api_key)
        if account:
            return account

    api_key = request.headers.get("Authorization")
    if api_key and len(api_key) and " " in api_key:
        method, api_key = api_key.split(" ", 1)
        if method.lower() == "apikey":
            account = Account.by_api_key(api_key)
            if account:
                return account
    return None
Beispiel #3
0
def load_user_from_request(request):
    api_key = request.args.get('api_key')
    if api_key and len(api_key):
        account = Account.by_api_key(api_key)
        if account:
            return account

    api_key = request.headers.get('Authorization')
    if api_key and len(api_key) and ' ' in api_key:
        method, api_key = api_key.split(' ', 1)
        if method.lower() == 'apikey':
            account = Account.by_api_key(api_key)
            if account:
                return account
    return None
Beispiel #4
0
def load_user_from_request(request):
    api_key = request.args.get('api_key')
    if api_key and len(api_key):
        account = Account.by_api_key(api_key)
        if account:
            return account

    api_key = request.headers.get('Authorization')
    if api_key and len(api_key) and ' ' in api_key:
        method, api_key = api_key.split(' ', 1)
        if method.lower() == 'apikey':
            account = Account.by_api_key(api_key)
            if account:
                return account
    return None
Beispiel #5
0
def trigger_reset():
    """
    Allow user to trigger a reset of the password in case they forget it
    """
    email = request_data().get('email')

    # Simple check to see if the email was provided. Flash error if not
    if email is None or not len(email):
        return jsonify(
            {
                'status': 'error',
                'message': _("Please enter an email address!")
            },
            status=400)

    account = Account.by_email(email)

    # If no account is found we let the user know that it's not registered
    if account is None:
        return jsonify(
            {
                'status': 'error',
                'message': _("No user is registered under this address!")
            },
            status=400)

    # Send the reset link to the email of this account
    send_reset_link(account)
    return jsonify({
        'status':
        'ok',
        'message':
        _("You've received an email with a link to reset your "
          "password. Please check your inbox.")
    })
Beispiel #6
0
def trigger_reset():
    """
    Allow user to trigger a reset of the password in case they forget it
    """
    email = request_data().get('email')

    # Simple check to see if the email was provided. Flash error if not
    if email is None or not len(email):
        return jsonify({
            'status': 'error',
            'message': _("Please enter an email address!")
        }, status=400)

    account = Account.by_email(email)

    # If no account is found we let the user know that it's not registered
    if account is None:
        return jsonify({
            'status': 'error',
            'message': _("No user is registered under this address!")
        }, status=400)

    # Send the reset link to the email of this account
    send_reset_link(account)
    return jsonify({
        'status': 'ok',
        'message': _("You've received an email with a link to reset your "
                     "password. Please check your inbox.")
    })
Beispiel #7
0
 def decode(self, cstruct):
     from spendb.model import Account
     if isinstance(cstruct, basestring):
         return Account.by_name(cstruct)
     if isinstance(cstruct, dict):
         return self.decode(cstruct.get('name'))
     return None
Beispiel #8
0
    def decode(self, cstruct):
        from spendb.model import Account

        if isinstance(cstruct, basestring):
            return Account.by_name(cstruct)
        if isinstance(cstruct, dict):
            return self.decode(cstruct.get("name"))
        return None
Beispiel #9
0
def login():
    data = request_data()
    account = Account.by_name(data.get("login"))
    if account is not None:
        if check_password_hash(account.password, data.get("password")):
            login_user(account, remember=True)
            return jsonify({"status": "ok", "message": _("Welcome back, %(name)s!", name=account.name)})
    return jsonify({"status": "error", "errors": {"password": _("Incorrect user name or password!")}}, status=400)
Beispiel #10
0
def register():
    """ Perform registration of a new user """
    require.account.create()
    data = AccountRegister().deserialize(request_data())

    # Check if the username already exists, return an error if so
    if Account.by_name(data['name']):
        raise colander.Invalid(
            AccountRegister.name,
            _("Login name already exists, please choose a "
              "different one"))

    # Check if passwords match, return error if not
    if not data['password1'] == data['password2']:
        raise colander.Invalid(AccountRegister.password1,
                               _("Passwords don't match!"))

    # Create the account
    account = Account()
    account.name = data['name']
    account.fullname = data['fullname']
    account.email = data['email']
    account.public_email = data['public_email']
    account.password = generate_password_hash(data['password1'])

    db.session.add(account)
    db.session.commit()

    # Perform a login for the user
    login_user(account, remember=True)

    # Registration successful - Redirect to the front page
    return jsonify(account)
Beispiel #11
0
def view(account):
    """ Generate a profile page for a user (from the provided name) """
    account = obj_or_404(Account.by_name(account))
    data = account.to_dict()
    if account == current_user or current_user.admin:
        data['email'] = account.email
        data['public_email'] = account.public_email
        data['twitter_handle'] = account.twitter_handle
        data['public_twitter'] = account.public_twitter
    return jsonify(data)
Beispiel #12
0
def view(account):
    """ Generate a profile page for a user (from the provided name) """
    account = obj_or_404(Account.by_name(account))
    data = account.to_dict()
    if account == current_user or current_user.admin:
        data['email'] = account.email
        data['public_email'] = account.public_email
        data['twitter_handle'] = account.twitter_handle
        data['public_twitter'] = account.public_twitter
    return jsonify(data)
Beispiel #13
0
def do_reset():
    email = request.args.get('email')
    if email is None or not len(email):
        return redirect('/login')

    account = Account.by_email(email)
    if account is None:
        return redirect('/login')

    if request.args.get('token') != account.token:
        return redirect('/login')

    login_user(account)
    return redirect('/settings')
Beispiel #14
0
def login():
    data = request_data()
    account = Account.by_name(data.get('login'))
    if account is not None:
        if check_password_hash(account.password, data.get('password')):
            login_user(account, remember=True)
            return jsonify({
                'status': 'ok',
                'message': _("Welcome back, %(name)s!", name=account.name)
            })
    return jsonify({
        'status': 'error',
        'errors': {
            'password': _("Incorrect user name or password!")
        }
    }, status=400)
Beispiel #15
0
def do_reset():
    email = request.args.get('email')
    if email is None or not len(email):
        # flash_error(_("The reset link is invalid!"))
        return redirect('/login')

    account = Account.by_email(email)
    if account is None:
        # flash_error(_("No user is registered under this address!"))
        return redirect('/login')

    if request.args.get('token') != account.token:
        # flash_error(_("The reset link is invalid!"))
        return redirect('/login')

    login_user(account)
    # flash_success(
    #     _("Thanks! You have now been signed in - please change "
    #       "your password!"))
    return redirect('/settings')
Beispiel #16
0
def login():
    data = request_data()
    account = Account.by_name(data.get('login'))
    if account is not None:
        if check_password_hash(account.password, data.get('password')):
            login_user(account, remember=True)
            return jsonify({
                'status':
                'ok',
                'message':
                _("Welcome back, %(name)s!", name=account.name)
            })
    return jsonify(
        {
            'status': 'error',
            'errors': {
                'password': _("Incorrect user name or password!")
            }
        },
        status=400)
Beispiel #17
0
def do_reset():
    email = request.args.get('email')
    if email is None or not len(email):
        # flash_error(_("The reset link is invalid!"))
        return redirect('/login')

    account = Account.by_email(email)
    if account is None:
        # flash_error(_("No user is registered under this address!"))
        return redirect('/login')

    if request.args.get('token') != account.token:
        # flash_error(_("The reset link is invalid!"))
        return redirect('/login')

    login_user(account)
    # flash_success(
    #     _("Thanks! You have now been signed in - please change "
    #       "your password!"))
    return redirect('/settings')