Mercurial > hg > cmmr2012-drupal-site
comparison core/modules/content_moderation/src/Access/LatestRevisionCheck.php @ 0:c75dbcec494b
Initial commit from drush-created site
author | Chris Cannam |
---|---|
date | Thu, 05 Jul 2018 14:24:15 +0000 |
parents | |
children | a9cd425dd02b |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c75dbcec494b |
---|---|
1 <?php | |
2 | |
3 namespace Drupal\content_moderation\Access; | |
4 | |
5 use Drupal\Core\Access\AccessException; | |
6 use Drupal\Core\Access\AccessResult; | |
7 use Drupal\Core\Entity\EntityInterface; | |
8 use Drupal\Core\Routing\Access\AccessInterface; | |
9 use Drupal\Core\Routing\RouteMatchInterface; | |
10 use Drupal\content_moderation\ModerationInformationInterface; | |
11 use Drupal\Core\Session\AccountInterface; | |
12 use Drupal\user\EntityOwnerInterface; | |
13 use Symfony\Component\Routing\Route; | |
14 | |
15 /** | |
16 * Access check for the entity moderation tab. | |
17 */ | |
18 class LatestRevisionCheck implements AccessInterface { | |
19 | |
20 /** | |
21 * The moderation information service. | |
22 * | |
23 * @var \Drupal\content_moderation\ModerationInformationInterface | |
24 */ | |
25 protected $moderationInfo; | |
26 | |
27 /** | |
28 * Constructs a new LatestRevisionCheck. | |
29 * | |
30 * @param \Drupal\content_moderation\ModerationInformationInterface $moderation_information | |
31 * The moderation information service. | |
32 */ | |
33 public function __construct(ModerationInformationInterface $moderation_information) { | |
34 $this->moderationInfo = $moderation_information; | |
35 } | |
36 | |
37 /** | |
38 * Checks that there is a pending revision available. | |
39 * | |
40 * This checker assumes the presence of an '_entity_access' requirement key | |
41 * in the same form as used by EntityAccessCheck. | |
42 * | |
43 * @param \Symfony\Component\Routing\Route $route | |
44 * The route to check against. | |
45 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match | |
46 * The parametrized route. | |
47 * @param \Drupal\Core\Session\AccountInterface $account | |
48 * The current user account. | |
49 * | |
50 * @return \Drupal\Core\Access\AccessResultInterface | |
51 * The access result. | |
52 * | |
53 * @see \Drupal\Core\Entity\EntityAccessCheck | |
54 */ | |
55 public function access(Route $route, RouteMatchInterface $route_match, AccountInterface $account) { | |
56 // This tab should not show up unless there's a reason to show it. | |
57 $entity = $this->loadEntity($route, $route_match); | |
58 if ($this->moderationInfo->hasPendingRevision($entity)) { | |
59 // Check the global permissions first. | |
60 $access_result = AccessResult::allowedIfHasPermissions($account, ['view latest version', 'view any unpublished content']); | |
61 if (!$access_result->isAllowed()) { | |
62 // Check entity owner access. | |
63 $owner_access = AccessResult::allowedIfHasPermissions($account, ['view latest version', 'view own unpublished content']); | |
64 $owner_access = $owner_access->andIf((AccessResult::allowedIf($entity instanceof EntityOwnerInterface && ($entity->getOwnerId() == $account->id())))); | |
65 $access_result = $access_result->orIf($owner_access); | |
66 } | |
67 | |
68 return $access_result->addCacheableDependency($entity); | |
69 } | |
70 | |
71 return AccessResult::forbidden()->addCacheableDependency($entity); | |
72 } | |
73 | |
74 /** | |
75 * Returns the default revision of the entity this route is for. | |
76 * | |
77 * @param \Symfony\Component\Routing\Route $route | |
78 * The route to check against. | |
79 * @param \Drupal\Core\Routing\RouteMatchInterface $route_match | |
80 * The parametrized route. | |
81 * | |
82 * @return \Drupal\Core\Entity\ContentEntityInterface | |
83 * returns the Entity in question. | |
84 * | |
85 * @throws \Drupal\Core\Access\AccessException | |
86 * An AccessException is thrown if the entity couldn't be loaded. | |
87 */ | |
88 protected function loadEntity(Route $route, RouteMatchInterface $route_match) { | |
89 $entity_type = $route->getOption('_content_moderation_entity_type'); | |
90 | |
91 if ($entity = $route_match->getParameter($entity_type)) { | |
92 if ($entity instanceof EntityInterface) { | |
93 return $entity; | |
94 } | |
95 } | |
96 throw new AccessException(sprintf('%s is not a valid entity route. The LatestRevisionCheck access checker may only be used with a route that has a single entity parameter.', $route_match->getRouteName())); | |
97 } | |
98 | |
99 } |