Exemple #1
0
def signup(name, password):
    """Signup with name and password. After creating this new user,
    we'll create a authToken by user_id and current timestamp,
    and return this authToken back to user.
    In the future, each request from user must contains authToken for us to fetch his user_id.

    Return:
    User Object + authToken.
    """
    if db_user.check_name_existance(name):
        raise errors.UserNameAlreadyUsed()
    new_user_id = -1
    final_create_success = False
    try:
        new_user_id = db_user.create_user(name, password)
        if new_user_id == -1:
            raise errors.DBError()
        user = db_user.get_user(new_user_id)
        user['encrypted_token'] = safetyutils.encrypt_auth_token(user['id'])
        final_create_success = True
        return user
    finally:
        if new_user_id != -1 and final_create_success is False:
            db_user.delete_user(new_user_id)
            raise errors.UserCreateFailure()
Exemple #2
0
def create_diary(user_id, uuid, title, content, time_created=None):
    """
    Raise: errors.DbCreateError
    """
    if user_id is None or uuid is None or title is None or content is None:
        raise errors.ArgumentShouldNotBeNull()

    _user = db_user.get_user(user_id)
    if _user is None:
        raise errors.InvalidUserIdDuringCreatingDiary()

    conn = base_db.get_conn(pymysql.cursors.DictCursor)
    if time_created is None:
        time_created = int(time.time())
    new_diary_id = -1
    try:
        with conn.cursor() as cursor:
            sql = "insert into `Diary` (`user_id`, `uuid`, `title`, `content`, `time_created`, `time_modified`) " \
                  "values (%s, %s, %s, %s, %s, %s)"
            cursor.execute(sql, (str(user_id), str(uuid), str(title), str(content), str(time_created), str(time_created)))
            new_diary_id = cursor.lastrowid
        conn.commit()
    except Exception as e:
        logger.exception(e)
        raise errors.DbCreateError()
    finally:
        conn.close()
        return new_diary_id
Exemple #3
0
def create_diary(user_id, uuid, title, content, time_created=None):
    """
    Raise: errors.DbCreateError
    """
    if user_id is None or uuid is None or title is None or content is None:
        raise errors.ArgumentShouldNotBeNull()

    _user = db_user.get_user(user_id)
    if _user is None:
        raise errors.InvalidUserIdDuringCreatingDiary()

    conn = base_db.get_conn(pymysql.cursors.DictCursor)
    if time_created is None:
        time_created = int(time.time())
    new_diary_id = -1
    try:
        with conn.cursor() as cursor:
            sql = "insert into `Diary` (`user_id`, `uuid`, `title`, `content`, `time_created`, `time_modified`) " \
                  "values (%s, %s, %s, %s, %s, %s)"
            cursor.execute(sql,
                           (str(user_id), str(uuid), str(title), str(content),
                            str(time_created), str(time_created)))
            new_diary_id = cursor.lastrowid
        conn.commit()
    except Exception as e:
        logger.exception(e)
        raise errors.DbCreateError()
    finally:
        conn.close()
        return new_diary_id
Exemple #4
0
def is_token_valid(encrypted_token):
    user_id = 0
    try:
        obj = safetyutils.decrypt_auth_token(encrypted_token)
        user_id = obj[0]
    except Exception as e:
        print e
        return False, user_id
    # if user is None or expire
    if db_user.get_user(user_id) is None or (time.time() - obj[1]) > conf.AUTH_TOKEN_EXPIRE_TIME:
        return False, user_id
    return True, user_id
Exemple #5
0
def is_token_valid(encrypted_token):
    user_id = 0
    try:
        obj = safetyutils.decrypt_auth_token(encrypted_token)
        user_id = obj[0]
    except Exception as e:
        print e
        return False, user_id
    # if user is None or expire
    if db_user.get_user(user_id) is None or (
            time.time() - obj[1]) > conf.AUTH_TOKEN_EXPIRE_TIME:
        return False, user_id
    return True, user_id
Exemple #6
0
def signup(email, password):
    """Signup with email and password. After creating this new user,
    we'll create a authToken by user_id and current timestamp,
    and return this authToken back to user.
    In the future, each request from user must contains authToken for us to fetch his user_id.

    Return:
    User Object + authToken.
    """
    new_user_id = -1
    final_create_success = False
    try:
        new_user_id = db_user.create_user(email, password)
        if new_user_id == -1:
            raise errors.DBError()
        user = db_user.get_user(new_user_id)
        user['encrypted_token'] = safetyutils.encrypt_auth_token(user['id'])
        final_create_success = True
        return user
    finally:
        if new_user_id != -1 and final_create_success is False:
            db_user.delete_user(new_user_id)
            raise errors.UserCreateFailure()
Exemple #7
0
def signup(name, password):
    """Signup with name and password. After creating this new user, 
	we'll create a authToken by user_id and current timestamp, and 
	return this authToken back to user. In the future, each request 
	from user must contains authToken for us to fetch his user_id.

	Return:
		User Object + authToken.
	"""
    new_user_id = -1
    final_create_success = False
    try:
        new_user_id = db_user.create_user(name, password)
        if new_user_id == -1:
            return {'rc': 1, 'msg': 'db error'}
        user = db_user.get_user(new_user_id)
        current_time = int(time.time())
        user['encrypted_token'] = safetyutils.encrypt_auth_token(user['id'])
        final_create_success = True
        return {'rc': 0, 'data': user}
    finally:
        if new_user_id != -1 and final_create_success == False:
            db_user.delete_user(new_user_id)
            return {'rc': 1, 'msg': 'create failure'}
Exemple #8
0
def get_user_by_id(user_id):
    return db_user.get_user(user_id)
Exemple #9
0
    if user is not None and 'id' in user:
        auth_token = create_auth_token(user['id'])
        user['encrypted_token'] = auth_token
        return True, user
    return False, None


def create_auth_token(user_id):
    if user_id is None:
        return None
    return safetyutils.encrypt_auth_token(user_id)


def get_user_by_id(user_id):
    return db_user.get_user(user_id)


def is_token_valid(encrypted_token):
    user_id = 0
    try:
        obj = safetyutils.decrypt_auth_token(encrypted_token)
        user_id = obj[0]
    except Exception, e:
        return False, user_id
    # if user is None or expire
    if db_user.get_user(user_id) is None or (time.time() - obj[1]) > conf.AUTH_TOKEN_EXPIRE_TIME:
        return False, user_id
    return True, user_id


Exemple #10
0
def get_user_by_id(user_id):
    return db_user.get_user(user_id)
Exemple #11
0
        new_user_id = db_user.create_user(name, password)
        if new_user_id == -1:
            return {'rc': 1, 'msg': 'db error'}
        user = db_user.get_user(new_user_id)
        current_time = int(time.time())
        user['encrypted_token'] = safetyutils.encrypt_auth_token(user['id'])
        final_create_success = True
        return {'rc': 0, 'data': user}
    finally:
        if new_user_id != -1 and final_create_success == False:
            db_user.delete_user(new_user_id)
            return {'rc': 1, 'msg': 'create failure'}


def get_user_by_id(user_id):
    return db_user.get_user(user_id)


def is_token_valid(encrypted_token):
    user_id = 0
    try:
        obj = safetyutils.decrypt_auth_token(encrypted_token)
        user_id = obj[0]
    except Exception, e:
        return False, user_id
    # if user is None or expire
    if db_user.get_user(user_id) is None or (
            time.time() - obj[1]) > conf.AUTH_TOKEN_EXPIRE_TIME:
        return False, user_id
    return True, user_id