/**
  * Checks if a downloadAuthorizeCallback was configured and calls it.
  * Will throw an UnauthorizedException if the callback returns false.
  *
  * @param Attachment $attachment Attachment Entity
  * @return void
  * @throws UnauthorizedException
  */
 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'));
             }
         }
     }
 }