示例#1
0
文件: user.py 项目: jia-kai/pynojo
def register_submit(request):
    """POST: username, passwd, dispname

    Return: fail, msg
    """
    p = request.POST
    p['dispname'] = cgi.escape(p['dispname'])
    uname = p['username']
    try:
        validate_username(uname)
        ses = make_session()
        u = UserMdl(username=uname, extra=dict())
        for i in ('dispname', ):
            u.extra[i] = p[i]
        u.auth_pw = UserAuthPWMdl(p['passwd'])
        ses.add(u)
        ses.commit()
    except PynojoRuntimeError as e:
        return {'fail': 1, 'msg': _('Failed to register: {msg}', msg=str(e))}
    return {
            'fail': 0,
            'msg': _('Congratulations! You have successfully registered.' \
                    '{line_break}Log in after 2 seconds...',
                    line_break = '<br />')
            }
示例#2
0
文件: user.py 项目: stanonyime/pynojo
def register_submit(request):
    """POST: username, passwd, dispname

    Return: fail, msg
    """
    p = request.POST
    p['dispname'] = cgi.escape(p['dispname'])
    uname = p['username']
    try:
        validate_username(uname)
        ses = make_session()
        u = UserMdl(username = uname, extra = dict())
        for i in ('dispname', ):
            u.extra[i] = p[i]
        u.auth_pw = UserAuthPWMdl(p['passwd'])
        ses.add(u)
        ses.commit()
    except PynojoRuntimeError as e:
        return {'fail': 1, 'msg': _('Failed to register: {msg}', msg = str(e))}
    return {
            'fail': 0,
            'msg': _('Congratulations! You have successfully registered.' \
                    '{line_break}Log in after 2 seconds...',
                    line_break = '<br />')
            }
示例#3
0
文件: user.py 项目: jia-kai/pynojo
def validate_username(username):
    """Validate the username, raise :exc:`pynojo.exc.PynojoRuntimeError` on
    error. This function checks the name's legality and non-existence."""
    user.validate_username(username)
    ses = make_session()
    if ses.query(UserMdl).filter(UserMdl.username == username).count():
        raise PynojoRuntimeError(
            _('Sorry, username "{0}" already exists.', username))
示例#4
0
文件: user.py 项目: stanonyime/pynojo
def validate_username(username):
    """Validate the username, raise :exc:`pynojo.exc.PynojoRuntimeError` on
    error. This function checks the name's legality and non-existence."""
    user.validate_username(username)
    ses = make_session()
    if ses.query(UserMdl).filter(UserMdl.username == username).count():
        raise PynojoRuntimeError(_('Sorry, username "{0}" already exists.',
            username))
示例#5
0
文件: user.py 项目: jia-kai/pynojo
def _get_model(req):
    uid = req.cookies.get('uid', -1)
    if uid == -1:
        return None

    ses = make_session()
    try:
        user = ses.query(UserMdl).filter(UserMdl.id == uid).one()
    except NoResultFound:
        return None

    if user.get_auth_code() == req.cookies.get('token'):
        return user
    return None
示例#6
0
文件: user.py 项目: jia-kai/pynojo
def check_login_pw(request, username, passwd, cookie_max_age=None):
    """Check user login via password authentication. Raise
    :exc:`pynojo.exc.PynojoRuntimeError` on error. If login is successful,
    corresponding cookies are set."""
    ses = make_session()
    ok = True
    try:
        user = ses.query(UserMdl).filter(UserMdl.username == username).one()
    except NoResultFound:
        ok = False
    else:
        if user.auth_pw is None:
            ok = False
        else:
            ok = user.auth_pw.check(passwd)

    if not ok:
        raise PynojoRuntimeError(_('incorrect username/password'))

    request.pynojo_cache[get_model] = user
    request.set_cookie('uid', user.id, cookie_max_age)
    request.set_cookie('token', user.update_auth_code(), cookie_max_age)
示例#7
0
 def from_id(cls, acl_id):
     """Return an ACL instance whose id is *acl_id*."""
     ses = make_session()
     mdl = ses.query(ACLMdl).filter(ACLMdl.id == acl_id).one()
     return cls.acl_impls[mdl.type].from_model(mdl)
示例#8
0
文件: _base.py 项目: jia-kai/pynojo
 def from_id(cls, acl_id):
     """Return an ACL instance whose id is *acl_id*."""
     ses = make_session()
     mdl = ses.query(ACLMdl).filter(ACLMdl.id == acl_id).one()
     return cls.acl_impls[mdl.type].from_model(mdl)