protected function _checkAuthorization(Attachment $attachment)
    {
        if ($attachmentsBehavior = $attachment->getRelatedTable()->behaviors()->get('Attachments')) {
            $behaviorConfig = $attachmentsBehavior->config();
            if (is_callable($behaviorConfig['downloadAuthorizeCallback'])) {
                $relatedEntity = $attachment->getRelatedEntity();
                $authorized = $behaviorConfig['downloadAuthorizeCallback']($attachment, $relatedEntity, $this->request);
                if ($authorized !== true) {
                    throw new UnauthorizedException(__d('attachments', 'attachments.unauthorized_for_attachment'));
                }
            }
        }
    }