/**
  * Checks access to the support_ticket preview page.
  *
  * @param \Drupal\Core\Session\AccountInterface $account
  *   The currently logged in account.
  * @param \Drupal\support_ticket\SupportTicketInterface $support_ticket_preview
  *   The support_ticket that is being previewed.
  *
  * @return string
  *   A \Drupal\Core\Access\AccessInterface constant value.
  */
 public function access(AccountInterface $account, SupportTicketInterface $support_ticket_preview)
 {
     if ($support_ticket_preview->isNew()) {
         $access_controller = $this->entityManager->getAccessControlHandler('support_ticket');
         return $access_controller->createAccess($support_ticket_preview->bundle(), $account, [], TRUE);
     } else {
         return $support_ticket_preview->access('update', $account, TRUE);
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $this->supportTicketStorage->deleteRevision($this->revision->getRevisionId());
     $this->logger('content')->notice('@type: deleted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     $support_ticket_type = $this->supportTicketTypeStorage->load($this->revision->bundle())->label();
     drupal_set_message(t('Revision from %revision-date of @type %title has been deleted.', array('%revision-date' => format_date($this->revision->getRevisionCreationTime()), '@type' => $support_ticket_type, '%title' => $this->revision->label())));
     $form_state->setRedirect('entity.support_ticket.canonical', array('support_ticket' => $this->revision->id()));
     if ($this->connection->query('SELECT COUNT(DISTINCT vid) FROM {support_ticket_field_revision} WHERE stid = :stid', array(':stid' => $this->revision->id()))->fetchField() > 1) {
         $form_state->setRedirect('entity.support_ticket.version_history', array('support_ticket' => $this->revision->id()));
     }
 }
 /**
  * {@inheritdoc}
  */
 public function submitForm(array &$form, FormStateInterface $form_state)
 {
     $revision = $this->prepareRevertedRevision($this->revision);
     // The revision timestamp will be updated when the revision is saved. Keep the
     // original one for the confirmation message.
     $original_revision_timestamp = $revision->getRevisionCreationTime();
     $revision->revision_log = t('Copy of the revision from %date.', array('%date' => format_date($original_revision_timestamp)));
     $revision->save();
     $this->logger('content')->notice('@type: reverted %title revision %revision.', array('@type' => $this->revision->bundle(), '%title' => $this->revision->label(), '%revision' => $this->revision->getRevisionId()));
     drupal_set_message(t('@type %title has been reverted to the revision from %revision-date.', array('@type' => support_ticket_get_type_label($this->revision), '%title' => $this->revision->label(), '%revision-date' => format_date($original_revision_timestamp))));
     $form_state->setRedirect('entity.support_ticket.version_history', array('support_ticket' => $this->revision->id()));
 }
/**
 * Controls access to a support ticket.
 *
 * Modules may implement this hook if they want to have a say in whether or not
 * a given user has access to perform a given operation on a support ticket.
 *
 * Note that not all modules will want to influence access on all support ticket
 * types. If your module does not want to explicitly allow or forbid access,
 * return an AccessResultInterface object with neither isAllowed() nor
 * isForbidden() equaling TRUE. Blindly returning an object with isForbidden()
 * equaling TRUE will break other support ticket access modules.
 *
 * @param \Drupal\support_ticket\SupportTicketInterface|string $support_ticket
 *   Either a support ticket entity or the machine name of the content type on
 *   which to perform the access check.
 * @param string $op
 *   The operation to be performed. Possible values:
 *   - "create"
 *   - "delete"
 *   - "update"
 *   - "view"
 * @param \Drupal\Core\Session\AccountInterface $account
 *   The user object to perform the access check operation on.
 *
 * @return \Drupal\Core\Access\AccessResultInterface
 *    The access result.
 *
 * @ingroup support_ticket_access
 */
function hook_support_ticket_access(\Drupal\support_ticket\SupportTicketInterface $support_ticket, $op, \Drupal\Core\Session\AccountInterface $account)
{
    $type = $support_ticket->bundle();
    switch ($op) {
        case 'create':
            return AccessResult::allowedIfHasPermission($account, 'create ' . $type . ' ticket');
        case 'update':
            if ($account->hasPermission('edit any ' . $type . ' ticket')) {
                return AccessResult::allowed()->cachePerPermissions();
            } else {
                return AccessResult::allowedIf($account->hasPermission('edit own ' . $type . ' ticket') && $account->id() == $support_ticket->getOwnerId())->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($support_ticket);
            }
        case 'delete':
            if ($account->hasPermission('delete any ' . $type . ' ticket')) {
                return AccessResult::allowed()->cachePerPermissions();
            } else {
                return AccessResult::allowedIf($account->hasPermission('delete own ' . $type . ' ticket') && $account->id() == $support_ticket->getOwnerId())->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($support_ticket);
            }
        default:
            // No opinion.
            return AccessResult::neutral();
    }
}
 /**
  * Checks support_ticket revision access.
  *
  * @param \Drupal\support_ticket\SupportTicketInterface $support_ticket
  *   The support_ticket to check.
  * @param \Drupal\Core\Session\AccountInterface $account
  *   A user object representing the user for whom the operation is to be
  *   performed.
  * @param string $op
  *   (optional) The specific operation being checked. Defaults to 'view.'
  *
  * @return bool
  *   TRUE if the operation may be performed, FALSE otherwise.
  */
 public function checkAccess(SupportTicketInterface $support_ticket, AccountInterface $account, $op = 'view')
 {
     $map = array('view' => 'view all revisions', 'update' => 'revert all revisions', 'delete' => 'delete all revisions');
     $bundle = $support_ticket->bundle();
     $type_map = array('view' => "view {$bundle} revisions", 'update' => "revert {$bundle} revisions", 'delete' => "delete {$bundle} revisions");
     if (!$support_ticket || !isset($map[$op]) || !isset($type_map[$op])) {
         // If there was no support_ticket to check against, or the $op was not one of
         // the supported ones, we return access denied.
         return FALSE;
     }
     // Statically cache access by revision ID, language code, user account ID,
     // and operation.
     $langcode = $support_ticket->language()->getId();
     $cid = $support_ticket->getRevisionId() . ':' . $langcode . ':' . $account->id() . ':' . $op;
     if (!isset($this->access[$cid])) {
         // Perform basic permission checks first.
         if (!$account->hasPermission($map[$op]) && !$account->hasPermission($type_map[$op]) && !$account->hasPermission('administer support tickets')) {
             $this->access[$cid] = FALSE;
             return FALSE;
         }
         // There should be at least two revisions. If the vid of the given
         // support_ticket and the vid of the default revision differ, then we already
         // have two different revisions so there is no need for a separate database
         // check. Also, if you try to revert to or delete the default revision, that's
         // not good.
         if ($support_ticket->isDefaultRevision() && ($this->supportTicketStorage->countDefaultLanguageRevisions($support_ticket) == 1 || $op == 'update' || $op == 'delete')) {
             $this->access[$cid] = FALSE;
         } elseif ($account->hasPermission('administer support tickets')) {
             $this->access[$cid] = TRUE;
         } else {
             // First check the access to the default revision and finally, if the
             // support_ticket passed in is not the default revision then access to that,
             // too.
             $this->access[$cid] = $this->supportTicketAccess->access($this->supportTicketStorage->load($support_ticket->id()), $op, $account) && ($support_ticket->isDefaultRevision() || $this->supportTicketAccess->access($support_ticket, $op, $account));
         }
     }
     return $this->access[$cid];
 }