You are here

protected function MailhandlerComment::validateUser in Mailhandler 8

Checks if the user is authenticated and authorized to post comments.

Parameters

\Drupal\inmail\DefaultAnalyzerResult $result: The analyzer result.

Return value

\Drupal\Core\Session\AccountInterface The identified account.

Throws

\Exception Throws an exception in case user is not validated.

1 call to MailhandlerComment::validateUser()
MailhandlerComment::createComment in mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php
Creates a new comment from given mail message.

File

mailhandler_comment/src/Plugin/inmail/Handler/MailhandlerComment.php, line 144

Class

MailhandlerComment
Message handler that supports posting comments via email.

Namespace

Drupal\mailhandler_comment\Plugin\inmail\Handler

Code

protected function validateUser(DefaultAnalyzerResult $result) {

  // Do not allow unverified PGP-signed messages.
  if ($result
    ->hasContext('verified') && !$result
    ->getContext('verified')
    ->getContextValue()) {
    throw new \Exception('Failed to process the message. PGP-signed message is not verified.');
  }

  // Get the current user.
  $account = \Drupal::currentUser()
    ->getAccount();

  // Authorize a user.
  $access = $this->entityTypeManager
    ->getAccessControlHandler('comment')
    ->createAccess('comment', $account, [], TRUE);
  if (!$access
    ->isAllowed()) {
    throw new \Exception('Failed to process the message. User is not authorized to post comments.');
  }
  return $account;
}