Skip to content

Commit fc12afb

Browse files
authored
[#106] Add and deprecate the ApiDocAccessControlHandler (#108)
* Add and deprecate the ApiDocAccessControlHandler * Fix phpcs warning
1 parent 99d06ee commit fc12afb

File tree

1 file changed

+161
-0
lines changed

1 file changed

+161
-0
lines changed
Lines changed: 161 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,161 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2019 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* version 2 as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
namespace Drupal\apigee_api_catalog\Entity\Access;
22+
23+
use Drupal\apigee_api_catalog\Entity\ApiDocInterface;
24+
use Drupal\Core\Entity\EntityAccessControlHandler;
25+
use Drupal\Core\Entity\EntityHandlerInterface;
26+
use Drupal\Core\Entity\EntityInterface;
27+
use Drupal\Core\Entity\EntityTypeInterface;
28+
use Drupal\Core\Entity\EntityTypeManagerInterface;
29+
use Drupal\Core\Session\AccountInterface;
30+
use Drupal\Core\Access\AccessResult;
31+
use Symfony\Component\DependencyInjection\ContainerInterface;
32+
33+
/**
34+
* Access controller for the API Doc entity.
35+
*
36+
* @deprecated in 2.x and is removed from 3.x. Use the node "apidoc" bundle instead.
37+
* @see https://github.com/apigee/apigee-api-catalog-drupal/pull/84
38+
*
39+
* @see \Drupal\apigee_api_catalog\Entity\ApiDoc.
40+
*/
41+
class ApiDocAccessControlHandler extends EntityAccessControlHandler implements EntityHandlerInterface {
42+
43+
/**
44+
* The entity type manager.
45+
*
46+
* @var \Drupal\Core\Entity\EntityTypeManagerInterface
47+
*/
48+
protected $entityTypeManager;
49+
50+
/**
51+
* Constructs an access control handler instance.
52+
*
53+
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
54+
* The entity type definition.
55+
* @param \Drupal\Core\Entity\EntityTypeManagerInterface $entityTypeManager
56+
* The entity type manager.
57+
*/
58+
public function __construct(EntityTypeInterface $entity_type, EntityTypeManagerInterface $entityTypeManager) {
59+
parent::__construct($entity_type);
60+
$this->entityTypeManager = $entityTypeManager;
61+
}
62+
63+
/**
64+
* {@inheritdoc}
65+
*/
66+
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
67+
return new static(
68+
$entity_type,
69+
$container->get('entity_type.manager')
70+
);
71+
}
72+
73+
/**
74+
* {@inheritdoc}
75+
*/
76+
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
77+
/** @var \Drupal\apigee_api_catalog\Entity\ApiDocInterface $entity */
78+
$access = parent::checkAccess($entity, $operation, $account);
79+
80+
// Access control for revisions.
81+
if (!$entity->isDefaultRevision()) {
82+
return $this->checkAccessRevisions($entity, $operation, $account);
83+
}
84+
85+
switch ($operation) {
86+
case 'view':
87+
return $access->orIf($entity->isPublished()
88+
? AccessResult::allowedIfHasPermission($account, 'view published apidoc entities')
89+
: AccessResult::allowedIfHasPermission($account, 'view unpublished apidoc entities')
90+
);
91+
92+
case 'reimport':
93+
return AccessResult::allowedIf($entity->spec_file_source->value === ApiDocInterface::SPEC_AS_URL)
94+
->andIf($entity->access('update', $account, TRUE));
95+
96+
case 'update':
97+
return $access->orIf(AccessResult::allowedIfHasPermission($account, 'edit apidoc entities'));
98+
99+
case 'delete':
100+
return $access->orIf(AccessResult::allowedIfHasPermission($account, 'delete apidoc entities'));
101+
}
102+
103+
// Unknown operation, no opinion.
104+
return $access;
105+
}
106+
107+
/**
108+
* Additional access control for revisions.
109+
*
110+
* @param \Drupal\Core\Entity\EntityInterface $entity
111+
* The entity for which to check access.
112+
* @param string $operation
113+
* The entity operation.
114+
* @param \Drupal\Core\Session\AccountInterface $account
115+
* The user for which to check access.
116+
*
117+
* @return \Drupal\Core\Access\AccessResultInterface
118+
* The access result.
119+
*/
120+
protected function checkAccessRevisions(EntityInterface $entity, $operation, AccountInterface $account) {
121+
/** @var \Drupal\Core\Entity\EntityStorageInterface $entity_storage */
122+
$entity_storage = $this->entityTypeManager->getStorage($this->entityTypeId);
123+
124+
// Must have access to the same operation on the default revision.
125+
$default_revision = $entity_storage->load($entity->id());
126+
$has_default_entity_rev_access = $default_revision->access($operation, $account);
127+
if (!$has_default_entity_rev_access) {
128+
return AccessResult::forbidden();
129+
}
130+
131+
$map = [
132+
'view' => "view apidoc revisions",
133+
'update' => "revert apidoc revisions",
134+
];
135+
136+
if (!$entity || !isset($map[$operation])) {
137+
// If there was no entity to check against, or the $op was not one of the
138+
// supported ones, we return access denied.
139+
return AccessResult::forbidden();
140+
}
141+
142+
$admin_permission = $this->entityType->getAdminPermission();
143+
144+
// Perform basic permission checks first.
145+
if ($account->hasPermission($map[$operation]) ||
146+
($admin_permission && $account->hasPermission($admin_permission))) {
147+
return AccessResult::allowed();
148+
}
149+
150+
return AccessResult::forbidden();
151+
}
152+
153+
/**
154+
* {@inheritdoc}
155+
*/
156+
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
157+
return AccessResult::allowedIfHasPermission($account, 'add apidoc entities')
158+
->orIf(AccessResult::allowedIfHasPermission($account, 'administer apigee api catalog'));
159+
}
160+
161+
}

0 commit comments

Comments
 (0)