コード例 #1
0
ファイル: user.py プロジェクト: smoothgrips/raggregate
def twit_sign(request):
    from raggregate.login_adapters import twitter
    if 'oauth_verifier' not in request.session['safe_params']:
        auth_toks = twitter.start_auth(request)
        request.session['tmp_tok_store'] = auth_toks
        return HTTPFound(auth_toks['auth_url'])
    else:
        twit_auth = twitter.complete_auth(request, request.session['tmp_tok_store'])
        del request.session['tmp_tok_store']
        try:
            queries.login_user(request, twit_auth['u'], None, bypass_password = True)
        except:
            request.session['last_login_status'] = 'Sorry, your password was wrong.'
            #raise
        return HTTPFound('/post')
コード例 #2
0
ファイル: user.py プロジェクト: smoothgrips/raggregate
def login(request):
    #@FIXME: this uses a request handling method with success with which I was experimenting
    # it is not used elsewhere and is a pain to read and write
    # success = False causes a page to stop drawing and "error out"
    # some error conditions therefore don't set success to false because it's more convenient
    # to draw the rest of the page.
    #
    # someone should adapt this to be less success-centric and read less branchy.
    s = request.session

    success = True

    # check for facebook login, provided by Facebook's JS SDK
    try:
        fb_cookie = fb.extract_from_cookie(request)
        try:
            u = queries.get_user_by_name(fb_cookie['local_username'])
        except sqlalchemy.orm.exc.NoResultFound:
            u = fb.create_local_user(fb_cookie['info'], fb_cookie['local_username'], request = request)
        try:
            queries.login_user(request, u, None, bypass_password = True)
        except LoginAdapterExc:
            pass
    except LoginAdapterExc:
        pass

    if 'logout' in request.session['safe_params']:
        if 'logged_in' in s:
            del s['logged_in']
            del s['users.id']
            if 'u_fbgraph' in s:
                del s['u_fbgraph']
                del s['u_fbinfo']
            if 'u_twit' in s:
                del s['u_twit']
            s['message'] = "You have been logged out, thanks."
            success = True
        else:
            s['message'] = "You are not logged in."
            success = True
    else:
        logged_in = False
        if 'logged_in' in s:
            s['message'] = "You are already logged in."
            logged_in = True
        else:
            if 'message' not in s:
                if 'last_login_status' in s:
                    s['message'] = s['last_login_status']
                    del s['last_login_status']
                else:
                    s['message'] = "Please log in."
        p = request.session['safe_post']
        prm = request.session['safe_params']
        username = None
        if 'username' in prm:
            username = queries.strip_all_html(prm['username'])
        if p:
            dbsession = DBSession()
            if request.session['safe_get']['act'] == 'register':
                if logged_in:
                    try:
                        u = queries.get_user_by_id(s['users.id'])
                        if u.temporary:
                            queries.create_user(temp_to_perm = True, extant_id = s['users.id'], username = username, password = p['password'], origination = 'site')
                            s['message'] = "Your anonymous profile has been converted, thanks."
                        else:
                            s['message'] = "You can't register while you're logged in."
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        dbsession.rollback()
                else:
                    try:
                        queries.create_user(username = username, password = p['password'], origination = 'site')
                        s['message'] = "Successfully registered."
                        success = True
                    except sqlalchemy.exc.IntegrityError:
                        s['message'] = "This username is already registered, sorry."
                        success = False
                        dbsession.rollback()
            elif request.session['safe_get']['act'] == 'update_pw':
                if p['new_password'] != p['new_password_confirm']:
                    s['message'] = 'New password doesn\'t match confirmation, please try again.'
                else:
                    u = queries.get_user_by_id(s['users.id'])
                    if u.verify_pw(p['old_password']):
                        u.password = u.hash_pw(p['new_password'])
                        dbsession.add(u)
                        s['message'] = 'Password updated.'
                        success = True
                    else:
                        s['message'] = 'Old password invalid.'
            else:
                try:
                    u = queries.get_user_by_name(username)
                    try:
                        queries.login_user(request, u, p['password'])
                        s['message'] = "Good, logged in"
                        success = True
                        return HTTPFound(request.route_url('post'))
                    except LoginAdapterExc:
                        s['message'] = "Incorrect password."
                        success = False
                except sqlalchemy.orm.exc.NoResultFound:
                    s['message'] = "Sorry, I don't know you."
                    success = False

    return {'success': success,}