Exemplo n.º 1
0
    def is_unique_username(self, username):
        all_users = UserDataHandler.get_all()
        for user in all_users:
            if username == user.username:
                return False

        return True
Exemplo n.º 2
0
    def post(self):
        signup = Signup()
        self.response.headers['Content-Type'] = 'text/html'

        errors = {}
        username = self.request.get('username')
        password = self.request.get('password')
        verify = self.request.get('verify')
        email = self.request.get('email')

        if not signup.is_valid_username(username):
            errors['username_error'] = 'Invalid username.'
        elif not signup.is_unique_username(username):
            errors['username_error'] = 'Username taken.'
        if not signup.is_valid_password(password):
            errors['password_error'] = 'Invalid password.'
        elif password != verify:
            errors['verify_error'] = 'Passwords don\'t match.'
        if not signup.is_valid_email(email):
            errors['email_error'] = 'Invalid email.'

        username = self.escape_html(username)
        email = self.escape_html(email)

        if errors:
            self.write_template('signup.html', username = username, email =
                    email, **errors)
        else:
            user_id = UserDataHandler.put(username, password, email)
            user_id_hash = Encryption.make_user_id_hash(user_id)
            self.response.headers.add_header('Set-Cookie',
                    'user_id=%(user_id)s|%(user_id_hash)s; Path=/'
                    % {'user_id': user_id, 'user_id_hash': user_id_hash})
            self.redirect('/unit5/welcome')
Exemplo n.º 3
0
    def get(self):
        self.response.headers['Content-Type'] = 'text/html'
        user_id_cookie = self.request.cookies.get('user_id')
        if user_id_cookie:
            user_id = user_id_cookie.split('|')[0]
            user_id_hash = user_id_cookie.split('|')[1]

            if Encryption.is_valid_cookie(user_id, user_id_hash):
                user = UserDataHandler.get_by_id(int(user_id))
                self.response.out.write('Welcome, <b>' + user.username + '</b>!')
        else:
            self.redirect('/unit4/signup')
Exemplo n.º 4
0
    def post(self):
        self.response.headers['Content-Type'] = 'text/html'

        username = self.request.get('username')
        password = self.request.get('password')

        username = self.escape_html(username)

        user_list = UserDataHandler.get_by_username(username)
        if user_list and Encryption.is_valid_password(username, password,
                user_list[0].password):
            user = user_list[0]
            user_id = user.key().id()
            user_id_hash = Encryption.make_user_id_hash(user_id)
            self.response.headers.add_header('Set-Cookie',
                    'user_id=%(user_id)s|%(user_id_hash)s; Path=/'
                    % {'user_id': user_id, 'user_id_hash': user_id_hash})
            self.redirect('/unit5/welcome')
        else:
            self.write_template('login.html', username = username, login_error =
                    'invalid login')