@@ -28,17 +28,49 @@ class {{ entity_class }}AccessControlHandler extends EntityAccessControlHandler
28
28
*/
29
29
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
30
30
/** @var \Drupal\{{ module }}\Entity\{{ entity_class }}Interface $entity */
31
+
31
32
switch ($operation) {
33
+
32
34
case 'view':
35
+
33
36
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 %}
34
44
return AccessResult::allowedIfHasPermission($account, 'view unpublished {{ label | lower }} entities');
35
45
}
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
+
36
54
return AccessResult::allowedIfHasPermission($account, 'view published {{ label | lower }} entities');
37
55
38
56
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 %}
39
64
return AccessResult::allowedIfHasPermission($account, 'edit {{ label | lower }} entities');
40
65
41
66
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 %}
42
74
return AccessResult::allowedIfHasPermission($account, 'delete {{ label | lower }} entities');
43
75
}
44
76
@@ -52,4 +84,51 @@ class {{ entity_class }}AccessControlHandler extends EntityAccessControlHandler
52
84
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
53
85
return AccessResult::allowedIfHasPermission($account, 'add {{ label | lower }} entities');
54
86
}
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 %}
55
134
{% endblock %}
0 commit comments