예제 #1
0
def authenticate():
    '''通过邮箱进行登陆,读取用户输入的信息和数据库内所存储的用户信息进行比较'''
    i = ctx.request.input(remember='')
    email = i.email.strip().lower()
    password = i.password
    remember = i.remember
    user = Users.find_first('where email=?', email)
    if user is None:
        raise APIError('auth:failed', 'email', 'Invalid email.')
    elif user.password != password:
        raise APIError('auth:failed', 'password', 'Invalid password.')
    max_age = 604800 if remember == 'true' else None
    cookie = make_signed_cookie(user.id, user.password, max_age)
    ctx.response.set_cookie(_COOKIE_NAME, cookie, max_age=max_age)
    user.password = '******'
    return user
예제 #2
0
파일: urls.py 프로젝트: shaw711/spider-mvc
def authenticate():
    i = ctx.request.input(remember='')
    email = i.email
    tel = i.tel
    remember = i.remember
    user = Users.find_first('where email=? and tel = ?', email, tel)
    if user is None:
        raise APIError('auth:failed', 'email', 'Invalid email or tel.')
    # make session cookie:
    max_age = 604800 if remember == 'true' else None
    print '111'
    cookie = make_signed_cookie(user.id, user.email, max_age)
    print '222'
    ctx.response.set_cookie(_COOKIE_NAME, cookie, max_age=max_age)
    print '333'
    user.tel = '******'
    return user
예제 #3
0
def register_user():
    i = ctx.request.input(name='', email='', password='')
    name = i.name.strip()
    email = i.email.strip().lower()
    password = i.password
    if not name:
        raise APIValueError('name')
    if not email or not _RE_EMAIL.match(email):
        raise APIValueError('email')
    if not password or not _RE_MD5.match(password):
        raise APIValueError('password')
    user = Users.find_first('where email=?', email)
    if user:
        raise APIError('register:failed', 'email', 'Email is already in use.')
    user = Users(name=name,
                 email=email,
                 password=password,
                 image='http://www.gravatar.com/avatar/%s?d=mm&s=120' %
                 hashlib.md5(email).hexdigest())
    user.insert()
    cookie = make_signed_cookie(user.id, user.password, None)
    ctx.response.set_cookie(_COOKIE_NAME, cookie)
    return user