Ejemplo n.º 1
0
async def change_user(username: str,
                      new_data: UserInput,
                      current_user: User = Depends(get_current_user),
                      session=Depends(get_db)):
    to_check = ["username", "email"]
    users = [
        user for user in get_local_resources(db=session, Resource=User).all()
    ]
    user = get_user(session,
                    username,
                    HTTP_404_NOT_FOUND,
                    err_msg="User '" + username + "' not found")
    if not user:
        raise exception
    for attr in to_check:  # Maybe put this in another function
        attr_check = [getattr(user_check, attr) for user_check in users]
        if getattr(new_data, attr) != getattr(user, attr) and getattr(
                new_data, attr) in attr_check:
            raise HTTPException(status_code=HTTP_409_CONFLICT,
                                detail=attr + " with value '" +
                                getattr(user, attr) + "' already exists")
    return add_local_resource(db=session,
                              Resource=User,
                              user_input=new_data,
                              r_id=user.id)
Ejemplo n.º 2
0
def authenticate_user(db, username: str, password: str, level: int = 0):
	user = get_local_resources(db= db, Resource= User, key_vals= { "username":username }).first()
	if not user:
		return False
	if not verify_password(user.password, password):
		return False
	return user
Ejemplo n.º 3
0
async def add_connection(new_conn: ConnectionInput,
                         session=Depends(get_db),
                         current_user: User = Depends(AuthUser(3))):
    exception = HTTPException(status_code=HTTP_409_CONFLICT,
                              detail="This connection already exists")
    #Must test connection here
    key_vals = {
        attr: getattr(new_conn, attr)
        for attr in ["hostname", "port", "type"]
    }
    connection = get_local_resources(db=session,
                                     Resource=Connection,
                                     key_vals=key_vals).first()
    if connection:
        raise exception
    new_connection = Connection(
        **new_conn.dict()
    )  # This object only wastes memory, should there be a way to send the object over 'add_local_resource' as 'user_input'
    test_connection(new_connection)
    added_conn = add_local_resource(db=session,
                                    Resource=Connection,
                                    user_input=new_conn)
    added_conn.setup()
    # Adding relationship parameters here - should change add_local_resource to include relationships
    added_conn.users.append(
        current_user
    )  # Currrently doesn't work because there is no instance of the User class representing the logged user
    session.commit()
    return added_conn
Ejemplo n.º 4
0
def get_user(db, username: str, status_code: int, err_msg: str = "", find= True):
	user = get_local_resources(db=db, Resource= User, key_vals= { "username":username }).first()
	exception = HTTPException(
		status_code=status_code,
		detail=err_msg,
	)
	if find:
		if not user:
			raise exception
		return user
	else:
		if user:
			raise exception
		return None
Ejemplo n.º 5
0
def get_connection(db, conn_id: int, status_code: int ,err_msg: str = "", find= True):
	connection = get_local_resources(db=db, Resource= Connection, r_id=conn_id)
	exception = HTTPException(
		status_code=status_code,
		detail=err_msg
	)
	if find:
		if not connection:
			raise exception
		return connection
	else:
		if connection:
			raise exception
		return None
Ejemplo n.º 6
0
async def get_current_user(token: str = Depends(oauth2_scheme), session= Depends(get_db)):
	credentials_exception = HTTPException(
		status_code=HTTP_401_UNAUTHORIZED,
		detail="Could not validate credentials",
		headers={"WWW-Authenticate": "Bearer"},
	)
	try:
		payload = jwt.decode(token, get_secret_key(), algorithms=[ALGORITHM])
		username: str = payload.get("sub")
		if username is None:
			raise credentials_exception
		token_data = TokenData(username=username)
	except PyJWTError as e:
		if type(e) == jwt.exceptions.ExpiredSignatureError:
			credentials_exception.detail = "Token expired"
		raise credentials_exception
	# with session_open(local_engine) as session:
	user = get_local_resources(db=session, Resource= User, key_vals= { "username":username }).first()
	if user is None:
		raise credentials_exception
	return user
Ejemplo n.º 7
0
async def change_connection(conn_id: int,
                            new_data: ConnectionInput,
                            session=Depends(get_db)):
    connection = get_connection(session,
                                conn_id,
                                HTTP_404_NOT_FOUND,
                                err_msg="Connection with id='" + str(conn_id) +
                                "' not found")
    connection_dict = connection.dict()
    exception = HTTPException(
        status_code=HTTP_409_CONFLICT,
        detail="Attempting to change to already existing connection")
    if not connection:
        raise exception
    for attr, val in new_data.dict().items():
        if val:
            connection_dict[attr] = val
    new_connection = Connection(**connection_dict)
    key_vals = {
        attr: getattr(new_connection, attr)
        for attr in ["hostname", "port", "type"]
    }
    new_connection_query = get_local_resources(db=session,
                                               Resource=Connection,
                                               key_vals=key_vals).first()
    if new_connection_query:
        raise exception
    new_connection_test = new_connection.test()
    if not new_connection_test[0]:
        exception.status_code = HTTP_503_SERVICE_UNAVAILABLE
        exception.detail = new_connection_test[1]
        raise exception
    return add_local_resource(db=session,
                              Resource=Connection,
                              user_input=new_data,
                              r_id=connection.id)
Ejemplo n.º 8
0
async def get_users(session=Depends(get_db)):
    return [
        user.username
        for user in get_local_resources(db=session, Resource=User).all()
    ]
Ejemplo n.º 9
0
async def get_connections(session=Depends(get_db)):
    return [
        connection for connection in get_local_resources(
            db=session, Resource=Connection).all()
    ]