Beispiel #1
0
def login():
    if env.user.id:
        return Response().redirect('%s://%s.%s/' % \
                                   (env.request.protocol,
                                    env.user.login, settings.domain))

    referer = get_referer()

    if env.request.method == 'GET':
        return render('/auth/login.html', referer=referer, fields=ULOGIN_FIELDS)

    try:
        login = env.request.args('login')
        password = env.request.args('password')
        if not login or not password:
            raise NotAuthorized
        env.user.authenticate(login, password)
        if env.request.is_xhr:
            return Response(json.dumps({'ok': True}),
                            mimetype='application/json')
        else:
            return Response(redirect=referer)
    except (KeyError, NotAuthorized):
        if env.request.is_xhr:
            return Response(json.dumps({'error': 'credentials'}),
                            mimetype='application/json')
        else:
            return render('/auth/login.html', errors=['credentials'],
                                         referer=referer, fields=ULOGIN_FIELDS)

    return Response(redirect=referer)
Beispiel #2
0
def ulogin():
    if env.user.id:
        raise AlreadyAuthorized

    sess = Session()

    if env.request.method == 'POST':
        url = "http://ulogin.ru/token.php?token=%s&host=%s" % \
                (env.request.args('token'), settings.domain)
        try:
            resp = urllib2.urlopen(url)
            data = dict.fromkeys(ULOGIN_FIELDS)
            data.update(json.loads(resp.read()))
            resp.close()
        except urllib2.URLError:
            return render('/auth/login.html', fields=ULOGIN_FIELDS,
                          errors=['ulogin-fail'])

        try:
            env.user.authenticate_ulogin(data['network'], data['uid'])
            if env.user.id:
                return Response(redirect=get_referer())
        except NotAuthorized:
            pass

        login = data['nickname'].strip(u' -+.')
        if login:
            login = re.sub(r'[\._\-\+]+', '-', login)

        info = {
            'login': login,
            'network': data['network'],
            'uid': data['uid'],
            'name': ('%s %s' % (data['first_name'], data['last_name'])).strip(),
            'email': data['email'],
            'avatar': data['photo_big'],
            'birthdate': data['bdate'],
            'gender': True if data['sex'] == '2' else False if data['sex'] == '1' else None,
            'location': "%s, %s" % (data['city'], data['country']) \
                        if data['city'] and data['country'] else \
                        data['city'] or data['country'],

            '_nickname': data['nickname'],
            '_name': ('%s %s' % (data['first_name'], data['last_name'])).strip(),
            '_profile': data['profile'],
        }

        sess['reg_info'] = info
        sess.save()

    else:
        info = sess['reg_info']

        if not info or not 'network' in info or not 'uid' in info:
            return Response(redirect='%s://%s/register' % \
                    (env.request.protocol, settings.domain))

    info['birthdate'] = parse_date(info['birthdate']) \
                        or datetime.now() - timedelta(days=365*16+4)

    return render('/auth/register_ulogin.html', info=info)
Beispiel #3
0
            env.user.set_info('avatar',
                        '%s?r=%d' % (filename, randint(1000, 9999)))

    elif 'avatar' in info and info['avatar']:
        filename = ('%s.%s' % (env.user.login, 'jpg')).lower()

        make_avatar(info['avatar'], filename)

        env.user.set_info('avatar', '%s?r=%d' % (filename, randint(1000, 9999)))

    env.user.save()

    env.user.authenticate()

    return Response(redirect=get_referer())

@catch_errors
def ulogin():
    if env.user.id:
        raise AlreadyAuthorized

    sess = Session()

    if env.request.method == 'POST':
        url = "http://ulogin.ru/token.php?token=%s&host=%s" % \
                (env.request.args('token'), settings.domain)
        try:
            resp = urllib2.urlopen(url)
            data = dict.fromkeys(ULOGIN_FIELDS)
            data.update(json.loads(resp.read()))