@@ -28,17 +28,49 @@ class {{ entity_class }}AccessControlHandler extends EntityAccessControlHandler
2828 */
2929 protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
3030 /** @var \Drupal\{{ module }}\Entity\{{ entity_class }}Interface $entity */
31+
3132 switch ($operation) {
33+
3234 case 'view':
35+
3336 if (!$entity->isPublished()) {
37+ {% if has_bundle_permissions %}
38+ $permission = $this->checkOwn($entity, 'view unpublished', $account);
39+ if (!empty($permission)) {
40+ return AccessResult::allowed();
41+ }
42+
43+ {% endif %}
3444 return AccessResult::allowedIfHasPermission($account, 'view unpublished {{ label | lower }} entities');
3545 }
46+
47+ {% if has_bundle_permissions %}
48+ $permission = $this->checkOwn($entity, $operation, $account);
49+ if (!empty($permission)) {
50+ return AccessResult::allowed();
51+ }
52+ {% endif %}
53+
3654 return AccessResult::allowedIfHasPermission($account, 'view published {{ label | lower }} entities');
3755
3856 case 'update':
57+
58+ {% if has_bundle_permissions %}
59+ $permission = $this->checkOwn($entity, $operation, $account);
60+ if (!empty($permission)) {
61+ return AccessResult::allowed();
62+ }
63+ {% endif %}
3964 return AccessResult::allowedIfHasPermission($account, 'edit {{ label | lower }} entities');
4065
4166 case 'delete':
67+
68+ {% if has_bundle_permissions %}
69+ $permission = $this->checkOwn($entity, $operation, $account);
70+ if (!empty($permission)) {
71+ return AccessResult::allowed();
72+ }
73+ {% endif %}
4274 return AccessResult::allowedIfHasPermission($account, 'delete {{ label | lower }} entities');
4375 }
4476
@@ -52,4 +84,51 @@ class {{ entity_class }}AccessControlHandler extends EntityAccessControlHandler
5284 protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
5385 return AccessResult::allowedIfHasPermission($account, 'add {{ label | lower }} entities');
5486 }
87+
88+ {% if has_bundle_permissions %}
89+ /**
90+ * Test for given 'own' permission.
91+ *
92+ * @param \Drupal\Core\Entity\EntityInterface $entity
93+ * @param $operation
94+ * @param \Drupal\Core\Session\AccountInterface $account
95+ *
96+ * @return string|null
97+ * The permission string indicating it's allowed.
98+ */
99+ protected function checkOwn(EntityInterface $entity, $operation, AccountInterface $account) {
100+ $status = $entity->isPublished();
101+ $uid = $entity->getOwnerId();
102+
103+ $is_own = $account->isAuthenticated() && $account->id() == $uid;
104+ if (!$is_own) {
105+ return;
106+ }
107+
108+ $bundle = $entity->bundle();
109+
110+ $ops = [
111+ 'create' => '%bundle add own %bundle entities',
112+ 'view unpublished' => '%bundle view own unpublished %bundle entities',
113+ 'view' => '%bundle view own entities',
114+ 'update' => '%bundle edit own entities',
115+ 'delete' => '%bundle delete own entities',
116+ ];
117+ $permission = strtr($ops[$operation], ['%bundle' => $bundle]);
118+
119+ if ($operation === 'view unpublished') {
120+ if (!$status && $account->hasPermission($permission)) {
121+ return $permission;
122+ }
123+ else {
124+ return NULL;
125+ }
126+ }
127+ if ($account->hasPermission($permission)) {
128+ return $permission;
129+ }
130+
131+ return NULL;
132+ }
133+ {% endif %}
55134{% endblock %}
0 commit comments