Esempio n. 1
0
def create_user(full_name="",
                birthday=arrow.get(586162800),
                config=None,
                username=None,
                password=None,
                email_addr=None,
                user_id=None,
                verified=True):
    """ Creates a new user and profile, and returns the user ID. """
    if username is None:
        username = "******" + str(next(_user_index))

    while True:
        user = add_entity(
            users.Login(login_name=get_sysname(username),
                        last_login=arrow.get(0).datetime))

        if user.userid not in staff.MODS and user.userid not in staff.DEVELOPERS:
            break

        db = d.connect()
        db.delete(user)
        db.flush()

    add_entity(
        users.Profile(userid=user.userid,
                      username=username,
                      full_name=full_name,
                      created_at=arrow.get(0).datetime,
                      config=config))
    add_entity(users.UserInfo(userid=user.userid, birthday=birthday))
    # Verify this user
    if verified:
        d.engine.execute(
            "UPDATE login SET voucher = userid WHERE userid = %(id)s",
            id=user.userid)
    # Set a password for this user
    d.engine.execute("INSERT INTO authbcrypt VALUES (%(id)s, %(bcrypthash)s)",
                     id=user.userid,
                     bcrypthash=_DEFAULT_PASSWORD
                     if password is None else login.passhash(password))
    # Set an email address for this user
    if email_addr is not None:
        d.engine.execute(
            "UPDATE login SET email = %(email)s WHERE userid = %(id)s",
            email=email_addr,
            id=user.userid)
    # Force the userID to a user-defined value and return it
    if user_id is not None:
        d.engine.execute(
            "UPDATE login SET userid = %(newid)s WHERE userid = %(oldid)s",
            newid=user_id,
            oldid=user.userid)
        return user_id
    return user.userid
Esempio n. 2
0
def get_userids(usernames):
    sysnames = [get_sysname(username) for username in usernames]

    result = engine.execute(
        "SELECT login_name, userid FROM login WHERE login_name = ANY (%(names)s)"
        " UNION ALL SELECT alias_name, userid FROM useralias WHERE alias_name = ANY (%(names)s)"
        " UNION ALL SELECT login_name, userid FROM username_history WHERE active AND login_name = ANY (%(names)s)",
        names=[sysname for sysname in sysnames if sysname],
    )

    sysname_userid = dict(result.fetchall())

    return {username: sysname_userid.get(sysname, 0) for sysname, username in zip(sysnames, usernames)}
Esempio n. 3
0
def get_userids(usernames):
    ret = {}
    lookup_usernames = []
    sysnames = []

    for username in usernames:
        sysname = get_sysname(username)

        if sysname:
            lookup_usernames.append(username)
            sysnames.append(sysname)
        else:
            ret[username] = 0

    ret.update(zip(lookup_usernames, _get_userids(*sysnames)))

    return ret