"""PDP authorization utilities.""" from ddtrace import tracer from flask import g from python_pdp_sdk.resource_getters.base import ForwardKwargsGetter from abacus_schedule.config import authorization_backend from abacus_schedule.constants import error @tracer.wrap() def pdp_authorize_resource( resource_id: int, resource_type: str, action: str = 'view', ) -> bool: """Authorize a resource with empty attributes.""" authorized = authorization_backend.is_authorized( action=action, resource_id=resource_id, resource_type=resource_type, resource_getter=ForwardKwargsGetter(), ) if not authorized: g.log.warn( error.ERROR_MESSAGE_UNAUTHORIZED_RESOURCE, resources={ 'identity_id': g.request_context.jwt_identity_id, 'resource_id': resource_id, 'resource_type': resource_type, 'auth_response': authorized, }, ) return False return True