from owsrequest import flask_request from artist.connectors import ows_permissions from artist.schema import LabelProfile from artist.utils import handler_util from artist.models import account def get_and_verify_vendor_id_from_grass_headers(request) -> int | None: """ Get the request's vendor id from the Grass request headers, and verify that the account has access to the vendor. Return: int: the vendor id if found and accessible from grass headers None: if not found or not accessible """ account_type, account_id = flask_request.get_grass_headers(request) artist_data = request.get_json() subaccount_id = handler_util.extract_subaccount_id( subaccount_id=artist_data.get('subaccount_id'), account_type=account_type, account_id=account_id ) vendor_id = handler_util.extract_vendor_id( vendor_id=artist_data.get('vendor_id'), account_type=account_type, account_id=account_id ) access_response = flask_request.verify_grass_access( request, required=False, subaccount=subaccount_id, vendor=vendor_id) if not access_response: return None if vendor_id: return vendor_id if subaccount_id: # Look up the vendor id for this subaccount account_response = account.get_vendor_id_for_subaccount_id( subaccount_id) if not account_response: return None subaccount_vendor_id = account_response.message # If we were given an explicit vendor id, make sure it matches the one # that this subaccount actually belongs to. if vendor_id and vendor_id != subaccount_vendor_id: return None return subaccount_vendor_id return None def get_and_verify_vendor_id_from_profile_headers(request_context: LabelProfile) -> int | None: """ Get the request's vendor id from the label profile, and verify that the account has access to the vendor. Return: int: the vendor id if found and accessible from profile headers None: if not found or not accessible """ if request_context.profile_type != 'LabelProfile': return None vendor_id = ows_permissions.get_single_vendor_id_for_label_profile(request_context) if vendor_id: return vendor_id subaccount_ids = ows_permissions.get_subaccount_ids_for_label_profile(request_context) subaccount_id = subaccount_ids[0] if subaccount_ids else None if subaccount_id: # Look up the vendor id for this subaccount account_response = account.get_vendor_id_for_subaccount_id( subaccount_id) if not account_response: return None return account_response.message