"""Access control logic.""" from typing import Any from flask import request from owsrequest import access from video.constants import error, header, product as product_constants from video.exceptions import AccessForbidden, ArtistOwnershipError, InvalidRequest from video.models.sql import queries from video.models.sql.classes import release def _is_video_product(product: dict[str, Any]) -> bool: """Check if product is a video product.""" return bool( product["distribution_format_id"] == product_constants.MUSIC_VIDEO_DISTRIBUTION_FORMAT_ID ) def check_access_to_product(product_id: int) -> None: """Check if the user in the request context has access to a product. Args: product_id (int): product_id of product to check access to. """ product = release.get_by_id_with_vendor(product_id) if not _is_video_product(product): raise InvalidRequest( "Not a video product.", error_code=error.ERROR_NOT_VIDEO_PRODUCT ) vendor_id = str(product["vendor_id"]) subaccount_id = str(product["subaccount_id"]) verify_grass_access_response = access.verify_grass_access( request.headers.get(header.GRASS_ACCOUNT_TYPE), request.headers.get(header.GRASS_ACCOUNT_ID), False, vendor=vendor_id, subaccount=subaccount_id, ) if not verify_grass_access_response: raise ArtistOwnershipError(error.ERROR_MESSAGE_FORBIDDEN_USER) def check_access_to_job(job_id: int) -> None: """Check if the user in the request context has access to a job. Args: job_id (int): job_id of job to check access to. """ context = queries.get_context(job_id=job_id) if not context: raise InvalidRequest( error.ERROR_MESSAGE_INVALID_USAGE, error_code=error.ERROR_CODE_INVALID_USAGE, ) verify_grass_access_response = access.verify_grass_access( request.headers.get(header.GRASS_ACCOUNT_TYPE), request.headers.get(header.GRASS_ACCOUNT_ID), False, vendor=context.get("vendor_id"), subaccount=context.get("subaccount_id"), ) if not verify_grass_access_response: raise AccessForbidden(error.ERROR_MESSAGE_FORBIDDEN_USER)