Esempio n. 1
0
File: user.py Progetto: JoshOY/vj4
async def add(uid: int, uname: str, password: str, mail: str, regip: str = ''):
  """Add a user."""
  validator.check_uname(uname)
  # TODO(iceboy): Filter uname by keywords.
  validator.check_password(password)
  validator.check_mail(mail)

  uname_lower = uname.strip().lower()
  mail_lower = mail.strip().lower()

  for user in builtin.USERS:
    if user['_id'] == uid or user['uname_lower'] == uname_lower or user['mail_lower'] == mail_lower:
      raise error.UserAlreadyExistError(uname)

  salt = pwhash.gen_salt()
  coll = db.Collection('user')
  try:
    await coll.insert({'_id': uid,
                       'uname': uname,
                       'uname_lower': uname_lower,
                       'mail': mail,
                       'mail_lower': mail_lower,
                       'salt': salt,
                       'hash': pwhash.hash_vj4(password, salt),
                       'regat': datetime.datetime.utcnow(),
                       'regip': regip,
                       'roles': {},
                       'priv': builtin.PRIV_USER_PROFILE,
                       'loginat': datetime.datetime.utcnow(),
                       'loginip': regip,
                       'gravatar': mail})
  except errors.DuplicateKeyError:
    raise error.UserAlreadyExistError(uid, uname, mail) from None
Esempio n. 2
0
async def add(uid: int, uname: str, password: str, mail: str, regip: str='', realname: str=''):
  """Add a user."""
  validator.check_uname(uname)
  # TODO(iceboy): Filter uname by keywords.
  validator.check_password(password)
  validator.check_mail(mail)

  uname_lower = uname.strip().lower()
  mail_lower = mail.strip().lower()

  for user in builtin.USERS:
    if user['_id'] == uid or user['uname_lower'] == uname_lower or user['mail_lower'] == mail_lower:
      raise error.UserAlreadyExistError(uname)

  salt = pwhash.gen_salt()
  coll = db.coll('user')
  try:
    await coll.insert_one({'_id': uid,
                           'uname': uname,
                           'uname_lower': uname_lower,
                           'realname': realname,
                           'mail': mail,
                           'mail_lower': mail_lower,
                           'salt': salt,
                           'hash': pwhash.hash_vj4(password, salt),
                           'regat': datetime.datetime.utcnow(),
                           'regip': regip,
                           'priv': builtin.DEFAULT_PRIV,
                           'loginat': datetime.datetime.utcnow(),
                           'loginip': regip,
                           'gravatar': mail})
  except errors.DuplicateKeyError:
    raise error.UserAlreadyExistError(uid, uname, mail) from None