You are here

public function UpdateThreadAccess::access in Opigno messaging 3.x

Checks the access.

Parameters

\Symfony\Component\Routing\Route $route: The route to check the access to.

\Drupal\Core\Session\AccountInterface $account: The currently logged in user's account.

\Drupal\private_message\Entity\PrivateMessageThreadInterface $private_message_thread: The thread to check the access to.

Return value

\Drupal\Core\Access\AccessResultInterface The access result.

File

src/Access/UpdateThreadAccess.php, line 32

Class

UpdateThreadAccess
Check if the user can edit the given PM thread.

Namespace

Drupal\opigno_messaging\Access

Code

public function access(Route $route, AccountInterface $account, PrivateMessageThreadInterface $private_message_thread) : AccessResultInterface {
  if (!$private_message_thread
    ->hasField('field_author')) {
    return AccessResult::forbidden();
  }

  // Only group discussions can be edited be the author.
  $owner = (int) $private_message_thread
    ->get('field_author')
    ->getString();
  $is_group = count($private_message_thread
    ->getMembers()) > 2;
  return AccessResult::allowedIf($is_group && $owner === (int) $account
    ->id());
}