async def update_user( request: Request, user_id: str, data: UserRequestAPI, user: Optional[UserAPI] = Depends(get_user)) -> UserAPI: """Update user data""" if user is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) projects: List[Project] = [ Project.get(id=project['id']) for project in data.projects ] context: Dict[Any] = { "username": data.username, "is_active": data.is_active, "projects": projects } if data.check_password: if not pwd_verify(data.check_password, user.password if user else ""): request.session[ "fail_password_message"]: str = f"Invalid password!" raise HTTPException( status_code=status.HTTP_400_BAD_REQUEST, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) if data.new_password: context["password"] = pwd_hash(data.new_password) return UserAPI.parse_obj(User.update(context, id=user_id).to_dict())
async def get_containers( project_slug: str, channel_slug: str, user: Optional[UserAPI] = Depends(get_user), token: Optional[APIKeyModel] = Depends(get_api_key), ) -> List[ContainerAPI]: # Authentication check if user is None and token is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) project: ProjectAPI = ProjectAPI.parse_obj( Project.get(slug=project_slug).to_dict()) channel: ChannelAPI = ChannelAPI.parse_obj( Channel.get(slug=channel_slug, project_id=project.id).to_dict()) if token and token.project_id and token.project_id != project.id: raise HTTPException(status.HTTP_403_FORBIDDEN) containers: Query = Container.get_all(project_id=project.id, channel_id=channel.id) return [ContainerAPI.parse_obj(item.to_dict()) for item in containers]
def get_projects(user: Optional[UserAPI] = Depends( get_user)) -> List[ProjectAPI]: # Authentication check if user is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) if user.role == UserRole.super: # Getting all existing projects projects: List[ProjectAPI] = [ ProjectAPI.parse_obj(project.to_dict()) for project in Project.get_all() ] else: # Getting projects available to the user projects: List[ProjectAPI] = [ ProjectAPI.parse_obj(project.to_dict()) for project in user.projects ] for project in projects: project.channels = [ ChannelAPI.parse_obj(channel.to_dict()) for channel in Channel.get_all(project_id=project.id) ] return projects
async def set_container( project_slug: str, channel_slug: str, request: Request, user: Optional[UserAPI] = Depends(get_user), token: Optional[APIKeyModel] = Depends(get_api_key), ) -> ContainerAPI: container: ContainerAPI = ContainerAPI.parse_obj( yaml.safe_load(await request.body())) try: project: ProjectAPI = ProjectAPI.parse_obj( Project.get(slug=project_slug).to_dict()) channel: ChannelAPI = ChannelAPI.parse_obj( Channel.get(slug=channel_slug, project_id=project.id).to_dict()) except AttributeError: raise HTTPException(status.HTTP_404_NOT_FOUND) # Authentication check if user is None and token is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) # Checking access rights elif (token and (not token.write or (token.project_id and token.project_id != project.id)) or (user and user.role != UserRole.super)): raise HTTPException(status.HTTP_403_FORBIDDEN) container.project_id = project.id container.channel_id = channel.id container.updated_time = time_utc_now() # prepare data data: Dict = { **container.dict(exclude={ "id", "auth", "slug", "project_id", "channel_id" }), **dict( auth=json.dumps(container.auth), slug=container.slug, project_id=project.id, channel_id=channel.id, ), } # create of get container record record = Container.update_or_create(data, slug=container.slug, project_id=project.id, channel_id=channel.id) # update or create container and then return container api return ContainerAPI.parse_obj(record.to_dict())
def create_project( data: ProjectAPI, user: Optional[UserAPI] = Depends(get_user)) -> ProjectAPI: # Authentication check if user is None or user.role != UserRole.super: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) return ProjectAPI.parse_obj( Project.create(data.dict(exclude_defaults=True)).to_dict())
async def create_user( request: Request, data: UserAPI, user: Optional[UserAPI] = Depends(get_user)) -> Any: if user is None or user.role != UserRole.super: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) # check if such username exists if User.get(username=data.username): request.session["username_exists"] = f"Such Username already exists!" raise HTTPException(status_code=status.HTTP_400_BAD_REQUEST) data.password = pwd_hash(data.password) data.projects = [ Project.get(id=project_id) for project_id in data.projects ] return UserAPI.parse_obj(User.create(data.dict()).to_dict())
def delete_project( project_id: int, user: Optional[UserAPI] = Depends(get_user)) -> ProjectAPI: # Authentication check if user is None or user.role != UserRole.super: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) # deleting a project project: ProjectAPI = ProjectAPI.parse_obj( Project.delete(id=project_id).to_dict()) # deleting all related channels project.channels = [ ChannelAPI.parse_obj(channel.to_dict()) for channel in Channel.delete_all(project_id=project_id) ] # deleting all related containers Container.delete_all(project_id=project_id) return project
def get_channels(user: Optional[UserAPI] = Depends( get_user)) -> List[ChannelAPI]: # Authentication check if user is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) # Getting all existing projects channels: List[ChannelAPI] = [ ChannelAPI.parse_obj(channel.to_dict()) for channel in Channel.get_all() ] if user.role != UserRole.super: # Filter projects available to the user channels: List[ChannelAPI] = [ c for c in channels if Project.get(id=c.project_id) in user.projects ] return channels
async def get_all_containers( user: Optional[UserAPI] = Depends(get_user), token: Optional[APIKeyModel] = Depends(get_api_key), ) -> List[ContainerAPI]: # Authentication check if user is None and token is None: raise HTTPException( status_code=status.HTTP_401_UNAUTHORIZED, detail="Could not validate credentials", headers={"WWW-Authenticate": "Token"}, ) # Getting all existing projects containers: List[ContainerAPI] = [ ContainerAPI.parse_obj(item.to_dict()) for item in Container.get_all() ] if user.role != UserRole.super: # Filter projects available to the user containers: List[ContainerAPI] = [ c for c in containers if Project.get(id=c.project_id) in user.projects ] return containers