diff --git a/studio/__main_unit__.py b/studio/__main_unit__.py index e6671fdbc..04485b5a8 100644 --- a/studio/__main_unit__.py +++ b/studio/__main_unit__.py @@ -17,6 +17,7 @@ run, users_admin, users_me, + users_search, workspace, ) from studio.app.dir_path import DIRPATH @@ -36,6 +37,7 @@ app.include_router(run.router) app.include_router(users_admin.router) app.include_router(users_me.router) +app.include_router(users_search.router) app.include_router(workspace.router) # optinist routers diff --git a/studio/app/common/routers/users_search.py b/studio/app/common/routers/users_search.py new file mode 100644 index 000000000..f98231ee0 --- /dev/null +++ b/studio/app/common/routers/users_search.py @@ -0,0 +1,44 @@ +from typing import List + +from fastapi import APIRouter, Depends, Query +from sqlmodel import Session, or_ + +from studio.app.common.core.auth.auth_dependencies import get_current_user +from studio.app.common.db.database import get_db +from studio.app.common.models import User as UserModel +from studio.app.common.schemas.users import User, UserInfo + +router = APIRouter(prefix="/users/search", tags=["users/search"]) + + +@router.get( + "/share_users", + response_model=List[UserInfo], + description=""" +- Get a list of users with whom content is shared. +- Note: Maximum of 10 responses. (security considerations) +""", +) +def search_share_users( + keyword: str = Query(description="partial match (user.name or user.email)"), + db: Session = Depends(get_db), + current_user: User = Depends(get_current_user), +): + MAX_RESPONSE_COUNT = 10 + users = ( + db.query(UserModel) + .filter( + UserModel.active.is_(True), + UserModel.organization_id == current_user.organization_id, + ) + .filter( + or_( + UserModel.name.like("%{0}%".format(keyword)), + UserModel.email.like("%{0}%".format(keyword)), + ) + ) + .order_by(UserModel.id) + .limit(MAX_RESPONSE_COUNT) + .all() + ) + return users