示例#1
0
def auth_login():

    error_message = []

    user_name = request.forms.get('user_name')
    input_password = request.forms.get('password')

    u = User.get_by_name(user_name)
    # O utilizador nao existe?
    if u is None:
        error_message.append('O utilizador nao existe')
        return jresp.reply(
                payload=None,
                success=False,
                error_message=error_message
            )
    else:
        db_password = u['password']

        # Validar palavra-passe
        if not auth.password_matches(input_password, db_password):
            error_message.append('A password esta incorrecta')
            return jresp.reply(
                    payload=None,
                    success=False,
                    error_message=error_message
                )

        # User existe e password esta correcta:
        # criar e gravar token de sessao
        # enviar mensagem com o token de sessao

        new_session_token = auth.generate_session_token()

        new_session = UserSession(
                user_id=u['id'],
                token=new_session_token
            )
        new_session.save() 


        # Tudo correu bem:
        payload = {
                'token': new_session_token
                }
        return jresp.reply(
                payload=payload,
                error_message=None,
                success=True
            )
示例#2
0
def create_account():

    error_message = []

    name = request.forms.get('name', '')
    password = request.forms.get('password', '')
    password_confirmation = request.forms.get('password_confirmation', '')
    email = request.forms.get('email', '')

    # Validaçao:
    if name == '':
        error_message.append('O nome de Utilizador esta vazio')
    if password == '':
        error_message.append('O campo password esta vazio')
    if password is not None and len(password) > 6:
        error_message.append('A password tem de ter pelo menos 6 caracteres')
    if password_confirmation == '':
        error_message.append('O campo confirmaçao de password esta vazio')
    if password != password_confirmation:
        error_message.append('As passwords nao sao iguais')

    # http://stackoverflow.com/questions/8022530/
    # python-check-for-valid-email-address/8022584#8022584
    if not re.match(r"[^@]+@[^@]+\.[^@]+", email):
        error_message.append('O endereço de email nao e valido')

    # Ja existe algum utilizador na base de dados com este nome?
    if name != '':
        u = User.get_by_name(name)
        if u is not None:
            error_message.append('O nome de utilizador ja existe')
    # Ja existe algum utilizador na base de dados com este Email?
    if email != '':
        u = User.get_by_email(email)
        if u is not None:
            error_message.append('O email ja existe')

    # Alguma das Validaç~oes devolveu erro?
    if len(error_message) > 0:
        return jresp.reply(
                payload=None,
                success=False,
                error_message=error_message
            )
    else:
        # 1->Inserir o novo utilizador
        # 2->Enviar o email
        plaintext_password = password
        hashed_password = auth.hash_password(plaintext_password)

        try:
            user = User(name=name, email=email, password=hashed_password)
            user.save()
        except:
            error_message.append('Erro ao comunicar com a base de dados!')
            return jresp.reply(
                    response=response,
                    payload=None,
                    success=False,
                    error_message=error_message,
                    status=500
                )
        '''
        from services import mailgun as mail
        try:
            mail.send(template='account_confirmation', name=name, email=email)
        except:
            error_message.append('Erro ao enviar email!')
            return jresp.reply(
                    response=response,
                    payload=None,
                    success=False,
                    error_message=error_message
                )
        '''
        # Tudo correu bem:
        payload = [{
                'message': 'Conta de utilizador criada com sucesso.'
                }]
        return jresp.reply(
                payload=payload,
                )
示例#3
0
from database.models import User

u = User.get_by_name('kjhgf')
print(u)